forked from Klortho/DtdAnalyzer
- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathxml2json-2.0.xsl
executable file
·960 lines (856 loc) · 29.7 KB
/
xml2json-2.0.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:np="http://ncbi.gov/portal/XSLT/namespace"
extension-element-prefixes="np">
<!-- Overridden by the instance stylesheet -->
<xsl:paramname="dtd-annotation"/>
<!-- Turn off pretty-printing by setting this to false() -->
<xsl:paramname='pretty'select='true()'/>
<!-- By default, do not convert all names to lowercase -->
<xsl:paramname='lcnames'select='false()'/>
<!-- $nl == the newline character -->
<xsl:variablename='nl'select='" "'/>
<!-- $nlp == newline when pretty-printing; otherwise empty string -->
<xsl:variablename='nlp'>
<xsl:choose>
<xsl:whentest='$pretty'>
<xsl:value-ofselect='$nl'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect='""'/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- $iu = indent unit (four spaces) when pretty-printing;
otherwise empty string -->
<xsl:variablename='iu'>
<xsl:choose>
<xsl:whentest='$pretty'>
<xsl:value-ofselect='" "'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect='""'/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--================================================
Utility templates and functions
-->
<!--
Convert a string to lowercase
-->
<xsl:variablename="lo"select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variablename="hi"select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:functionname='np:lower-case'>
<xsl:paramname='s'/>
<xsl:value-ofselect="translate($s, $hi, $lo)"/>
</xsl:function>
<!--
Converts the name of the current context element or attribute
into lowercase, only if the $lcnames param is true.
-->
<xsl:templatename="np:translate-name">
<xsl:paramname="s"select='name(.)'/>
<xsl:choose>
<xsl:whentest='$lcnames'>
<xsl:value-ofselect="np:lower-case($s)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect='$s'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:functionname="np:translate-name">
<xsl:paramname="s"/>
<xsl:call-templatename="np:translate-name">
<xsl:with-paramname="s"select="$s"/>
</xsl:call-template>
</xsl:function>
<!--
Quote a string to prepare it for insertion into a JSON literal value.
There are two versions coded up here: one with EXSLT's replace function, and
one (for older versions of libxslt) that uses straight XSLT recursion.
Point the np:json-escape function to the one you want to use.
-->
<xsl:functionname='np:json-escape'>
<xsl:paramname='s'/>
<xsl:value-ofselect='np:json-escape-with-replace($s)'/>
</xsl:function>
<xsl:functionname="np:json-escape-with-replace">
<xsl:paramname="s"/>
<xsl:variablename="quot">"</xsl:variable>
<xsl:variablename="bs">\</xsl:variable>
<xsl:variablename='nl'select='" "'/>
<xsl:variablename='cr'select='" "'/>
<xsl:variablename='tab'select='"	"'/>
<xsl:variablename="result"
select='replace(
replace(
replace(
replace(
replace($s, "\\", "\\\\"),
'"', '\\"'
),
"\n", "\\n"),
"\r", "\\r"),
"\t", "\\t")
'/>
<!--
select="replace(
replace(
replace(
replace(
replace($s, '\\', concat($bs, $bs)),
$quot, concat($bs, $quot)
),
'\n', concat($bs, 'n')
),
'\r', concat($bs, 'r')
),
'\t', concat($bs, 't')
)"
-->
<xsl:value-ofselect="$result"/>
</xsl:function>
<xsl:functionname="np:json-escape-with-recursion">
<xsl:paramname="s"/>
<xsl:variablename="quot">"</xsl:variable>
<xsl:variablename="bs">\</xsl:variable>
<xsl:variablename='nl'select='" "'/>
<xsl:variablename='cr'select='" "'/>
<xsl:variablename='tab'select='"	"'/>
<xsl:call-templatename='replace-char'>
<xsl:with-paramname='s'>
<xsl:call-templatename="replace-char">
<xsl:with-paramname='s'>
<xsl:call-templatename="replace-char">
<xsl:with-paramname='s'>
<xsl:call-templatename="replace-char">
<xsl:with-paramname='s'>
<xsl:call-templatename="replace-char">
<xsl:with-paramname='s'select='$s'/>
<xsl:with-paramname="c"select='$bs'/>
<xsl:with-paramname="r"select='concat($bs, $bs)'/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-paramname="c"select='$quot'/>
<xsl:with-paramname="r"select='concat($bs, $quot)'/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-paramname="c"select='$nl'/>
<xsl:with-paramname="r"select='concat($bs, "n")'/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-paramname="c"select='$cr'/>
<xsl:with-paramname="r"select='concat($bs, "r")'/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-paramname="c"select='$tab'/>
<xsl:with-paramname="r"select='concat($bs, "t")'/>
</xsl:call-template>
</xsl:function>
<xsl:templatename='replace-char'>
<xsl:paramname="s"/>
<xsl:paramname="c"/>
<xsl:paramname="r"/>
<xsl:choose>
<xsl:whentest='contains($s, $c)'>
<xsl:value-ofselect='concat(substring-before($s, $c), $r)'/>
<xsl:call-templatename='replace-char'>
<xsl:with-paramname="s"select="substring-after($s, $c)"/>
<xsl:with-paramname="c"select='$c'/>
<xsl:with-paramname="r"select='$r'/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect='$s'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
Convenience function to wrap any string in double-quotes. This
reduces the need for a lot of XML character escaping.
-->
<xsl:functionname='np:dq'>
<xsl:paramname='s'/>
<xsl:value-ofselect="concat('"', $s, '"')"/>
</xsl:function>
<!--
mkey = member key - this produces a string which is the key in double-quotes,
followed by a colon, space. It is used whenever outputting a member of a JSON
object.
-->
<xsl:functionname='np:mkey'>
<xsl:paramname='k'/>
<xsl:value-ofselect='concat(np:dq($k), ": ")'/>
</xsl:function>
<!--
This function takes a boolean indicating whether or not we want a trailing
comma. If false, it returns the empty string; if true, a comma. It's like
the ternary-operator expression ($trailing-comma ? "," : "").
-->
<xsl:functionname='np:tc'>
<xsl:paramname='trailing-comma'/>
<xsl:iftest='$trailing-comma'>
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:function>
<!--
There are five main utility functions for outputing stuff, as illustrated here.
Trailing commas are included only when the trailing-comma parameter is true.
Each of these is designed to be invoked at the start of a new line, so each
first outputs an indent, if that's given.
Function name Output
============= ======
simple(i, v, tc) value,\n
key-simple(i, k, v, tc) "k": value,\n
start-object(i) {\n
key-start-object(i, k) "k": {\n
end-object(i, tc) },\n
start-array(i) [\n
key-start-array(i, k) "k": [\n
end-array(i, tc) ],\n
-->
<xsl:functionname='np:simple'>
<xsl:paramname='indent'/>
<xsl:paramname='value'/>
<xsl:paramname='trailing-comma'/>
<xsl:value-ofselect='concat($indent, $value, np:tc($trailing-comma), $nlp)'/>
</xsl:function>
<xsl:functionname='np:key-simple'>
<xsl:paramname='indent'/>
<xsl:paramname='k'/>
<xsl:paramname='value'/>
<xsl:paramname='trailing-comma'/>
<xsl:value-ofselect='concat($indent, np:mkey($k), $value, np:tc($trailing-comma), $nlp)'/>
</xsl:function>
<xsl:functionname='np:start-object'>
<xsl:paramname='indent'/>
<xsl:value-ofselect='concat($indent, "{", $nlp)'/>
</xsl:function>
<xsl:functionname='np:key-start-object'>
<xsl:paramname='indent'/>
<xsl:paramname='k'/>
<xsl:value-ofselect='concat($indent, np:mkey($k), "{", $nlp)'/>
</xsl:function>
<xsl:functionname='np:end-object'>
<xsl:paramname='indent'/>
<xsl:paramname='trailing-comma'/>
<xsl:value-ofselect='concat($indent, "}", np:tc($trailing-comma), $nlp)'/>
</xsl:function>
<xsl:functionname='np:start-array'>
<xsl:paramname='indent'/>
<xsl:value-ofselect='concat($indent, "[", $nlp)'/>
</xsl:function>
<xsl:functionname='np:key-start-array'>
<xsl:paramname='indent'/>
<xsl:paramname='k'/>
<xsl:value-ofselect='concat($indent, np:mkey($k), "[", $nlp)'/>
</xsl:function>
<xsl:functionname='np:end-array'>
<xsl:paramname='indent'/>
<xsl:paramname='trailing-comma'/>
<xsl:value-ofselect='concat($indent, "]", np:tc($trailing-comma), $nlp)'/>
</xsl:function>
<!--
The following three return the json-escaped values for simple types
-->
<xsl:functionname='np:string-value'>
<xsl:paramname='v'/>
<xsl:value-ofselect='np:dq(np:json-escape($v))'/>
</xsl:function>
<xsl:functionname='np:strip-leading-zeros'>
<xsl:paramname='n'/>
<xsl:choose>
<xsl:whentest="starts-with($n, '0')">
<xsl:value-ofselect="np:strip-leading-zeros(substring($n, 2))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect="$n"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:functionname='np:number-value'>
<xsl:paramname='v'/>
<xsl:variablename='nsv'select='normalize-space($v)'/>
<xsl:choose>
<xsl:whentest='starts-with($nsv, "0") and not(starts-with($nsv, "0."))'>
<xsl:value-ofselect='np:strip-leading-zeros($nsv)'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect='$nsv'/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:functionname='np:boolean-value'>
<xsl:paramname='v'/>
<xsl:variablename='nv'select='np:lower-case(normalize-space($v))'/>
<xsl:choose>
<xsl:whentest='$nv = "0" or $nv = "no" or $nv = "n" or $nv = "false" or
$nv = "f" or $nv = "off" or $nv = ""'>
<xsl:text>false</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>true</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!--============================================================
Generic templates
-->
<!--
Default template for the document root node. This generates the outermost
JSON object and the header.
-->
<!--
Don't override this template that matches "/" in your custom stylesheets.
Instead, override the template named "root", which generates jxml.
-->
<xsl:templatematch="/">
<xsl:variablename='jxml'>
<xsl:call-templatename='root'/>
</xsl:variable>
<xsl:apply-templatesselect='$jxml/*'mode='serialize-jxml-array'/>
</xsl:template>
<xsl:templatename='root'>
<xsl:paramname='header-strings'select='$dtd-annotation/json'/>
<o>
<xsl:iftest='$header-strings'>
<xsl:call-templatename='header'>
<xsl:with-paramname='header-strings'select='$header-strings'/>
</xsl:call-template>
</xsl:if>
<xsl:apply-templatesselect='*'>
<xsl:with-paramname="context"select="'o'"/>
</xsl:apply-templates>
</o>
</xsl:template>
<xsl:templatename='header'>
<xsl:paramname='header-strings'/>
<ok='header'>
<xsl:for-eachselect='$header-strings/@*'>
<sk='{name(.)}'>
<xsl:value-ofselect='.'/>
</s>
</xsl:for-each>
</o>
</xsl:template>
<!--
s
There are separate template here for when we don't know the context
("s") or when we do ("s-in-o", "s-in-a").
-->
<xsl:templatename='s'>
<xsl:paramname='context'select='"unknown"'/>
<xsl:paramname='k'>
<xsl:choose>
<xsl:whentest='self::text()'>
<xsl:text>value</xsl:text>
</xsl:when>
<xsl:otherwise> <!-- This is an attribute or element node -->
<xsl:value-ofselect='np:translate-name(name(.))'/>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:paramname='value'select='.'/>
<xsl:choose>
<xsl:whentest='$context = "o"'>
<sk='{$k}'>
<xsl:value-ofselect='$value'/>
</s>
</xsl:when>
<xsl:whentest='$context = "a"'>
<s>
<xsl:value-ofselect='$value'/>
</s>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Error: context is not defined for element </xsl:text>
<xsl:value-ofselect='name(.)'/>
<xsl:text> ($context = "</xsl:text>
<xsl:value-ofselect='$context'/>
<xsl:text>", $k = "</xsl:text>
<xsl:value-ofselect='$k'/>
<xsl:text>")</xsl:text>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
s-in-o
This translates the node into a key:value pair. If it's a text node, then, by
default, the key will be "value". If it's an attribute or element node, then,
by default, the key will be the name converted to lowercase (it's up to you
to make sure they are unique within the object).
-->
<xsl:templatename='s-in-o'>
<xsl:paramname='k'>
<xsl:choose>
<xsl:whentest='self::text()'>
<xsl:text>value</xsl:text>
</xsl:when>
<xsl:otherwise> <!-- This is an attribute or element node -->
<xsl:value-ofselect='np:translate-name(name(.))'/>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:paramname='value'select='.'/>
<sk='{$k}'>
<xsl:value-ofselect='$value'/>
</s>
</xsl:template>
<!--
s-in-a
For text nodes, attributes, or elements that have simple content, when
in the context of a JSON array. This discards the attribute or element name,
and produces a quoted string from the content.
-->
<xsl:templatename='s-in-a'>
<xsl:paramname='value'select='.'/>
<s>
<xsl:value-ofselect='$value'/>
</s>
</xsl:template>
<!--
n
Delegates either to n-in-o or n-in-a.
-->
<xsl:templatename='n'>
<xsl:paramname='context'select='"unknown"'/>
<xsl:paramname='k'>
<xsl:choose>
<xsl:whentest='self::text()'>
<xsl:text>value</xsl:text>
</xsl:when>
<xsl:otherwise> <!-- This is an attribute or element node -->
<xsl:value-ofselect='np:translate-name(name(.))'/>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:paramname='value'select='.'/>
<xsl:choose>
<xsl:whentest='$context = "o"'>
<nk='{$k}'>
<xsl:value-ofselect='np:number-value($value)'/>
</n>
</xsl:when>
<xsl:whentest='$context = "a"'>
<n>
<xsl:value-ofselect='np:number-value($value)'/>
</n>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Error: context is not defined for element </xsl:text>
<xsl:value-ofselect='name(.)'/>
<xsl:text> ($context = "</xsl:text>
<xsl:value-ofselect='$context'/>
<xsl:text>", $k = "</xsl:text>
<xsl:value-ofselect='$k'/>
<xsl:text>")</xsl:text>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
n-in-o
For text nodes, attributes, or elements that have simple
content, when in the context of a JSON object.
-->
<xsl:templatename='n-in-o'>
<xsl:paramname='k'>
<xsl:choose>
<xsl:whentest='self::text()'>
<xsl:text>value</xsl:text>
</xsl:when>
<xsl:otherwise> <!-- This is an attribute or element node -->
<xsl:value-ofselect='np:translate-name(name(.))'/>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:paramname='value'select='.'/>
<nk='{$k}'>
<xsl:value-ofselect='np:number-value($value)'/>
</n>
</xsl:template>
<!--
n-in-a
For text nodes, attributes, or elements that have simple content, when
in the context of a JSON array. This discards the attribute or element name,
and produces a quoted string from the content.
-->
<xsl:templatename='n-in-a'>
<xsl:paramname='value'select='.'/>
<n>
<xsl:value-ofselect='np:number-value($value)'/>
</n>
</xsl:template>
<!--
b
Delegates either to b-in-o or b-in-a.
-->
<xsl:templatename='b'>
<xsl:paramname='context'select='"unknown"'/>
<xsl:paramname='k'>
<xsl:choose>
<xsl:whentest='self::text()'>
<xsl:text>value</xsl:text>
</xsl:when>
<xsl:otherwise> <!-- This is an attribute or element node -->
<xsl:value-ofselect='np:translate-name(name(.))'/>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:paramname='value'select='.'/>
<xsl:choose>
<xsl:whentest='$context = "o"'>
<bk='{$k}'>
<xsl:value-ofselect='np:boolean-value($value)'/>
</b>
</xsl:when>
<xsl:whentest='$context = "a"'>
<b>
<xsl:value-ofselect='np:boolean-value($value)'/>
</b>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Error: context is not defined for element </xsl:text>
<xsl:value-ofselect='name(.)'/>
<xsl:text> ($context = "</xsl:text>
<xsl:value-ofselect='$context'/>
<xsl:text>", $k = "</xsl:text>
<xsl:value-ofselect='$k'/>
<xsl:text>")</xsl:text>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
b-in-o
For text nodes, attributes, or elements that have simple
content, when in the context of a JSON object.
-->
<xsl:templatename='b-in-o'>
<xsl:paramname='k'>
<xsl:choose>
<xsl:whentest='self::text()'>
<xsl:text>value</xsl:text>
</xsl:when>
<xsl:otherwise> <!-- This is an attribute or element node -->
<xsl:value-ofselect='np:translate-name(name(.))'/>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:paramname='value'select='.'/>
<bk='{$k}'>
<xsl:value-ofselect='np:boolean-value($value)'/>
</b>
</xsl:template>
<!--
b-in-a
For text nodes, attributes, or elements that have simple content, when
in the context of a JSON array. This discards the attribute or element name,
and produces a quoted string from the content.
-->
<xsl:templatename='b-in-a'>
<xsl:paramname='value'select='.'/>
<b>
<xsl:value-ofselect='np:boolean-value($value)'/>
</b>
</xsl:template>
<!--
a
Delegates either to a-in-o or a-in-a.
-->
<xsl:templatename='a'>
<xsl:paramname='context'select='"unknown"'/>
<xsl:paramname='k'select='""'/>
<xsl:paramname='kids'select='*'/>
<xsl:choose>
<xsl:whentest='$context = "o" and $k = ""'>
<xsl:call-templatename='a-in-o'>
<xsl:with-paramname='kids'select='$kids'/>
</xsl:call-template>
</xsl:when>
<xsl:whentest='$context = "o" and $k != ""'>
<xsl:call-templatename='a-in-o'>
<xsl:with-paramname='k'select='$k'/>
<xsl:with-paramname='kids'select='$kids'/>
</xsl:call-template>
</xsl:when>
<xsl:whentest='$context = "a"'>
<xsl:call-templatename="a-in-a">
<xsl:with-paramname='kids'select='$kids'/>
</xsl:call-template>
</xsl:when>
<xsl:whentest='$context = "o"'>
<xsl:message>
<xsl:text>Error: bad key passed in for element </xsl:text>
<xsl:value-ofselect='name(.)'/>
</xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Error: context is not defined for element </xsl:text>
<xsl:value-ofselect='name(.)'/>
<xsl:text> ($context = "</xsl:text>
<xsl:value-ofselect='$context'/>
<xsl:text>", $k = "</xsl:text>
<xsl:value-ofselect='$k'/>
<xsl:text>")</xsl:text>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
a-in-o
Call this template for array-type elements. That is, usually, elements
whose content is a list of child elements with the same name.
By default, the key will be the name converted to lowercase.
This produces a JSON array. The "kids" are the set of child elements only,
so attributes and text node children are discarded.
-->
<xsl:templatename='a-in-o'>
<xsl:paramname='k'select='np:translate-name(name(.))'/>
<xsl:paramname='kids'select='*'/>
<ak='{$k}'>
<xsl:apply-templatesselect='$kids'>
<xsl:with-paramname='context'select='"a"'/>
</xsl:apply-templates>
</a>
</xsl:template>
<!--
a-in-a
Array-type elements that occur inside other arrays.
-->
<xsl:templatename='a-in-a'>
<xsl:paramname='k'select='np:translate-name(name(.))'/>
<xsl:paramname='kids'select='*'/>
<a>
<xsl:apply-templatesselect='$kids'>
<xsl:with-paramname='context'select='"a"'/>
</xsl:apply-templates>
</a>
</xsl:template>
<!--
o
Delegates either to o-in-o or o-in-a.
-->
<xsl:templatename='o'>
<xsl:paramname='context'select='"unknown"'/>
<xsl:paramname='k'select='np:translate-name(name(.))'/>
<xsl:paramname='kids'select='@*|*'/>
<xsl:choose>
<xsl:whentest='$context = "o"'>
<ok='{$k}'>
<xsl:apply-templatesselect='$kids'>
<xsl:with-paramname='context'select='"o"'/>
</xsl:apply-templates>
</o>
</xsl:when>
<xsl:whentest='$context = "a"'>
<o>
<xsl:apply-templatesselect='$kids'>
<xsl:with-paramname='context'select='"o"'/>
</xsl:apply-templates>
</o>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Error: context is not defined for element </xsl:text>
<xsl:value-ofselect='name(.)'/>
<xsl:text> ($context = "</xsl:text>
<xsl:value-ofselect='$context'/>
<xsl:text>", $k = "</xsl:text>
<xsl:value-ofselect='$k'/>
<xsl:text>")</xsl:text>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
o-in-o
For elements that have attributes and/or heterogenous content. These are
converted into JSON objects.
The key, by default, is taken from this element's name, converted to lowercase.
By default, this recurses by calling apply-templates on all attributes and
element children. So text-node children are discarded. You can override that
by passing the $kids param.
-->
<xsl:templatename='o-in-o'>
<xsl:paramname='k'select='np:translate-name(name(.))'/>
<xsl:paramname='kids'select='@*|*'/>
<ok='{$k}'>
<xsl:apply-templatesselect='$kids'>
<xsl:with-paramname='context'select='"o"'/>
</xsl:apply-templates>
</o>
</xsl:template>
<!--
o-in-a
For elements that contain heterogenous content. These are converted
into JSON objects.
-->
<xsl:templatename='o-in-a'>
<xsl:paramname='kids'select='@*|*'/>
<o>
<xsl:apply-templatesselect='$kids'>
<xsl:with-paramname='context'select='"o"'/>
</xsl:apply-templates>
</o>
</xsl:template>
<!--
Default template for an element or attribute.
Reports a problem.
-->
<xsl:templatematch='@*|*'>
<xsl:paramname='context'select='"o"'/>
<xsl:message>
<xsl:text>FIXME: No template defined for </xsl:text>
<xsl:choose>
<xsl:whentest='self::*'>
<xsl:text>element </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>attribute </xsl:text>
<xsl:value-ofselect='concat(name(..), "/@")'/>
</xsl:otherwise>
</xsl:choose>
<xsl:value-ofselect='name(.)'/>
<xsl:value-ofselect='concat(" (context = ", $context, ")")'/>
</xsl:message>
<xsl:choose>
<xsl:whentest='$context = "a"'>
<xsl:call-templatename='s-in-a'/>
</xsl:when>
<xsl:otherwise>
<xsl:call-templatename='s-in-o'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Default template for text nodes. Throw them away if they
are all blanks. Report a problem otherwise.
Let's not report a problem and see if that's okay. See sample5, test45
and following.
-->
<xsl:templatematch="text()" >
<xsl:paramname='context'select='"o"'/>
<xsl:iftest='normalize-space(.) != ""'>
<!--
<xsl:message>
<xsl:text>FIXME: non-blank text node with no template match. Parent element: </xsl:text>
<xsl:value-of select='name(..)'/>
</xsl:message>
-->
<xsl:choose>
<xsl:whentest='$context = "a"'>
<xsl:call-templatename='s-in-a'/>
</xsl:when>
<xsl:otherwise>
<xsl:call-templatename='s-in-o'/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!--=====================================================
The following templates serialize jxml into JSON.
These use mode to also encode the context: either -object
or -array. I think this will probably give better
performance than passing it down as a parameter.
-->
<xsl:templatematch='o'mode='serialize-jxml-array'>
<xsl:paramname='indent'select='""'/>
<xsl:value-ofselect='np:start-object($indent)'/>
<xsl:apply-templatesselect='*'mode='serialize-jxml-object'>
<xsl:with-paramname='indent'select='concat($indent, $iu)'/>
</xsl:apply-templates>
<xsl:value-ofselect='np:end-object($indent, position() != last())'/>
</xsl:template>
<xsl:templatematch='o'mode='serialize-jxml-object'>
<xsl:paramname='indent'select='""'/>
<xsl:value-ofselect='np:key-start-object($indent, @k)'/>
<xsl:apply-templatesselect='*'mode='serialize-jxml-object'>
<xsl:with-paramname='indent'select='concat($indent, $iu)'/>
</xsl:apply-templates>
<xsl:value-ofselect='np:end-object($indent, position() != last())'/>
</xsl:template>
<xsl:templatematch='a'mode='serialize-jxml-array'>
<xsl:paramname='indent'select='""'/>
<xsl:value-ofselect='np:start-array($indent)'/>
<xsl:apply-templatesselect='*'mode='serialize-jxml-array'>
<xsl:with-paramname='indent'select='concat($indent, $iu)'/>
</xsl:apply-templates>
<xsl:value-ofselect='np:end-array($indent, position() != last())'/>
</xsl:template>
<xsl:templatematch='a'mode='serialize-jxml-object'>
<xsl:paramname='indent'select='""'/>
<xsl:value-ofselect='np:key-start-array($indent, @k)'/>
<xsl:apply-templatesselect='*'mode='serialize-jxml-array'>
<xsl:with-paramname='indent'select='concat($indent, $iu)'/>
</xsl:apply-templates>
<xsl:value-ofselect='np:end-array($indent, position() != last())'/>
</xsl:template>
<xsl:templatematch='s|n|b'mode='serialize-jxml-array'>
<xsl:paramname='indent'select='""'/>
<xsl:variablename='v'>
<xsl:choose>
<xsl:whentest="name(.) = 'n'">
<xsl:value-ofselect='np:number-value(.)'/>
</xsl:when>
<xsl:whentest='. = "" or name(.) != "b"'>
<xsl:value-ofselect='np:dq(np:json-escape(.))'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect='.'/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-ofselect='np:simple($indent, $v, position() != last())'/>
</xsl:template>
<xsl:templatematch='s|n|b'mode='serialize-jxml-object'>
<xsl:paramname='indent'select='""'/>
<xsl:variablename='v'>
<xsl:choose>
<xsl:whentest="name(.) = 'n'">
<xsl:value-ofselect='np:number-value(.)'/>
</xsl:when>
<xsl:whentest='. = "" or name(.) != "b"'>
<xsl:value-ofselect='np:dq(np:json-escape(.))'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect='.'/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-ofselect='np:key-simple($indent, @k, $v, position() != last())'/>
</xsl:template>
<!--=====================================================
This last template is used in testing the output for consistency, and
is enabled when the 'check-json' command line option is given. It first
generates the JXML, and then runs it, recursively, through the checker
template with mode="check-jxml".
-->
<xsl:templatename='check-json'>
<xsl:variablename='jxml-v'>
<xsl:call-templatename="root"/>
</xsl:variable>
<xsl:variablename='jxml'select='$jxml-v/*'/>
<xsl:for-eachselect='$jxml'>
<xsl:apply-templatesmode='check-jxml'/>
</xsl:for-each>
<xsl:apply-templatesselect='$jxml'mode='serialize-jxml-array'/>
</xsl:template>
<xsl:templatematch='*'mode='check-jxml'>
<xsl:variablename='k'select='@k'/>
<xsl:iftest='$k = ""'>
<xsl:messageterminate="yes">Empty key found in JSON result!</xsl:message>
</xsl:if>
<xsl:iftest='$k != "" and preceding-sibling::*[@k = $k]'>
<xsl:messageterminate="yes">Duplicate key found in JSON result!</xsl:message>
</xsl:if>
<xsl:apply-templatesselect='*'mode='check-jxml'/>
</xsl:template>
</xsl:stylesheet>