-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxmlverbatim.xsl
323 lines (299 loc) · 12.4 KB
/
xmlverbatim.xsl
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
XML to HTML Verbatim Formatter with Syntax Highlighting
Version 1.1
Contributors: Doug Dicks, added auto-indent (parameter indent-elements)
for pretty-print
Copyright 2002 Oliver Becker
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
Alternatively, this software may be used under the terms of the
GNU Lesser General Public License (LGPL).
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:verb="http://informatik.hu-berlin.de/xmlverbatim"
exclude-result-prefixes="verb">
<xsl:output method="html" omit-xml-declaration="yes" indent="no"/>
<xsl:param name="indent-elements" select="false()" />
<xsl:template match="/">
<xsl:apply-templates select="." mode="xmlverb" />
</xsl:template>
<!-- root -->
<xsl:template match="/" mode="xmlverb">
<xsl:text>
</xsl:text>
<xsl:comment>
<xsl:text> converted by xmlverbatim.xsl 1.1, (c) O. Becker </xsl:text>
</xsl:comment>
<xsl:text>
</xsl:text>
<div class="xmlverb-default">
<xsl:apply-templates mode="xmlverb">
<xsl:with-param name="indent-elements" select="$indent-elements" />
</xsl:apply-templates>
</div>
<xsl:text>
</xsl:text>
</xsl:template>
<!-- wrapper -->
<xsl:template match="verb:wrapper">
<xsl:apply-templates mode="xmlverb">
<xsl:with-param name="indent-elements" select="$indent-elements" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="verb:wrapper" mode="xmlverb">
<xsl:apply-templates mode="xmlverb">
<xsl:with-param name="indent-elements" select="$indent-elements" />
</xsl:apply-templates>
</xsl:template>
<!-- element nodes -->
<xsl:template match="*" mode="xmlverb">
<xsl:param name="indent-elements" select="false()" />
<xsl:param name="indent" select="''" />
<xsl:param name="indent-increment" select="'   '" />
<xsl:if test="$indent-elements">
<br/>
<xsl:value-of select="$indent" />
</xsl:if>
<xsl:text><</xsl:text>
<xsl:variable name="ns-prefix"
select="substring-before(name(),':')" />
<xsl:if test="$ns-prefix != ''">
<span class="xmlverb-element-nsprefix">
<xsl:value-of select="$ns-prefix"/>
</span>
<xsl:text>:</xsl:text>
</xsl:if>
<span class="xmlverb-element-name">
<xsl:value-of select="local-name()"/>
</span>
<xsl:variable name="pns" select="../namespace::*"/>
<xsl:if test="$pns[name()=''] and not(namespace::*[name()=''])">
<span class="xmlverb-ns-name">
<xsl:text> xmlns</xsl:text>
</span>
<xsl:text>=""</xsl:text>
</xsl:if>
<xsl:for-each select="namespace::*">
<xsl:if test="not($pns[name()=name(current()) and
.=current()])">
<xsl:call-template name="xmlverb-ns" />
</xsl:if>
</xsl:for-each>
<xsl:for-each select="@*">
<xsl:call-template name="xmlverb-attrs" />
</xsl:for-each>
<xsl:choose>
<xsl:when test="node()">
<xsl:text>></xsl:text>
<xsl:apply-templates mode="xmlverb">
<xsl:with-param name="indent-elements"
select="$indent-elements"/>
<xsl:with-param name="indent"
select="concat($indent, $indent-increment)"/>
<xsl:with-param name="indent-increment"
select="$indent-increment"/>
</xsl:apply-templates>
<xsl:if test="* and $indent-elements">
<br/>
<xsl:value-of select="$indent" />
</xsl:if>
<xsl:text></</xsl:text>
<xsl:if test="$ns-prefix != ''">
<span class="xmlverb-element-nsprefix">
<xsl:value-of select="$ns-prefix"/>
</span>
<xsl:text>:</xsl:text>
</xsl:if>
<span class="xmlverb-element-name">
<xsl:value-of select="local-name()"/>
</span>
<xsl:text>></xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> /></xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="not(parent::*)"><br /><xsl:text>
</xsl:text></xsl:if>
</xsl:template>
<!-- attribute nodes -->
<xsl:template name="xmlverb-attrs">
<xsl:text> </xsl:text>
<span class="xmlverb-attr-name">
<xsl:value-of select="name()"/>
</span>
<xsl:text>="</xsl:text>
<span class="xmlverb-attr-content">
<xsl:call-template name="html-replace-entities">
<xsl:with-param name="text" select="normalize-space(.)" />
<xsl:with-param name="attrs" select="true()" />
</xsl:call-template>
</span>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- namespace nodes -->
<xsl:template name="xmlverb-ns">
<xsl:if test="name()!='xml'">
<span class="xmlverb-ns-name">
<xsl:text> xmlns</xsl:text>
<xsl:if test="name()!=''">
<xsl:text>:</xsl:text>
</xsl:if>
<xsl:value-of select="name()"/>
</span>
<xsl:text>="</xsl:text>
<span class="xmlverb-ns-uri">
<xsl:value-of select="."/>
</span>
<xsl:text>"</xsl:text>
</xsl:if>
</xsl:template>
<!-- text nodes -->
<xsl:template match="text()" mode="xmlverb">
<span class="xmlverb-text">
<xsl:call-template name="preformatted-output">
<xsl:with-param name="text">
<xsl:call-template name="html-replace-entities">
<xsl:with-param name="text" select="." />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</span>
</xsl:template>
<!-- comments -->
<xsl:template match="comment()" mode="xmlverb">
<xsl:text><!--</xsl:text>
<span class="xmlverb-comment">
<xsl:call-template name="preformatted-output">
<xsl:with-param name="text" select="." />
</xsl:call-template>
</span>
<xsl:text>--></xsl:text>
<xsl:if test="not(parent::*)"><br /><xsl:text>
</xsl:text></xsl:if>
</xsl:template>
<!-- processing instructions -->
<xsl:template match="processing-instruction()" mode="xmlverb">
<xsl:text><?</xsl:text>
<span class="xmlverb-pi-name">
<xsl:value-of select="name()"/>
</span>
<xsl:if test=".!=''">
<xsl:text> </xsl:text>
<span class="xmlverb-pi-content">
<xsl:value-of select="."/>
</span>
</xsl:if>
<xsl:text>?></xsl:text>
<xsl:if test="not(parent::*)"><br /><xsl:text>
</xsl:text></xsl:if>
</xsl:template>
<!-- =========================================================== -->
<!-- Procedures / Functions -->
<!-- =========================================================== -->
<!-- generate entities by replacing &, ", < and > in $text -->
<xsl:template name="html-replace-entities">
<xsl:param name="text" />
<xsl:param name="attrs" />
<xsl:variable name="tmp">
<xsl:call-template name="replace-substring">
<xsl:with-param name="from" select="'>'" />
<xsl:with-param name="to" select="'&gt;'" />
<xsl:with-param name="value">
<xsl:call-template name="replace-substring">
<xsl:with-param name="from" select="'<'" />
<xsl:with-param name="to" select="'&lt;'" />
<xsl:with-param name="value">
<xsl:call-template name="replace-substring">
<xsl:with-param name="from"
select="'&'" />
<xsl:with-param name="to"
select="'&amp;'" />
<xsl:with-param name="value"
select="$text" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<!-- $text is an attribute value -->
<xsl:when test="$attrs">
<xsl:call-template name="replace-substring">
<xsl:with-param name="from" select="'
'" />
<xsl:with-param name="to" select="'&#xA;'" />
<xsl:with-param name="value">
<xsl:call-template name="replace-substring">
<xsl:with-param name="from"
select="'"'" />
<xsl:with-param name="to"
select="'&quot;'" />
<xsl:with-param name="value" select="$tmp" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tmp" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- replace in $value substring $from with $to -->
<xsl:template name="replace-substring">
<xsl:param name="value" />
<xsl:param name="from" />
<xsl:param name="to" />
<xsl:choose>
<xsl:when test="contains($value,$from)">
<xsl:value-of select="substring-before($value,$from)" />
<xsl:value-of select="$to" />
<xsl:call-template name="replace-substring">
<xsl:with-param name="value"
select="substring-after($value,$from)" />
<xsl:with-param name="from" select="$from" />
<xsl:with-param name="to" select="$to" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- preformatted output: space as , tab as 8
nl as <br> -->
<xsl:template name="preformatted-output">
<xsl:param name="text" />
<xsl:call-template name="output-nl">
<xsl:with-param name="text">
<xsl:call-template name="replace-substring">
<xsl:with-param name="value"
select="translate($text,' ',' ')" />
<xsl:with-param name="from" select="'	'" />
<xsl:with-param name="to"
select="'        '" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<!-- output nl as <br> -->
<xsl:template name="output-nl">
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="contains($text,'
')">
<xsl:value-of select="substring-before($text,'
')" />
<br />
<xsl:text>
</xsl:text>
<xsl:call-template name="output-nl">
<xsl:with-param name="text"
select="substring-after($text,'
')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>