This repository has been archived by the owner on Dec 1, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rng-simplify.xsl
1163 lines (966 loc) · 47.4 KB
/
rng-simplify.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"?>
<!--
Copyright 2015 Max Toro Q.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<stylesheet version="2.0" exclude-result-prefixes="#all"
xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:rng="http://relaxng.org/ns/structure/1.0"
xmlns:f="http://maxtoroq.github.io/rng.xsl/syntax"
xmlns:s="http://maxtoroq.github.io/rng.xsl/simplify">
<import href="rng-syntax.xsl"/>
<variable name="s:rng-ns" select="'http://relaxng.org/ns/structure/1.0'"/>
<template match="document-node()[rng:*]" mode="s:main">
<call-template name="s:phase1-main">
<with-param name="pattern" select="."/>
</call-template>
</template>
<template match="document-node()[not(rng:*)]" mode="s:main">
<sequence select="error($f:error-code, f:error-message(., concat('Expecting element in ', $s:rng-ns, ' namespace.')))"/>
</template>
<template match="node()" mode="s:main">
<sequence select="error($f:error-code, f:error-message(., 'Expecting document node.'))"/>
</template>
<template match="/ | @* | * | text()" priority="-2"
mode="s:phase1 s:phase2 s:phase3 s:phase4 s:phase5 s:phase6 s:phase7 s:phase8">
<!-- Comments and PIs are not copied -->
<copy copy-namespaces="no">
<apply-templates select="@*, node()" mode="#current"/>
</copy>
</template>
<!--
## Phase 1: Inclusions and data types simplification
-->
<template name="s:phase1-main">
<param name="pattern" as="node()"/>
<variable name="valid-syntax" as="xs:boolean">
<apply-templates select="$pattern" mode="f:pattern"/>
</variable>
<if test="$valid-syntax">
<variable name="result" as="node()">
<apply-templates select="$pattern" mode="s:phase1"/>
</variable>
<call-template name="s:phase2-main">
<with-param name="pattern" select="$result"/>
</call-template>
</if>
</template>
<template match="*" mode="s:phase1" priority="-1">
<!--
Phase 1 must preserve namespaces for phase 2.
-->
<copy>
<apply-templates select="@*, node()" mode="#current"/>
</copy>
</template>
<template match="*[not(self::rng:*)] | @*[namespace-uri()]" mode="s:phase1">
<!--
4.1. Foreign attributes and elements are removed.
-->
</template>
<template match="*[* and not(text()[normalize-space()])]/text()" mode="s:phase1">
<!--
4.2 For each element other than value and param, each child that is a string containing only whitespace characters is removed.
-->
</template>
<template match="@name | @type | @combine" mode="s:phase1">
<!--
4.2 Leading and trailing whitespace characters are removed from the value of each name, type and combine attribute.
-->
<attribute name="{name()}" select="normalize-space()"/>
</template>
<template match="rng:name/text()" mode="s:phase1">
<!--
4.2 Leading and trailing whitespace characters are removed from the content of each name element.
-->
<value-of select="normalize-space()"/>
</template>
<template match="rng:*[not(self::rng:data or self::rng:value)]/@datatypeLibrary" mode="s:phase1">
<!--
4.3. Any datatypeLibrary attribute that is on an element other than data or value is removed.
-->
</template>
<template match="rng:data[not(@datatypeLibrary)]" mode="s:phase1">
<copy>
<!--
4.3 For any data or value element that does not have a datatypeLibrary attribute, a datatypeLibrary attribute is added. The value of the added datatypeLibrary attribute is the value of the datatypeLibrary attribute of the nearest ancestor element that has a datatypeLibrary attribute, or the empty string if there is no such ancestor.
-->
<attribute name="datatypeLibrary" select="(ancestor::*/@datatypeLibrary)[1]"/>
<apply-templates select="@*, node()" mode="#current"/>
</copy>
</template>
<template match="rng:value[@type and not(@datatypeLibrary)]" mode="s:phase1">
<copy>
<!--
4.3 For any data or value element that does not have a datatypeLibrary attribute, a datatypeLibrary attribute is added. The value of the added datatypeLibrary attribute is the value of the datatypeLibrary attribute of the nearest ancestor element that has a datatypeLibrary attribute, or the empty string if there is no such ancestor.
-->
<attribute name="datatypeLibrary" select="(ancestor::*/@datatypeLibrary)[1]"/>
<apply-templates select="@*, node()" mode="#current"/>
</copy>
</template>
<template match="rng:value[not(@type)]" mode="s:phase1">
<copy>
<!--
4.4 For any value element that does not have a type attribute, a type attribute is added with value token and the value of the datatypeLibrary attribute is changed to the empty string.
-->
<attribute name="type" select="'token'"/>
<attribute name="datatypeLibrary" select="''"/>
<apply-templates select="@* except @datatypeLibrary, node()" mode="#current"/>
</copy>
</template>
<template match="rng:externalRef" mode="s:phase1">
<param name="ref-ns" as="attribute(ns)?"/>
<param name="docs" select="root()" as="document-node()*" tunnel="yes"/>
<variable name="ref-doc" select="doc(resolve-uri(s:phase1-transform-href(@href), base-uri()))"/>
<if test="some $d in $docs satisfies $d is $ref-doc">
<sequence select="error($f:error-code, f:error-message(., 'Circular reference.'))"/>
</if>
<variable name="valid-syntax" as="xs:boolean">
<apply-templates select="$ref-doc" mode="f:pattern"/>
</variable>
<if test="$valid-syntax">
<apply-templates select="$ref-doc/rng:*" mode="s:phase1-external">
<with-param name="ref-ns" select="(@ns, $ref-ns)[1]"/>
<with-param name="docs" select="$docs, $ref-doc" tunnel="yes"/>
</apply-templates>
</if>
</template>
<template match="/rng:*[not(self::rng:externalRef)]" mode="s:phase1-external">
<param name="ref-ns" as="attribute(ns)?"/>
<copy>
<copy-of select="($ref-ns, @ns)[1]"/>
<apply-templates select="@* except @ns, node()" mode="s:phase1"/>
</copy>
</template>
<template match="/rng:externalRef" mode="s:phase1-external">
<param name="ref-ns" as="attribute(ns)?"/>
<apply-templates select="." mode="s:phase1">
<with-param name="ref-ns" select="$ref-ns"/>
</apply-templates>
</template>
<template match="rng:include" mode="s:phase1">
<param name="docs" select="root()" as="document-node()*" tunnel="yes"/>
<variable name="ref-doc" select="doc(resolve-uri(s:phase1-transform-href(@href), base-uri()))"/>
<if test="some $d in $docs satisfies $d is $ref-doc">
<sequence select="error($f:error-code, f:error-message(., 'Circular reference.'))"/>
</if>
<variable name="valid-syntax" as="xs:boolean">
<apply-templates select="$ref-doc" mode="f:pattern"/>
</variable>
<if test="$valid-syntax">
<!--
4.7 The include element is transformed into a div element. The attributes of the div element are the attributes of the include element other than the href attribute. The children of the div element are the grammar element (after the removal of the start and define components described by the preceding paragraph) followed by the children of the include element.
-->
<element name="div" namespace="{$s:rng-ns}">
<copy-of select="@* except @href"/>
<apply-templates select="$ref-doc/rng:grammar" mode="s:phase1-include">
<with-param name="including" select="true()" tunnel="yes"/>
<with-param name="include-has-start" select="not(empty(rng:start))" tunnel="yes"/>
<with-param name="include-defs" select="rng:define/@name/normalize-space()" tunnel="yes"/>
<with-param name="docs" select="$docs, $ref-doc" tunnel="yes"/>
</apply-templates>
<copy-of select="node()"/>
</element>
</if>
</template>
<template match="/rng:grammar" mode="s:phase1-include">
<param name="include-has-start" tunnel="yes"/>
<param name="include-defs" tunnel="yes"/>
<!--
4.7
-->
<if test="$include-has-start and empty(rng:start)">
<sequence select="error($f:error-code, f:error-message(., 'If the include element has a start component, then the grammar element must have a start component.'))"/>
</if>
<!--
4.7
-->
<if test="not(empty($include-defs))
and not(every $d in $include-defs satisfies rng:define[@name/normalize-space() eq $d])">
<sequence select="error($f:error-code, f:error-message(., 'If the include element has a define component, then the grammar element must have a define component with the same name.'))"/>
</if>
<!--
4.7 The grammar element is then renamed to div.
-->
<element name="div" namespace="{$s:rng-ns}">
<apply-templates select="@*, node()" mode="s:phase1"/>
</element>
</template>
<template match="rng:start" mode="s:phase1">
<param name="including" tunnel="yes"/>
<param name="include-has-start" tunnel="yes"/>
<!--
4.7 If the include element has a start component, then all start components are removed from the grammar element.
-->
<if test="not($including and $include-has-start)">
<next-match/>
</if>
</template>
<template match="rng:define" mode="s:phase1">
<param name="including" tunnel="yes"/>
<param name="include-defs" tunnel="yes"/>
<!--
4.7 For every define component of the include element, all define components with the same name are removed from the grammar element.
-->
<if test="not($including and @name/normalize-space() = $include-defs)">
<next-match/>
</if>
</template>
<function name="s:phase1-transform-href">
<param name="href" as="node()"/>
<!--
4.5 The href attribute must not include a fragment identifier unless the registration of the media type of the resource identified by the attribute defines the interpretation of fragment identifiers for that media type.
-->
<sequence select="if (contains($href, '#')) then
error($f:error-code, f:error-message($href, 'The href attribute must not include a fragment identifier.'))
else $href"/>
</function>
<!--
## Phase 2: Names simplification
-->
<template name="s:phase2-main">
<param name="pattern" as="node()"/>
<variable name="result" as="node()">
<apply-templates select="$pattern" mode="s:phase2"/>
</variable>
<call-template name="s:phase3-main">
<with-param name="pattern" select="$result"/>
</call-template>
</template>
<template match="rng:element[@name] | rng:attribute[@name]" mode="s:phase2">
<copy copy-namespaces="no">
<apply-templates select="@* except @name" mode="#current"/>
<element name="name" namespace="{$s:rng-ns}">
<choose>
<when test="contains(@name, ':')">
<!--
4.10 For any name element containing a prefix, the prefix is removed and an ns attribute is added replacing any existing ns attribute. The value of the added ns attribute is the value to which the namespace map of the context of the name element maps the prefix. The context must have a mapping for the prefix.
-->
<attribute name="ns" select="namespace-uri-for-prefix(substring-before(@name, ':'), .)"/>
<value-of select="substring-after(@name, ':')"/>
</when>
<otherwise>
<!--
4.8 If an attribute element has a name attribute but no ns attribute, then an ns="" attribute is added to the name child element.
4.9 For any name, nsName or value element that does not have an ns attribute, an ns attribute is added. The value of the added ns attribute is the value of the ns attribute of the nearest ancestor element that has an ns attribute, or the empty string if there is no such ancestor.
-->
<attribute name="ns" select="
if (self::rng:attribute) then (@ns, '')[1]
else (ancestor-or-self::*/@ns)[1]"/>
<value-of select="@name"/>
</otherwise>
</choose>
</element>
<apply-templates select="node()" mode="#current"/>
</copy>
</template>
<template match="rng:name" mode="s:phase2">
<copy copy-namespaces="no">
<choose>
<when test="contains(text(), ':')">
<!--
4.10 For any name element containing a prefix, the prefix is removed and an ns attribute is added replacing any existing ns attribute. The value of the added ns attribute is the value to which the namespace map of the context of the name element maps the prefix. The context must have a mapping for the prefix.
-->
<attribute name="ns" select="namespace-uri-for-prefix(substring-before(text(), ':'), .)"/>
<apply-templates select="@* except @ns" mode="#current"/>
<value-of select="substring-after(text(), ':')"/>
<apply-templates select="node() except text()" mode="#current"/>
</when>
<otherwise>
<if test="not(@ns)">
<!--
4.9 For any name, nsName or value element that does not have an ns attribute, an ns attribute is added. The value of the added ns attribute is the value of the ns attribute of the nearest ancestor element that has an ns attribute, or the empty string if there is no such ancestor.
-->
<attribute name="ns" select="(ancestor::*/@ns)[1]"/>
</if>
<apply-templates select="@*, node()" mode="#current"/>
</otherwise>
</choose>
</copy>
</template>
<template match="rng:nsName[not(@ns)] | rng:value[not(@ns)]" mode="s:phase2">
<copy copy-namespaces="no">
<!--
4.9 For any name, nsName or value element that does not have an ns attribute, an ns attribute is added. The value of the added ns attribute is the value of the ns attribute of the nearest ancestor element that has an ns attribute, or the empty string if there is no such ancestor.
-->
<attribute name="ns" select="(ancestor::*/@ns)[1]"/>
<apply-templates select="@*, node()" mode="#current"/>
</copy>
</template>
<template match="rng:*[not(self::rng:name or self::rng:nsName or self::rng:value)]/@ns" mode="s:phase2">
<!--
4.9. Any ns attribute that is on an element other than name, nsName or value is removed.
-->
</template>
<template match="rng:div" mode="s:phase2">
<!--
4.11 Each div element is replaced by its children.
-->
<apply-templates mode="#current"/>
</template>
<!--
## Phase 3: Patterns simplification
-->
<template name="s:phase3-main">
<param name="pattern" as="node()"/>
<variable name="result" as="node()">
<apply-templates select="$pattern" mode="s:phase3"/>
</variable>
<variable name="constraints" as="item()*">
<apply-templates select="$result" mode="s:phase3-constraints"/>
</variable>
<if test="empty($constraints)">
<call-template name="s:phase4-main">
<with-param name="pattern" select="$result"/>
</call-template>
</if>
</template>
<template match="rng:define[count(*) gt 1] | rng:oneOrMore[count(*) gt 1] | rng:list[count(*) gt 1]" mode="s:phase3">
<!--
4.12 A define, oneOrMore, zeroOrMore, optional, list or mixed element is transformed so that it has exactly one child element. If it has more than one child element, then its child elements are wrapped in a group element.
-->
<copy>
<apply-templates select="@*" mode="#current"/>
<variable name="group" as="element()">
<element name="group" namespace="{$s:rng-ns}">
<sequence select="*"/>
</element>
</variable>
<apply-templates select="$group" mode="#current"/>
</copy>
</template>
<template match="rng:element[count(*) gt 2]" mode="s:phase3">
<!--
4.12 Similarly, an element element is transformed so that it has exactly two child elements, the first being a name class and the second being a pattern. If it has more than two child elements, then the child elements other than the first are wrapped in a group element.
-->
<copy>
<apply-templates select="@*, *[1]" mode="#current"/>
<variable name="group" as="element()">
<element name="group" namespace="{$s:rng-ns}">
<sequence select="*[position() gt 1]"/>
</element>
</variable>
<apply-templates select="$group" mode="#current"/>
</copy>
</template>
<template match="rng:attribute[count(*) eq 1]" mode="s:phase3">
<!--
4.12 If an attribute element has only one child element (a name class), then a text element is added.
-->
<copy>
<apply-templates select="@*, node()" mode="#current"/>
<element name="text" namespace="{$s:rng-ns}"/>
</copy>
</template>
<template match="rng:mixed" mode="s:phase3">
<!--
4.13 A mixed element is transformed into an interleaving with a text element.
-->
<element name="interleave" namespace="{$s:rng-ns}">
<choose>
<when test="count(*) gt 1">
<!--
4.12 A define, oneOrMore, zeroOrMore, optional, list or mixed element is transformed so that it has exactly one child element. If it has more than one child element, then its child elements are wrapped in a group element.
-->
<variable name="group" as="element()">
<element name="group" namespace="{$s:rng-ns}">
<sequence select="*"/>
</element>
</variable>
<apply-templates select="$group" mode="#current"/>
</when>
<otherwise>
<apply-templates mode="#current"/>
</otherwise>
</choose>
<element name="text" namespace="{$s:rng-ns}"/>
</element>
</template>
<template match="rng:optional" mode="s:phase3">
<!--
4.14 An optional element is transformed into a choice element with one child being the child of the optional element and the other child being empty.
-->
<element name="choice" namespace="{$s:rng-ns}">
<choose>
<when test="count(*) gt 1">
<!--
4.12 A define, oneOrMore, zeroOrMore, optional, list or mixed element is transformed so that it has exactly one child element. If it has more than one child element, then its child elements are wrapped in a group element.
-->
<variable name="group" as="element()">
<element name="group" namespace="{$s:rng-ns}">
<sequence select="*"/>
</element>
</variable>
<apply-templates select="$group" mode="#current"/>
</when>
<otherwise>
<apply-templates mode="#current"/>
</otherwise>
</choose>
<element name="empty" namespace="{$s:rng-ns}"/>
</element>
</template>
<template match="rng:except[count(*) gt 1]" mode="s:phase3">
<!--
4.12 A except element is transformed so that it has exactly one child element. If it has more than one child element, then its child elements are wrapped in a choice element.
-->
<copy>
<apply-templates select="@*" mode="#current"/>
<variable name="choice" as="element()">
<element name="choice" namespace="{$s:rng-ns}">
<sequence select="*"/>
</element>
</variable>
<apply-templates select="$choice" mode="#current"/>
</copy>
</template>
<template match="rng:zeroOrMore" mode="s:phase3">
<!--
4.15 A zeroOrMore element is transformed into a choice element with one child being an <oneOrMore> p </oneOrMore> element and the other child being an empty element, where p is the child of the zeroOrMore element.
-->
<element name="choice" namespace="{$s:rng-ns}">
<element name="oneOrMore" namespace="{$s:rng-ns}">
<choose>
<when test="count(*) gt 1">
<!--
4.12 A define, oneOrMore, zeroOrMore, optional, list or mixed element is transformed so that it has exactly one child element. If it has more than one child element, then its child elements are wrapped in a group element.
-->
<variable name="group" as="element()">
<element name="group" namespace="{$s:rng-ns}">
<sequence select="*"/>
</element>
</variable>
<apply-templates select="$group" mode="#current"/>
</when>
<otherwise>
<apply-templates mode="#current"/>
</otherwise>
</choose>
</element>
<element name="empty" namespace="{$s:rng-ns}"/>
</element>
</template>
<template match="rng:choice[count(*) eq 1] | rng:group[count(*) eq 1] | rng:interleave[count(*) eq 1]" mode="s:phase3">
<!--
4.12 A choice, group or interleave element is transformed so that it has exactly two child elements. If it has one child element, then it is replaced by its child element.
-->
<apply-templates mode="#current"/>
</template>
<template match="rng:choice[count(*) gt 2] | rng:group[count(*) gt 2] | rng:interleave[count(*) gt 2]" mode="s:phase3">
<!--
4.12 A choice, group or interleave element is transformed so that it has exactly two child elements. If it has one child element, then it is replaced by its child element. If it has more than two child elements, then the first two child elements are combined into a new element with the same name as the parent element and with the first two child elements as its children.
-->
<variable name="copy" as="element()">
<copy>
<copy-of select="@*"/>
<element name="{local-name()}" namespace="{$s:rng-ns}">
<sequence select="*[position() = (1, 2)]"/>
</element>
<sequence select="*[position() gt 2]"/>
</copy>
</variable>
<!--
The transformation is applied repeatedly until there are exactly two child elements.
-->
<apply-templates select="$copy" mode="#current"/>
</template>
<template match="text()" mode="s:phase3-constraints"/>
<template match="rng:anyName/rng:except//rng:anyName" mode="s:phase3-constraints">
<!--
4.16
-->
<sequence select="error($f:error-code, f:error-message(., 'An except element that is a child of an anyName element must not have any anyName descendant elements.'))"/>
</template>
<template match="rng:nsName/rng:except//rng:nsName | rng:nsName/rng:except//rng:anyName" mode="s:phase3-constraints">
<!--
4.16
-->
<sequence select="error($f:error-code, f:error-message(., 'An except element that is a child of an nsName element must not have any nsName or anyName descendant elements.'))"/>
</template>
<template match="rng:attribute/rng:name[empty(preceding-sibling::rng:*) and @ns = '' and text() = 'xmlns']
| rng:attribute/rng:*[1]//rng:name[@ns = '' and text() = 'xmlns']" mode="s:phase3-constraints">
<!--
4.16
-->
<sequence select="error($f:error-code, f:error-message(., 'A name element that occurs as the first child of an attribute element or as the descendant of the first child of an attribute element and that has an ns attribute with value equal to the empty string must not have content equal to xmlns.'))"/>
</template>
<template match="rng:attribute/rng:name[empty(preceding-sibling::rng:*) and @ns = 'http://www.w3.org/2000/xmlns']
| rng:attribute/rng:*[1]//rng:name[@ns = 'http://www.w3.org/2000/xmlns']
| rng:attribute/rng:nsName[empty(preceding-sibling::rng:*) and @ns = 'http://www.w3.org/2000/xmlns']
| rng:attribute/rng:*[1]//rng:nsName[@ns = 'http://www.w3.org/2000/xmlns']" mode="s:phase3-constraints">
<!--
4.16
-->
<sequence select="error($f:error-code, f:error-message(., 'A name or nsName element that occurs as the first child of an attribute element or as the descendant of the first child of an attribute element must not have an ns attribute with value http://www.w3.org/2000/xmlns.'))"/>
</template>
<template match="rng:value[@datatypeLibrary = '' and not(@type = ('token', 'string'))]
| rng:data[@datatypeLibrary = '' and not(@type = ('token', 'string'))]" mode="s:phase3-constraints">
<!--
4.16
-->
<sequence select="error($f:error-code, f:error-message(., 'The type attribute must identify a datatype within the datatype library identified by the value of the datatypeLibrary attribute.'))"/>
</template>
<template match="rng:data[@datatypeLibrary = '' and rng:param]" mode="s:phase3-constraints">
<!--
4.16
-->
<sequence select="error($f:error-code, f:error-message(., 'Types in the built-in datatype library do not accept any parameters.'))"/>
</template>
<!--
## Phase 4: define/start combining
-->
<template name="s:phase4-main">
<param name="pattern" as="node()"/>
<variable name="result" as="node()">
<apply-templates select="$pattern" mode="s:phase4"/>
</variable>
<call-template name="s:phase5-main">
<with-param name="pattern" select="$result"/>
</call-template>
</template>
<template match="rng:grammar" mode="s:phase4">
<copy>
<apply-templates select="@*" mode="#current"/>
<!--
4.17 For each grammar element, all define elements with the same name are combined together.
-->
<for-each-group select="rng:define" group-by="@name">
<choose>
<when test="count(current-group()) eq 1">
<apply-templates select="current-group()" mode="#current"/>
</when>
<otherwise>
<variable name="combine" select="distinct-values(current-group()/@combine/string())"/>
<if test="count(current-group()[not(@combine)]) gt 1">
<sequence select="error($f:error-code, f:error-message(., 'For any name, there must not be more than one define element with that name that does not have a combine attribute.'))"/>
</if>
<if test="count($combine) gt 1">
<sequence select="error($f:error-code, f:error-message(., 'For any name, if there is a define element with that name that has a combine attribute with the value choice, then there must not also be a define element with that name that has a combine attribute with the value interleave.'))"/>
</if>
<variable name="define" as="element()">
<element name="define" namespace="{$s:rng-ns}">
<attribute name="name" select="current-grouping-key()"/>
<element name="{$combine}" namespace="{$s:rng-ns}">
<sequence select="current-group()[position() = (1, 2)]/node()"/>
</element>
<sequence select="current-group()[position() gt 2]"/>
</element>
</variable>
<apply-templates select="$define" mode="s:phase4-combine"/>
</otherwise>
</choose>
</for-each-group>
<!--
4.17 Similarly, for each grammar element all start elements are combined together.
-->
<choose>
<when test="count(rng:start) le 1">
<apply-templates select="rng:start" mode="#current"/>
</when>
<otherwise>
<variable name="combine" select="distinct-values(rng:start/@combine/string())"/>
<if test="count(rng:start[not(@combine)]) gt 1">
<sequence select="error($f:error-code, f:error-message(., 'There must not be more than one start element that does not have a combine attribute.'))"/>
</if>
<if test="count($combine) gt 1">
<sequence select="error($f:error-code, f:error-message(., 'If there is a start element that has a combine attribute with the value choice, there must not also be a start element that has a combine attribute with the value interleave.'))"/>
</if>
<variable name="start" as="element()">
<element name="start" namespace="{$s:rng-ns}">
<element name="{$combine}" namespace="{$s:rng-ns}">
<sequence select="rng:start[position() = (1, 2)]/node()"/>
</element>
<sequence select="rng:start[position() gt 2]"/>
</element>
</variable>
<apply-templates select="$start" mode="s:phase4-combine"/>
</otherwise>
</choose>
<apply-templates select="* except (rng:define, rng:start)" mode="#current"/>
</copy>
</template>
<template match="rng:define[not(rng:define)] | rng:start[not(rng:start)]" mode="s:phase4-combine">
<apply-templates select="." mode="s:phase4"/>
</template>
<template match="rng:define[rng:define] | rng:start[rng:start]" mode="s:phase4-combine">
<variable name="copy" as="element()">
<copy>
<copy-of select="@*"/>
<element name="{*[1]/local-name()}" namespace="{$s:rng-ns}">
<sequence select="*[1], *[2]/node()"/>
</element>
<sequence select="*[position() gt 2]"/>
</copy>
</variable>
<apply-templates select="$copy" mode="#current"/>
</template>
<!--
## Phase 5: Grammars simplification
-->
<template name="s:phase5-main">
<param name="pattern" as="node()"/>
<variable name="result" as="node()">
<apply-templates select="$pattern" mode="s:phase5"/>
</variable>
<call-template name="s:phase6-main">
<with-param name="pattern" select="$result"/>
</call-template>
</template>
<template match="/" mode="s:phase5">
<document>
<!--
4.18 First, transform the top-level pattern p into <grammar><start>p</start></grammar>.
-->
<element name="grammar" namespace="{$s:rng-ns}">
<element name="start" namespace="{$s:rng-ns}">
<apply-templates select="rng:*" mode="s:phase5">
<with-param name="ignore-define" select="true()" tunnel="yes"/>
</apply-templates>
</element>
<!--
4.18 Move all define elements to be children of the top-level grammar element.
-->
<apply-templates select="descendant::rng:define" mode="s:phase5"/>
</element>
</document>
</template>
<template match="rng:define" mode="s:phase5">
<param name="ignore-define" tunnel="yes"/>
<if test="not($ignore-define)">
<copy>
<!--
4.18 Rename define elements so that no two define elements anywhere in the schema have the same name.
-->
<attribute name="name" select="s:phase5-define-name(.)"/>
<apply-templates select="@* except @name, node()" mode="#current"/>
</copy>
</if>
</template>
<template match="rng:grammar" mode="s:phase5">
<if test="not(rng:start)">
<sequence select="error($f:error-code, f:error-message(., 'A grammar must have a start child element.'))"/>
</if>
<!--
4.18 Replace each nested grammar element by the child of its start element.
-->
<apply-templates select="rng:start/node()" mode="#current"/>
</template>
<template match="rng:ref/@name" mode="s:phase5">
<variable name="define" select="ancestor::rng:grammar[1]/rng:define[@name=current()]"/>
<if test="not($define)">
<sequence select="error($f:error-code, f:error-message(., 'Every ref element must refer to a define element.'))"/>
</if>
<!--
4.18 To rename a define element, change the value of its name attribute and change the value of the name attribute of all ref and parentRef elements that refer to that define element.
-->
<attribute name="name" select="$define/s:phase5-define-name(.)"/>
</template>
<template match="rng:parentRef" mode="s:phase5">
<!--
4.18 Rename each parentRef element to ref.
-->
<element name="ref" namespace="{$s:rng-ns}">
<apply-templates select="@*, node()" mode="#current"/>
</element>
</template>
<template match="rng:parentRef/@name" mode="s:phase5">
<variable name="define" select="ancestor::rng:grammar[2]/rng:define[@name=current()]"/>
<if test="not($define)">
<sequence select="error($f:error-code, f:error-message(., 'Every parentRef element must refer to a define element.'))"/>
</if>
<!--
4.18 To rename a define element, change the value of its name attribute and change the value of the name attribute of all ref and parentRef elements that refer to that define element.
-->
<attribute name="name" select="$define/s:phase5-define-name(.)"/>
</template>
<function name="s:phase5-define-name">
<param name="define" as="element(rng:define)"/>
<sequence select="concat($define/@name, '_', generate-id($define))"/>
</function>
<!--
## Phase 6: Move elements to define
-->
<template name="s:phase6-main">
<param name="pattern" as="node()"/>
<variable name="result" as="node()">
<apply-templates select="$pattern" mode="s:phase6"/>
</variable>
<call-template name="s:phase7-main">
<with-param name="pattern" select="$result"/>
</call-template>
</template>
<template match="rng:grammar" mode="s:phase6">
<!--
4.19 Now, for each element element that is not the child of a define element, add a define element to the grammar element, and replace the element element by a ref element referring to the added define element. The child of the added define element is the element element.
-->
<copy>
<apply-templates select="@*, node()" mode="#current"/>
<for-each select=".//rng:element[not(parent::rng:define)]">
<element name="define" namespace="{$s:rng-ns}">
<attribute name="name" select="s:phase6-define-name(.)"/>
<element name="element" namespace="{$s:rng-ns}">
<apply-templates select="@*, node()" mode="#current"/>
</element>
</element>
</for-each>
</copy>
</template>
<template match="rng:define" mode="s:phase6">
<!--
4.19 First, remove any define element that is not reachable.
-->
<if test="root()//rng:ref[@name=current()/@name]">
<next-match/>
</if>
</template>
<template match="rng:define[not(rng:element)]" mode="s:phase6">
<!--
4.19 Finally, remove any define element whose child is not an element element.
-->
</template>
<template match="rng:element[not(parent::rng:define)]" mode="s:phase6">
<!--
4.19 Now, for each element element that is not the child of a define element, add a define element to the grammar element, and replace the element element by a ref element referring to the added define element.
-->
<element name="ref" namespace="{$s:rng-ns}">
<attribute name="name" select="s:phase6-define-name(.)"/>
</element>
</template>
<template match="rng:ref" mode="s:phase6">
<param name="defs" as="element(rng:define)*" tunnel="yes"/>
<!--
4.19 Define a ref element to be expandable if it refers to a define element whose child is not an element element. For each ref element that is expandable and is a descendant of a start element or an element element, expand it by replacing the ref element by the child of the define element to which it refers and then recursively expanding any expandable ref elements in this replacement.
-->
<variable name="define" select="ancestor::rng:grammar/rng:define[@name=current()/@name]"/>
<choose>
<when test="not($define/rng:element)">
<if test="some $d in $defs satisfies $d is $define">
<sequence select="error($f:error-code, f:error-message(., 'Circular reference.'))"/>
</if>
<apply-templates select="$define/node()" mode="#current">
<with-param name="defs" select="$defs, $define" tunnel="yes"/>
</apply-templates>
</when>
<otherwise>
<next-match/>
</otherwise>
</choose>
</template>
<function name="s:phase6-define-name">
<param name="element" as="element(rng:element)"/>
<sequence select="
if ($element/rng:name) then concat($element/rng:name[1], '_', generate-id($element))
else generate-id($element)
"/>
</function>
<!--
## Phase 7: notAllowed simplification
-->
<template name="s:phase7-main">
<param name="pattern" as="node()"/>
<variable name="result" as="node()">
<apply-templates select="$pattern" mode="s:phase7"/>
</variable>
<call-template name="s:phase8-main">
<with-param name="pattern" select="$result"/>
</call-template>
</template>
<template match="rng:grammar" mode="s:phase7">
<variable name="copy" as="element()">
<copy>
<apply-templates select="@*, node()" mode="#current"/>
</copy>
</variable>
<choose>
<!--
4.20 In this rule, the grammar is transformed so that a notAllowed element occurs only as the child of a start or element element.
-->
<when test="$copy//rng:notAllowed[not(parent::rng:start or parent::rng:element)]">
<apply-templates select="$copy" mode="#current"/>
</when>
<otherwise>
<sequence select="$copy"/>
</otherwise>
</choose>
</template>
<template match="rng:attribute[rng:notAllowed] | rng:list[rng:notAllowed] | rng:group[rng:notAllowed] | rng:interleave[rng:notAllowed] | rng:oneOrMore[rng:notAllowed]" mode="s:phase7">
<!--
4.20 An attribute, list, group, interleave, or oneOrMore element that has a notAllowed child element is transformed into a notAllowed element.
-->
<element name="notAllowed" namespace="{$s:rng-ns}"/>
</template>
<template match="rng:choice[every $child in * satisfies $child[self::rng:notAllowed]]" mode="s:phase7">
<!--
4.20 A choice element that has two notAllowed child elements is transformed into a notAllowed element.
-->
<element name="notAllowed" namespace="{$s:rng-ns}"/>
</template>
<template match="rng:choice[rng:notAllowed and *[not(self::rng:notAllowed)]]" mode="s:phase7">
<!--
4.20 A choice element that has one notAllowed child element is transformed into its other child element.
-->
<apply-templates select="*[not(self::rng:notAllowed)]" mode="#current"/>
</template>
<template match="rng:except[rng:notAllowed]" mode="s:phase7">
<!--
4.20 An except element that has a notAllowed child element is removed.
-->
</template>
<!--
## Phase 8: empty simplification
-->
<template name="s:phase8-main">
<param name="pattern" as="node()"/>
<variable name="result" as="node()">
<apply-templates select="$pattern" mode="s:phase8"/>
</variable>
<variable name="errors" as="item()*">
<apply-templates select="$result" mode="s:prohibited-path"/>
</variable>
<sequence select="$result[empty($errors)]"/>
</template>
<template match="rng:grammar" mode="s:phase8">
<variable name="copy" as="element()">
<copy>
<apply-templates select="@*, node()" mode="#current"/>
</copy>
</variable>
<choose>
<!--