]> git.stg.codes - stg.git/blob - doc/xslt/slides/keynote/xsltsl/node.xsl
Ticket 37. The switch case construction for changePolicy
[stg.git] / doc / xslt / slides / keynote / xsltsl / node.xsl
1 <?xml version="1.0"?>
2
3 <xsl:stylesheet version="1.0"
4         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5         xmlns:doc="http://xsltsl.org/xsl/documentation/1.0"
6         xmlns:node="http://xsltsl.org/node"
7         extension-element-prefixes="doc node">
8
9   <doc:reference xmlns="">
10     <referenceinfo>
11       <releaseinfo role="meta">
12         $Id: node.xsl 3991 2004-11-10 06:51:55Z balls $
13       </releaseinfo>
14       <author>
15         <surname>Ball</surname>
16         <firstname>Steve</firstname>
17       </author>
18       <copyright>
19         <year>2001</year>
20         <holder>Steve Ball</holder>
21       </copyright>
22     </referenceinfo>
23
24     <title>Node Templates</title>
25
26     <partintro>
27       <section>
28         <title>Introduction</title>
29
30         <para>This stylesheet module provides functions for reporting on or manipulating nodes and nodesets.</para>
31
32       </section>
33     </partintro>
34
35   </doc:reference>
36
37   <doc:template name="node:xpath" xmlns="">
38     <refpurpose>Returns an XPath location path</refpurpose>
39
40     <refdescription>
41       <para>This template returns an XPath location path that uniquely identifies the given node within the document.</para>
42     </refdescription>
43
44     <refparameter>
45       <variablelist>
46         <varlistentry>
47           <term>node</term>
48           <listitem>
49             <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>
50           </listitem>
51         </varlistentry>
52       </variablelist>
53     </refparameter>
54
55     <refreturn>
56       <para>Returns an XPath location path as a string.</para>
57     </refreturn>
58   </doc:template>
59
60   <xsl:template name="node:xpath">
61     <xsl:param name="node" select="."/>
62
63     <xsl:choose>
64
65       <xsl:when test="$node">
66
67         <xsl:for-each select="$node[1]/ancestor-or-self::*">
68           <xsl:text/>/<xsl:value-of select="name()"/>
69           <xsl:text/>[<xsl:value-of select="count(preceding-sibling::*[name() = name(current())]) + 1"/>]<xsl:text/>
70         </xsl:for-each>
71
72         <xsl:choose>
73
74           <xsl:when test="$node[1]/self::comment()">
75             <xsl:text>/comment()</xsl:text>
76             <xsl:text/>[<xsl:value-of select="count($node[1]/preceding-sibling::comment()) + 1" />]<xsl:text/>
77           </xsl:when>
78
79           <xsl:when test="$node[1]/self::processing-instruction()">
80             <xsl:text>/processing-instruction()</xsl:text>
81             <xsl:text/>[<xsl:value-of select="count($node[1]/preceding-sibling::processing-instruction()) + 1" />]<xsl:text/>
82           </xsl:when>
83
84           <xsl:when test="$node[1]/self::text()">
85             <xsl:text>/text()</xsl:text>
86             <xsl:text/>[<xsl:value-of select="count($node[1]/preceding-sibling::text()) + 1" />]<xsl:text/>
87           </xsl:when>
88
89           <xsl:when test="not($node[1]/..)">
90             <xsl:text>/</xsl:text>
91           </xsl:when>
92
93           <xsl:when test="count($node[1]/../namespace::* | $node[1]) = count($node[1]/../namespace::*)">
94             <xsl:text/>/namespace::<xsl:value-of select="name($node[1])" />
95           </xsl:when>
96
97           <xsl:when test="count($node[1]/../@* | $node[1]) = count($node[1]/../@*)">
98             <xsl:text/>/@<xsl:value-of select="name($node[1])" />
99           </xsl:when>
100
101         </xsl:choose>      
102       </xsl:when>
103
104       <xsl:otherwise>
105         <xsl:text>/..</xsl:text>
106       </xsl:otherwise>
107
108     </xsl:choose>
109
110   </xsl:template>
111
112   <doc:template name="node:type" xmlns="">
113     <refpurpose>Return node type</refpurpose>
114
115     <refdescription>
116       <para>Returns the type of a node as a string.</para>
117     </refdescription>
118
119     <refparameter>
120       <variablelist>
121         <varlistentry>
122           <term>node</term>
123           <listitem>
124             <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>
125           </listitem>
126         </varlistentry>
127       </variablelist>
128     </refparameter>
129
130     <refreturn>
131       <para>Returns node type as a string.  Values returned are:</para>
132       <variablelist>
133         <varlistentry>
134           <term>Element</term>
135           <listitem>
136             <para><literal>element</literal></para>
137           </listitem>
138         </varlistentry>
139         <varlistentry>
140           <term>Text Node</term>
141           <listitem>
142             <para><literal>text</literal></para>
143           </listitem>
144         </varlistentry>
145         <varlistentry>
146           <term>Comment</term>
147           <listitem>
148             <para><literal>comment</literal></para>
149           </listitem>
150         </varlistentry>
151         <varlistentry>
152           <term>Processing Instruction</term>
153           <listitem>
154             <para><literal>processing instruction</literal></para>
155           </listitem>
156         </varlistentry>
157       </variablelist>
158     </refreturn>
159   </doc:template>
160
161   <xsl:template name="node:type">
162     <xsl:param name="node" select="."/>
163
164     <xsl:choose>
165       <xsl:when test="not($node)"/>
166       <xsl:when test="$node[1]/self::*">
167         <xsl:text>element</xsl:text>
168       </xsl:when>
169       <xsl:when test="$node[1]/self::text()">
170         <xsl:text>text</xsl:text>
171       </xsl:when>
172       <xsl:when test="$node[1]/self::comment()">
173         <xsl:text>comment</xsl:text>
174       </xsl:when>
175       <xsl:when test="$node[1]/self::processing-instruction()">
176         <xsl:text>processing instruction</xsl:text>
177       </xsl:when>
178       <xsl:when test="not($node[1]/parent::*)">
179         <xsl:text>root</xsl:text>
180       </xsl:when>
181       <xsl:when test="count($node[1] | $node[1]/../namespace::*) = count($node[1]/../namespace::*)">
182         <xsl:text>namespace</xsl:text>
183       </xsl:when>
184       <xsl:when test="count($node[1] | $node[1]/../@*) = count($node[1]/../@*)">
185         <xsl:text>attribute</xsl:text>
186       </xsl:when>
187     </xsl:choose>
188   </xsl:template>
189
190   <doc:template name="node:copy" xmlns="">
191     <refpurpose>Copy Nodes</refpurpose>
192
193     <refdescription>
194       <para>Makes a copy of the given nodes, including attributes and descendants.</para>
195     </refdescription>
196
197     <refparameter>
198       <variablelist>
199         <varlistentry>
200           <term>nodes</term>
201           <listitem>
202             <para>The nodes to copy.</para>
203           </listitem>
204         </varlistentry>
205       </variablelist>
206     </refparameter>
207
208     <refreturn>
209       <para>Returns the copied nodes as a result tree fragment.</para>
210     </refreturn>
211   </doc:template>
212
213   <xsl:template name='node:copy'>
214     <xsl:param name='nodes' select='.'/>
215
216     <xsl:for-each select='$nodes'>
217       <xsl:copy>
218         <xsl:for-each select='@*'>
219           <xsl:copy/>
220         </xsl:for-each>
221
222         <xsl:for-each select='node()'>
223           <xsl:call-template name='node:copy'/>
224         </xsl:for-each>
225       </xsl:copy>
226     </xsl:for-each>
227   </xsl:template>
228 </xsl:stylesheet>
229