-
Notifications
You must be signed in to change notification settings - Fork 266
/
generic-xml-to-json.xslt
439 lines (404 loc) · 16.8 KB
/
generic-xml-to-json.xslt
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
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://json.org/">
<xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<!--
XSLTJSON v1.0.93.
You can use these parameters to control the output by supplying them to
stylesheet. Consult the manual of your XSLT processor for instructions
on how to pass parameters to a stylesheet.
* debug - Enable or disable the output of the temporary
XML tree used to generate JSON output.
* use-rabbitfish - Output basic JSON with a '@' to indicate XML
attributes.
* use-badgerfish - Use the BadgerFish (http://badgerfish.ning.com/)
convention to output JSON without XML namespaces.
* use-rayfish - Use the RayFish (http://onperl.org/blog/onperl/page/rayfish)
convention to output JSON without XML namespaces.
* use-namespaces - Output XML namespaces according to the
BadgerFish convention.
* skip-root - Skip the root XML element.
* jsonp - Enable JSONP; the JSON output will be prepended with
the value of the jsonp parameter and wrapped in parentheses.
Credits:
Chick Markley ([email protected]) - Octal number & numbers with terminating period.
Torben Schreiter ([email protected]) - Suggestions for skip root and node list.
Michael Nilsson - Bug report and unit tests for json:force-array feature.
Frank Schwichtenberg - Namespace prefix name bug.
Wilson Cheung - Bug report and fix for invalid number serialization.
Danny Cohn - Bug report and fix for invalid floating point number serialization.
Copyright:
2006-2014, Bram Stein
Licensed under the new BSD License.
All rights reserved.
-->
<xsl:param name="debug" as="xs:boolean" select="false()"/>
<xsl:param name="use-rabbitfish" as="xs:boolean" select="false()"/>
<xsl:param name="use-badgerfish" as="xs:boolean" select="false()"/>
<xsl:param name="use-namespaces" as="xs:boolean" select="false()"/>
<xsl:param name="use-rayfish" as="xs:boolean" select="false()"/>
<xsl:param name="jsonp" as="xs:string" select="''"/>
<xsl:param name="skip-root" as="xs:boolean" select="false()"/>
<!--
If you import or include the stylesheet in your own stylesheet you
can use this function to transform any XML node to JSON.
-->
<xsl:function name="json:generate" as="xs:string">
<xsl:param name="input" as="node()"/>
<xsl:variable name="json-tree">
<json:object>
<xsl:copy-of select="if (not($use-rayfish)) then json:create-node($input, false()) else json:create-simple-node($input)"/>
</json:object>
</xsl:variable>
<xsl:variable name="json-mtree">
<xsl:choose>
<xsl:when test="$skip-root">
<xsl:copy-of select="$json-tree/json:object/json:member/json:value/child::node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$json-tree"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="output">
<xsl:choose>
<xsl:when test="normalize-space($jsonp)">
<xsl:value-of select="$jsonp"/><xsl:text>(</xsl:text><xsl:apply-templates select="$json-mtree" mode="json"/><xsl:text>)</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text/><xsl:apply-templates select="$json-mtree" mode="json"/><xsl:text/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:sequence select="$output"/>
</xsl:function>
<!--
Template to match the root node so that the stylesheet can also
be used on the command line.
-->
<xsl:template match="/*">
<xsl:choose>
<xsl:when test="$debug">
<xsl:variable name="json-tree">
<json:object>
<xsl:copy-of select="if (not($use-rayfish)) then json:create-node(., false()) else json:create-simple-node(.)"/>
</json:object>
</xsl:variable>
<debug>
<xsl:copy-of select="$json-tree"/>
</debug>
<xsl:apply-templates select="$json-tree" mode="json"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="json:generate(.)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
All methods below are private methods and should not be used
standalone.
-->
<xsl:template name="json:build-tree">
<xsl:param name="input" as="node()"/>
<json:object>
<xsl:copy-of select="if (not($use-rayfish)) then json:create-node($input, false()) else json:create-simple-node($input/child::node())"/>
</json:object>
</xsl:template>
<xsl:function name="json:create-simple-node-member" as="node()">
<xsl:param name="type" as="xs:string"/>
<xsl:param name="value"/>
<json:member>
<json:name><xsl:value-of select="$type"/></json:name>
<json:value><xsl:copy-of select="$value"/></json:value>
</json:member>
</xsl:function>
<xsl:function name="json:create-simple-node" as="node()*">
<xsl:param name="node" as="node()"/>
<xsl:copy-of select="json:create-simple-node-member('#name', $node/local-name())"/>
<xsl:copy-of select="json:create-simple-node-member('#text', $node/child::text())"/>
<xsl:variable name="empty-array">
<json:array/>
</xsl:variable>
<xsl:variable name="children">
<json:array>
<xsl:for-each select="$node/@*">
<json:array-value>
<json:value>
<json:object>
<xsl:copy-of select="json:create-simple-node-member('#name', concat('@',./local-name()))"/>
<xsl:copy-of select="json:create-simple-node-member('#text', string(.))"/>
<xsl:copy-of select="json:create-simple-node-member('#children', $empty-array)"/>
</json:object>
</json:value>
</json:array-value>
</xsl:for-each>
<xsl:for-each select="$node/child::element()">
<json:array-value>
<json:value>
<json:object>
<xsl:copy-of select="json:create-simple-node(.)"/>
</json:object>
</json:value>
</json:array-value>
</xsl:for-each>
</json:array>
</xsl:variable>
<xsl:copy-of select="json:create-simple-node-member('#children', $children)"/>
</xsl:function>
<xsl:function name="json:create-node" as="node()">
<xsl:param name="node" as="node()"/>
<xsl:param name="in-array" as="xs:boolean"/>
<xsl:choose>
<xsl:when test="$in-array">
<json:array-value>
<json:value>
<xsl:copy-of select="json:create-children($node)"/>
</json:value>
</json:array-value>
</xsl:when>
<xsl:otherwise>
<json:member>
<xsl:copy-of select="json:create-string($node)"/>
<json:value>
<xsl:copy-of select="json:create-children($node)"/>
</json:value>
</json:member>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="json:create-children">
<xsl:param name="node" as="node()"/>
<xsl:choose>
<xsl:when test="exists($node/child::text()) and count($node/child::node()) eq 1">
<xsl:choose>
<xsl:when test="(count($node/namespace::*) gt 0 and $use-namespaces) or count($node/@*[not(../@json:force-array) or count(.|../@json:force-array)=2]) gt 0">
<json:object>
<xsl:copy-of select="json:create-namespaces($node)"/>
<xsl:copy-of select="json:create-attributes($node)"/>
<json:member>
<json:name>$</json:name>
<json:value><xsl:value-of select="$node"/></json:value>
</json:member>
</json:object>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="json:create-text-value($node)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="exists($node/child::text())">
<xsl:choose>
<xsl:when test="(count($node/namespace::*) gt 0 and $use-namespaces) or count($node/@*[not(../@json:force-array) or count(.|../@json:force-array)=2]) gt 0">
<json:object>
<xsl:copy-of select="json:create-namespaces($node)"/>
<xsl:copy-of select="json:create-attributes($node)"/>
<json:member>
<json:name>$</json:name>
<json:value>
<xsl:copy-of select="json:create-mixed-array($node)"/>
</json:value>
</json:member>
</json:object>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="json:create-mixed-array($node)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="exists($node/child::node()) or ((count($node/namespace::*) gt 0 and $use-namespaces) or count($node/@*[not(../@json:force-array) or count(.|../@json:force-array)=2]) gt 0)">
<json:object>
<xsl:copy-of select="json:create-namespaces($node)"/>
<xsl:copy-of select="json:create-attributes($node)"/>
<xsl:for-each-group select="$node/child::node()" group-adjacent="local-name()">
<xsl:choose>
<xsl:when test="count(current-group()) eq 1 and (not(exists(./@json:force-array)) or ./@json:force-array eq 'false')">
<xsl:copy-of select="json:create-node(current-group()[1], false())"/>
</xsl:when>
<xsl:otherwise>
<json:member>
<json:name><xsl:value-of select="if($use-namespaces) then current-group()[1]/name() else current-group()[1]/local-name()"/></json:name>
<json:value>
<json:array>
<xsl:for-each select="current-group()">
<xsl:copy-of select="json:create-node(.,true())"/>
</xsl:for-each>
</json:array>
</json:value>
</json:member>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</json:object>
</xsl:when>
</xsl:choose>
</xsl:function>
<xsl:function name="json:create-mixed-array" as="node()">
<xsl:param name="node" as="node()"/>
<json:array>
<xsl:for-each select="$node/child::node()">
<json:array-value>
<json:value>
<xsl:choose>
<xsl:when test="self::text()">
<xsl:copy-of select="json:create-text-value(.)"/>
</xsl:when>
<xsl:otherwise>
<json:object>
<xsl:copy-of select="json:create-node(.,false())"/>
</json:object>
</xsl:otherwise>
</xsl:choose>
</json:value>
</json:array-value>
</xsl:for-each>
</json:array>
</xsl:function>
<xsl:function name="json:create-text-value" as="node()">
<xsl:param name="node" as="node()"/>
<xsl:choose>
<xsl:when test="$use-badgerfish">
<json:object>
<json:member>
<json:name>$</json:name>
<json:value>
<xsl:value-of select="$node"/>
</json:value>
</json:member>
</json:object>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$node"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="json:create-string" as="node()">
<xsl:param name="node" as="node()"/>
<xsl:choose>
<xsl:when test="$use-namespaces">
<json:name><xsl:value-of select="$node/name()"/></json:name>
</xsl:when>
<xsl:otherwise>
<json:name><xsl:value-of select="$node/local-name()"/></json:name>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="json:create-attributes" as="node()*">
<xsl:param name="node" as="node()"/>
<xsl:for-each select="$node/@*[not(../@json:force-array) or count(.|../@json:force-array)=2]">
<json:member>
<json:name><xsl:if test="$use-badgerfish or $use-rabbitfish">@</xsl:if><xsl:value-of select="if($use-namespaces) then name() else local-name()"/></json:name>
<json:value><xsl:value-of select="."/></json:value>
</json:member>
</xsl:for-each>
</xsl:function>
<xsl:function name="json:create-namespaces" as="node()*">
<xsl:param name="node" as="node()"/>
<xsl:if test="$use-namespaces">
<xsl:if test="count($node/namespace::*) gt 0">
<json:member>
<json:name><xsl:if test="$use-badgerfish or $use-rabbitfish">@</xsl:if>xmlns</json:name>
<json:value>
<json:object>
<xsl:for-each select="$node/namespace::*">
<json:member>
<xsl:choose>
<xsl:when test="local-name(.) eq ''">
<json:name>$</json:name>
</xsl:when>
<xsl:otherwise>
<json:name><xsl:value-of select="local-name(.)"/></json:name>
</xsl:otherwise>
</xsl:choose>
<json:value><xsl:value-of select="."/></json:value>
</json:member>
</xsl:for-each>
</json:object>
</json:value>
</json:member>
</xsl:if>
</xsl:if>
</xsl:function>
<!--
These are output functions that transform the temporary tree
to JSON.
-->
<xsl:template match="json:parameter" mode="json">
<xsl:variable name="parameters"><xsl:apply-templates mode="json"/></xsl:variable>
<xsl:value-of select="string-join($parameters/parameter, ', ')"/>
</xsl:template>
<xsl:template match="json:object" mode="json">
<xsl:variable name="members"><xsl:apply-templates mode="json"/></xsl:variable>
<parameter>
<xsl:text/>{<xsl:text/>
<xsl:value-of select="string-join($members/member,',')"/>
<xsl:text/>}<xsl:text/>
</parameter>
</xsl:template>
<xsl:template match="json:member" mode="json">
<xsl:text/><member><xsl:apply-templates mode="json"/></member><xsl:text/>
</xsl:template>
<xsl:function name="json:encode-string" as="xs:string">
<xsl:param name="string" as="xs:string"/>
<xsl:sequence select="replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace($string,
'\\','\\\\'),
'/', '\\/'),
'"', '\\"'),
'
','\\n'),
'
','\\r'),
'	','\\t'),
'\n','\\n'),
'\r','\\r'),
'\t','\\t')"/>
</xsl:function>
<xsl:template match="json:name" mode="json">
<xsl:text/>"<xsl:value-of select="json:encode-string(.)"/>":<xsl:text/>
</xsl:template>
<xsl:template match="json:value" mode="json">
<xsl:choose>
<xsl:when test="node() and not(text())">
<xsl:apply-templates mode="json"/>
</xsl:when>
<xsl:when test="text()">
<xsl:choose>
<!--
A value is considered a string if the following conditions are met:
* There is whitespace/formatting around the value of the node.
* The value is not a valid JSON number (i.e. '01', '+1', '-01', '1.', and '.5' are not valid JSON numbers.)
* The value does not equal the any of the following strings: 'false', 'true', 'null'.
-->
<xsl:when test="normalize-space(.) ne . or not((string(.) castable as xs:integer and not(starts-with(string(.),'+') or starts-with(string(.),'-')) and not(starts-with(string(.),'0') and not(. = '0'))) or (string(.) castable as xs:decimal and not(starts-with(string(.),'+')) and not(starts-with(.,'-.')) and not(starts-with(.,'.')) and not(starts-with(.,'-0') and not(starts-with(.,'-0.'))) and not(ends-with(.,'.')) and not(starts-with(.,'0') and not(starts-with(.,'0.'))))) and not(. = 'false') and not(. = 'true') and not(. = 'null')">
<xsl:text/>"<xsl:value-of select="json:encode-string(.)"/>"<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:text/><xsl:value-of select="."/><xsl:text/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:text/>null<xsl:text/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="json:array-value" mode="json">
<xsl:text/><value><xsl:apply-templates mode="json"/></value><xsl:text/>
</xsl:template>
<xsl:template match="json:array" mode="json">
<xsl:variable name="values">
<xsl:apply-templates mode="json"/>
</xsl:variable>
<xsl:text/>[<xsl:text/>
<xsl:value-of select="string-join($values/value,',')"/>
<xsl:text/>]<xsl:text/>
</xsl:template>
</xsl:stylesheet>