/
eval.xsl
514 lines (482 loc) · 19.8 KB
/
eval.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?xml version="1.0" encoding="UTF-8"?>
<stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
/**
* Copyright (c) 2009, Chris Thatcher
* License MIT/GPL
* Based on work by Doeke Zanstra
*/
-->
<output indent="no"
omit-xml-declaration="yes"
method="text"
encoding="UTF-8"
media-type="application/javascript"/>
<strip-space elements="*"/>
<param name="force_array" />
<!-- convert root element to an anonymous container -->
<template match="/">
<text>{</text>
<apply-templates select="*"/>
<text>}</text>
</template>
<!-- simple object or array filter -->
<template match="*" name="base">
<variable name="this" select="."/>
<choose>
<when test="name(following-sibling::node())=name()
or name(preceding-sibling::node()[1])=name()
or contains($force_array, concat('|' , name(), '|'))">
<if test="not(name(preceding-sibling::node()[1])=name())">
<!-- array of like-named elements -->
<call-template name="array_wrapper">
<with-param name="aw" select=". | following-sibling::node()[name()=name($this)]"/>
</call-template>
<if test="following-sibling::node()[name()!=name($this)]">
<text>,</text>
</if>
</if>
</when>
<otherwise>
<choose>
<!-- complex property or nested object -->
<when test="count(./*)>0 or count(./@*)>0">
<call-template name='nested_object'>
<with-param name='no' select='.'/>
</call-template>
</when>
<!-- simple content with no attributes -->
<when test="count(./*)=0 and ./text() and count(./@*)=0">
<call-template name="property">
<with-param name="p" select="."/>
</call-template>
</when>
<otherwise>
<!-- item:null -->
<call-template name='null_property'>
<with-param name='np' select='.'/>
</call-template>
</otherwise>
</choose>
</otherwise>
</choose>
</template>
<!-- mixed document text -->
<template name='mixed_content'>
<param name='m'/>
<text> '$':</text>
<text>[</text>
<for-each select="$m">
<variable name='this' select='.'/>
<choose>
<when test='name()'>
<choose>
<when test="name(following-sibling::node())=name()
or name(preceding-sibling::node()[1])=name()
or contains($force_array, concat('|' , name(), '|'))">
<if test="not(name(preceding-sibling::node()[1])=name())">
<!-- array of like-named elements -->
<call-template name="array_wrapper">
<with-param name="aw" select=". | following-sibling::node()[name()=name($this)]"/>
<with-param name='ew' select='true()'/>
</call-template>
<if test="following-sibling::node()[name()!=name($this)]">
<text>,</text>
</if>
</if>
</when>
<otherwise>
<choose>
<!-- complex property or nested object -->
<when test="count(./*)>0 or count(./@*)>0">
<call-template name='nested_object'>
<with-param name='no' select='.'/>
<with-param name='ew' select='true()'/>
</call-template>
</when>
<!-- simple content with no attributes -->
<when test="count(./*)=0 and ./text() and count(./@*)=0">
<call-template name="property">
<with-param name="p" select="."/>
<with-param name='ew' select='true()'/>
</call-template>
</when>
<otherwise>
<!-- item:null -->
<call-template name='null_property'>
<with-param name='np' select='.'/>
<with-param name='ew' select='true()'/>
</call-template>
</otherwise>
</choose>
</otherwise>
</choose>
</when>
<otherwise >
<if test='string-length(normalize-space(.))>0'>
<call-template name='value'>
<with-param name="v" select="."/>
</call-template>
<if test="following-sibling::*">
<text>,</text>
</if>
</if>
</otherwise>
</choose>
</for-each>
<text>]</text>
</template>
<template name='nested_object'>
<param name='no'/>
<param name='ew' select='false()'/>
<call-template name="indent"/>
<if test='$ew'><text>{</text></if>
<call-template name="quote-property">
<with-param name="name" select="translate(name($no),':','$')"/>
</call-template>
<text>:</text>
<call-template name="object_wrapper">
<with-param name="ow" select="$no"/>
</call-template>
<if test='$ew'><text>}</text></if>
<if test="$no/following-sibling::*">
<text>,</text>
</if>
</template>
<template name='object_wrapper'>
<param name='w'/>
<text>{</text>
<call-template name="object">
<with-param name="o" select="."/>
</call-template>
<call-template name="indent"/>
<text>}</text>
</template>
<template name='object'>
<param name="o"/>
<!-- JS: handle namespaces -->
<call-template name="namespaces"/>
<!-- JS: handle attributes -->
<call-template name="attrs"/>
<if test="count($o/@*)>0 and count($o/text() | $o/*)>0">
<text>,</text>
</if>
<choose>
<when test="count($o/text()) > 0 and
count($o/@*) > 0 and
count($o/text()) > 0 and
string-length(normalize-space($o/text()))>0">
<call-template name="indent"/>
<call-template name="mixed_content">
<with-param name="m" select="$o/*|$o/text()"/>
</call-template>
</when>
<otherwise>
<apply-templates select="$o/*"/>
</otherwise>
</choose>
</template>
<template name='array_wrapper'>
<param name='aw'/>
<param name='ew' select='false()'/>
<call-template name="indent"/>
<if test='$ew'><text>{</text></if>
<call-template name="quote-property">
<with-param name="name" select="translate(name($aw),':','$')"/>
</call-template>
<text>:</text>
<text>[</text>
<for-each select="$aw">
<call-template name='array_item'>
<with-param name='ai' select='.'/>
</call-template>
<if test="not(position()=last() or last()=1)">
<text>,</text>
</if>
</for-each>
<text>]</text>
<if test='$ew'><text>}</text></if>
</template>
<template name='array_item'>
<param name='ai'/>
<choose>
<when test="count($ai/*)>0">
<call-template name="object_wrapper">
<with-param name="w" select="$ai"/>
</call-template>
</when>
<when test="count($ai/*)=0 and $ai/text()">
<call-template name="value">
<with-param name="v" select="$ai/text()"/>
</call-template>
</when>
<otherwise>
<text>null</text>
</otherwise>
</choose>
</template>
<template name='property'>
<param name='p'/>
<param name='ew' select='false()'/>
<call-template name="indent"/>
<if test='$ew'><text>{</text></if>
<call-template name="quote-property">
<with-param name="name" select="translate(name($p),':','$')"/>
</call-template>
<text>:</text>
<call-template name="value">
<with-param name="v" select="$p/text()"/>
</call-template>
<if test='$ew'><text>}</text></if>
<if test="$p/following-sibling::*">
<text>,</text>
</if>
</template>
<template name='null_property'>
<param name='np'/>
<param name='ew' select='false()'/>
<call-template name="indent"/>
<if test='$ew'><text>{</text></if>
<call-template name="quote-property">
<with-param name="name" select="translate(name($np),':','$')"/>
</call-template>
<text>:null</text>
<if test='$ew'><text>}</text></if>
<if test="$np/following-sibling::*">,</if>
</template>
<!-- JS: attributes -->
<template name="attrs">
<if test="not(count(attribute::*)=0)">
<for-each select="attribute::*">
<call-template name="indent"/>
<call-template name="quote-property">
<with-param name="name" select="concat('$',translate(name(),':','$'))"/>
</call-template>
<text> : </text>
<call-template name="value">
<with-param name="v" select="."/>
</call-template>
<if test="not(position()=last() or last()=1)">
<text>,</text>
</if>
</for-each>
</if>
</template>
<!-- JS: namespaces -->
<template name="namespaces">
<if test="not(namespace-uri() = namespace-uri(parent::node()))">
<call-template name="indent"/>
<text> "$xmlns" : </text>
<call-template name="escape-string">
<with-param name="s" select="namespace-uri()"/>
</call-template>
<text>, </text>
</if>
<for-each select="child::* | attribute::*">
<variable name="this" select="."/>
<if test="not(name() = local-name()) and
not(preceding-sibling::node()[namespace-uri() = namespace-uri($this)])">
<variable name="prefix" select="substring-before(name(), ':')"/>
<call-template name="indent"/>
<call-template name="quote-property">
<with-param name="name" select="concat('$xmlns$',$prefix)"/>
</call-template>
<text>: </text>
<call-template name="escape-string">
<with-param name="s" select="namespace-uri()"/>
</call-template>
<if test="not(position()=last() or last()=1)">
<text>,</text>
</if>
</if>
</for-each>
</template>
<!-- Main template for escaping strings; used by above template and for object-properties
Responsibilities: placed quotes around string, and chain up to next filter, escape-bs-string -->
<template name="escape-string">
<param name="s"/>
<text>"</text>
<call-template name="escape-bs-string">
<with-param name="s" select="$s"/>
</call-template>
<text>"</text>
</template>
<!-- Escape the backslash (\) before everything else. -->
<template name="escape-bs-string">
<param name="s"/>
<choose>
<when test="contains($s,'\')">
<call-template name="escape-quot-string">
<with-param name="s" select="concat(substring-before($s,'\'),'\\')"/>
</call-template>
<call-template name="escape-bs-string">
<with-param name="s" select="substring-after($s,'\')"/>
</call-template>
</when>
<otherwise>
<call-template name="escape-quot-string">
<with-param name="s" select="$s"/>
</call-template>
</otherwise>
</choose>
</template>
<!-- Escape the double quote ("). -->
<template name="escape-quot-string">
<param name="s"/>
<choose>
<when test="contains($s,'"')">
<call-template name="encode-string">
<with-param name="s" select="concat(substring-before($s,'"'),'\"')"/>
</call-template>
<call-template name="escape-quot-string">
<with-param name="s" select="substring-after($s,'"')"/>
</call-template>
</when>
<otherwise>
<call-template name="encode-string">
<with-param name="s" select="$s"/>
</call-template>
</otherwise>
</choose>
</template>
<!-- Replace tab, line feed and/or carriage return by its matching escape code. Also escape tag-closes
(</tag> to <\/tag> for client-side javascript compliance). Can't escape backslash
or double quote here, because they don't replace characters (� becomes \t), but they prefix
characters (\ becomes \\). Besides, backslash should be seperate anyway, because it should be
processed first. This function can't do that. -->
<template name="encode-string">
<param name="s"/>
<value-of select="normalize-space($s)"/>
</template>
<!-- simple value type checking -->
<template name="value">
<param name="v"/>
<choose>
<!-- number (no support for javascript mantise) -->
<when test="not(string(number($v))='NaN') or
not(string(number($v))='NaN') ">
<value-of select="."/>
</when>
<!-- boolean, case-insensitive -->
<when test="translate($v,'TRUE','true')='true' or
translate($v,'TRUE','true')='true'">true</when>
<when test="translate($v,'FALSE','false')='false' or
translate($v,'FALSE','false')='false'">false</when>
<!-- JS: include comments -->
<when test="comment()">/*<value-of select="$v"/>*/</when>
<!-- JS: Date: YYYY-dd-mm[Thh] -->
<when test="string-length($v)=10
and string-length(translate(substring($v,1,4),'0123456789',''))=0
and substring($v,5,1)='-'
and string-length(translate(substring($v,6,2),'0123456789',''))=0
and substring($v,8,1)='-'
and string-length(translate(substring($v,9,2),'0123456789',''))=0">
<text>new Date(</text>
<value-of select="substring($v,1,4)"/>
<text>,</text>
<value-of select="substring($v,6,2)"/>
<text>-1,</text>
<value-of select="substring($v,9,2)"/>
<text>)</text>
</when>
<!-- JS: Date: YYYY-dd-mmThh:mm -->
<when test="string-length($v)=16
and string-length(translate(substring($v,1,4),'0123456789',''))=0
and substring($v,5,1)='-'
and string-length(translate(substring($v,6,2),'0123456789',''))=0
and substring($v,8,1)='-'
and string-length(translate(substring($v,9,2),'0123456789',''))=0
and substring($v,11,1)='T'
and string-length(translate(substring($v,12,2),'0123456789',''))=0
and substring($v,14,1)=':'
and string-length(translate(substring($v,15,2),'0123456789',''))=0 ">
<text>new Date(</text>
<value-of select="substring($v,1,4)"/>
<text>,</text>
<value-of select="substring($v,6,2)"/>
<text>-1,</text>
<value-of select="substring($v,9,2)"/>
<text>,</text>
<value-of select="substring($v,12,2)"/>
<text>,</text>
<value-of select="substring($v,15,2)"/>
<text>)</text>
</when>
<!-- JS: Date: YYYY-dd-mmThh:mm:ss -->
<when test="string-length($v)=19
and string-length(translate(substring($v,1,4),'0123456789',''))=0
and substring($v,5,1)='-'
and string-length(translate(substring($v,6,2),'0123456789',''))=0
and substring($v,8,1)='-'
and string-length(translate(substring($v,9,2),'0123456789',''))=0
and substring($v,11,1)='T'
and string-length(translate(substring($v,12,2),'0123456789',''))=0
and substring($v,14,1)=':'
and string-length(translate(substring($v,15,2),'0123456789',''))=0
and substring($v,17,1)=':'
and string-length(translate(substring($v,18,2),'0123456789',''))=0">
<text>new Date(</text>
<value-of select="substring($v,1,4)"/>
<text>,</text>
<value-of select="substring($v,6,2)"/>
<text>-1,</text>
<value-of select="substring($v,9,2)"/>
<text>,</text>
<value-of select="substring($v,12,2)"/>
<text>,</text>
<value-of select="substring($v,15,2)"/>
<text>,</text>
<value-of select="substring($v,18,2)"/>
<text>)</text>
</when>
<!-- string -->
<otherwise>
<call-template name="escape-string">
<with-param name="s" select="$v"/>
</call-template>
</otherwise>
</choose>
</template>
<!-- JS: don't quote if not necessary -->
<template name="quote-property">
<param name="name"/>
<call-template name="escape-string">
<with-param name="s" select="$name"/>
</call-template>
</template>
<!-- JS: indent for reability -->
<template name="indent">
<text>
</text>
<for-each select="ancestor::*">
<text> </text>
</for-each>
</template>
<!--
vvvvvvvvvvvvvvv Original Header Retained Below vvvvvvvvvvvvvvvvv
Copyright (c) 2006, Doeke Zanstra
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. 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.
Neither the name of the dzLib nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.
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.
-->
</stylesheet>