/
TAN-fn-nodes-standard.xsl
1949 lines (1732 loc) · 95.5 KB
/
TAN-fn-nodes-standard.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
<xsl:stylesheet exclude-result-prefixes="#all"
xmlns="tag:textalign.net,2015:ns"
xmlns:tan="tag:textalign.net,2015:ns"
xmlns:tei="http://www.tei-c.org/ns/1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="3.0">
<!-- TAN Function Library: standard functions on nodes -->
<xsl:function name="tan:shallow-copy" as="item()*" visibility="public">
<!-- one-parameter version of the fuller one, below -->
<xsl:param name="items" as="item()*"/>
<xsl:copy-of select="tan:shallow-copy($items, 1)"/>
</xsl:function>
<xsl:function name="tan:shallow-copy" as="item()*" visibility="public">
<!-- Input: any document fragment; boolean indicating whether attributes should be kept -->
<!-- Output: a shallow copy of the fragment -->
<!-- Attributes will be preserved in a shallow-copied element. -->
<!-- Maps and arrays will be discarded. -->
<!-- This function was written to truncate large trees for output to messages and diagnostic
result trees. -->
<!--kw: nodes, tree manipulation -->
<xsl:param name="items" as="item()*"/>
<xsl:param name="depth" as="xs:integer"/>
<xsl:apply-templates select="$items" mode="tan:fn-shallow-copy">
<xsl:with-param name="levels-to-go" select="$depth"/>
</xsl:apply-templates>
</xsl:function>
<xsl:mode name="tan:fn-shallow-copy" on-no-match="shallow-skip"/>
<xsl:template match="node() | document-node()" mode="tan:fn-shallow-copy">
<xsl:param name="levels-to-go" as="xs:integer?"/>
<xsl:if test="$levels-to-go gt 0">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="#current">
<xsl:with-param name="levels-to-go" select="$levels-to-go - 1"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:function name="tan:xml-to-string" as="xs:string?" visibility="public">
<!-- one-parameter version of the fuller one, below -->
<xsl:param name="fragment" as="item()*"/>
<xsl:value-of select="tan:xml-to-string($fragment, false())"/>
</xsl:function>
<xsl:function name="tan:xml-to-string" as="xs:string?" visibility="public">
<!-- Input: any fragment of XML; boolean indicating whether whitespace nodes should be ignored -->
<!-- Output: a string representation of the fragment -->
<!-- This function is a proxy of serialize(), used to represent XML fragments in plain text, useful in validation reports or in generating guidelines -->
<!--kw: nodes, serialization, strings -->
<xsl:param name="fragment" as="item()*"/>
<xsl:param name="ignore-whitespace-text-nodes" as="xs:boolean"/>
<xsl:variable name="results" as="xs:string*">
<xsl:apply-templates select="$fragment" mode="tan:fragment-to-text">
<xsl:with-param name="ignore-whitespace-text-nodes"
select="$ignore-whitespace-text-nodes" tunnel="yes"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:value-of select="string-join($results, '')"/>
</xsl:function>
<xsl:mode name="tan:fragment-to-text" on-no-match="shallow-skip"/>
<xsl:template match="*" mode="tan:fragment-to-text">
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:choose>
<xsl:when test="exists(node())">
<xsl:text>></xsl:text>
<xsl:apply-templates select="node()" mode="#current"/>
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> /></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*" mode="tan:fragment-to-text">
<xsl:text> </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>='</xsl:text>
<xsl:value-of select="."/>
<xsl:text>'</xsl:text>
</xsl:template>
<xsl:template match="comment()" mode="tan:fragment-to-text">
<xsl:text><!--</xsl:text>
<xsl:value-of select="."/>
<xsl:text>--></xsl:text>
</xsl:template>
<xsl:template match="processing-instruction()" mode="tan:fragment-to-text">
<xsl:text><?</xsl:text>
<xsl:value-of select="."/>
<xsl:text>?></xsl:text>
</xsl:template>
<xsl:template match="text()" mode="tan:fragment-to-text">
<xsl:param name="ignore-whitespace-text-nodes" tunnel="yes" as="xs:boolean"/>
<xsl:if test="not($ignore-whitespace-text-nodes) or matches(., '\S')">
<xsl:value-of select="."/>
</xsl:if>
</xsl:template>
<xsl:function name="tan:trim-long-text" as="item()*" visibility="public">
<!-- Input: an XML fragment; an integer -->
<!-- Output: the fragment with text nodes longer than the integer value abbreviated with an ellipsis -->
<!--kw: nodes, tree manipulation -->
<xsl:param name="xml-fragment" as="item()*"/>
<xsl:param name="too-long" as="xs:integer"/>
<xsl:variable name="input-as-node" as="element()">
<node>
<xsl:copy-of select="$xml-fragment"/>
</node>
</xsl:variable>
<xsl:apply-templates select="$input-as-node/node()" mode="tan:trim-long-text">
<xsl:with-param name="too-long" select="$too-long" tunnel="yes"/>
</xsl:apply-templates>
</xsl:function>
<xsl:mode name="tan:trim-long-text" on-no-match="shallow-copy"/>
<xsl:template match="text()" mode="tan:trim-long-text">
<xsl:param name="too-long" as="xs:integer" tunnel="yes"/>
<xsl:variable name="this-length" select="string-length(.)"/>
<xsl:choose>
<xsl:when test="$this-length ge $too-long and $too-long ge 3">
<xsl:variable name="portion-length" select="($too-long - 1) idiv 2"/>
<xsl:value-of select="substring(., 1, $portion-length)"/>
<xsl:text>…</xsl:text>
<xsl:value-of select="substring(., ($this-length - $portion-length))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:function name="tan:copy-of-except" as="item()*" visibility="public">
<!-- short version of the full function, below -->
<xsl:param name="doc-fragment" as="item()*"/>
<xsl:param name="exclude-elements-named" as="xs:string*"/>
<xsl:param name="exclude-attributes-named" as="xs:string*"/>
<xsl:param name="exclude-elements-with-attributes-named" as="xs:string*"/>
<xsl:copy-of
select="tan:copy-of-except($doc-fragment, $exclude-elements-named, $exclude-attributes-named, $exclude-elements-with-attributes-named, (), ())"
/>
</xsl:function>
<xsl:function name="tan:copy-of-except" as="item()*" visibility="public">
<!-- Input: any document fragment; sequences of strings specifying names of elements to exclude, names of attributes to exclude, and names of attributes whose parent elements should be excluded; an integer beyond which depth copies should not be made -->
<!-- Output: the same fragment, altered -->
<!-- This function was written primarily to service the merge of TAN-A sources, where realigned divs could be extracted from their source documents -->
<!--kw: nodes, tree manipulation -->
<xsl:param name="doc-fragment" as="item()*"/>
<xsl:param name="exclude-elements-named" as="xs:string*"/>
<xsl:param name="exclude-attributes-named" as="xs:string*"/>
<xsl:param name="exclude-elements-with-attributes-named" as="xs:string*"/>
<xsl:param name="exclude-elements-beyond-what-depth" as="xs:integer?"/>
<xsl:param name="shallow-skip-elements-named" as="xs:string*"/>
<xsl:apply-templates select="$doc-fragment" mode="tan:copy-of-except">
<xsl:with-param name="exclude-elements-named" as="xs:string*"
select="$exclude-elements-named" tunnel="yes"/>
<xsl:with-param name="exclude-attributes-named" as="xs:string*"
select="$exclude-attributes-named" tunnel="yes"/>
<xsl:with-param name="exclude-elements-with-attributes-named" as="xs:string*"
select="$exclude-elements-with-attributes-named" tunnel="yes"/>
<xsl:with-param name="exclude-elements-beyond-what-depth"
select="$exclude-elements-beyond-what-depth" tunnel="yes"/>
<xsl:with-param name="current-depth" select="0"/>
<xsl:with-param name="shallow-skip-elements-named" select="$shallow-skip-elements-named"
tunnel="yes"/>
</xsl:apply-templates>
</xsl:function>
<xsl:mode name="tan:copy-of-except" on-no-match="shallow-copy"/>
<xsl:template match="*" mode="tan:copy-of-except">
<xsl:param name="exclude-elements-named" as="xs:string*" tunnel="yes"/>
<xsl:param name="exclude-elements-named-regex" as="xs:string?" tunnel="yes"/>
<xsl:param name="exclude-elements-not-named-regex" as="xs:string?" tunnel="yes"/>
<xsl:param name="exclude-attributes-named" as="xs:string*" tunnel="yes"/>
<xsl:param name="exclude-elements-with-attributes-named" as="xs:string*" tunnel="yes"/>
<xsl:param name="exclude-elements-beyond-what-depth" as="xs:integer?" tunnel="yes"/>
<xsl:param name="shallow-skip-elements-named" as="xs:string*" tunnel="yes"/>
<xsl:param name="current-depth" as="xs:integer?"/>
<xsl:variable name="this-local-name" select="local-name(.)"/>
<xsl:choose>
<xsl:when test="$this-local-name = $exclude-elements-named"/>
<xsl:when
test="string-length($exclude-elements-named-regex) gt 0 and matches($this-local-name, $exclude-elements-named-regex)"
/>
<xsl:when
test="string-length($exclude-elements-not-named-regex) gt 0 and not(matches($this-local-name, $exclude-elements-not-named-regex))"
/>
<xsl:when test="$this-local-name = $shallow-skip-elements-named">
<xsl:apply-templates mode="#current">
<xsl:with-param name="current-depth" select="
if (exists($current-depth)) then
$current-depth + 1
else
()"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="
not(some $i in @*
satisfies local-name($i) = $exclude-elements-with-attributes-named)
and not($current-depth ge $exclude-elements-beyond-what-depth)">
<xsl:copy>
<xsl:copy-of select="@*[not(local-name() = $exclude-attributes-named)]"/>
<xsl:apply-templates mode="#current">
<xsl:with-param name="current-depth" select="
if (exists($current-depth)) then
$current-depth + 1
else
()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:mode name="tan:strip-attributes" on-no-match="shallow-copy"/>
<xsl:template match="@*" mode="tan:strip-attributes"/>
<xsl:mode name="tan:strip-duplicate-children-by-attribute-value" on-no-match="shallow-copy"/>
<xsl:template match="*" mode="tan:strip-duplicate-children-by-attribute-value">
<xsl:param name="attribute-to-check" as="xs:string"/>
<xsl:param name="keep-last-duplicate" as="xs:boolean"/>
<!-- This template is used for merging sets of elements. -->
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each-group select="*" group-by="
if (exists(@*[name(.) = $attribute-to-check])) then
@*[name(.) = $attribute-to-check]
else
generate-id()">
<xsl:choose>
<xsl:when
test="(string-length(current-grouping-key()) gt 0) and (count(current-group()) gt 1)">
<xsl:copy-of select="
current-group()[if ($keep-last-duplicate) then
last()
else
1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:function name="tan:group-elements-by-shared-node-values" as="element()*" visibility="public">
<!-- One-parameter version of the fuller one below. -->
<xsl:param name="elements-to-group" as="element()*"/>
<xsl:copy-of select="tan:group-elements-by-shared-node-values($elements-to-group, ())"/>
</xsl:function>
<xsl:function name="tan:group-elements-by-IRI" as="element()*" visibility="private">
<!-- One-parameter version of the fuller one below. -->
<xsl:param name="elements-to-group" as="element()*"/>
<xsl:copy-of select="tan:group-elements-by-shared-node-values($elements-to-group, '^IRI$')"/>
</xsl:function>
<xsl:function name="tan:group-divs-by-ref" as="element()*" visibility="private">
<!-- One-parameter version of the fuller one below. -->
<xsl:param name="elements-to-group" as="element()*"/>
<xsl:copy-of select="tan:group-elements-by-shared-node-values($elements-to-group, '^ref$')"/>
</xsl:function>
<xsl:function name="tan:group-elements-by-shared-node-values" as="element()*" visibility="public">
<!-- Two-parameter version of the fuller one below -->
<xsl:param name="elements-to-group" as="element()*"/>
<xsl:param name="regex-of-names-of-nodes-to-group-by" as="xs:string?"/>
<xsl:copy-of select="tan:group-elements-by-shared-node-values($elements-to-group, $regex-of-names-of-nodes-to-group-by, false())"/>
</xsl:function>
<xsl:function name="tan:group-elements-by-shared-node-values" as="element()*" visibility="public">
<!-- Input: a sequence of elements; an optional string representing the name of children in the elements -->
<!-- Output: the same elements, but grouped in <group> according to whether the text contents of the child elements specified are equal -->
<!-- Each <group> will have an @n stipulating the position of the first element put in the group. That way the results can be sorted in
order of their original elements -->
<!-- Transitivity is assumed. Suppose elements X, Y, and Z have children values A and B; B and C; and C and D, respectively. All
three elements will be grouped, even though Y and Z do not directly share children values. -->
<!--kw: nodes, grouping -->
<xsl:param name="elements-to-group" as="element()*"/>
<xsl:param name="regex-of-names-of-nodes-to-group-by" as="xs:string?"/>
<xsl:param name="group-by-shallow-node-value" as="xs:boolean"/>
<xsl:variable name="group-by-all-children" as="xs:boolean"
select="string-length($regex-of-names-of-nodes-to-group-by) lt 1 or $regex-of-names-of-nodes-to-group-by = '*'
or not(tan:regex-is-valid($regex-of-names-of-nodes-to-group-by))"
/>
<xsl:variable name="elements-prepped-pass-1" as="element()*">
<xsl:for-each select="$elements-to-group">
<xsl:variable name="these-grouping-key-nodes" select="descendant::node()[matches(name(.), $regex-of-names-of-nodes-to-group-by)]"/>
<item n="{position()}">
<xsl:choose>
<xsl:when test="$group-by-all-children">
<xsl:apply-templates select="node()" mode="tan:build-grouping-key">
<xsl:with-param name="group-by-shallow-node-value"
select="$group-by-shallow-node-value"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="not(exists($these-grouping-key-nodes))">
<grouping-key/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates
select="$these-grouping-key-nodes"
mode="tan:build-grouping-key">
<xsl:with-param name="group-by-shallow-node-value"
select="$group-by-shallow-node-value"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
<xsl:copy-of select="."/>
</item>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="duplicate-grouping-keys" select="tan:duplicate-items($elements-prepped-pass-1/tan:grouping-key)"/>
<xsl:variable name="elements-prepped-pass-2" as="element()*">
<xsl:for-each select="$elements-prepped-pass-1">
<xsl:choose>
<xsl:when test="tan:grouping-key = $duplicate-grouping-keys">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:otherwise>
<group>
<xsl:copy-of select="@n"/>
<xsl:copy-of select="*"/>
</group>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="items-with-duplicatative-keys-grouped" select="tan:group-elements-by-shared-node-values-loop($elements-prepped-pass-2/self::tan:item, (), 0)"/>
<xsl:variable name="diagnostics-on" select="false()"/>
<xsl:if test="$diagnostics-on">
<xsl:message select="'diagnostics on for tan:group-elements-by-shared-node-values()'"/>
<xsl:message select="'elements to group: ', $elements-to-group"/>
<xsl:message select="'name of node to group by (regular expression): ', $regex-of-names-of-nodes-to-group-by"/>
<xsl:message select="'group by the shallow value of the node?', $group-by-shallow-node-value"/>
<xsl:message select="'group by all children?', $group-by-all-children"/>
<xsl:message select="'pass 1: ', $elements-prepped-pass-1"/>
<xsl:message select="'duplicate grouping keys: ', $duplicate-grouping-keys"/>
<xsl:message select="'pass 2 (pregrouped items that have unique keys): ', $elements-prepped-pass-2"/>
<xsl:message select="'pass 3 (items with duplicative keys grouped): ', $items-with-duplicatative-keys-grouped"/>
</xsl:if>
<xsl:for-each select="$elements-prepped-pass-2/self::tan:group, $items-with-duplicatative-keys-grouped">
<xsl:sort select="number(@n)"/>
<xsl:copy>
<xsl:copy-of select="@n"/>
<xsl:copy-of select="* except tan:grouping-key"/>
</xsl:copy>
</xsl:for-each>
</xsl:function>
<xsl:mode name="tan:build-grouping-key" on-no-match="shallow-copy"/>
<xsl:template match="text()" mode="tan:build-grouping-key">
<grouping-key>
<xsl:value-of select="."/>
</grouping-key>
</xsl:template>
<xsl:template match="*" mode="tan:build-grouping-key">
<xsl:param name="group-by-shallow-node-value" as="xs:boolean?"/>
<xsl:choose>
<xsl:when test="$group-by-shallow-node-value">
<xsl:apply-templates select="text()" mode="#current"/>
</xsl:when>
<xsl:otherwise>
<grouping-key>
<xsl:value-of select="."/>
</grouping-key>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:function name="tan:group-elements-by-shared-node-values-loop" as="element()*" visibility="private">
<!-- supporting loop for the function above -->
<xsl:param name="items-to-group" as="element()*"/>
<xsl:param name="groups-so-far" as="element()*"/>
<xsl:param name="loop-count" as="xs:integer"/>
<xsl:choose>
<xsl:when test="count($items-to-group) lt 1">
<xsl:copy-of select="$groups-so-far"/>
</xsl:when>
<xsl:when test="$loop-count gt $tan:loop-tolerance">
<xsl:message select="'loop exceeds tolerance'"/>
<xsl:copy-of select="$groups-so-far"/>
<xsl:for-each select="$items-to-group">
<group>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="*"/>
</group>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="next-item" select="$items-to-group[1]"/>
<xsl:variable name="related-items" select="$items-to-group[tan:grouping-key = $next-item/tan:grouping-key]"/>
<xsl:variable name="groups-that-match" select="$groups-so-far[tan:grouping-key = $related-items/tan:grouping-key]"/>
<xsl:variable name="new-group" as="element()">
<group>
<xsl:copy-of select="($groups-that-match/@n, $related-items/@n)[1]"/>
<xsl:copy-of select="$groups-that-match/*, $related-items/*"/>
</group>
</xsl:variable>
<xsl:copy-of
select="tan:group-elements-by-shared-node-values-loop(($items-to-group except $related-items), (($groups-so-far except $groups-that-match), $new-group), $loop-count +1)"
/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="tan:element-fingerprint" as="xs:string*" visibility="public">
<!-- Input: any elements -->
<!-- Output: for each element the string value of its name, its namespace, its attributes, and all descendant nodes -->
<!-- This function is useful for determining whether any number of elements are deeply equal -->
<!-- The built-in function deep-equal() works for pairs of elements; this looks for a way to evaluate sequences of elements -->
<!--kw: nodes, identifiers -->
<xsl:param name="element" as="element()*"/>
<xsl:for-each select="$element">
<xsl:variable name="results" as="xs:string*">
<xsl:apply-templates select="$element" mode="tan:element-fingerprint"/>
</xsl:variable>
<xsl:value-of select="string-join($results, '')"/>
</xsl:for-each>
</xsl:function>
<xsl:mode name="tan:element-fingerprint" on-no-match="shallow-copy"/>
<xsl:template match="*" mode="tan:element-fingerprint">
<xsl:text>e#</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>ns#</xsl:text>
<xsl:value-of select="namespace-uri()"/>
<xsl:text>aa#</xsl:text>
<xsl:for-each select="@*">
<xsl:sort select="name()"/>
<xsl:text>a#</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>#</xsl:text>
<xsl:value-of select="normalize-space(.)"/>
<xsl:text>#</xsl:text>
</xsl:for-each>
<xsl:apply-templates select="node()" mode="#current"/>
</xsl:template>
<!-- We presume (perhaps wrongly) that comments and pi's in an element don't matter -->
<xsl:template match="comment() | processing-instruction()" mode="tan:element-fingerprint"/>
<xsl:template match="text()" mode="tan:element-fingerprint">
<xsl:if test="matches(., '\S')">
<xsl:text>t#</xsl:text>
<xsl:value-of select="normalize-space(.)"/>
<xsl:text>#</xsl:text>
</xsl:if>
</xsl:template>
<xsl:function name="tan:stamp-q-id" as="item()*" visibility="public">
<!-- 1-param version of the full one below -->
<xsl:param name="items-to-stamp" as="item()*"/>
<xsl:copy-of select="tan:stamp-q-id($items-to-stamp, false())"/>
</xsl:function>
<xsl:function name="tan:stamp-q-id" as="item()*" visibility="public">
<!-- Input: any XML fragments -->
<!-- Output: the fragments with @q added to each element via generate-id() -->
<!--kw: nodes, identifiers -->
<xsl:param name="items-to-stamp" as="item()*"/>
<xsl:param name="stamp-shallowly" as="xs:boolean"/>
<xsl:apply-templates select="$items-to-stamp" mode="tan:stamp-q-id">
<xsl:with-param name="stamp-shallowly" select="$stamp-shallowly" tunnel="yes"/>
</xsl:apply-templates>
</xsl:function>
<xsl:mode on-no-match="shallow-copy" name="tan:stamp-q-id"/>
<xsl:template match="*" mode="tan:stamp-q-id">
<xsl:param name="stamp-shallowly" as="xs:boolean" tunnel="yes" select="false()"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="q" select="generate-id(.)"/>
<xsl:choose>
<xsl:when test="$stamp-shallowly">
<xsl:copy-of select="node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates mode="#current"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
<xsl:function name="tan:indent-value" as="xs:integer*" visibility="private">
<!-- Input: elements -->
<!-- Output: the length of their indentation -->
<xsl:param name="elements" as="element()*"/>
<xsl:variable name="ancestor-preceding-text-nodes" as="xs:string*">
<xsl:for-each select="$elements">
<xsl:variable name="this-preceding-white-space-text-node"
select="preceding-sibling::node()[1]/self::text()[not(matches(., '\S'))]"/>
<xsl:choose>
<xsl:when test="string-length($this-preceding-white-space-text-node) gt 0">
<!-- strip away the line feed and anything preceding it -->
<xsl:value-of select="replace($this-preceding-white-space-text-node, '.*\n', '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="
for $i in $ancestor-preceding-text-nodes
return
string-length($i)"/>
</xsl:function>
<xsl:function name="tan:copy-indentation" as="item()*" visibility="public">
<!-- 2-parameter version of fuller one below -->
<xsl:param name="items-to-indent" as="item()*"/>
<xsl:param name="model-element" as="element()"/>
<xsl:sequence select="tan:copy-indentation($items-to-indent, $model-element, 'short')"/>
</xsl:function>
<xsl:function name="tan:copy-indentation" as="item()*" visibility="public">
<!-- Input: items that should be indented; an element whose indentation should be imitated; a string: 'full', 'short', or 'none' -->
<!-- Output: the items, indented according to the pattern -->
<!-- If the third parameter is 'full', the last indentation after the series will be like the first; if it is 'short', it will
be one indentation less than full (appropriate for the last child of a wrapping element); if it is 'none' no final indentation
will be supplied. This parameter affects only the topmost sequence, not the children, which are formatted as demanded. -->
<!--kw: nodes, tree manipulation, spacing-->
<xsl:param name="items-to-indent" as="item()*"/>
<xsl:param name="model-element" as="element()"/>
<xsl:param name="tail-indentation-type" as="xs:string?"/>
<!-- short tail indentation is the default -->
<xsl:variable name="tail-type-norm" select="($tail-indentation-type[. = ('full', 'none')], 'short')[1]"/>
<xsl:variable name="model-ancestors" select="$model-element/ancestor-or-self::*"/>
<xsl:variable name="inherited-indentation-quantities" select="tan:indent-value($model-ancestors)"/>
<xsl:variable name="this-default-indentation" select="
if (count($model-ancestors) gt 1) then
ceiling($inherited-indentation-quantities[last()] idiv (count($model-ancestors) - 1))
else
$tan:default-indent-value"/>
<xsl:apply-templates select="$items-to-indent[not(position() eq last())]" mode="tan:indent-items">
<xsl:with-param name="current-context-average-indentation" select="$inherited-indentation-quantities[last()]"/>
<xsl:with-param name="default-indentation-increase" select="$this-default-indentation" tunnel="yes"/>
<xsl:with-param name="tail-indentation-type" select="'none'"/>
</xsl:apply-templates>
<xsl:apply-templates select="$items-to-indent[last()]" mode="tan:indent-items">
<xsl:with-param name="current-context-average-indentation" select="$inherited-indentation-quantities[last()]"/>
<xsl:with-param name="default-indentation-increase" select="$this-default-indentation" tunnel="yes"/>
<xsl:with-param name="tail-indentation-type" select="$tail-type-norm"/>
</xsl:apply-templates>
</xsl:function>
<xsl:mode name="tan:indent-items" on-no-match="shallow-copy"/>
<xsl:template match="*" mode="tan:indent-items">
<xsl:param name="current-context-average-indentation" as="xs:integer"/>
<xsl:param name="default-indentation-increase" as="xs:integer" tunnel="yes"/>
<xsl:param name="tail-indentation-type" as="xs:string" select="'short'"/>
<xsl:variable name="has-mixed-content" select="exists(*) and exists(text()[matches(., '\S')])"/>
<xsl:value-of select="'
' || tan:fill(' ', $current-context-average-indentation)"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:choose>
<xsl:when test="$has-mixed-content">
<xsl:copy-of select="node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates mode="#current">
<xsl:with-param name="current-context-average-indentation"
select="$current-context-average-indentation + $default-indentation-increase"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
<xsl:if test="not(exists(following-sibling::*))">
<xsl:choose>
<xsl:when test="$tail-indentation-type eq 'none'"/>
<xsl:when test="$tail-indentation-type eq 'full'">
<xsl:value-of select="'
' || tan:fill(' ', ($current-context-average-indentation))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'
' || tan:fill(' ', ($current-context-average-indentation - $default-indentation-increase))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template match="text()" mode="tan:indent-items">
<xsl:if test="matches(., '\S')">
<xsl:value-of select="."/>
</xsl:if>
</xsl:template>
<xsl:function name="tan:attr" as="attribute()?" visibility="public">
<!-- Input: two strings -->
<!-- Output: an attribute by the name of the first string, with the value of the second -->
<!--kw: nodes, attributes -->
<xsl:param name="attribute-name" as="xs:string?"/>
<xsl:param name="attribute-value" as="xs:string?"/>
<xsl:choose>
<xsl:when test="$attribute-name castable as xs:NCName">
<xsl:attribute name="{$attribute-name}" select="$attribute-value"/>
</xsl:when>
<xsl:otherwise>
<xsl:message select="$attribute-name, ' is not a legal attribute name'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="tan:path" as="xs:string*" visibility="public">
<!-- Input: any nodes -->
<!-- Output: the path of each node -->
<!--kw: nodes -->
<xsl:param name="nodes" as="node()*"/>
<xsl:for-each select="$nodes">
<xsl:variable name="this-node" select="."/>
<xsl:variable name="these-steps" as="xs:string*">
<xsl:for-each select="ancestor-or-self::node()">
<xsl:variable name="this-step" select="."/>
<xsl:variable name="this-name" select="name(.)"/>
<xsl:variable name="preceding-siblings-with-same-name"
select="preceding-sibling::node()[name() = $this-name]"/>
<xsl:variable name="this-filter" as="xs:string?">
<xsl:if test="string-length($this-name) gt 0">
<xsl:value-of
select="'[' || string(count($preceding-siblings-with-same-name) + 1) || ']'"
/>
</xsl:if>
</xsl:variable>
<xsl:value-of select="$this-name || $this-filter"/>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="string-join($these-steps, '/')"/>
</xsl:for-each>
</xsl:function>
<!-- TAN-specific -->
<xsl:function name="tan:tan-type" as="xs:string*" visibility="private">
<!-- Input: any nodes -->
<!-- Output: the names of the root elements; if not present, a zero-length string is returned -->
<xsl:param name="nodes" as="node()*"/>
<xsl:for-each select="$nodes/root()/*">
<xsl:value-of select="local-name(.)"/>
</xsl:for-each>
</xsl:function>
<xsl:function name="tan:class-number" as="xs:integer*" visibility="private">
<!-- Input: any nodes of a TAN document -->
<!-- Output: one integer per node, specifying the TAN class for the file, based on the name of the root element. If no match is found in the root element, 0 is returned -->
<xsl:param name="nodes" as="node()*"/>
<xsl:for-each select="$nodes">
<xsl:variable name="this-root-name" select="tan:tan-type(.)"/>
<xsl:variable name="this-class" as="xs:integer?"
select="xs:integer($tan:tan-classes/tan:class[tan:root = $this-root-name]/@n)"/>
<xsl:sequence select="
if (exists($this-class)) then
$this-class
else
0"/>
</xsl:for-each>
</xsl:function>
<xsl:function name="tan:must-refer-to-external-tan-file" as="xs:boolean" visibility="private">
<!-- Input: node in a TAN document. -->
<!-- Output: boolean value indicating whether the node or its parent must name or refer to a TAN file. -->
<xsl:param name="node" as="node()"/>
<xsl:variable name="class-2-elements-that-must-always-refer-to-tan-files" select="('source')"/>
<xsl:variable name="this-class" select="tan:class-number($node)"/>
<xsl:value-of select="
if (
((name($node),
name($node/parent::node())) = $tan:names-of-elements-that-must-always-refer-to-tan-files)
or ((((name($node),
name($node/parent::node())) = $class-2-elements-that-must-always-refer-to-tan-files)
)
and $this-class = 2)
)
then
true()
else
false()"/>
</xsl:function>
<xsl:function name="tan:doc-id-namespace" as="xs:string?" visibility="private">
<!-- Input: an item from a TAN file -->
<!-- Output: the namespace of the doc's @id -->
<xsl:param name="TAN-doc" as="item()?"/>
<xsl:variable name="this-id" select="root($TAN-doc)/*/@id"/>
<xsl:if test="string-length($this-id) gt 0">
<xsl:analyze-string select="$this-id" regex="^tag:[^:]+">
<xsl:matching-substring>
<xsl:value-of select="."/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:if>
</xsl:function>
<xsl:function name="tan:last-change-agent" as="element()*" visibility="public">
<!-- Input: any TAN document -->
<!-- Output: the <person>, <organization>, or <algorithm> who made the last change -->
<!--kw: nodes-->
<xsl:param name="TAN-doc" as="document-node()*"/>
<xsl:for-each select="$TAN-doc">
<xsl:variable name="this-doc" select="."/>
<xsl:variable name="this-doc-history" select="tan:get-doc-history(.)"/>
<xsl:variable name="this-doc-head" select="$this-doc/*/tan:head"/>
<xsl:variable name="last-change" select="$this-doc-history/*[@who][1]"/>
<xsl:if test="exists($this-doc-head)">
<xsl:copy-of
select="tan:vocabulary(('person', 'organization', 'algorithm'), $last-change/@who, $this-doc/*/tan:head)/*"
/>
</xsl:if>
</xsl:for-each>
</xsl:function>
<xsl:function name="tan:all-conditions-hold" as="xs:boolean?" visibility="private">
<!-- 2-param version of the master one, below -->
<xsl:param name="element-with-condition-attributes" as="element()?"/>
<xsl:param name="context-to-evaluate-against" as="item()*"/>
<xsl:copy-of
select="tan:all-conditions-hold($element-with-condition-attributes, $context-to-evaluate-against, (), true())"
/>
</xsl:function>
<xsl:function name="tan:all-conditions-hold" as="xs:boolean" visibility="private">
<!-- Input: a TAN element with attributes that should be checked for their truth value; a context against which to check the values; an optional sequence of strings indicates the names of elements that should be processed and in what order; a boolean indicating what value to return by default -->
<!-- Output: true, if every condition holds; false otherwise -->
<!-- If no conditions are found, the output reverts to the default -->
<xsl:param name="element-with-condition-attributes" as="element()?"/>
<xsl:param name="context-to-evaluate-against" as="item()*"/>
<xsl:param name="evaluation-sequence" as="xs:string*"/>
<xsl:param name="default-value" as="xs:boolean"/>
<xsl:variable name="element-with-condition-attributes-sorted-and-distributed" as="element()*">
<xsl:for-each select="$element-with-condition-attributes/@*">
<xsl:sort select="(index-of($evaluation-sequence, name(.)), 999)[1]"/>
<where>
<xsl:copy-of select="."/>
</where>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="loop-results" as="xs:boolean"
select="tan:all-conditions-hold-evaluation-loop($element-with-condition-attributes-sorted-and-distributed, $context-to-evaluate-against, $default-value)"
/>
<xsl:variable name="diagnostics-on" select="false()"/>
<xsl:if test="$diagnostics-on">
<xsl:message select="'diagnostics on for tan:conditions-hold()'"/>
<xsl:message select="'element with condition attributes: ', $element-with-condition-attributes"/>
<xsl:message select="'context to evaluate against: ', $context-to-evaluate-against"/>
<xsl:message select="'evaluation sequence: ', $evaluation-sequence"/>
<xsl:message select="'conditions sorted and distributed: ', $element-with-condition-attributes-sorted-and-distributed"/>
<xsl:message select="'loop results: ', $loop-results"/>
</xsl:if>
<xsl:value-of select="$loop-results"/>
</xsl:function>
<xsl:function name="tan:all-conditions-hold-evaluation-loop" as="xs:boolean" visibility="private">
<!-- Companion function to the one above, indicating whether every condition holds -->
<!-- This loop function iterates through elements with condition attributes and checks each against the context; if a false is found, the loop ends returning false; if no conditions are found the default value is returned; otherwise it returns true -->
<!-- We use a loop function to avoid evaluating conditions that might be time-consuming -->
<xsl:param name="elements-with-condition-attributes-to-be-evaluated" as="element()*"/>
<xsl:param name="context-to-evaluate-against" as="item()*"/>
<xsl:param name="current-value" as="xs:boolean"/>
<xsl:choose>
<xsl:when test="not(exists($elements-with-condition-attributes-to-be-evaluated))">
<xsl:value-of select="$current-value"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="next-element-to-evaluate"
select="$elements-with-condition-attributes-to-be-evaluated[1]"/>
<xsl:variable name="this-analysis" as="element()">
<xsl:apply-templates select="$next-element-to-evaluate" mode="tan:evaluate-conditions">
<xsl:with-param name="context" select="$context-to-evaluate-against" tunnel="yes"
/>
</xsl:apply-templates>
</xsl:variable>
<xsl:choose>
<xsl:when test="$this-analysis/@* = false()">
<xsl:copy-of select="false()"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of
select="tan:all-conditions-hold-evaluation-loop($elements-with-condition-attributes-to-be-evaluated[position() gt 1], $context-to-evaluate-against, true())"
/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:mode name="tan:evaluate-conditions" on-no-match="shallow-skip"/>
<xsl:template match="*" mode="tan:evaluate-conditions">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
</xsl:copy>
</xsl:template>
<!-- If an unknown attribute is encountered, skip it -->
<xsl:template match="@*" mode="tan:evaluate-conditions"/>
<!-- Otherwise execute the following. -->
<!-- TAN-A-lm -->
<xsl:template match="@m-matches" mode="tan:evaluate-conditions">
<xsl:param name="context" tunnel="yes"/>
<xsl:attribute name="{name()}">
<xsl:value-of select="matches($context/text()[1], .)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@m-has-how-many-features | @m-has-how-many-codes" mode="tan:evaluate-conditions">
<!-- @m-has-how-many-features was renamed @m-has-how-many-codes in 2021; the older name is
retained for legacy -->
<xsl:param name="context" tunnel="yes"/>
<xsl:variable name="this-val" select="tan:expand-numerical-expression(., 999)"/>
<xsl:attribute name="{name()}">
<xsl:value-of select="count($context/tan:f[text()]) = $this-val"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@m-has-features | @m-has-codes" mode="tan:evaluate-conditions">
<!-- @m-has-features was renamed @m-has-codes in 2021; the older name is retained for legacy -->
<xsl:param name="context" tunnel="yes"/>
<xsl:variable name="these-conditions" as="element()*">
<xsl:analyze-string select="." regex="(\+ )?\S+">
<xsl:matching-substring>
<xsl:variable name="this-item" as="xs:string+" select="tokenize(., ' ')"/>
<xsl:element name="{if (count($this-item) gt 1) then 'and' else 'feature'}">
<xsl:value-of select="$this-item[last()]"/>
</xsl:element>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:variable name="these-conditions-pass-2" as="element()*">
<xsl:for-each-group select="$these-conditions" group-starting-with="tan:feature">
<group>
<xsl:for-each select="current-group()">
<feature>
<xsl:value-of select="."/>
</feature>
</xsl:for-each>
</group>
</xsl:for-each-group>
</xsl:variable>
<xsl:variable name="diagnostics-on" as="xs:boolean" select="false()"/>
<xsl:if test="$diagnostics-on">
<xsl:message select="'= = Diagnostics on template mode tan:evaluate-conditions on @' || name(.) || ' with value ' || string(.)"/>
<xsl:message select="'Context: ', $context"/>
<xsl:message select="'Conditions pass 1:', $these-conditions"/>
<xsl:message select="'Conditions pass 2:', $these-conditions-pass-2"/>
</xsl:if>
<xsl:attribute name="{name(.)}">
<xsl:value-of select="
some $i in $these-conditions-pass-2
satisfies
every $j in $i/tan:feature
satisfies
$context/tan:f = $j"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@tok-matches" mode="tan:evaluate-conditions">
<xsl:param name="context" tunnel="yes"/>
<xsl:variable name="this-val" select="."/>
<xsl:attribute name="{name()}">
<xsl:value-of select="
some $i in $context/ancestor::tan:ana//tan:tok/tan:result
satisfies tan:matches($i, tan:escape($this-val))"/>
</xsl:attribute>
</xsl:template>
<xsl:function name="tan:data-type-check" as="xs:boolean" visibility="public">
<!-- Input: an item and a string naming a data type -->
<!-- Output: a boolean indicating whether the item can be cast into that data type -->
<!-- If the first parameter doesn't match a data type, the function returns false -->
<!--kw: nodes, datatypes -->
<xsl:param name="item" as="item()?"/>
<xsl:param name="data-type" as="xs:string"/>
<xsl:choose>
<xsl:when test="$data-type eq 'string'">
<xsl:sequence select="$item castable as xs:string"/>
</xsl:when>
<xsl:when test="$data-type eq 'boolean'">
<xsl:sequence select="$item castable as xs:boolean"/>
</xsl:when>
<xsl:when test="$data-type eq 'decimal'">
<xsl:sequence select="$item castable as xs:decimal"/>
</xsl:when>
<xsl:when test="$data-type eq 'float'">
<xsl:sequence select="$item castable as xs:float"/>
</xsl:when>
<xsl:when test="$data-type eq 'double'">
<xsl:sequence select="$item castable as xs:double"/>
</xsl:when>
<xsl:when test="$data-type eq 'duration'">
<xsl:sequence select="$item castable as xs:duration"/>
</xsl:when>
<xsl:when test="$data-type eq 'dateTime'">
<xsl:sequence select="$item castable as xs:dateTime"/>
</xsl:when>
<xsl:when test="$data-type eq 'time'">
<xsl:sequence select="$item castable as xs:time"/>
</xsl:when>
<xsl:when test="$data-type eq 'date'">
<xsl:sequence select="$item castable as xs:date"/>
</xsl:when>
<xsl:when test="$data-type eq 'gYearMonth'">
<xsl:sequence select="$item castable as xs:gYearMonth"/>
</xsl:when>
<xsl:when test="$data-type eq 'gYear'">
<xsl:sequence select="$item castable as xs:gYear"/>
</xsl:when>
<xsl:when test="$data-type eq 'gMonthDay'">
<xsl:sequence select="$item castable as xs:gMonthDay"/>
</xsl:when>
<xsl:when test="$data-type eq 'gDay'">
<xsl:sequence select="$item castable as xs:gDay"/>
</xsl:when>
<xsl:when test="$data-type eq 'gMonth'">
<xsl:sequence select="$item castable as xs:gMonth"/>
</xsl:when>
<xsl:when test="$data-type eq 'hexBinary'">
<xsl:sequence select="$item castable as xs:hexBinary"/>
</xsl:when>
<xsl:when test="$data-type eq 'base64Binary'">
<xsl:sequence select="$item castable as xs:base64Binary"/>
</xsl:when>
<xsl:when test="$data-type eq 'anyURI'">
<xsl:sequence select="$item castable as xs:anyURI"/>
</xsl:when>
<xsl:when test="$data-type eq 'QName'">
<xsl:sequence select="$item castable as xs:QName"/>
</xsl:when>
<xsl:when test="$data-type eq 'normalizedString'">
<xsl:sequence select="$item castable as xs:normalizedString"/>
</xsl:when>
<xsl:when test="$data-type eq 'token'">
<xsl:sequence select="$item castable as xs:token"/>
</xsl:when>
<xsl:when test="$data-type eq 'language'">
<xsl:sequence select="$item castable as xs:language"/>
</xsl:when>
<xsl:when test="$data-type eq 'NMTOKEN'">
<xsl:sequence select="$item castable as xs:NMTOKEN"/>
</xsl:when>
<xsl:when test="$data-type eq 'NMTOKENS'">
<xsl:sequence select="$item castable as xs:NMTOKENS"/>
</xsl:when>
<xsl:when test="$data-type eq 'Name'">
<xsl:sequence select="$item castable as xs:Name"/>
</xsl:when>
<xsl:when test="$data-type eq 'NCName'">
<xsl:sequence select="$item castable as xs:NCName"/>
</xsl:when>
<xsl:when test="$data-type eq 'ID'">
<xsl:sequence select="$item castable as xs:ID"/>
</xsl:when>
<xsl:when test="$data-type eq 'IDREF'">
<xsl:sequence select="$item castable as xs:IDREF"/>
</xsl:when>
<xsl:when test="$data-type eq 'IDREFS'">
<xsl:sequence select="$item castable as xs:IDREFS"/>
</xsl:when>
<xsl:when test="$data-type eq 'ENTITY'">
<xsl:sequence select="$item castable as xs:ENTITY"/>
</xsl:when>
<xsl:when test="$data-type eq 'ENTITIES'">
<xsl:sequence select="$item castable as xs:ENTITIES"/>
</xsl:when>