1 <?xml version="1.0" encoding="ISO-8859-1"?>
4 XML to HTML Verbatim Formatter with Syntax Highlighting
6 Contributors: Doug Dicks, added auto-indent (parameter indent-elements)
9 Copyright 2002 Oliver Becker
12 Licensed under the Apache License, Version 2.0 (the "License");
13 you may not use this file except in compliance with the License.
14 You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
15 Unless required by applicable law or agreed to in writing, software distributed
16 under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 CONDITIONS OF ANY KIND, either express or implied. See the License for the
18 specific language governing permissions and limitations under the License.
20 Alternatively, this software may be used under the terms of the
21 GNU Lesser General Public License (LGPL).
24 <xsl:stylesheet version="1.0"
25 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
26 xmlns:verb="http://informatik.hu-berlin.de/xmlverbatim"
27 exclude-result-prefixes="verb">
29 <xsl:output method="html" omit-xml-declaration="yes" indent="no"/>
31 <xsl:param name="indent-elements" select="false()" />
33 <xsl:template match="/">
34 <xsl:apply-templates select="." mode="xmlverb" />
38 <xsl:template match="/" mode="xmlverb">
39 <xsl:text>
</xsl:text>
41 <xsl:text> converted by xmlverbatim.xsl 1.1, (c) O. Becker </xsl:text>
43 <xsl:text>
</xsl:text>
44 <div class="xmlverb-default">
45 <xsl:apply-templates mode="xmlverb">
46 <xsl:with-param name="indent-elements" select="$indent-elements" />
47 </xsl:apply-templates>
49 <xsl:text>
</xsl:text>
53 <xsl:template match="verb:wrapper">
54 <xsl:apply-templates mode="xmlverb">
55 <xsl:with-param name="indent-elements" select="$indent-elements" />
56 </xsl:apply-templates>
59 <xsl:template match="verb:wrapper" mode="xmlverb">
60 <xsl:apply-templates mode="xmlverb">
61 <xsl:with-param name="indent-elements" select="$indent-elements" />
62 </xsl:apply-templates>
65 <!-- element nodes -->
66 <xsl:template match="*" mode="xmlverb">
67 <xsl:param name="indent-elements" select="false()" />
68 <xsl:param name="indent" select="''" />
69 <xsl:param name="indent-increment" select="'   '" />
70 <xsl:if test="$indent-elements">
72 <xsl:value-of select="$indent" />
74 <xsl:text><</xsl:text>
75 <xsl:variable name="ns-prefix"
76 select="substring-before(name(),':')" />
77 <xsl:if test="$ns-prefix != ''">
78 <span class="xmlverb-element-nsprefix">
79 <xsl:value-of select="$ns-prefix"/>
81 <xsl:text>:</xsl:text>
83 <span class="xmlverb-element-name">
84 <xsl:value-of select="local-name()"/>
86 <xsl:variable name="pns" select="../namespace::*"/>
87 <xsl:if test="$pns[name()=''] and not(namespace::*[name()=''])">
88 <span class="xmlverb-ns-name">
89 <xsl:text> xmlns</xsl:text>
91 <xsl:text>=""</xsl:text>
93 <xsl:for-each select="namespace::*">
94 <xsl:if test="not($pns[name()=name(current()) and
96 <xsl:call-template name="xmlverb-ns" />
99 <xsl:for-each select="@*">
100 <xsl:call-template name="xmlverb-attrs" />
103 <xsl:when test="node()">
104 <xsl:text>></xsl:text>
105 <xsl:apply-templates mode="xmlverb">
106 <xsl:with-param name="indent-elements"
107 select="$indent-elements"/>
108 <xsl:with-param name="indent"
109 select="concat($indent, $indent-increment)"/>
110 <xsl:with-param name="indent-increment"
111 select="$indent-increment"/>
112 </xsl:apply-templates>
113 <xsl:if test="* and $indent-elements">
115 <xsl:value-of select="$indent" />
117 <xsl:text></</xsl:text>
118 <xsl:if test="$ns-prefix != ''">
119 <span class="xmlverb-element-nsprefix">
120 <xsl:value-of select="$ns-prefix"/>
122 <xsl:text>:</xsl:text>
124 <span class="xmlverb-element-name">
125 <xsl:value-of select="local-name()"/>
127 <xsl:text>></xsl:text>
130 <xsl:text> /></xsl:text>
133 <xsl:if test="not(parent::*)"><br /><xsl:text>
</xsl:text></xsl:if>
136 <!-- attribute nodes -->
137 <xsl:template name="xmlverb-attrs">
138 <xsl:text> </xsl:text>
139 <span class="xmlverb-attr-name">
140 <xsl:value-of select="name()"/>
142 <xsl:text>="</xsl:text>
143 <span class="xmlverb-attr-content">
144 <xsl:call-template name="html-replace-entities">
145 <xsl:with-param name="text" select="normalize-space(.)" />
146 <xsl:with-param name="attrs" select="true()" />
149 <xsl:text>"</xsl:text>
152 <!-- namespace nodes -->
153 <xsl:template name="xmlverb-ns">
154 <xsl:if test="name()!='xml'">
155 <span class="xmlverb-ns-name">
156 <xsl:text> xmlns</xsl:text>
157 <xsl:if test="name()!=''">
158 <xsl:text>:</xsl:text>
160 <xsl:value-of select="name()"/>
162 <xsl:text>="</xsl:text>
163 <span class="xmlverb-ns-uri">
164 <xsl:value-of select="."/>
166 <xsl:text>"</xsl:text>
171 <xsl:template match="text()" mode="xmlverb">
172 <span class="xmlverb-text">
173 <xsl:call-template name="preformatted-output">
174 <xsl:with-param name="text">
175 <xsl:call-template name="html-replace-entities">
176 <xsl:with-param name="text" select="." />
184 <xsl:template match="comment()" mode="xmlverb">
185 <xsl:text><!--</xsl:text>
186 <span class="xmlverb-comment">
187 <xsl:call-template name="preformatted-output">
188 <xsl:with-param name="text" select="." />
191 <xsl:text>--></xsl:text>
192 <xsl:if test="not(parent::*)"><br /><xsl:text>
</xsl:text></xsl:if>
195 <!-- processing instructions -->
196 <xsl:template match="processing-instruction()" mode="xmlverb">
197 <xsl:text><?</xsl:text>
198 <span class="xmlverb-pi-name">
199 <xsl:value-of select="name()"/>
201 <xsl:if test=".!=''">
202 <xsl:text> </xsl:text>
203 <span class="xmlverb-pi-content">
204 <xsl:value-of select="."/>
207 <xsl:text>?></xsl:text>
208 <xsl:if test="not(parent::*)"><br /><xsl:text>
</xsl:text></xsl:if>
212 <!-- =========================================================== -->
213 <!-- Procedures / Functions -->
214 <!-- =========================================================== -->
216 <!-- generate entities by replacing &, ", < and > in $text -->
217 <xsl:template name="html-replace-entities">
218 <xsl:param name="text" />
219 <xsl:param name="attrs" />
220 <xsl:variable name="tmp">
221 <xsl:call-template name="replace-substring">
222 <xsl:with-param name="from" select="'>'" />
223 <xsl:with-param name="to" select="'&gt;'" />
224 <xsl:with-param name="value">
225 <xsl:call-template name="replace-substring">
226 <xsl:with-param name="from" select="'<'" />
227 <xsl:with-param name="to" select="'&lt;'" />
228 <xsl:with-param name="value">
229 <xsl:call-template name="replace-substring">
230 <xsl:with-param name="from"
232 <xsl:with-param name="to"
233 select="'&amp;'" />
234 <xsl:with-param name="value"
243 <!-- $text is an attribute value -->
244 <xsl:when test="$attrs">
245 <xsl:call-template name="replace-substring">
246 <xsl:with-param name="from" select="'
'" />
247 <xsl:with-param name="to" select="'&#xA;'" />
248 <xsl:with-param name="value">
249 <xsl:call-template name="replace-substring">
250 <xsl:with-param name="from"
252 <xsl:with-param name="to"
253 select="'&quot;'" />
254 <xsl:with-param name="value" select="$tmp" />
260 <xsl:value-of select="$tmp" />
265 <!-- replace in $value substring $from with $to -->
266 <xsl:template name="replace-substring">
267 <xsl:param name="value" />
268 <xsl:param name="from" />
269 <xsl:param name="to" />
271 <xsl:when test="contains($value,$from)">
272 <xsl:value-of select="substring-before($value,$from)" />
273 <xsl:value-of select="$to" />
274 <xsl:call-template name="replace-substring">
275 <xsl:with-param name="value"
276 select="substring-after($value,$from)" />
277 <xsl:with-param name="from" select="$from" />
278 <xsl:with-param name="to" select="$to" />
282 <xsl:value-of select="$value" />
287 <!-- preformatted output: space as , tab as 8
289 <xsl:template name="preformatted-output">
290 <xsl:param name="text" />
291 <xsl:call-template name="output-nl">
292 <xsl:with-param name="text">
293 <xsl:call-template name="replace-substring">
294 <xsl:with-param name="value"
295 select="translate($text,' ',' ')" />
296 <xsl:with-param name="from" select="'	'" />
297 <xsl:with-param name="to"
298 select="'        '" />
304 <!-- output nl as <br> -->
305 <xsl:template name="output-nl">
306 <xsl:param name="text" />
308 <xsl:when test="contains($text,'
')">
309 <xsl:value-of select="substring-before($text,'
')" />
311 <xsl:text>
</xsl:text>
312 <xsl:call-template name="output-nl">
313 <xsl:with-param name="text"
314 select="substring-after($text,'
')" />
318 <xsl:value-of select="$text" />