forked from TEIC/Stylesheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRngToRnc.xsl
1502 lines (1463 loc) · 58.1 KB
/
RngToRnc.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
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"?>
<!--*- XML -*-->
<!-- adapted by Sebastian Rahtz from: -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- $Id$ -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!--
modified 2010-08-14 by Syd Bauman:
bug-fix: modified expressions defining 'pfx' variables
so that only 1 namespace is handed to name().
-->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 2002, Pantor Engineering AB
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Pantor Engineering AB 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.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Created by [email protected]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
RngToRncText.xsl converts a RELAX NG schema in XML syntax to the
compact syntax.
If the source document is a RELAX NG schema, an extension
function converting result tree fragments to node sets is
required. If the processor supports the exslt:node-set function,
no configuration should be needed. See RngToRncProcessorConfig.xsl
for further information.
This stylesheet can also be applied to the intermediate XML
representation of a compact schema as output by the
RngToRncXml.xsl stylesheet. In this case, no extension function
is required.
For a description of the underlying XML to compact syntax
transformation, see RngToRncXml.xsl.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:transform xmlns:rng="http://relaxng.org/ns/structure/1.0"
xmlns:sch="http://purl.oclc.org/dsdl/schematron"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
version="2.0"
exclude-result-prefixes="rng sch a">
<xsl:param name="top"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Parameters -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- collapse-lines:
If true, output constructs spanning multiple lines will be
groupd into a single line unless it exceeds $line-width chars. -->
<xsl:param name="collapse-lines" select="true ()"/>
<!-- indent-width:
The number of characters to indent at each indentation level -->
<xsl:param name="indent-width" select="3"/>
<!-- line-width:
see the group-lines parameter. -->
<xsl:param name="line-width" select="80"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- $Id$ -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 2002, Pantor Engineering AB
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Pantor Engineering AB 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.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Created by [email protected]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
RngToRncXml.xsl converts a RELAX NG schema in XML syntax to an
XML representation of the compact syntax.
The output is an intermediate format suitable for further
transformation into, for example, plain text or HTML.
The default namespace declaration is chosen from the first
(in document order) occurrence of an ns attribute, unless
supplied as the parameter default-ns.
If the value of an href attribute ends with '.rng', the suffix
will be replace by the string '.rnc'. This feature can be
disabled by setting the stylesheet parameter rewrite-suffix to
false.
Current limitations/known deficiencies:
* Annotations are not handled in name classes
* Uses of namespace prefixes must be consistent throughout the
source schema. That is, a namespace prefix used in two QNames
must be declared to the same namespace URI for both
occurrences.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Parameters -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- rewrite-suffix:
If true and the value of an href attribute ends with '.rng', the
suffix will be replace by the string '.rnc'. -->
<xsl:param name="rewrite-suffix" select="true ()"/>
<!-- retain-prefixes:
If true, namespace declarations will use prefixes from the source
document if possible. (Doesn't work with MSXML 4.0 or Xalan-J 2.4.0) -->
<xsl:param name="retain-prefixes">true</xsl:param>
<!-- default-ns:
The uri of the default namespace. There must be at least one
ns attribute in the schema containing this uri. -->
<xsl:param name="default-ns" select="string (/descendant::rng:*[@ns][1]/@ns)"/>
<!-- prefixes:
A space separated list of prefix mappings: prefix '=' namespace-uri.
Note: Since space is used as a delimiter, no space is allowed
immediately before or after the equal sign. Example:
"foo=http://example.org/ns/foo bar=http://example.org/ns/bar"
The prefixes will be used whenever a prefix is needed for the
corresponding namespace URI. Prefixes declared in this
parameter has higher precedence than the prefixes declared
in the prefix-map parameter and those found in the schema.
-->
<xsl:param name="prefixes">tei=http://www.tei-c.org/ns/1.0 teix=http://www.tei-c.org/ns/Examples rng=http://relaxng.org/ns/structure/1.0</xsl:param>
<!-- prefix-map:
A node set containing element nodes like:
<ns prefix="x">http://example.org/x</ns>
The name of the element is not significant. Prefixes from this
map will be used in favor of generated and the ones found in the
source schema. A prefix must not be the empty string. Also, it
must not conflict with any other prefixes in the map or in the
schema. -->
<xsl:param name="prefix-map" select="()"/>
<!-- recursive:
If true, recursively process includes and external
references. Note: no include logic is applied. The external
schemas are simply structurally included, wrapped in an <external>
element. -->
<xsl:param name="recursive" select="false ()"/>
<xsl:template name="str">
<xsl:param name="text"/>
<xsl:param name="quote" select="'"'"/>
<xsl:param name="is-multiline" select="false ()"/>
<xsl:variable name="lit" select="concat ($quote, $text, $quote)"/>
<str size="{string-length ($lit)}">
<xsl:if test="$is-multiline">
<xsl:attribute name="multiline">yes</xsl:attribute>
</xsl:if>
<xsl:value-of select="$lit"/>
</str>
</xsl:template>
<xsl:template name="text">
<xsl:param name="text"/>
<xsl:attribute name="size">
<xsl:value-of select="string-length ($text)"/>
</xsl:attribute>
<xsl:value-of select="$text"/>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:key name="prefix"
match="rng:element/@name [contains (., ':')] | rng:attribute/@name [contains (., ':')]"
use="substring-before (., ':')"/>
<xsl:key name="ns" match="*" use="namespace::*"/>
<xsl:key name="annot-ns" match="*[not (self::rng:*)]" use="namespace-uri (.)"/>
<xsl:key name="annot-ns" match="@*[namespace-uri (.) != '']" use="namespace-uri (.)"/>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="rng:*"/>
<xsl:preserve-space elements="rng:value rng:param"/>
<xsl:variable name="xsd" select="'http://www.w3.org/2001/XMLSchema-datatypes'"/>
<xsl:variable name="default-ns-nd" select="/descendant::rng:*[@ns = $default-ns][1]/@ns"/>
<xsl:variable name="default-ns-id-rtf">
<xsl:call-template name="get-prefix">
<xsl:with-param name="nd" select="$default-ns-nd"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="default-ns-id" select="string ($default-ns-id-rtf)"/>
<xsl:variable name="has-default-ns" select="boolean ($default-ns-nd)"/>
<xsl:variable name="has-local" select="not (key ('prefix', 'local'))"/>
<xsl:template name="make-compact-schema">
<compact-schema>
<!-- Declarations -->
<xsl:if test="$has-default-ns">
<xsl:call-template name="make-ns-declaration">
<xsl:with-param name="is-default" select="true()"/>
<xsl:with-param name="prefix" select="$default-ns-id"/>
<xsl:with-param name="uri" select="$default-ns"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="$has-local">
<xsl:call-template name="make-ns-declaration">
<xsl:with-param name="prefix" select="'local'"/>
<xsl:with-param name="uri" select="''"/>
</xsl:call-template>
</xsl:if>
<xsl:call-template name="inhnamespace"/>
<!--
<xsl:choose>
<xsl:when test="$retain-prefixes">
<xsl:for-each select="//*">
<xsl:variable name="elm" select="self::*"/>
<xsl:for-each select="namespace::*">
<xsl:variable name="ns" select="string (.)"/>
<xsl:if test="(not ($has-default-ns) or $ns != $default-ns) and
count (key ('ns', $ns)[1] | $elm) = 1 and
name (.) != 'xml' and
((not ($ns = 'http://relaxng.org/ns/structure/1.0') and
//*[namespace-uri (.) = $ns or @ns = $ns or
@*[namespace-uri (.) = $ns]]) or
//*[namespace-uri (.) !=
'http://relaxng.org/ns/structure/1.0']//
*[namespace-uri (.) = $ns or @*[namespace-uri (.) = $ns]] or
key ('prefix', name (.)))">
<xsl:call-template name="make-ns-declaration">
<xsl:with-param name="prefix">
<xsl:variable name="mapped">
<xsl:call-template name="mapped-prefix">
<xsl:with-param name="ns" select="string (.)"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="string ($mapped)">
<xsl:value-of select="$mapped"/>
</xsl:when>
<xsl:when test="name (.)">
<xsl:value-of select="name (.)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="generate-id ($elm)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="uri" select="."/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select=".//rng:element | .//rng:attribute">
<xsl:if test="@name [contains (., ':')]">
<xsl:variable name="pfx" select="substring-before (@name, ':')"/>
<xsl:if test="count (key ('prefix', $pfx)[1] | @name) = 1">
<xsl:variable name="ns"
select="string (namespace::*[name () = $pfx])"/>
<xsl:call-template name="make-ns-declaration">
<xsl:with-param name="prefix">
<xsl:variable name="mapped">
<xsl:call-template name="mapped-prefix">
<xsl:with-param name="ns" select="$ns"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="string ($mapped)">
<xsl:value-of select="$mapped"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pfx"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="uri" select="$ns"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="//*[not (self::rng:*)] |
//*/@*[namespace-uri (.) != '']">
<xsl:variable name="ns" select="namespace-uri (.)"/>
<xsl:if test="count (key ('annot-ns', $ns)[1] | .) = 1">
<xsl:call-template name="make-ns-declaration">
<xsl:with-param name="prefix">
<xsl:variable name="mapped">
<xsl:call-template name="mapped-prefix">
<xsl:with-param name="ns" select="$ns"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="string ($mapped)">
<xsl:value-of select="$mapped"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="generate-id (.)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="uri" select="$ns"/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
<xsl:for-each select="//@datatypeLibrary">
<xsl:if test=". != $xsd and . != ''">
<declaration size="9">datatypes</declaration>
<sp size="1"/>
<prefix>
<xsl:call-template name="text">
<xsl:with-param name="text" select="generate-id (.)"/>
</xsl:call-template>
</prefix>
<t size="3"> = </t>
<xsl:call-template name="str">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<nl size="1"/>
</xsl:if>
</xsl:for-each>
-->
<nl size="1"/>
<!-- Pattern -->
<xsl:apply-templates mode="RNC"/>
</compact-schema>
</xsl:template>
<xsl:template match="/rng:grammar">
<grammar>
<xsl:apply-templates mode="RNC"/>
</grammar>
</xsl:template>
<xsl:template name="make-block">
<xsl:param name="head"/>
<xsl:param name="body"/>
<xsl:param name="collapse" select="true ()"/>
<group>
<xsl:if test="not ($collapse)">
<xsl:attribute name="collapse">no</xsl:attribute>
</xsl:if>
<xsl:copy-of select="$head"/>
<nl size="1"/>
<t size="1">{</t>
<indent>
<nl size="1"/>
<xsl:copy-of select="$body"/>
</indent>
<nl size="1"/>
<t size="1">}</t>
</group>
</xsl:template>
<xsl:template name="make-parenthesis">
<xsl:param name="body"/>
<t size="1">(</t>
<indent>
<nl size="1"/>
<xsl:copy-of select="$body"/>
</indent>
<nl size="1"/>
<t size="1">)</t>
</xsl:template>
<xsl:template match="rng:grammar" mode="RNC">
<grammar>
<group>
<xsl:call-template name="annotations"/>
<xsl:call-template name="make-block">
<xsl:with-param name="collapse" select="false ()"/>
<xsl:with-param name="head">
<keyword size="7">grammar</keyword>
</xsl:with-param>
<xsl:with-param name="body">
<xsl:apply-templates mode="RNC"/>
</xsl:with-param>
</xsl:call-template>
</group>
</grammar>
</xsl:template>
<xsl:template match="rng:list | rng:mixed" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<xsl:call-template name="make-block">
<xsl:with-param name="head">
<keyword>
<xsl:call-template name="text">
<xsl:with-param name="text" select="local-name (.)"/>
</xsl:call-template>
</keyword>
</xsl:with-param>
<xsl:with-param name="body">
<xsl:call-template name="group-body"/>
</xsl:with-param>
</xsl:call-template>
</group>
</xsl:template>
<xsl:template match="rng:start" mode="RNC">
<xsl:call-template name="annotations"/>
<group>
<define size="5">
<xsl:copy-of select="@combine"/>
<xsl:text>start</xsl:text>
</define>
<xsl:call-template name="assignment-op"/>
<indent>
<nl size="1"/>
<xsl:call-template name="group-body"/>
</indent>
</group>
<xsl:call-template name="spacer"/>
</xsl:template>
<xsl:template name="assignment-op">
<xsl:choose>
<xsl:when test="@combine = 'choice'">
<t size="3"> |=</t>
</xsl:when>
<xsl:when test="@combine = 'interleave'">
<t size="3"> &=</t>
</xsl:when>
<xsl:otherwise>
<t size="2"> =</t>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="rng:define" mode="RNC">
<xsl:variable name="grammar" select="ancestor::rng:grammar [1]"/>
<xsl:call-template name="annotations"/>
<group>
<define name="{@name}">
<xsl:copy-of select="@combine"/>
<xsl:call-template name="text">
<xsl:with-param name="text">
<xsl:call-template name="quote-keyword">
<xsl:with-param name="name" select="@name"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</define>
<xsl:call-template name="assignment-op"/>
<indent>
<nl size="1"/>
<xsl:call-template name="group-body"/>
</indent>
</group>
<xsl:call-template name="spacer"/>
</xsl:template>
<xsl:template match="rng:div" mode="RNC">
<xsl:call-template name="annotations"/>
<xsl:call-template name="make-block">
<xsl:with-param name="collapse" select="false ()"/>
<xsl:with-param name="head">
<keyword size="3">div</keyword>
</xsl:with-param>
<xsl:with-param name="body">
<xsl:apply-templates mode="RNC"/>
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="spacer"/>
</xsl:template>
<xsl:template match="rng:include" mode="RNC">
<xsl:call-template name="annotations"/>
<group collapse="no">
<keyword size="7">include</keyword>
<sp size="1"/>
<include-href ref="{generate-id ()}">
<xsl:call-template name="external"/>
</include-href>
<xsl:if test="*">
<nl size="1"/>
<t size="1">{</t>
<indent>
<nl size="1"/>
<xsl:apply-templates mode="RNC"/>
</indent>
<nl size="1"/>
<t size="1">}</t>
</xsl:if>
</group>
<xsl:call-template name="spacer"/>
<xsl:if test="$recursive">
<include href="{@href}" id="{generate-id ()}">
<xsl:for-each select="document (@href)/rng:grammar">
<xsl:call-template name="make-compact-schema"/>
</xsl:for-each>
</include>
</xsl:if>
</xsl:template>
<xsl:template match="rng:externalRef" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<keyword size="8">external</keyword>
<sp size="1"/>
<external-href ref="{generate-id ()}">
<xsl:call-template name="external"/>
</external-href>
</group>
<xsl:if test="$recursive">
<external href="{@href}" id="{generate-id ()}">
<xsl:for-each select="document (@href)">
<xsl:call-template name="make-compact-schema"/>
</xsl:for-each>
</external>
</xsl:if>
</xsl:template>
<xsl:template name="make-inherit">
<xsl:param name="prefix"/>
<sp size="1"/>
<keyword size="7">inherit</keyword>
<t size="3"> = </t>
<prefix>
<xsl:call-template name="text">
<xsl:with-param name="text" select="$prefix"/>
</xsl:call-template>
</prefix>
</xsl:template>
<xsl:template name="rewrite-suffix">
<xsl:param name="href" select="@href"/>
<xsl:variable name="suffix" select="substring ($href, (string-length ($href) - 4) + 1, 4)"/>
<xsl:choose>
<xsl:when test="$rewrite-suffix and $suffix = '.rng'">
<xsl:value-of select="substring ($href, 1, string-length ($href) - 4)"/>
<xsl:text>.rnc</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$href"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="external">
<xsl:call-template name="str">
<xsl:with-param name="text">
<xsl:call-template name="rewrite-suffix"/>
</xsl:with-param>
</xsl:call-template>
<xsl:variable name="ns" select="ancestor-or-self::*[@ns][1]/@ns"/>
<xsl:choose>
<xsl:when test="$has-default-ns and $ns = $default-ns"/>
<xsl:when test="$has-local and $ns = ''">
<xsl:call-template name="make-inherit">
<xsl:with-param name="prefix">local</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="not($ns='')">
<xsl:call-template name="make-inherit">
<xsl:with-param name="prefix">
<xsl:call-template name="get-prefix">
<xsl:with-param name="nd" select="$ns"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="$has-default-ns">
<xsl:call-template name="make-inherit">
<xsl:with-param name="prefix">inh</xsl:with-param>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="rng:ref" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<ref name="{@name}">
<xsl:call-template name="text">
<xsl:with-param name="text">
<xsl:call-template name="quote-keyword">
<xsl:with-param name="name" select="@name"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</ref>
</group>
</xsl:template>
<xsl:template match="rng:parentRef" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<keyword size="6">parent</keyword>
<sp size="1"/>
<parent-ref name="{@name}">
<xsl:call-template name="text">
<xsl:with-param name="text">
<xsl:call-template name="quote-keyword">
<xsl:with-param name="name" select="@name"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</parent-ref>
</group>
</xsl:template>
<xsl:template match="rng:element | rng:attribute" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<group>
<xsl:variable name="head">
<keyword>
<xsl:call-template name="text">
<xsl:with-param name="text" select="local-name (.)"/>
</xsl:call-template>
</keyword>
<sp size="1"/>
<xsl:call-template name="name-class">
<xsl:with-param name="is-attr" select="boolean (self::rng:attribute)"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="not (rng:*) or (count (rng:*) = 1 and not (@name))">
<xsl:copy-of select="$head"/>
<t size="3"> { </t>
<atom size="4">text</atom>
<t size="2"> }</t>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="make-block">
<xsl:with-param name="head">
<xsl:copy-of select="$head"/>
</xsl:with-param>
<xsl:with-param name="body">
<xsl:choose>
<xsl:when test="@name">
<xsl:call-template name="group-body"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="group-body">
<xsl:with-param name="patterns" select="rng:*[1]/following-sibling::rng:*"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</group>
</group>
</xsl:template>
<xsl:template name="expression">
<xsl:param name="operator"/>
<xsl:param name="is-prefix-operator" select="false ()"/>
<group>
<xsl:call-template name="annotations"/>
<group>
<xsl:variable name="need-paren-rtf">
<xsl:call-template name="need-paren"/>
</xsl:variable>
<xsl:variable name="need-paren" select="boolean (string ($need-paren-rtf))"/>
<xsl:choose>
<xsl:when test="$need-paren">
<xsl:call-template name="make-parenthesis">
<xsl:with-param name="body">
<xsl:call-template name="expression-body">
<xsl:with-param name="operator" select="$operator"/>
<xsl:with-param name="is-prefix-operator" select="$is-prefix-operator"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="expression-body">
<xsl:with-param name="operator" select="$operator"/>
<xsl:with-param name="is-prefix-operator" select="$is-prefix-operator"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</group>
</group>
</xsl:template>
<xsl:template match="rng:group" mode="RNC">
<xsl:call-template name="expression">
<xsl:with-param name="operator" select="','"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="rng:choice" mode="RNC" name="make-choice">
<xsl:call-template name="expression">
<xsl:with-param name="operator" select="'| '"/>
<xsl:with-param name="is-prefix-operator" select="true ()"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="rng:interleave" mode="RNC">
<xsl:call-template name="expression">
<xsl:with-param name="operator" select="'& '"/>
<xsl:with-param name="is-prefix-operator" select="true ()"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="rng:optional" mode="RNC">
<xsl:call-template name="expression">
<xsl:with-param name="operator" select="','"/>
</xsl:call-template>
<t size="1">?</t>
</xsl:template>
<xsl:template match="rng:zeroOrMore" mode="RNC">
<xsl:call-template name="expression">
<xsl:with-param name="operator" select="','"/>
</xsl:call-template>
<t size="1">*</t>
</xsl:template>
<xsl:template match="rng:oneOrMore" mode="RNC">
<xsl:call-template name="expression">
<xsl:with-param name="operator" select="','"/>
</xsl:call-template>
<t size="1">+</t>
</xsl:template>
<xsl:template match="rng:text" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<atom size="4">text</atom>
</group>
</xsl:template>
<xsl:template match="rng:empty" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<atom size="5">empty</atom>
</group>
</xsl:template>
<xsl:template match="rng:notAllowed" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<atom size="10">notAllowed</atom>
</group>
</xsl:template>
<xsl:template match="rng:data" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<group>
<xsl:call-template name="type-name"/>
<xsl:if test="rng:param">
<nl size="1"/>
<t size="1">{</t>
<indent>
<xsl:for-each select="rng:param">
<nl size="1"/>
<group>
<xsl:call-template name="annotations"/>
<param>
<xsl:call-template name="text">
<xsl:with-param name="text" select="@name"/>
</xsl:call-template>
</param>
<t size="3"> = </t>
<xsl:call-template name="make-string-literal"/>
</group>
</xsl:for-each>
</indent>
<nl size="1"/>
<t size="1">}</t>
</xsl:if>
<xsl:for-each select="rng:except">
<nl size="1"/>
<t size="1">-</t>
<nl size="1"/>
<xsl:choose>
<xsl:when test="count (rng:*) > 1">
<xsl:call-template name="make-choice"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*" mode="RNC"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</group>
</group>
</xsl:template>
<xsl:template match="rng:value" mode="RNC">
<group>
<xsl:call-template name="annotations"/>
<xsl:variable name="type">
<xsl:call-template name="type-name"/>
</xsl:variable>
<xsl:if test="$type != 'token'">
<xsl:copy-of select="$type"/>
<sp size="1"/>
</xsl:if>
<xsl:call-template name="make-string-literal"/>
</group>
</xsl:template>
<xsl:template name="quote-quotes">
<xsl:param name="str"/>
<xsl:param name="pos" select="1"/>
<xsl:variable name="c" select="substring ($str, $pos, 1)"/>
<xsl:if test="$c">
<xsl:choose>
<xsl:when test="$c = '"'">" '"' "</xsl:when>
<xsl:when test="$c = '
'">\x{A}</xsl:when>
<xsl:when test="$c = '
'">\x{D}</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$c"/>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="quote-quotes">
<xsl:with-param name="str" select="$str"/>
<xsl:with-param name="pos" select="$pos + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="make-string-literal">
<xsl:choose>
<xsl:when test="not (contains (., '"') or contains (., "'") or contains (., '
') or contains (., '
'))">
<xsl:call-template name="str">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:when>
<xsl:when test="not (contains (., '"""'))">
<xsl:call-template name="str">
<xsl:with-param name="quote" select="'"""'"/>
<xsl:with-param name="text" select="."/>
<xsl:with-param name="is-multiline" select="true ()"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="not (contains (., "'''"))">
<xsl:call-template name="str">
<xsl:with-param name="quote">'''</xsl:with-param>
<xsl:with-param name="text" select="."/>
<xsl:with-param name="is-multiline" select="true ()"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="str">
<xsl:with-param name="text">
<xsl:call-template name="quote-quotes">
<xsl:with-param name="str" select="."/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="type-name">
<xsl:variable name="dt"
select="ancestor-or-self::rng:*[@datatypeLibrary][1]/@datatypeLibrary"/>
<xsl:variable name="dts" select="string ($dt)"/>
<type>
<xsl:call-template name="text">
<xsl:with-param name="text">
<xsl:choose>
<xsl:when test="self::rng:value and not (@type)">token</xsl:when>
<xsl:when test="$dts = '' and @type= 'string'">string</xsl:when>
<xsl:when test="$dts = '' and @type= 'token'">token</xsl:when>
<xsl:when test="$dts = ''">
<xsl:text>xsd:</xsl:text>
<xsl:value-of select="@type"/>
</xsl:when>
<xsl:when test="$dts = $xsd">
<xsl:text>xsd:</xsl:text>
<xsl:value-of select="@type"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="generate-id ($dt)"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="@type"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</type>
</xsl:template>
<xsl:template name="need-paren">
<xsl:choose>
<xsl:when test="self::rng:optional or self::rng:zeroOrMore or self::rng:oneOrMore">
<xsl:if test="count (rng:*) > 1">true</xsl:if>
</xsl:when>
<xsl:when test="parent::rng:element[not (@name)] or parent::rng:attribute [not (@name)]">
<xsl:if test="count (../rng:*) > 2">true</xsl:if>
</xsl:when>
<xsl:when test="count (../rng:*) > 1">true</xsl:when>
<xsl:when test="count (rng:*) > 1 and (parent::rng:optional or parent::rng:zeroOrMore or parent::rng:oneOrMore)">true</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="expression-body">
<xsl:param name="patterns" select="rng:*"/>
<xsl:param name="operator"/>
<xsl:param name="is-prefix-operator" select="false ()"/>
<xsl:choose>
<xsl:when test="$is-prefix-operator">
<xsl:for-each select="$patterns">
<xsl:if test="position () != 1">
<nl size="1"/>
<op size="{string-length ($operator)}">
<xsl:value-of select="$operator"/>
</op>
</xsl:if>
<xsl:apply-templates select="." mode="RNC"/>
<xsl:call-template name="follow-annotations"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$patterns">
<xsl:if test="position () != 1">
<op size="{string-length ($operator)}">
<xsl:value-of select="$operator"/>
</op>
<nl size="1"/>
</xsl:if>
<xsl:apply-templates select="." mode="RNC"/>
<xsl:call-template name="follow-annotations"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="group-body">
<xsl:param name="patterns" select="rng:*"/>
<xsl:call-template name="expression-body">
<xsl:with-param name="patterns" select="$patterns"/>
<xsl:with-param name="operator" select="','"/>
</xsl:call-template>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Name class -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template name="name-class">
<xsl:param name="is-attr" select="false ()"/>
<nc>
<xsl:call-template name="text">
<xsl:with-param name="text">
<xsl:choose>
<xsl:when test="@name">
<xsl:choose>
<xsl:when test="not ($is-attr) and not (contains (@name, ':'))">
<xsl:call-template name="make-name-nc">
<xsl:with-param name="name" select="@name"/>
<xsl:with-param name="is-attr" select="false ()"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="@ns">
<xsl:text>[</xsl:text>
<xsl:value-of select="@ns"/>
<xsl:text>]</xsl:text>
<xsl:value-of select="@name"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="rng:*[1]" mode="name-class">
<xsl:with-param name="is-attr" select="$is-attr"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</nc>
</xsl:template>
<xsl:template match="rng:name" mode="name-class">
<xsl:param name="is-attr"/>
<xsl:call-template name="make-name-nc">
<xsl:with-param name="name" select="."/>
<xsl:with-param name="is-attr" select="$is-attr"/>
</xsl:call-template>
</xsl:template>