-
Notifications
You must be signed in to change notification settings - Fork 0
/
X3dToJson.xslt
4152 lines (4011 loc) · 302 KB
/
X3dToJson.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
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2001-2023 held by the author(s). 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 names of the Naval Postgraduate School (NPS)
Modeling Virtual Environments and Simulation (MOVES) Institute
(https://www.nps.edu and https://www.MovesInstitute.org)
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.
-->
<!--
<head>
<meta name="title" content="X3dToJson.xslt"/>
<meta name="author" content="Don Brutzman"/>
<meta name="contributor" content="John Carlson provided lots of design guidance and co-development support"/>
<meta name="contributor" content="Roy Walmsley fixed recursive array overflows through tokenization and also helped with encoding development"/>
<meta name="created" content="8 October 2014"/>
<meta name="description" content="XSLT stylesheet to convert X3D source into JSON syntax."/>
<meta name="reference" content="https://www.web3d.org/x3d/stylesheets/X3dToJson.html"/>
<meta name="reference" content="https://www.web3d.org/wiki/index.php/X3D_JSON_Encoding"/>
<meta name="reference" content="https://www.freeformatter.com/xml-to-json-converter.html"/>
<meta name="reference" content="https://json.org"/>
<meta name="reference" content="https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf"/>
<meta name="reference" content="https://www.w3.org/TR/xslt"/>
<meta name="reference" content="XML Spy, https://www.xmlspy.com"/>
<meta name="reference" content="SAXON XML Toolkit, https://saxon.sourceforge.net"/>
<meta name="generator" content="X3D-Edit 4.0, https://savage.nps.edu/X3D-Edit"/>
<meta name="identifier" content="https://www.web3d.org/x3d/stylesheets/X3dToJson.xslt"/>
<meta name="reference" content="https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/stylesheets/X3dToJson.xslt"/>
<meta name="license" content="license.html"/>
</head>
-->
<!--
=======================================================================
X3D JSON Design Considerations and X3dToJson.xslt Converter Status
https://www.web3d.org/x3d/stylesheets/X3dToJson.html
=======================================================================
-->
<xsl:stylesheet version="2.0" exclude-result-prefixes="ds saxon"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:saxon="http://saxon.sf.net/">
<!--
xmlns="http://www.w3.org/TR/xhtml1/strict"
xmlns:fn="http://www.w3.org/2005/xpath-functions" -->
<xsl:param name="stripComments"><xsl:text>false</xsl:text></xsl:param>
<xsl:param name="stripDefaultAttributes"><xsl:text>true</xsl:text></xsl:param>
<xsl:param name="indentEnabled"><xsl:text>true</xsl:text></xsl:param>
<xsl:param name="normalizeCommentWhitespace"><xsl:text>true</xsl:text></xsl:param>
<xsl:param name="sourceTextMode"><xsl:text>strings</xsl:text></xsl:param> <!-- escaped | strings | plaintext -->
<xsl:param name="traceEnabled" ><xsl:text>false</xsl:text></xsl:param>
<xsl:param name="traceScripts" ><xsl:text>false</xsl:text></xsl:param>
<!-- TODO future feature: whether to apply changes to meta references, url file extensions, etc. -->
<xsl:param name="updateContent" ><xsl:text>false</xsl:text></xsl:param>
<xsl:variable name="x3dVersion" select="normalize-space(//X3D/@version)"/>
<xsl:variable name="isX3D3" select="starts-with($x3dVersion,'3')"/>
<xsl:variable name="isX3D4" select="starts-with($x3dVersion,'4')"/>
<!-- saxon9he problem: fails due to line length, licensing issue: saxon:line-length="10000" -->
<!-- https://stackoverflow.com/questions/23084785/xslt-avoid-new-line-added-between-element-attributes/43301327#43301327 -->
<xsl:output method="text" encoding="UTF-8"/> <!-- output methods: xml html text -->
<!-- encodings references -->
<!-- https://www.sagehill.net/docbookxsl/SpecialChars.html -->
<!-- https://stackoverflow.com/questions/9328882/encoding-special-chars-in-xslt-output -->
<!-- https://stackoverflow.com/questions/4901133/json-and-escaping-characters -->
<!-- global variables -->
<!-- Identification of unit-test scenes rule is used in X3D Schematron and X3dToJson.xslt -->
<xsl:variable name="isTestScene"
select="(//meta[(@name='error') or (@name='warning') or (@name='hint') or (@name='info') or (@name='TODO')][starts-with(lower-case(@content),'test')]) or
(//meta[(@name='title')][starts-with(lower-case(@content),'test') or ends-with(lower-case(@content),'test.x3d')])"/>
<xsl:template match="/"> <!-- process root of input document -->
<xsl:apply-templates select="X3D | AllX3dElementsAttributes"> <!-- process top-level X3D element. DOCTYPE dropped, external comments handled next. -->
<xsl:with-param name="indent"><xsl:text>0</xsl:text></xsl:with-param>
</xsl:apply-templates>
<!-- report if comments encountered outside of the X3D document, do not convert/move them into the JSON object -->
<!-- note preceding:: and following:: constraints refer to the position of the X3D element with respect to the comment, hence logic appears reversed -->
<xsl:if test="comment()[following::X3D]">
<xsl:message>
<xsl:text>Warning: comments preceding the top-level X3D element are not translated as part of the X3D JSON object file.</xsl:text>
<xsl:text> </xsl:text>
<xsl:for-each select="comment()[following::X3D]">
<xsl:text><!--</xsl:text>
<xsl:value-of select="."/>
<xsl:text>--></xsl:text>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:message>
</xsl:if>
<xsl:if test="comment()[preceding::X3D]">
<xsl:message>
<xsl:text>Warning: comments following the X3D element are not translated as part of the X3D JSON object file.</xsl:text>
<xsl:text> </xsl:text>
<xsl:for-each select="comment()[preceding::X3D]">
<xsl:text><!--</xsl:text>
<xsl:value-of select="."/>
<xsl:text>--></xsl:text>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:message>
</xsl:if>
</xsl:template>
<xsl:template name="comments-elements-ROUTEs"> <!-- rule to process all child comments, elements and ROUTEs (X3D nodes and scene-graph structure statements) -->
<xsl:param name="indent"><xsl:text>0</xsl:text></xsl:param>
<!-- debug: fieldValue trace
<xsl:if test="(local-name() = 'fieldValue')">
<xsl:text> with contained nodes found: </xsl:text>
<xsl:text>@name=</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>, $parentName=</xsl:text>
<xsl:value-of select="local-name(..)"/>
<xsl:text> @name=</xsl:text>
<xsl:value-of select="../@name"/>
</xsl:if>
-->
<xsl:variable name="debugTrace" select="false()"/><!-- true() false() -->
<xsl:if test="$debugTrace">
<xsl:message>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>* comments-elements-ROUTEs: </xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:text> with </xsl:text>
<xsl:value-of select="count(*)"/>
<xsl:text> child nodes found:</xsl:text>
<xsl:for-each select="*">
<xsl:text> </xsl:text>
<xsl:value-of select="local-name()"/>
</xsl:for-each>
</xsl:message>
</xsl:if>
<!-- No commenting mechanism proved in JSON specification. Nevertheless comments are included in X3D JSON encoding
in order to support round-trip interoperability among X3D encodings. -->
<!-- each child element is represented in a JSON array of JSON objects -->
<!-- process entire set of immediate-child peers in an ordered fashion: comments first, field/fieldValue next, IS next, elements next, ROUTEs last -->
<xsl:variable name="allContainedCommentsElements">
<!-- order gets shuffled a bit due to JSON unique-key requirements
<xsl:apply-templates select="comment()">
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates> -->
<xsl:apply-templates select="* | comment()">
<!-- listed in reverse order -->
<!-- sorting does not appear to be necessary (and makes comment sorting problematic), so skip it
<xsl:sort select="not((local-name() = 'IS') or (local-name() = 'field') or (local-name() = 'fieldValue'))"/>
<xsl:sort select=" (local-name() = 'IS')"/>
<xsl:sort select=" (local-name() = 'field')"/>
<xsl:sort select=" (local-name() = 'fieldValue')"/> -->
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates>
</xsl:variable>
<!-- string results provides source output for the converted nodes -->
<!-- remove final comma from preceding construct to match JSON syntax: no comma at end of an array -->
<xsl:value-of select="substring($allContainedCommentsElements,1,string-length($allContainedCommentsElements)-1)"/>
</xsl:template>
<xsl:template match="* | comment()"> <!-- rule to process each element (X3D nodes and scene-graph structure statements) -->
<xsl:param name="indent"><xsl:text>0</xsl:text></xsl:param>
<xsl:call-template name="trace">
<xsl:with-param name="message"><xsl:text></xsl:text></xsl:with-param>
<xsl:with-param name="element" select="local-name()"/>
<xsl:with-param name="name" select="@name"/>
<xsl:with-param name="DEF" select="@DEF"/>
<xsl:with-param name="USE" select="@USE"/>
<xsl:with-param name="indent" select="$indent"/>
</xsl:call-template>
<xsl:variable name="elementName" select="local-name()"/>
<xsl:variable name= "parentName" select="local-name(..)"/>
<xsl:variable name="debugTrace" select="false()"/><!-- true() false() -->
<xsl:if test="$debugTrace">
<xsl:message>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>** xsl:template match="* | comment()" #</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text> </xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:if test="(string-length(@DEF) > 0)">
<xsl:text>, DEF=</xsl:text>
<xsl:value-of select="@DEF"/>
</xsl:if>
<xsl:if test="(string-length(@USE) > 0)">
<xsl:text>, USE=</xsl:text>
<xsl:value-of select="@USE"/>
</xsl:if>
<xsl:if test="(string-length(@name) > 0)">
<xsl:text>, name=</xsl:text>
<xsl:value-of select="@name"/>
</xsl:if>
</xsl:message>
</xsl:if>
<xsl:choose>
<xsl:when test="($elementName = 'div') or ($elementName = 'span') or ($elementName = 'ui') or ($elementName = 'li') or ($elementName = 'canvas')">
<xsl:if test="$debugTrace">
<!-- Drop the X3DOM elements (thanks to John Carlson) -->
<xsl:message>
<xsl:text>*** (drop)</xsl:text>
</xsl:message>
</xsl:if>
</xsl:when>
<!-- ============================================================================================ -->
<!-- scene-graph structure statements -->
<xsl:when test="($elementName = 'X3D') or ($elementName = 'head') or ($elementName = 'Scene') or
($elementName = 'component') or ($elementName = 'meta') or ($elementName = 'unit') or
($elementName = 'IS') or ($elementName = 'connect') or
($elementName = 'field') or ($elementName = 'fieldValue') or ($elementName = 'ProtoInterface') or
($elementName = 'ProtoBody') or
($elementName = 'AllX3dElementsAttributes')"> <!-- for scene graph code-coverage testing -->
<!-- special statements: scene-graph structure element may have attributes, contains arrays, but NOT surrounded by {squiggly brackets} -->
<xsl:if test="$debugTrace">
<xsl:message>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>*** (scene-graph structure statements)</xsl:text>
</xsl:message>
</xsl:if>
<!-- if first of multiple siblings, process all at once -->
<xsl:if test="not(preceding-sibling::*[local-name() = $elementName])">
<!-- first peer of its kind, found no preceding siblings with same name. process all similar-sibling elements of this type at once. -->
<xsl:if test="$debugTrace">
<xsl:message>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>*** first peer of its kind: #</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text> </xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:if test="(string-length(@DEF) > 0)">
<xsl:text>, DEF=</xsl:text>
<xsl:value-of select="@DEF"/>
</xsl:if>
<xsl:if test="(string-length(@USE) > 0)">
<xsl:text>, USE=</xsl:text>
<xsl:value-of select="@USE"/>
</xsl:if>
<xsl:if test="(string-length(@name) > 0)">
<xsl:text>, name=</xsl:text>
<xsl:value-of select="@name"/>
</xsl:if>
</xsl:message>
</xsl:if>
<!-- special case: scene-graph top-level structure elements, pre-fix -->
<xsl:choose>
<xsl:when test="(($elementName='X3D') and not($parentName='AllX3dElementsAttributes')) or ($elementName='AllX3dElementsAttributes')">
<xsl:text>{ </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:value-of select="$elementName"/>
<xsl:text>"</xsl:text>
<xsl:text>:</xsl:text><!-- [scene-graph structure element contains keys -->
<!-- special case: scene-graph structure elements, pre-fix -->
<xsl:choose>
<xsl:when test="($elementName='X3D')">
<xsl:text> {</xsl:text><!-- attributes + follow-on nodes in scene-graph structure array -->
<xsl:if test="not($parentName='AllX3dElementsAttributes')">
<xsl:text> </xsl:text>
<xsl:text> </xsl:text><!-- indent -->
<xsl:text>"encoding":"UTF-8",</xsl:text> <!-- also allowed UTF-16 UTF-32 -->
</xsl:if>
</xsl:when>
<xsl:when test="($elementName='head') or ($elementName='Scene') or ($elementName='ProtoInterface') or ($elementName='ProtoBody') or ($elementName='IS') or ($elementName='AllX3dElementsAttributes')">
<xsl:if test="(count(@*) = 0)">
<xsl:text> {</xsl:text>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:text> [</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:for-each select="(self::* | following-sibling::*[local-name() = $elementName])">
<!-- process all sibling elements of this type at once -->
<xsl:if test="$debugTrace">
<xsl:message>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>**** process all sibling elements of this type at once: #</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text> </xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:if test="(string-length(@DEF) > 0)">
<xsl:text>, DEF=</xsl:text>
<xsl:value-of select="@DEF"/>
</xsl:if>
<xsl:if test="(string-length(@USE) > 0)">
<xsl:text>, USE=</xsl:text>
<xsl:value-of select="@USE"/>
</xsl:if>
<xsl:if test="(string-length(@name) > 0)">
<xsl:text>, name=</xsl:text>
<xsl:value-of select="@name"/>
</xsl:if>
</xsl:message>
</xsl:if>
<!-- process attributes for this element, if any -->
<xsl:if test="@* and not($elementName='X3D') and not($elementName='AllX3dElementsAttributes')">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>{</xsl:text><!-- attributes + follow-on nodes in scene-graph structure array -->
</xsl:if>
<xsl:if test="@*">
<xsl:call-template name="attributes">
<xsl:with-param name="indent"><xsl:value-of select="$indent+2"/></xsl:with-param>
</xsl:call-template>
</xsl:if>
<xsl:if test="($elementName='X3D') and not($parentName='AllX3dElementsAttributes')">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<!-- TODO no guidance yet on how to properly mark the governing schema within a JSON file at https://json-schema.org -->
<xsl:text>"JSON schema":"https://www.web3d.org/specifications/x3d-4.0-JSONSchema.autogenerated.json"</xsl:text>
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:call-template name="comments-elements-ROUTEs"> <!-- contained content -->
<xsl:with-param name="indent"><xsl:value-of select="$indent+4"/></xsl:with-param>
</xsl:call-template>
<!-- process attributes for this element, post-fix -->
<xsl:if test="@* or ($elementName='X3D')">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>}</xsl:text><!-- attributes + follow-on nodes in scene-graph structure array -->
</xsl:if>
<!-- special case: add meta tags for conversion, if others exist. TODO handle if no head element provided. -->
<xsl:if test="(local-name() = 'meta') and (last() = position()) and not($parentName='AllX3dElementsAttributes')">
<xsl:text>,</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>{</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>"@name":"translated",</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>"@content":"</xsl:text>
<xsl:value-of select="format-date(current-date(), '[D01] [MNn] [Y0001]')"/><xsl:text>"</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>},</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>{</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>"@name":"generator",</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>"@content":"X3dToJson.xslt, https://www.web3d.org/x3d/stylesheets/X3dToJson.html"</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<!-- TODO omit warning when encoding is finished and stable -->
<xsl:text>},</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>{</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>"@name":"reference",</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>"@content":"X3D JSON encoding: https://www.web3d.org/wiki/index.php/X3D_JSON_Encoding"</xsl:text><xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>}</xsl:text>
</xsl:if>
<!-- commas separate objects in this array. do not terminate with comma. -->
<xsl:if test="(last() > position())">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<!-- special case: scene-graph structure elements, post-fix -->
<xsl:choose>
<xsl:when test="(($elementName='X3D') and not($parentName='AllX3dElementsAttributes'))">
<xsl:text> </xsl:text>
<xsl:text>}</xsl:text>
</xsl:when>
<xsl:when test="($elementName='AllX3dElementsAttributes')"> <!-- testing complete -->
<xsl:text>}</xsl:text>
<xsl:text> </xsl:text>
<xsl:text>}</xsl:text>
</xsl:when>
<xsl:when test="($elementName='head') or ($elementName='Scene') or ($elementName = 'ProtoInterface') or ($elementName='ProtoBody') or ($elementName='IS')">
<xsl:if test="(count(@*) = 0)">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>}</xsl:text>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>]</xsl:text>
</xsl:otherwise>
</xsl:choose>
<!-- add comma after array elements, lingering final comma is stripped later -->
<xsl:if test="not($elementName='X3D') and not($elementName='AllX3dElementsAttributes')">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:if> <!-- initial sibling -->
</xsl:when>
<xsl:when test="($elementName = 'Signature')"><!-- ds:Signature -->
<xsl:message>
<xsl:text>Warning: XML Digital Signature not supported in X3D JSON encoding, <ds:Signature/> authentication block ignored. </xsl:text>
</xsl:message>
</xsl:when>
<!-- ============================================================================================ -->
<xsl:otherwise>
<!-- base case: simple element, optional attributes, accessed by containerField (or overriding fieldName) -->
<!-- includes ROUTE and ProtoInstance -->
<xsl:variable name="fieldName">
<xsl:choose>
<!-- special test case for full coverage of scene-graph elements, attributes and default values -->
<xsl:when test="($parentName = 'AllX3dElementsAttributes') and ($elementName = 'X3D')">
<xsl:text>children</xsl:text>
</xsl:when>
<!-- ProtoDeclare and ExternProtoDeclare names are encoded with children nodes -->
<xsl:when test="($elementName = 'ProtoDeclare') or ($elementName = 'ExternProtoDeclare')">
<!-- <xsl:value-of select="$elementName"/> similarly to containerField names, each contains an array-->
<xsl:text>children</xsl:text>
</xsl:when>
<!-- comments and ROUTE elements are treated as objects under -children field, regardless of parent node -->
<xsl:when test="self::comment()">
<xsl:text>children</xsl:text>
</xsl:when>
<xsl:when test="($elementName = 'ROUTE')">
<xsl:text>children</xsl:text>
</xsl:when>
<xsl:when test="($elementName = 'IMPORT') or ($elementName = 'EXPORT')">
<!-- IMPORT and EXPORT names are encoded with children nodes -->
<xsl:text>children</xsl:text>
</xsl:when>
<xsl:when test="($parentName = 'Scene')">
<!-- Metadata* nodes are allowed as children -->
<xsl:text>children</xsl:text>
</xsl:when>
<xsl:when test="($parentName = 'ProtoBody')">
<!-- note that all children of ProtoBody go into "-children" field; first node is node type for that prototype declaration -->
<xsl:text>children</xsl:text>
</xsl:when>
<xsl:when test="($parentName = 'field') or ($parentName = 'fieldValue')">
<!-- parent ProtoInterface handled separately -->
<xsl:text>children</xsl:text>
</xsl:when>
<xsl:when test="($parentName = 'Scene') and starts-with($elementName, 'Metadata')">
<!-- Metadata* nodes as root nodes at top level of Scene are special case, treated as children. TODO confirm mantis spec tooltips and webpage are reviewed/approved/updated. -->
<xsl:text>children</xsl:text>
</xsl:when>
<xsl:when test="string-length(@containerField) > 0">
<!-- following depends on schema being properly read and providing containerField values -->
<xsl:value-of select="@containerField"/>
</xsl:when>
<xsl:otherwise>
<!-- Note that this error condition occurs if DTD default attribute values are suppressed before invocation -->
<xsl:text>IllegalChildNodeFieldNameNotFound</xsl:text>
<xsl:message>
<xsl:text>Error: IllegalChildNodeFieldNameNotFound no containerField or field name found for the X3D JSON object. Check spelling of node.</xsl:text>
<xsl:text> </xsl:text>
<xsl:text> $elementName=</xsl:text>
<xsl:value-of select="$elementName"/>
<xsl:if test="(string-length(@DEF) > 0)">
<xsl:text>, DEF=</xsl:text>
<xsl:value-of select="@DEF"/>
</xsl:if>
<xsl:if test="(string-length(@USE) > 0)">
<xsl:text>, USE=</xsl:text>
<xsl:value-of select="@USE"/>
</xsl:if>
<xsl:text>, $parentName=</xsl:text>
<xsl:value-of select="$parentName"/>
<xsl:text> </xsl:text>
<xsl:choose>
<xsl:when test="not($isTestScene)">
<xsl:text> Please report this error to [email protected] - thanks for your help improving X3D Quality Assurance (QA).</xsl:text>
</xsl:when>
<xsl:when test="(string-length(//meta[(@name='title')]/@content) > 0)">
<xsl:text> </xsl:text>
<xsl:value-of select="//meta[(@name='title')]/@content"/>
<xsl:text> is a test scene supporting X3D Quality Assurance (QA).</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- check some special cases that are unambiguously fixable -->
<xsl:variable name="expectedContainerField">
<xsl:choose>
<xsl:when test="(local-name(..) = 'GeoLOD') and not(local-name() = 'GeoOrigin') and not(starts-with(local-name(), 'Metadata'))">
<xsl:text>rootNode</xsl:text>
</xsl:when>
<xsl:when test="(local-name(..) = 'HAnimHumanoid') and (local-name() = 'HAnimSegment')">
<xsl:text>segments</xsl:text>
</xsl:when>
<!-- HAnimHumanoid can contain HAnimJoint with containerField = joints or skeleton -->
<!-- HAnimHumanoid can contain HAnimSite with containerField = sites, skeleton or viewpoints -->
<!-- HAnimHumanoid can contain X3DCoordinateNode with containerField = skinCoord or skinBindingCoords -->
<!-- HAnimHumanoid can contain X3DNormalNode with containerField = skinNormal or skinBindingNormals -->
</xsl:choose>
</xsl:variable>
<xsl:if test="(string-length($expectedContainerField) > 0) and not(@containerField = $expectedContainerField)">
<xsl:message>
<xsl:text>... containerField mismatch for </xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:text> DEF='</xsl:text>
<xsl:value-of select="@DEF"/>
<xsl:text>', found containerField='</xsl:text>
<xsl:value-of select="@containerField"/>
<xsl:text>' but expected containerField='</xsl:text>
<xsl:value-of select="$expectedContainerField"/>
<xsl:text>'</xsl:text>
</xsl:message>
</xsl:if>
<!-- Metadata nodes are allowed at top level of scene, but have different default containerField and thus require special attention -->
<xsl:variable name="isSceneMetadata"
select="($parentName = 'Scene') and starts-with($elementName,'Metadata')"/>
<xsl:variable name="hasPriorSibling"
select="preceding-sibling::*[@containerField = $fieldName] or
(($fieldName = 'children') and
(preceding-sibling::*[$parentName = 'field'] or
preceding-sibling::*[$parentName = 'fieldValue'] or
preceding-sibling::*[$parentName = 'ProtoBody'] or
preceding-sibling::ProtoDeclare or
preceding-sibling::ExternProtoDeclare or
preceding-sibling::ROUTE or
preceding-sibling::IMPORT or
preceding-sibling::EXPORT or
preceding-sibling::comment() or
preceding-sibling::*[$parentName = 'Scene'][starts-with($elementName,'Metadata')]))"/>
<xsl:choose>
<!-- if first of multiple siblings, process all at once -->
<xsl:when test="not($hasPriorSibling)">
<!-- first peer of its kind, found no preceding siblings with same name -->
<xsl:if test="$debugTrace">
<xsl:message>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>*** (base case, first of multiple siblings)</xsl:text>
</xsl:message>
</xsl:if>
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:variable name="SFNodeType" select="
($fieldName = 'back') or ($fieldName = 'bottom') or ($fieldName = 'front') or
($fieldName = 'left') or ($fieldName = 'right') or ($fieldName = 'top') or
($fieldName = 'backTexture') or ($fieldName = 'bottomTexture') or ($fieldName = 'frontTexture') or
($fieldName = 'leftTexture') or ($fieldName = 'rightTexture') or ($fieldName = 'topTexture') or
($fieldName = 'appearance') or ($fieldName = 'body1') or ($fieldName = 'body2') or
($fieldName = 'collidable') or ($fieldName = 'collider') or
($fieldName = 'color') or ($fieldName = 'colorRamp') or ($fieldName = 'coord') or
($fieldName = 'controlPoint') or ($fieldName = 'controlPoints') or ($fieldName = 'crossSectionCurve') or
($fieldName = 'emitter') or ($fieldName = 'fillProperties') or
($fieldName = 'fogCoord') or ($fieldName = 'fontStyle') or ($fieldName = 'geoOrigin') or
($fieldName = 'geometry') or ($fieldName = 'geometry1') or ($fieldName = 'geometry2') or
($fieldName = 'gradients') or ($fieldName = 'layout') or ($fieldName = 'lineProperties') or
($fieldName = 'massDensityModel') or ($fieldName = 'material') or
($fieldName = 'metadata') or ($fieldName = 'normal') or ($fieldName = 'pickingGeometry') or
($fieldName = 'profileCurve') or ($fieldName = 'proxy') or
($fieldName = 'segmentIdentifiers') or ($fieldName = 'skinCoord') or ($fieldName = 'skinNormal') or
($fieldName = 'surface') or ($fieldName = 'surfaceNormals') or
($fieldName = 'targetObject') or ($fieldName = 'texCoordRamp') or ($fieldName = 'textureProperties') or
($fieldName = 'trajectoryCurve') or ($fieldName = 'transferFunction') or
($fieldName = 'viewpoint') or ($fieldName = 'voxels') or
($fieldName = 'weightTransferFunction1') or
($fieldName = 'weightTransferFunction2') or
(($fieldName = 'renderStyle') and (($parentName='BlendedVolumeStyle') or ($parentName='VolumeData'))) or
(($fieldName = 'source') and ($parentName='Sound')) or
(($fieldName = 'texCoord') and not($parentName='MultiTextureCoordinate')) or
(($fieldName = 'texture') and not(($parentName='ComposedTexture3D') or ($parentName='MultiTexture'))) or
(($fieldName = 'textureTransform') and not($parentName='MultiTextureTransform')) or
(($fieldName = 'shape') and (($parentName='CADFace') or ($parentName='CollidableShape')))" />
<!-- contained node containerField -->
<xsl:if test="not(local-name() = 'ProtoInterface') and not(local-name() = 'ProtoBody')">
<xsl:text>"</xsl:text>
<xsl:text>-</xsl:text><!-- visual assist, TODO determine if part of final pattern -->
<xsl:value-of select="$fieldName"/>
<xsl:text>"</xsl:text>
<xsl:text>:</xsl:text>
<!-- SFNode tests -->
<xsl:if test="not($SFNodeType) or //AllX3dElementsAttributes">
<xsl:text>[</xsl:text>
</xsl:if>
</xsl:if>
<!-- select node() list, note use of | operator rather than "or" -->
<xsl:for-each select="
(self::* |
self::comment() |
following-sibling::*[@containerField = $fieldName] |
following-sibling::*[$parentName = 'field'] [$fieldName = 'children'] |
following-sibling::*[$parentName = 'fieldValue'] [$fieldName = 'children'] |
following-sibling::*[$parentName = 'ProtoBody'] [$fieldName = 'children'] |
following-sibling::ProtoDeclare [$fieldName = 'children'] |
following-sibling::ExternProtoDeclare [$fieldName = 'children'] |
following-sibling::ROUTE [$fieldName = 'children'] |
following-sibling::IMPORT [$fieldName = 'children'] |
following-sibling::EXPORT [$fieldName = 'children'] |
following-sibling::comment() [$fieldName = 'children'] |
following-sibling::*[$parentName = 'Scene'][starts-with(local-name(),'Metadata')][$fieldName = 'children'])">
<!-- greedy algorithm to process all elements of this field type at once -->
<!-- base case: simple element, optional attributes, accessed by containerField -->
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>{</xsl:text><!-- base case: simple element inside containerField -->
<xsl:choose>
<xsl:when test="self::comment()">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>"</xsl:text>
<xsl:text>#comment</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> "</xsl:text>
<xsl:value-of select="local-name()"/> <!-- $elementName not working?? -->
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
<xsl:text>:</xsl:text>
<!-- process attributes for this element, if any -->
<xsl:if test="@*">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>{</xsl:text><!-- attributes array for otherwise case (followed by additional contained nodes, if any) -->
<xsl:call-template name="attributes">
<xsl:with-param name="indent" select="$indent+4"/>
</xsl:call-template>
</xsl:if>
<!-- comment prose -->
<xsl:if test="self::comment()">
<xsl:text>"</xsl:text>
<!-- escaped quote requires preceding backslash in JSON encoding -->
<xsl:call-template name="escape-comment-recurse">
<xsl:with-param name="inputValue">
<xsl:value-of select="normalize-space(string(.))" disable-output-escaping="yes"/>
</xsl:with-param>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:if>
<xsl:if test="* | comment()"> <!-- node() includes CDATA text -->
<!-- recurse; each child element is represented in a JSON array of JSON objects -->
<xsl:call-template name="comments-elements-ROUTEs"> <!-- contained content -->
<xsl:with-param name="indent"><xsl:value-of select="$indent+6"/></xsl:with-param>
</xsl:call-template>
</xsl:if>
<!-- process contained child text(), looking for source code -->
<xsl:if test="(string-length(normalize-space(string(.))) > 0) and not(normalize-space(string(.)) = ' ')">
<!-- add comma separator if field/IS/connect definitions are preceding and this is indeed the node containing source code (rather than a descendant) -->
<xsl:if test="((local-name() = 'Script') or (local-name() = 'ShaderPart') or (local-name() = 'ShaderProgram'))">
<xsl:call-template name="trace">
<xsl:with-param name="message">
<xsl:text>trace: count(*)=</xsl:text>
<xsl:value-of select="count(*)"/>
<xsl:text>, count(@*)=</xsl:text>
<xsl:value-of select="count(@*)"/>
<xsl:text>, attribute value(s)=</xsl:text>
<xsl:value-of select="@*[(string-length(normalize-space(string(.))) > 0) and not(local-name()='containerField')]"/>
</xsl:with-param>
<xsl:with-param name="element"><xsl:value-of select="local-name()"/></xsl:with-param>
<xsl:with-param name="traceEnabled"><xsl:text>false</xsl:text></xsl:with-param>
</xsl:call-template>
<!-- only output comma prior to #sourceTextMode if preceded by a child node or (non-containerField) attribute value. watch out for default values! -->
<xsl:if test="(count(*) > 0) or (count(@*[(string-length(normalize-space(string(.))) > 0) and not(local-name()='containerField')]) > 0)">
<xsl:if test="(count(*) > 0) or not((local-name() = 'Script') and (@directOutput = 'false') and (@mustEvaluate = 'false') and (string-length(normalize-space(@DEF)) = 0) and (string-length(normalize-space(@url)) = 0))">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:if>
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+6"/></xsl:call-template>
<xsl:apply-templates select="text()"/> <!-- Script/ShaderPart/ShaderProgram #sourceCode -->
</xsl:if>
</xsl:if>
<xsl:if test="@*">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+4"/></xsl:call-template>
<xsl:text>}</xsl:text><!-- attributes array for otherwise case (followed by additional contained nodes, if any) -->
</xsl:if>
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>}</xsl:text><!-- base case: simple element inside containerField -->
<!-- commas separate objects in this array. do not terminate with comma. -->
<xsl:if test="(last() > position())">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<!-- contained node containerField -->
<xsl:if test="not(local-name() = 'ProtoInterface') and not(local-name() = 'ProtoBody')">
<!-- SFNode tests -->
<xsl:if test="not($SFNodeType) or //AllX3dElementsAttributes">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>]</xsl:text>
</xsl:if>
</xsl:if>
<!-- <xsl:if test="parent::Scene or parent::ProtoBody or (not(../@*) and not(../preceding-sibling::comment()))">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>}</xsl:text>base case: brackets outside containerField
</xsl:if> -->
<xsl:text>,</xsl:text>
</xsl:when> <!-- initial sibling -->
<xsl:otherwise>
<xsl:if test="$debugTrace or not($hasPriorSibling)">
<!-- no match on first of multiple siblings -->
<xsl:message>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>*** Warning: (base case element processing, no match on first of multiple siblings, was it handled?) </xsl:text>
<xsl:text> $hasPriorSibling=</xsl:text>
<xsl:value-of select="$hasPriorSibling"/>
<xsl:text>, $isSceneMetadata=</xsl:text>
<xsl:value-of select="$isSceneMetadata"/>
</xsl:message>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
<!-- ============================================================================================ -->
</xsl:choose>
</xsl:template> <!-- end match="*" -->
<xsl:template match="hide/comment()">
<xsl:param name="indent"><xsl:text>0</xsl:text></xsl:param>
<xsl:param name="stripComments"><xsl:value-of select="$stripComments"/></xsl:param>
<xsl:call-template name="trace">
<xsl:with-param name="message">
<xsl:text>$stripComments=</xsl:text>
<xsl:value-of select="$stripComments"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="normalize-space(string(.))"/>
</xsl:with-param>
<xsl:with-param name="element"><xsl:text>#comment</xsl:text></xsl:with-param>
<xsl:with-param name="indent" select="$indent"/>
<xsl:with-param name="traceEnabled"><xsl:text>false</xsl:text></xsl:with-param>
</xsl:call-template>
<xsl:if test="not($stripComments = 'true') and (count(preceding-sibling::comment()) = 0)">
<!-- "#comment" is used as key for this JSON object, similar to ROUTE or other containerField values, which collects an array of JSON strings -->
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<!-- <xsl:if test="parent::Scene or parent::ProtoBody or not(../@*)">
<xsl:text>{ </xsl:text>base case: brackets outside comment
</xsl:if> -->
<xsl:text>"</xsl:text>
<xsl:text>#comment</xsl:text> <!-- comment prefix -->
<xsl:value-of select="name()"/>
<xsl:text>"</xsl:text>
<xsl:text>:[</xsl:text>
<xsl:variable name="allCommentsStringArray">
<!-- greedily put all peer comments in this array of strings -->
<xsl:for-each select="self::comment() | following-sibling::comment()">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent+2"/></xsl:call-template>
<xsl:text>"</xsl:text>
<!-- escaped quote requires preceding backslash in JSON encoding -->
<xsl:call-template name="escape-comment-recurse">
<xsl:with-param name="inputValue" select="normalize-space(string(.))"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
<xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:variable>
<!-- strip trailing comma -->
<xsl:value-of select="substring($allCommentsStringArray,1,string-length($allCommentsStringArray)-1)"/>
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>]</xsl:text>
<!-- <xsl:if test="parent::Scene or parent::ProtoBody or not(../@*)">
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>}</xsl:text>base case: brackets outside comment
</xsl:if> -->
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:template> <!-- match="comment()" -->
<xsl:template name="attributes"> <!-- rule to process all attributes -->
<xsl:param name="indent"><xsl:text>0</xsl:text></xsl:param>
<xsl:variable name="allAttributesString">
<xsl:apply-templates select="@*">
<xsl:sort select="not(local-name() = 'name')"/>
<xsl:sort select=" (local-name() = 'name')"/>
<xsl:with-param name="indent" select="$indent+2"/>
</xsl:apply-templates>
</xsl:variable>
<!-- strip final comma, when follow-on element(s) or comment(s) are contained -->
<xsl:choose>
<xsl:when test="* | comment()"> <!-- node() includes CDATA text -->
<xsl:value-of select="$allAttributesString"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($allAttributesString,1,string-length($allAttributesString)-1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template> <!-- end match="attributes" -->
<xsl:template match="@*"> <!-- rule to process each @attribute -->
<xsl:param name="isComment"><xsl:text>false</xsl:text></xsl:param>
<xsl:param name="indent"><xsl:text>0</xsl:text></xsl:param>
<!-- an attribute is handled as a JSON "@name":_value_ pair with appropriate type matching -->
<xsl:variable name="normalizedValue" select="normalize-space(string(.))"/>
<!-- Detect and suppress default attribute values -->
<xsl:variable name="notDefaultAttributeValue">
<xsl:call-template name="not-default-attribute-value">
</xsl:call-template>
</xsl:variable>
<!-- eliminate default attribute values (based on parameter setting) otherwise they will all appear in output -->
<xsl:if test="((local-name()='DEF') or (local-name()='USE') or (local-name()='name') or
((string-length($notDefaultAttributeValue) > 0) and not(local-name() = 'containerField'))
and not(/AllX3dElementsAttributes))">
<xsl:variable name="attributeName" select="local-name()"/>
<xsl:variable name="fieldValueName" select="../@name"/>
<xsl:variable name="protoInstanceName" select="(../../@name)"/>
<xsl:variable name="fieldValueType1" select=" //ProtoDeclare[@name = $protoInstanceName][1]/ProtoInterface/field[@name=$fieldValueName][1]/@type"/>
<xsl:variable name="fieldValueType2" select="//ExternProtoDeclare[@name = $protoInstanceName][1] /field[@name=$fieldValueName][1]/@type"/>
<!-- only one of these should be available -->
<xsl:variable name="fieldValueType" select="concat($fieldValueType1[1],$fieldValueType2[1])"/>
<!-- debug ProtoInstance
<xsl:if test="(string-length($protoInstanceName) > 0) and ($attributeName = 'value')">
<xsl:message>
<xsl:text>$protoInstanceName=</xsl:text>
<xsl:value-of select="$protoInstanceName"/>
<xsl:text>, fieldValue $attributeName=</xsl:text>
<xsl:value-of select="$attributeName"/>
<xsl:text>, $fieldValueName=</xsl:text>
<xsl:value-of select="$fieldValueName"/>
<xsl:text>, $normalizedValue=</xsl:text>
<xsl:value-of select="$normalizedValue"/>
<xsl:text>, @name=</xsl:text>
<xsl:value-of select="local-name(//*[contains(local-name(),'ProtoDeclare')][@name = $protoInstanceName])"/>
<xsl:text>, @type=</xsl:text>
<xsl:value-of select="//*[contains(local-name(),'ProtoDeclare')][@name = $protoInstanceName]//field[@name=$fieldValueName]/@type"/>
<xsl:text>, $fieldValueType=</xsl:text>
<xsl:value-of select="$fieldValueType"/>
</xsl:message>
</xsl:if>
-->
<!-- debug field, fieldValue
<xsl:if test="starts-with(local-name(..),'field') and (local-name()='name')">
<xsl:message>
<xsl:text>></xsl:text>
<xsl:value-of select="local-name(..)"/>
<xsl:text> name='</xsl:text>
<xsl:value-of select="."/>
<xsl:if test="(string-length(normalize-space(../@type)) > 0)">
<xsl:text>' type='</xsl:text>
<xsl:value-of select="../@type"/>
</xsl:if>
<xsl:if test="(string-length(normalize-space(../@accessType)) > 0)">
<xsl:text>' accessType='</xsl:text>
<xsl:value-of select="../@accessType"/>
</xsl:if>
<xsl:text>' value='</xsl:text>
<xsl:value-of select="../@value"/>
<xsl:text>'/<</xsl:text>
</xsl:message>
</xsl:if>
-->
<!-- common processing for each attribute -->
<!-- escaped quote requires preceding backslash in JSON encoding
<xsl:variable name="normalizedSFStringEmbeddedQuotes">
<xsl:call-template name="escape-special-characters-quotes-recurse">
<xsl:with-param name="inputValue" select="normalize-space(string(.))"/>
<xsl:with-param name="inputType"><xsl:text>SFString</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable> -->
<xsl:variable name="containsQuote" select="contains($normalizedValue, '"')"/>
<xsl:variable name="containsEscapedQuote" select="contains($normalizedValue,'\"')"/>
<!-- now process attribute value. if this attribute is part of a USE node, then only output USE, containerField, class attributes. -->
<xsl:if test="((string-length(../@USE) = 0) or (local-name()='USE') or (local-name()='name') or
(local-name()='containerField') or (local-name()='class') or
(local-name(..)='X3D') and ((local-name()='profile') or (local-name()='version')))
and not(/AllX3dElementsAttributes)
and not((local-name(..) = 'X3D') and (local-name() = 'width' or local-name() = 'height' or local-name() = 'backend' or local-name() = 'showStat' or local-name() = 'showLog')) and not(local-name() = 'skyTransparency') and not(local-name() = 'groundTransparency') and not(local-name() = 'id') and not(local-name() = 'subdivision')"> <!-- This line are X3DOM attributes -->
<xsl:text> </xsl:text>
<xsl:call-template name="print-indent"><xsl:with-param name="indent" select="$indent"/></xsl:call-template>
<xsl:text>"</xsl:text>
<xsl:text>@</xsl:text> <!-- attribute prefix -->
<xsl:value-of select="name()"/>
<xsl:text>"</xsl:text>
<xsl:text>:</xsl:text>
<!-- output attribute values according to data type ========================================================================== -->
<xsl:variable name="attributeType">
<xsl:call-template name="attribute-type"/>
</xsl:variable>
<!-- debug -->
<xsl:variable name="debugTrace" select="false()"/><!-- true() false() -->
<xsl:if test="$debugTrace">
<xsl:message>
<xsl:text>[@* choose type handling] $attributeType=</xsl:text>
<xsl:value-of select="$attributeType"/>
<xsl:text>, $normalizedValue=</xsl:text>
<xsl:value-of select="$normalizedValue"/>
</xsl:message>
</xsl:if>
<xsl:choose>
<!-- TODO optimize duplication of type checking which is present due to integration of rule attribute-type with original rules -->
<!-- deterministic rules first: use type information for normalizing text or numbers or booleans ========================= -->
<!-- single boolean -->
<xsl:when test="not($attributeType = 'MFBool') and
(($attributeType = 'SFBool') or
($normalizedValue='true') or ($normalizedValue='false') or
((local-name()='value') and ((../@type='SFBool') or (contains(local-name(../..),'Proto') and ($fieldValueType='SFBool')))))">
<xsl:value-of select="$normalizedValue"/>
</xsl:when>
<!-- boolean array -->
<xsl:when test="($attributeType = 'MFBool') or
((local-name()='value') and ((../@type='MFBool') or (contains(local-name(../..),'Proto') and ($fieldValueType='MFBool')))) or
((local-name(..)='BooleanSequencer') and (local-name()='keyValue')) or
((local-name(..)='MetadataBoolean') and (local-name()='value')) or
((local-name(..)='CADLayer') and ($attributeName='visible') and starts-with(//X3D/@version,'3')) or
((local-name(..)='SegmentedVolumeData') and (local-name()='segmentEnabled'))">
<xsl:text>[</xsl:text>
<!-- array values require comma between values in JSON encoding -->
<xsl:call-template name="insert-commas-recurse">
<xsl:with-param name="inputValue" select="$normalizedValue"/>
<xsl:with-param name="inputType"><xsl:text>MFBool</xsl:text></xsl:with-param>
</xsl:call-template>
<xsl:text>]</xsl:text>
</xsl:when>
<!-- no xs:string type handling for X3D statements, rather SFString according to Object Model for X3D (OM4X3D) -->
<!-- comments handled separately -->
<xsl:when test="(local-name(..)='meta') or