summaryrefslogtreecommitdiffstats
blob: bf4fd7f8ef7bcaedbc91e7ba6f0b517b7d890821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:doc="http://xsltsl.org/xsl/documentation/1.0"
	xmlns:node="http://xsltsl.org/node"
	extension-element-prefixes="doc node">

  <doc:reference xmlns="">
    <referenceinfo>
      <releaseinfo role="meta">
	$Id: node.xsl 3991 2004-11-10 06:51:55Z balls $
      </releaseinfo>
      <author>
	<surname>Ball</surname>
	<firstname>Steve</firstname>
      </author>
      <copyright>
	<year>2001</year>
	<holder>Steve Ball</holder>
      </copyright>
    </referenceinfo>

    <title>Node Templates</title>

    <partintro>
      <section>
	<title>Introduction</title>

	<para>This stylesheet module provides functions for reporting on or manipulating nodes and nodesets.</para>

      </section>
    </partintro>

  </doc:reference>

  <doc:template name="node:xpath" xmlns="">
    <refpurpose>Returns an XPath location path</refpurpose>

    <refdescription>
      <para>This template returns an XPath location path that uniquely identifies the given node within the document.</para>
    </refdescription>

    <refparameter>
      <variablelist>
	<varlistentry>
	  <term>node</term>
	  <listitem>
	    <para>The node to create an XPath for.  If this parameter is given as a nodeset, then the first node in the nodeset is used.</para>
	  </listitem>
	</varlistentry>
      </variablelist>
    </refparameter>

    <refreturn>
      <para>Returns an XPath location path as a string.</para>
    </refreturn>
  </doc:template>

  <xsl:template name="node:xpath">
    <xsl:param name="node" select="."/>

    <xsl:choose>

      <xsl:when test="$node">

        <xsl:for-each select="$node[1]/ancestor-or-self::*">
          <xsl:text/>/<xsl:value-of select="name()"/>
          <xsl:text/>[<xsl:value-of select="count(preceding-sibling::*[name() = name(current())]) + 1"/>]<xsl:text/>
        </xsl:for-each>

        <xsl:choose>

          <xsl:when test="$node[1]/self::comment()">
            <xsl:text>/comment()</xsl:text>
            <xsl:text/>[<xsl:value-of select="count($node[1]/preceding-sibling::comment()) + 1" />]<xsl:text/>
          </xsl:when>

          <xsl:when test="$node[1]/self::processing-instruction()">
            <xsl:text>/processing-instruction()</xsl:text>
            <xsl:text/>[<xsl:value-of select="count($node[1]/preceding-sibling::processing-instruction()) + 1" />]<xsl:text/>
          </xsl:when>

          <xsl:when test="$node[1]/self::text()">
            <xsl:text>/text()</xsl:text>
            <xsl:text/>[<xsl:value-of select="count($node[1]/preceding-sibling::text()) + 1" />]<xsl:text/>
          </xsl:when>

          <xsl:when test="not($node[1]/..)">
            <xsl:text>/</xsl:text>
          </xsl:when>

          <xsl:when test="count($node[1]/../namespace::* | $node[1]) = count($node[1]/../namespace::*)">
            <xsl:text/>/namespace::<xsl:value-of select="name($node[1])" />
          </xsl:when>

          <xsl:when test="count($node[1]/../@* | $node[1]) = count($node[1]/../@*)">
            <xsl:text/>/@<xsl:value-of select="name($node[1])" />
          </xsl:when>

        </xsl:choose>      
      </xsl:when>

      <xsl:otherwise>
        <xsl:text>/..</xsl:text>
      </xsl:otherwise>

    </xsl:choose>

  </xsl:template>

  <doc:template name="node:type" xmlns="">
    <refpurpose>Return node type</refpurpose>

    <refdescription>
      <para>Returns the type of a node as a string.</para>
    </refdescription>

    <refparameter>
      <variablelist>
	<varlistentry>
	  <term>node</term>
	  <listitem>
	    <para>The node to get the type for.  If this parameter is given as a nodeset, then the first node in the nodeset is used.</para>
	  </listitem>
	</varlistentry>
      </variablelist>
    </refparameter>

    <refreturn>
      <para>Returns node type as a string.  Values returned are:</para>
      <variablelist>
	<varlistentry>
	  <term>Element</term>
	  <listitem>
	    <para><literal>element</literal></para>
	  </listitem>
	</varlistentry>
	<varlistentry>
	  <term>Text Node</term>
	  <listitem>
	    <para><literal>text</literal></para>
	  </listitem>
	</varlistentry>
	<varlistentry>
	  <term>Comment</term>
	  <listitem>
	    <para><literal>comment</literal></para>
	  </listitem>
	</varlistentry>
	<varlistentry>
	  <term>Processing Instruction</term>
	  <listitem>
	    <para><literal>processing instruction</literal></para>
	  </listitem>
	</varlistentry>
      </variablelist>
    </refreturn>
  </doc:template>

  <xsl:template name="node:type">
    <xsl:param name="node" select="."/>

    <xsl:choose>
      <xsl:when test="not($node)"/>
      <xsl:when test="$node[1]/self::*">
	<xsl:text>element</xsl:text>
      </xsl:when>
      <xsl:when test="$node[1]/self::text()">
	<xsl:text>text</xsl:text>
      </xsl:when>
      <xsl:when test="$node[1]/self::comment()">
	<xsl:text>comment</xsl:text>
      </xsl:when>
      <xsl:when test="$node[1]/self::processing-instruction()">
	<xsl:text>processing instruction</xsl:text>
      </xsl:when>
      <xsl:when test="not($node[1]/parent::*)">
        <xsl:text>root</xsl:text>
      </xsl:when>
      <xsl:when test="count($node[1] | $node[1]/../namespace::*) = count($node[1]/../namespace::*)">
        <xsl:text>namespace</xsl:text>
      </xsl:when>
      <xsl:when test="count($node[1] | $node[1]/../@*) = count($node[1]/../@*)">
        <xsl:text>attribute</xsl:text>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <doc:template name="node:copy" xmlns="">
    <refpurpose>Copy Nodes</refpurpose>

    <refdescription>
      <para>Makes a copy of the given nodes, including attributes and descendants.</para>
    </refdescription>

    <refparameter>
      <variablelist>
	<varlistentry>
	  <term>nodes</term>
	  <listitem>
	    <para>The nodes to copy.</para>
	  </listitem>
	</varlistentry>
      </variablelist>
    </refparameter>

    <refreturn>
      <para>Returns the copied nodes as a result tree fragment.</para>
    </refreturn>
  </doc:template>

  <xsl:template name='node:copy'>
    <xsl:param name='nodes' select='.'/>

    <xsl:for-each select='$nodes'>
      <xsl:copy>
        <xsl:for-each select='@*'>
          <xsl:copy/>
        </xsl:for-each>

        <xsl:for-each select='node()'>
          <xsl:call-template name='node:copy'/>
        </xsl:for-each>
      </xsl:copy>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>