source: other-projects/tipple-android/i-greenstone-server-files/greenstone/webapps/greenstone3/interfaces/basic/transform/xml-to-string.xsl@ 26899

Last change on this file since 26899 was 26899, checked in by davidb, 11 years ago

Tipple reborn after Chris's Summer of Code 2013

File size: 12.8 KB
Line 
1<!--
2Copyright (c) 2001-2009, Evan Lenz
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 * Neither the name of Lenz Consulting Group nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12
13Recent changes:
14
152010-06-10: Added the $force-exclude-all-namespaces parameter
162009-10-19: Added the $exclude-these-namespaces parameter
172009-10-08: Added $att-value parameter and template name to template rule for attributes.
18
19-->
20<xsl:stylesheet version="1.0"
21 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
22
23 <xsl:output omit-xml-declaration="yes"/>
24
25 <xsl:param name="use-empty-syntax" select="true()"/>
26 <xsl:param name="exclude-unused-prefixes" select="true()"/>
27
28 <xsl:param name="force-exclude-all-namespaces" select="false()"/>
29
30 <!-- a node-set; each node's string-value
31 will be interpreted as a namespace URI to be
32 excluded from the serialization. -->
33 <xsl:param name="namespaces-to-exclude" select="/.."/>
34 <!-- initialized to empty node-set -->
35
36 <xsl:param name="start-tag-start" select="'&lt;'"/>
37 <xsl:param name="start-tag-end" select="'>'"/>
38 <xsl:param name="empty-tag-end" select="'/>'"/>
39 <xsl:param name="end-tag-start" select="'&lt;/'"/>
40 <xsl:param name="end-tag-end" select="'>'"/>
41 <xsl:param name="space" select="' '"/>
42 <xsl:param name="ns-decl" select="'xmlns'"/>
43 <xsl:param name="colon" select="':'"/>
44 <xsl:param name="equals" select="'='"/>
45 <xsl:param name="attribute-delimiter" select="'&quot;'"/>
46 <xsl:param name="comment-start" select="'&lt;!--'"/>
47 <xsl:param name="comment-end" select="'-->'"/>
48 <xsl:param name="pi-start" select="'&lt;?'"/>
49 <xsl:param name="pi-end" select="'?>'"/>
50
51 <xsl:template name="xml-to-string">
52 <xsl:param name="node-set" select="."/>
53 <xsl:apply-templates select="$node-set" mode="xml-to-string">
54 <xsl:with-param name="depth" select="1"/>
55 </xsl:apply-templates>
56 </xsl:template>
57
58<!--
59 <xsl:template match="/" name="xml-to-string-root-rule">
60 <xsl:call-template name="xml-to-string"/>
61 </xsl:template>
62-->
63
64 <xsl:template match="/" mode="xml-to-string">
65 <xsl:param name="depth"/>
66 <xsl:apply-templates mode="xml-to-string">
67 <xsl:with-param name="depth" select="$depth"/>
68 </xsl:apply-templates>
69 </xsl:template>
70
71 <xsl:template match="*" mode="xml-to-string">
72 <xsl:param name="depth"/>
73 <xsl:variable name="element" select="."/>
74 <xsl:value-of select="$start-tag-start"/>
75 <xsl:call-template name="element-name">
76 <xsl:with-param name="text" select="name()"/>
77 </xsl:call-template>
78 <xsl:apply-templates select="@*" mode="xml-to-string"/>
79 <xsl:if test="not($force-exclude-all-namespaces)">
80 <xsl:for-each select="namespace::*">
81 <xsl:call-template name="process-namespace-node">
82 <xsl:with-param name="element" select="$element"/>
83 <xsl:with-param name="depth" select="$depth"/>
84 </xsl:call-template>
85 </xsl:for-each>
86 </xsl:if>
87 <xsl:choose>
88 <xsl:when test="node() or not($use-empty-syntax)">
89 <xsl:value-of select="$start-tag-end"/>
90 <xsl:apply-templates mode="xml-to-string">
91 <xsl:with-param name="depth" select="$depth + 1"/>
92 </xsl:apply-templates>
93 <xsl:value-of select="$end-tag-start"/>
94 <xsl:call-template name="element-name">
95 <xsl:with-param name="text" select="name()"/>
96 </xsl:call-template>
97 <xsl:value-of select="$end-tag-end"/>
98 </xsl:when>
99 <xsl:otherwise>
100 <xsl:value-of select="$empty-tag-end"/>
101 </xsl:otherwise>
102 </xsl:choose>
103 </xsl:template>
104
105 <xsl:template name="process-namespace-node">
106 <xsl:param name="element"/>
107 <xsl:param name="depth"/>
108 <xsl:variable name="declaredAbove">
109 <xsl:call-template name="isDeclaredAbove">
110 <xsl:with-param name="depth" select="$depth - 1"/>
111 <xsl:with-param name="element" select="$element/.."/>
112 </xsl:call-template>
113 </xsl:variable>
114
115 <xsl:variable name="is-used-on-this-element" select="($element | $element/@*) [namespace-uri() = current()]"/>
116 <xsl:variable name="is-used-on-a-descendant" select="($element//* | $element//@*)[namespace-uri() = current()]"/>
117 <xsl:variable name="is-unused" select="not($is-used-on-this-element) and
118 not($is-used-on-a-descendant)"/>
119 <xsl:variable name="exclude-ns" select="($is-unused and $exclude-unused-prefixes) or
120 (. = $namespaces-to-exclude)"/>
121
122 <xsl:variable name="force-include" select="$is-used-on-this-element and (. = $namespaces-to-exclude)"/>
123
124 <xsl:if test="(name() != 'xml') and ($force-include or (not($exclude-ns) and not(string($declaredAbove))))">
125 <xsl:value-of select="$space"/>
126 <xsl:value-of select="$ns-decl"/>
127 <xsl:if test="name()">
128 <xsl:value-of select="$colon"/>
129 <xsl:call-template name="ns-prefix">
130 <xsl:with-param name="text" select="name()"/>
131 </xsl:call-template>
132 </xsl:if>
133 <xsl:value-of select="$equals"/>
134 <xsl:value-of select="$attribute-delimiter"/>
135 <xsl:call-template name="ns-uri">
136 <xsl:with-param name="text" select="string(.)"/>
137 </xsl:call-template>
138 <xsl:value-of select="$attribute-delimiter"/>
139 </xsl:if>
140 </xsl:template>
141
142 <xsl:template name="isDeclaredAbove">
143 <xsl:param name="element"/>
144 <xsl:param name="depth"/>
145 <xsl:if test="$depth > 0">
146 <xsl:choose>
147 <xsl:when test="$element/namespace::*[name(.)=name(current()) and .=current()]">1</xsl:when>
148 <xsl:when test="$element/namespace::*[name(.)=name(current())]"/>
149 <xsl:otherwise>
150 <xsl:call-template name="isDeclaredAbove">
151 <xsl:with-param name="depth" select="$depth - 1"/>
152 <xsl:with-param name="element" select="$element/.."/>
153 </xsl:call-template>
154 </xsl:otherwise>
155 </xsl:choose>
156 </xsl:if>
157 </xsl:template>
158
159 <xsl:template match="@*" mode="xml-to-string" name="serialize-attribute">
160 <xsl:param name="att-value" select="string(.)"/>
161 <xsl:value-of select="$space"/>
162 <xsl:call-template name="attribute-name">
163 <xsl:with-param name="text" select="name()"/>
164 </xsl:call-template>
165 <xsl:value-of select="$equals"/>
166 <xsl:value-of select="$attribute-delimiter"/>
167 <xsl:call-template name="attribute-value">
168 <xsl:with-param name="text" select="$att-value"/>
169 </xsl:call-template>
170 <xsl:value-of select="$attribute-delimiter"/>
171 </xsl:template>
172
173 <xsl:template match="comment()" mode="xml-to-string">
174 <xsl:value-of select="$comment-start"/>
175 <xsl:call-template name="comment-text">
176 <xsl:with-param name="text" select="string(.)"/>
177 </xsl:call-template>
178 <xsl:value-of select="$comment-end"/>
179 </xsl:template>
180
181 <xsl:template match="processing-instruction()" mode="xml-to-string">
182 <xsl:value-of select="$pi-start"/>
183 <xsl:call-template name="pi-target">
184 <xsl:with-param name="text" select="name()"/>
185 </xsl:call-template>
186 <xsl:value-of select="$space"/>
187 <xsl:call-template name="pi-text">
188 <xsl:with-param name="text" select="string(.)"/>
189 </xsl:call-template>
190 <xsl:value-of select="$pi-end"/>
191 </xsl:template>
192
193 <xsl:template match="text()" mode="xml-to-string">
194 <xsl:call-template name="text-content">
195 <xsl:with-param name="text" select="string(.)"/>
196 </xsl:call-template>
197 </xsl:template>
198
199 <xsl:template name="element-name">
200 <xsl:param name="text"/>
201 <xsl:value-of select="$text"/>
202 </xsl:template>
203
204 <xsl:template name="attribute-name">
205 <xsl:param name="text"/>
206 <xsl:value-of select="$text"/>
207 </xsl:template>
208
209 <xsl:template name="attribute-value">
210 <xsl:param name="text"/>
211 <xsl:variable name="escaped-markup">
212 <xsl:call-template name="escape-markup-characters">
213 <xsl:with-param name="text" select="$text"/>
214 </xsl:call-template>
215 </xsl:variable>
216 <xsl:choose>
217 <xsl:when test="$attribute-delimiter = &quot;'&quot;">
218 <xsl:call-template name="replace-string">
219 <xsl:with-param name="text" select="$escaped-markup"/>
220 <xsl:with-param name="replace" select="&quot;'&quot;"/>
221 <xsl:with-param name="with" select="'&amp;apos;'"/>
222 </xsl:call-template>
223 </xsl:when>
224 <xsl:when test="$attribute-delimiter = '&quot;'">
225 <xsl:call-template name="replace-string">
226 <xsl:with-param name="text" select="$escaped-markup"/>
227 <xsl:with-param name="replace" select="'&quot;'"/>
228 <xsl:with-param name="with" select="'&amp;quot;'"/>
229 </xsl:call-template>
230 </xsl:when>
231 <xsl:otherwise>
232 <xsl:call-template name="replace-string">
233 <xsl:with-param name="text" select="$escaped-markup"/>
234 <xsl:with-param name="replace" select="$attribute-delimiter"/>
235 <xsl:with-param name="with" select="''"/>
236 </xsl:call-template>
237 </xsl:otherwise>
238 </xsl:choose>
239 </xsl:template>
240
241 <xsl:template name="ns-prefix">
242 <xsl:param name="text"/>
243 <xsl:value-of select="$text"/>
244 </xsl:template>
245
246 <xsl:template name="ns-uri">
247 <xsl:param name="text"/>
248 <xsl:call-template name="attribute-value">
249 <xsl:with-param name="text" select="$text"/>
250 </xsl:call-template>
251 </xsl:template>
252
253 <xsl:template name="text-content">
254 <xsl:param name="text"/>
255 <xsl:call-template name="escape-markup-characters">
256 <xsl:with-param name="text" select="$text"/>
257 </xsl:call-template>
258 </xsl:template>
259
260 <xsl:template name="pi-target">
261 <xsl:param name="text"/>
262 <xsl:value-of select="$text"/>
263 </xsl:template>
264
265 <xsl:template name="pi-text">
266 <xsl:param name="text"/>
267 <xsl:value-of select="$text"/>
268 </xsl:template>
269
270 <xsl:template name="comment-text">
271 <xsl:param name="text"/>
272 <xsl:value-of select="$text"/>
273 </xsl:template>
274
275 <xsl:template name="escape-markup-characters">
276 <xsl:param name="text"/>
277 <xsl:variable name="ampEscaped">
278 <xsl:call-template name="replace-string">
279 <xsl:with-param name="text" select="$text"/>
280 <xsl:with-param name="replace" select="'&amp;'"/>
281 <xsl:with-param name="with" select="'&amp;amp;'"/>
282 </xsl:call-template>
283 </xsl:variable>
284 <xsl:variable name="ltEscaped">
285 <xsl:call-template name="replace-string">
286 <xsl:with-param name="text" select="$ampEscaped"/>
287 <xsl:with-param name="replace" select="'&lt;'"/>
288 <xsl:with-param name="with" select="'&amp;lt;'"/>
289 </xsl:call-template>
290 </xsl:variable>
291 <xsl:call-template name="replace-string">
292 <xsl:with-param name="text" select="$ltEscaped"/>
293 <xsl:with-param name="replace" select="']]>'"/>
294 <xsl:with-param name="with" select="']]&amp;gt;'"/>
295 </xsl:call-template>
296 </xsl:template>
297
298 <xsl:template name="replace-string">
299 <xsl:param name="text"/>
300 <xsl:param name="replace"/>
301 <xsl:param name="with"/>
302 <xsl:variable name="stringText" select="string($text)"/>
303 <xsl:choose>
304 <xsl:when test="contains($stringText,$replace)">
305 <xsl:value-of select="substring-before($stringText,$replace)"/>
306 <xsl:value-of select="$with"/>
307 <xsl:call-template name="replace-string">
308 <xsl:with-param name="text" select="substring-after($stringText,$replace)"/>
309 <xsl:with-param name="replace" select="$replace"/>
310 <xsl:with-param name="with" select="$with"/>
311 </xsl:call-template>
312 </xsl:when>
313 <xsl:otherwise>
314 <xsl:value-of select="$stringText"/>
315 </xsl:otherwise>
316 </xsl:choose>
317 </xsl:template>
318
319</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.