-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfunctx.xquery
1410 lines (1099 loc) · 39.8 KB
/
functx.xquery
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
(:~
: --------------------------------
: The FunctX XQuery Function Library
: --------------------------------
: Copyright (C) 2007 Datypic
: This library is free software; you can redistribute it and/or
: modify it under the terms of the GNU Lesser General Public
: License as published by the Free Software Foundation; either
: version 2.1 of the License.
: This library is distributed in the hope that it will be useful,
: but WITHOUT ANY WARRANTY; without even the implied warranty of
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
: Lesser General Public License for more details.
: You should have received a copy of the GNU Lesser General Public
: License along with this library; if not, write to the Free Software
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
: For more information on the FunctX XQuery library, contact [email protected].
: @version 1.0
: @see http://www.xqueryfunctions.com
:)
module namespace functx = "http://www.functx.com" ;
declare function functx:add-attributes
( $elements as element()* ,
$attrNames as xs:QName* ,
$attrValues as xs:anyAtomicType* ) as element()? {
for $element in $elements
return element { node-name($element)}
{ for $attrName at $seq in $attrNames
return if ($element/@*[node-name(.) = $attrName])
then ()
else attribute {$attrName}
{$attrValues[$seq]},
$element/@*,
$element/node() }
} ;
declare function functx:add-months
( $date as xs:anyAtomicType? ,
$months as xs:integer ) as xs:date? {
xs:date($date) + functx:yearMonthDuration(0,$months)
} ;
declare function functx:add-or-update-attributes
( $elements as element()* ,
$attrNames as xs:QName* ,
$attrValues as xs:anyAtomicType* ) as element()? {
for $element in $elements
return element { node-name($element)}
{ for $attrName at $seq in $attrNames
return attribute {$attrName}
{$attrValues[$seq]},
$element/@*[not(node-name(.) = $attrNames)],
$element/node() }
} ;
declare function functx:all-whitespace
( $arg as xs:string? ) as xs:boolean {
normalize-space($arg) = ''
} ;
declare function functx:are-distinct-values
( $seq as xs:anyAtomicType* ) as xs:boolean {
count(distinct-values($seq)) = count($seq)
} ;
declare function functx:atomic-type
( $values as xs:anyAtomicType* ) as xs:string* {
for $val in $values
return
(if ($val instance of xs:untypedAtomic) then 'xs:untypedAtomic'
else if ($val instance of xs:anyURI) then 'xs:anyURI'
else if ($val instance of xs:ENTITY) then 'xs:ENTITY'
else if ($val instance of xs:ID) then 'xs:ID'
else if ($val instance of xs:NMTOKEN) then 'xs:NMTOKEN'
else if ($val instance of xs:language) then 'xs:language'
else if ($val instance of xs:NCName) then 'xs:NCName'
else if ($val instance of xs:Name) then 'xs:Name'
else if ($val instance of xs:token) then 'xs:token'
else if ($val instance of xs:normalizedString)
then 'xs:normalizedString'
else if ($val instance of xs:string) then 'xs:string'
else if ($val instance of xs:QName) then 'xs:QName'
else if ($val instance of xs:boolean) then 'xs:boolean'
else if ($val instance of xs:base64Binary) then 'xs:base64Binary'
else if ($val instance of xs:hexBinary) then 'xs:hexBinary'
else if ($val instance of xs:byte) then 'xs:byte'
else if ($val instance of xs:short) then 'xs:short'
else if ($val instance of xs:int) then 'xs:int'
else if ($val instance of xs:long) then 'xs:long'
else if ($val instance of xs:unsignedByte) then 'xs:unsignedByte'
else if ($val instance of xs:unsignedShort) then 'xs:unsignedShort'
else if ($val instance of xs:unsignedInt) then 'xs:unsignedInt'
else if ($val instance of xs:unsignedLong) then 'xs:unsignedLong'
else if ($val instance of xs:positiveInteger)
then 'xs:positiveInteger'
else if ($val instance of xs:nonNegativeInteger)
then 'xs:nonNegativeInteger'
else if ($val instance of xs:negativeInteger)
then 'xs:negativeInteger'
else if ($val instance of xs:nonPositiveInteger)
then 'xs:nonPositiveInteger'
else if ($val instance of xs:integer) then 'xs:integer'
else if ($val instance of xs:decimal) then 'xs:decimal'
else if ($val instance of xs:float) then 'xs:float'
else if ($val instance of xs:double) then 'xs:double'
else if ($val instance of xs:date) then 'xs:date'
else if ($val instance of xs:time) then 'xs:time'
else if ($val instance of xs:dateTime) then 'xs:dateTime'
else if ($val instance of xs:dayTimeDuration)
then 'xs:dayTimeDuration'
else if ($val instance of xs:yearMonthDuration)
then 'xs:yearMonthDuration'
else if ($val instance of xs:duration) then 'xs:duration'
else if ($val instance of xs:gMonth) then 'xs:gMonth'
else if ($val instance of xs:gYear) then 'xs:gYear'
else if ($val instance of xs:gYearMonth) then 'xs:gYearMonth'
else if ($val instance of xs:gDay) then 'xs:gDay'
else if ($val instance of xs:gMonthDay) then 'xs:gMonthDay'
else 'unknown')
} ;
declare function functx:avg-empty-is-zero
( $values as xs:anyAtomicType* ,
$allNodes as node()* ) as xs:double {
if (empty($allNodes))
then 0
else sum($values[string(.) != '']) div count($allNodes)
} ;
declare function functx:between-exclusive
( $value as xs:anyAtomicType? ,
$minValue as xs:anyAtomicType ,
$maxValue as xs:anyAtomicType ) as xs:boolean {
$value > $minValue and $value < $maxValue
} ;
declare function functx:between-inclusive
( $value as xs:anyAtomicType? ,
$minValue as xs:anyAtomicType ,
$maxValue as xs:anyAtomicType ) as xs:boolean {
$value >= $minValue and $value <= $maxValue
} ;
declare function functx:camel-case-to-words
( $arg as xs:string? ,
$delim as xs:string ) as xs:string {
concat(substring($arg,1,1),
replace(substring($arg,2),'(\p{Lu})',
concat($delim, '$1')))
} ;
declare function functx:capitalize-first
( $arg as xs:string? ) as xs:string? {
concat(upper-case(substring($arg,1,1)),
substring($arg,2))
} ;
declare function functx:change-element-names-deep
( $nodes as node()* ,
$oldNames as xs:QName* ,
$newNames as xs:QName* ) as node()* {
if (count($oldNames) != count($newNames))
then error(xs:QName('functx:Different_number_of_names'))
else
for $node in $nodes
return if ($node instance of element())
then element
{functx:if-empty
($newNames[index-of($oldNames,
node-name($node))],
node-name($node)) }
{$node/@*,
functx:change-element-names-deep($node/node(),
$oldNames, $newNames)}
else if ($node instance of document-node())
then functx:change-element-names-deep($node/node(),
$oldNames, $newNames)
else $node
} ;
declare function functx:change-element-ns-deep
( $nodes as node()* ,
$newns as xs:string ,
$prefix as xs:string ) as node()* {
for $node in $nodes
return if ($node instance of element())
then (element
{QName ($newns,
concat($prefix,
if ($prefix = '')
then ''
else ':',
local-name($node)))}
{$node/@*,
functx:change-element-ns-deep($node/node(),
$newns, $prefix)})
else if ($node instance of document-node())
then functx:change-element-ns-deep($node/node(),
$newns, $prefix)
else $node
} ;
declare function functx:change-element-ns
( $elements as element()* ,
$newns as xs:string ,
$prefix as xs:string ) as element()? {
for $element in $elements
return
element {QName ($newns,
concat($prefix,
if ($prefix = '')
then ''
else ':',
local-name($element)))}
{$element/@*, $element/node()}
} ;
declare function functx:chars
( $arg as xs:string? ) as xs:string* {
for $ch in string-to-codepoints($arg)
return codepoints-to-string($ch)
} ;
declare function functx:contains-any-of
( $arg as xs:string? ,
$searchStrings as xs:string* ) as xs:boolean {
some $searchString in $searchStrings
satisfies contains($arg,$searchString)
} ;
declare function functx:contains-case-insensitive
( $arg as xs:string? ,
$substring as xs:string ) as xs:boolean? {
contains(upper-case($arg), upper-case($substring))
} ;
declare function functx:contains-word
( $arg as xs:string? ,
$word as xs:string ) as xs:boolean {
matches(upper-case($arg),
concat('^(.*\W)?',
upper-case(functx:escape-for-regex($word)),
'(\W.*)?$'))
} ;
declare function functx:copy-attributes
( $copyTo as element() ,
$copyFrom as element() ) as element() {
element { node-name($copyTo)}
{ $copyTo/@*[not(node-name(.) = $copyFrom/@*/node-name(.))],
$copyFrom/@*,
$copyTo/node() }
} ;
declare function functx:date
( $year as xs:anyAtomicType ,
$month as xs:anyAtomicType ,
$day as xs:anyAtomicType ) as xs:date {
xs:date(
concat(
functx:pad-integer-to-length(xs:integer($year),4),'-',
functx:pad-integer-to-length(xs:integer($month),2),'-',
functx:pad-integer-to-length(xs:integer($day),2)))
} ;
declare function functx:dateTime
( $year as xs:anyAtomicType ,
$month as xs:anyAtomicType ,
$day as xs:anyAtomicType ,
$hour as xs:anyAtomicType ,
$minute as xs:anyAtomicType ,
$second as xs:anyAtomicType ) as xs:dateTime {
xs:dateTime(
concat(functx:date($year,$month,$day),'T',
functx:time($hour,$minute,$second)))
} ;
declare function functx:day-in-year
( $date as xs:anyAtomicType? ) as xs:integer? {
days-from-duration(
xs:date($date) - functx:first-day-of-year($date)) + 1
} ;
declare function functx:day-of-week-abbrev-en
( $date as xs:anyAtomicType? ) as xs:string? {
('Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat')
[functx:day-of-week($date) + 1]
} ;
declare function functx:day-of-week-name-en
( $date as xs:anyAtomicType? ) as xs:string? {
('Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday')
[functx:day-of-week($date) + 1]
} ;
declare function functx:day-of-week
( $date as xs:anyAtomicType? ) as xs:integer? {
if (empty($date))
then ()
else xs:integer((xs:date($date) - xs:date('1901-01-06'))
div xs:dayTimeDuration('P1D')) mod 7
} ;
declare function functx:dayTimeDuration
( $days as xs:decimal? ,
$hours as xs:decimal? ,
$minutes as xs:decimal? ,
$seconds as xs:decimal? ) as xs:dayTimeDuration {
(xs:dayTimeDuration('P1D') * functx:if-empty($days,0)) +
(xs:dayTimeDuration('PT1H') * functx:if-empty($hours,0)) +
(xs:dayTimeDuration('PT1M') * functx:if-empty($minutes,0)) +
(xs:dayTimeDuration('PT1S') * functx:if-empty($seconds,0))
} ;
declare function functx:days-in-month
( $date as xs:anyAtomicType? ) as xs:integer? {
if (month-from-date(xs:date($date)) = 2 and
functx:is-leap-year($date))
then 29
else
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
[month-from-date(xs:date($date))]
} ;
declare function functx:depth-of-node
( $node as node()? ) as xs:integer {
count($node/ancestor-or-self::node())
} ;
declare function functx:distinct-attribute-names
( $nodes as node()* ) as xs:string* {
distinct-values($nodes//@*/name(.))
} ;
declare function functx:distinct-deep
( $nodes as node()* ) as node()* {
for $seq in (1 to count($nodes))
return $nodes[$seq][not(functx:is-node-in-sequence-deep-equal(
.,$nodes[position() < $seq]))]
} ;
declare function functx:distinct-element-names
( $nodes as node()* ) as xs:string* {
distinct-values($nodes/descendant-or-self::*/name(.))
} ;
declare function functx:distinct-element-paths
( $nodes as node()* ) as xs:string* {
distinct-values(functx:path-to-node($nodes/descendant-or-self::*))
} ;
declare function functx:distinct-nodes
( $nodes as node()* ) as node()* {
for $seq in (1 to count($nodes))
return $nodes[$seq][not(functx:is-node-in-sequence(
.,$nodes[position() < $seq]))]
} ;
declare function functx:duration-from-timezone
( $timezone as xs:string ) as xs:dayTimeDuration {
xs:dayTimeDuration(
if (not(matches($timezone,'Z|[\+\-]\d{2}:\d{2}')))
then error(xs:QName('functx:Invalid_Timezone_Value'))
else if ($timezone = 'Z')
then 'PT0S'
else replace($timezone,'\+?(\d{2}):\d{2}','PT$1H')
)
} ;
declare function functx:dynamic-path
( $parent as node() ,
$path as xs:string ) as item()* {
let $nextStep := functx:substring-before-if-contains($path,'/')
let $restOfSteps := substring-after($path,'/')
for $child in
($parent/*[functx:name-test(name(),$nextStep)],
$parent/@*[functx:name-test(name(),
substring-after($nextStep,'@'))])
return if ($restOfSteps)
then functx:dynamic-path($child, $restOfSteps)
else $child
} ;
declare function functx:escape-for-regex
( $arg as xs:string? ) as xs:string {
replace($arg,
'(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')
} ;
declare function functx:exclusive-or
( $arg1 as xs:boolean? ,
$arg2 as xs:boolean? ) as xs:boolean? {
$arg1 != $arg2
} ;
declare function functx:first-day-of-month
( $date as xs:anyAtomicType? ) as xs:date? {
functx:date(year-from-date(xs:date($date)),
month-from-date(xs:date($date)),
1)
} ;
declare function functx:first-day-of-year
( $date as xs:anyAtomicType? ) as xs:date? {
functx:date(year-from-date(xs:date($date)), 1, 1)
} ;
declare function functx:first-node
( $nodes as node()* ) as node()? {
($nodes/.)[1]
} ;
declare function functx:follows-not-descendant
( $a as node()? ,
$b as node()? ) as xs:boolean {
$a >> $b and empty($b intersect $a/ancestor::node())
} ;
declare function functx:format-as-title-en
( $titles as xs:string* ) as xs:string* {
let $wordsToMoveToEnd := ('A', 'An', 'The')
for $title in $titles
let $firstWord := functx:substring-before-match($title,'\W')
return if ($firstWord = $wordsToMoveToEnd)
then replace($title,'(.*?)\W(.*)', '$2, $1')
else $title
} ;
declare function functx:fragment-from-uri
( $uri as xs:string? ) as xs:string? {
substring-after($uri,'#')
} ;
declare function functx:get-matches-and-non-matches
( $string as xs:string? ,
$regex as xs:string ) as element()* {
let $iomf := functx:index-of-match-first($string, $regex)
return
if (empty($iomf))
then <non-match>{$string}</non-match>
else
if ($iomf > 1)
then (<non-match>{substring($string,1,$iomf - 1)}</non-match>,
functx:get-matches-and-non-matches(
substring($string,$iomf),$regex))
else
let $length :=
string-length($string) -
string-length(functx:replace-first($string, $regex,''))
return (<match>{substring($string,1,$length)}</match>,
if (string-length($string) > $length)
then functx:get-matches-and-non-matches(
substring($string,$length + 1),$regex)
else ())
} ;
declare function functx:get-matches
( $string as xs:string? ,
$regex as xs:string ) as xs:string* {
functx:get-matches-and-non-matches($string,$regex)/
string(self::match)
} ;
declare function functx:has-element-only-content
( $element as element() ) as xs:boolean {
not($element/text()[normalize-space(.) != '']) and $element/*
} ;
declare function functx:has-empty-content
( $element as element() ) as xs:boolean {
not($element/node())
} ;
declare function functx:has-mixed-content
( $element as element() ) as xs:boolean {
$element/text()[normalize-space(.) != ''] and $element/*
} ;
declare function functx:has-simple-content
( $element as element() ) as xs:boolean {
$element/text() and not($element/*)
} ;
declare function functx:id-from-element
( $element as element()? ) as xs:string? {
data(($element/@*[id(.) is ..])[1])
} ;
declare function functx:id-untyped
( $node as node()* ,
$id as xs:anyAtomicType ) as element()* {
$node//*[@* = $id]
} ;
declare function functx:if-absent
( $arg as item()* ,
$value as item()* ) as item()* {
if (exists($arg))
then $arg
else $value
} ;
declare function functx:if-empty
( $arg as item()? ,
$value as item()* ) as item()* {
if (string($arg) != '')
then data($arg)
else $value
} ;
declare function functx:index-of-deep-equal-node
( $nodes as node()* ,
$nodeToFind as node() ) as xs:integer* {
for $seq in (1 to count($nodes))
return $seq[deep-equal($nodes[$seq],$nodeToFind)]
} ;
declare function functx:index-of-match-first
( $arg as xs:string? ,
$pattern as xs:string ) as xs:integer? {
if (matches($arg,$pattern))
then string-length(tokenize($arg, $pattern)[1]) + 1
else ()
} ;
declare function functx:index-of-node
( $nodes as node()* ,
$nodeToFind as node() ) as xs:integer* {
for $seq in (1 to count($nodes))
return $seq[$nodes[$seq] is $nodeToFind]
} ;
declare function functx:index-of-string-first
( $arg as xs:string? ,
$substring as xs:string ) as xs:integer? {
if (contains($arg, $substring))
then string-length(substring-before($arg, $substring))+1
else ()
} ;
declare function functx:index-of-string-last
( $arg as xs:string? ,
$substring as xs:string ) as xs:integer? {
functx:index-of-string($arg, $substring)[last()]
} ;
declare function functx:index-of-string
( $arg as xs:string? ,
$substring as xs:string ) as xs:integer* {
if (contains($arg, $substring))
then (string-length(substring-before($arg, $substring))+1,
for $other in
functx:index-of-string(substring-after($arg, $substring),
$substring)
return
$other +
string-length(substring-before($arg, $substring)) +
string-length($substring))
else ()
} ;
declare function functx:insert-string
( $originalString as xs:string? ,
$stringToInsert as xs:string? ,
$pos as xs:integer ) as xs:string {
concat(substring($originalString,1,$pos - 1),
$stringToInsert,
substring($originalString,$pos))
} ;
declare function functx:is-a-number
( $value as xs:anyAtomicType? ) as xs:boolean {
string(number($value)) != 'NaN'
} ;
declare function functx:is-absolute-uri
( $uri as xs:string? ) as xs:boolean {
matches($uri,'^[a-z]+:')
} ;
declare function functx:is-ancestor
( $node1 as node() ,
$node2 as node() ) as xs:boolean {
exists($node1 intersect $node2/ancestor::node())
} ;
declare function functx:is-descendant
( $node1 as node() ,
$node2 as node() ) as xs:boolean {
boolean($node2 intersect $node1/ancestor::node())
} ;
declare function functx:is-leap-year
( $date as xs:anyAtomicType? ) as xs:boolean {
for $year in xs:integer(substring(string($date),1,4))
return ($year mod 4 = 0 and
$year mod 100 != 0) or
$year mod 400 = 0
} ;
declare function functx:is-node-among-descendants-deep-equal
( $node as node()? ,
$seq as node()* ) as xs:boolean {
some $nodeInSeq in $seq/descendant-or-self::*/(.|@*)
satisfies deep-equal($nodeInSeq,$node)
} ;
declare function functx:is-node-among-descendants
( $node as node()? ,
$seq as node()* ) as xs:boolean {
some $nodeInSeq in $seq/descendant-or-self::*/(.|@*)
satisfies $nodeInSeq is $node
} ;
declare function functx:is-node-in-sequence-deep-equal
( $node as node()? ,
$seq as node()* ) as xs:boolean {
some $nodeInSeq in $seq satisfies deep-equal($nodeInSeq,$node)
} ;
declare function functx:is-node-in-sequence
( $node as node()? ,
$seq as node()* ) as xs:boolean {
some $nodeInSeq in $seq satisfies $nodeInSeq is $node
} ;
declare function functx:is-value-in-sequence
( $value as xs:anyAtomicType? ,
$seq as xs:anyAtomicType* ) as xs:boolean {
$value = $seq
} ;
declare function functx:last-day-of-month
( $date as xs:anyAtomicType? ) as xs:date? {
functx:date(year-from-date(xs:date($date)),
month-from-date(xs:date($date)),
functx:days-in-month($date))
} ;
declare function functx:last-day-of-year
( $date as xs:anyAtomicType? ) as xs:date? {
functx:date(year-from-date(xs:date($date)), 12, 31)
} ;
declare function functx:last-node
( $nodes as node()* ) as node()? {
($nodes/.)[last()]
} ;
declare function functx:leaf-elements
( $root as node()? ) as element()* {
$root/descendant-or-self::*[not(*)]
} ;
declare function functx:left-trim
( $arg as xs:string? ) as xs:string {
replace($arg,'^\s+','')
} ;
declare function functx:line-count
( $arg as xs:string? ) as xs:integer {
count(functx:lines($arg))
} ;
declare function functx:lines
( $arg as xs:string? ) as xs:string* {
tokenize($arg, '(\r\n?|\n\r?)')
} ;
declare function functx:max-depth
( $root as node()? ) as xs:integer? {
if ($root/*)
then max($root/*/functx:max-depth(.)) + 1
else 1
} ;
declare function functx:max-determine-type
( $seq as xs:anyAtomicType* ) as xs:anyAtomicType? {
if (every $value in $seq satisfies ($value castable as xs:double))
then max(for $value in $seq return xs:double($value))
else max(for $value in $seq return xs:string($value))
} ;
declare function functx:max-line-length
( $arg as xs:string? ) as xs:integer {
max(
for $line in functx:lines($arg)
return string-length($line))
} ;
declare function functx:max-node
( $nodes as node()* ) as node()* {
$nodes[. = max($nodes)]
} ;
declare function functx:max-string
( $strings as xs:anyAtomicType* ) as xs:string? {
max(for $string in $strings return string($string))
} ;
declare function functx:min-determine-type
( $seq as xs:anyAtomicType* ) as xs:anyAtomicType? {
if (every $value in $seq satisfies ($value castable as xs:double))
then min(for $value in $seq return xs:double($value))
else min(for $value in $seq return xs:string($value))
} ;
declare function functx:min-node
( $nodes as node()* ) as node()* {
$nodes[. = min($nodes)]
} ;
declare function functx:min-non-empty-string
( $strings as xs:string* ) as xs:string? {
min($strings[. != ''])
} ;
declare function functx:min-string
( $strings as xs:anyAtomicType* ) as xs:string? {
min(for $string in $strings return string($string))
} ;
declare function functx:mmddyyyy-to-date
( $dateString as xs:string? ) as xs:date? {
if (empty($dateString))
then ()
else if (not(matches($dateString,
'^\D*(\d{2})\D*(\d{2})\D*(\d{4})\D*$')))
then error(xs:QName('functx:Invalid_Date_Format'))
else xs:date(replace($dateString,
'^\D*(\d{2})\D*(\d{2})\D*(\d{4})\D*$',
'$3-$1-$2'))
} ;
declare function functx:month-abbrev-en
( $date as xs:anyAtomicType? ) as xs:string? {
('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
[month-from-date(xs:date($date))]
} ;
declare function functx:month-name-en
( $date as xs:anyAtomicType? ) as xs:string? {
('January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December')
[month-from-date(xs:date($date))]
} ;
declare function functx:name-test
( $testname as xs:string? ,
$names as xs:string* ) as xs:boolean {
$testname = $names
or
$names = '*'
or
functx:substring-after-if-contains($testname,':') =
(for $name in $names
return substring-after($name,'*:'))
or
substring-before($testname,':') =
(for $name in $names[contains(.,':*')]
return substring-before($name,':*'))
} ;
declare function functx:namespaces-in-use
( $root as node()? ) as xs:anyURI* {
distinct-values(
$root/descendant-or-self::*/(.|@*)/namespace-uri(.))
} ;
declare function functx:next-day
( $date as xs:anyAtomicType? ) as xs:date? {
xs:date($date) + xs:dayTimeDuration('P1D')
} ;
declare function functx:node-kind
( $nodes as node()* ) as xs:string* {
for $node in $nodes
return
if ($node instance of element()) then 'element'
else if ($node instance of attribute()) then 'attribute'
else if ($node instance of text()) then 'text'
else if ($node instance of document-node()) then 'document-node'
else if ($node instance of comment()) then 'comment'
else if ($node instance of processing-instruction())
then 'processing-instruction'
else 'unknown'
} ;
declare function functx:non-distinct-values
( $seq as xs:anyAtomicType* ) as xs:anyAtomicType* {
for $val in distinct-values($seq)
return $val[count($seq[. = $val]) > 1]
} ;
declare function functx:number-of-matches
( $arg as xs:string? ,
$pattern as xs:string ) as xs:integer {
count(tokenize($arg,$pattern)) - 1
} ;
declare function functx:open-ref-document
( $refNode as node() ) as document-node() {
if (base-uri($refNode))
then doc(resolve-uri($refNode, base-uri($refNode)))
else doc(resolve-uri($refNode))
} ;
declare function functx:ordinal-number-en
( $num as xs:integer? ) as xs:string {
concat(xs:string($num),
if (matches(xs:string($num),'[04-9]$|1[1-3]$')) then 'th'
else if (ends-with(xs:string($num),'1')) then 'st'
else if (ends-with(xs:string($num),'2')) then 'nd'
else if (ends-with(xs:string($num),'3')) then 'rd'
else '')
} ;
declare function functx:pad-integer-to-length
( $integerToPad as xs:anyAtomicType? ,
$length as xs:integer ) as xs:string {
if ($length < string-length(string($integerToPad)))
then error(xs:QName('functx:Integer_Longer_Than_Length'))
else concat
(functx:repeat-string(
'0',$length - string-length(string($integerToPad))),
string($integerToPad))
} ;
declare function functx:pad-string-to-length
( $stringToPad as xs:string? ,
$padChar as xs:string ,
$length as xs:integer ) as xs:string {
substring(
string-join (
($stringToPad, for $i in (1 to $length) return $padChar)
,'')
,1,$length)
} ;
declare function functx:path-to-node-with-pos
( $node as node()? ) as xs:string {
string-join(
for $ancestor in $node/ancestor-or-self::*
let $sibsOfSameName := $ancestor/../*[name() = name($ancestor)]
return concat(name($ancestor),
if (count($sibsOfSameName) <= 1)
then ''
else concat(
'[',functx:index-of-node($sibsOfSameName,$ancestor),']'))
, '/')
} ;
declare function functx:path-to-node
( $nodes as node()* ) as xs:string* {
$nodes/string-join(ancestor-or-self::*/name(.), '/')
} ;
declare function functx:precedes-not-ancestor
( $a as node()? ,
$b as node()? ) as xs:boolean {
$a << $b and empty($a intersect $b/ancestor::node())
} ;
declare function functx:previous-day
( $date as xs:anyAtomicType? ) as xs:date? {
xs:date($date) - xs:dayTimeDuration('P1D')
} ;
declare function functx:remove-attributes-deep
( $nodes as node()* ,
$names as xs:string* ) as node()* {
for $node in $nodes
return if ($node instance of element())
then element { node-name($node)}
{ $node/@*[not(functx:name-test(name(),$names))],
functx:remove-attributes-deep($node/node(), $names)}
else if ($node instance of document-node())
then functx:remove-attributes-deep($node/node(), $names)
else $node
} ;
declare function functx:remove-attributes
( $elements as element()* ,
$names as xs:string* ) as element() {