forked from ilinsky/jquery-xpath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.xpath.js
6352 lines (5180 loc) · 202 KB
/
jquery.xpath.js
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
/*
* jQuery XPath plugin v0.2.5
* https://github.com/ilinsky/jquery-xpath
* Copyright 2013, Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses.
*
* Includes xpath.js - XPath 2.0 implementation in JavaScript
* https://github.com/ilinsky/xpath.js
* Copyright 2013, Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses.
*
*/
(function () {
var cString = window.String,
cBoolean = window.Boolean,
cNumber = window.Number,
cObject = window.Object,
cArray = window.Array,
cRegExp = window.RegExp,
cDate = window.Date,
cFunction = window.Function,
cMath = window.Math,
cError = window.Error,
cSyntaxError= window.SyntaxError,
cTypeError = window.TypeError,
fIsNaN = window.isNaN,
fIsFinite = window.isFinite,
nNaN = window.NaN,
nInfinity = window.Infinity,
fString_trim =(function() {
return cString.prototype.trim ? function(sValue) {return cString(sValue).trim();} : function(sValue) {
return cString(sValue).replace(/^\s+|\s+$/g, '');
};
})(),
fArray_indexOf =(function() {
return cArray.prototype.indexOf ? function(aValue, vItem) {return aValue.indexOf(vItem);} : function(aValue, vItem) {
for (var nIndex = 0, nLength = aValue.length; nIndex < nLength; nIndex++)
if (aValue[nIndex] === vItem)
return nIndex;
return -1;
};
})();
var sNS_XSD = "http://www.w3.org/2001/XMLSchema",
sNS_XPF = "http://www.w3.org/2005/xpath-functions",
sNS_XNS = "http://www.w3.org/2000/xmlns/",
sNS_XML = "http://www.w3.org/XML/1998/namespace";
function cException(sCode
, sMessage
) {
this.code = sCode;
this.message =
sMessage ||
oException_messages[sCode];
};
cException.prototype = new cError;
var oException_messages = {};
oException_messages["XPDY0002"] = "Evaluation of an expression relies on some part of the dynamic context that has not been assigned a value.";
oException_messages["XPST0003"] = "Expression is not a valid instance of the grammar";
oException_messages["XPTY0004"] = "Type is not appropriate for the context in which the expression occurs";
oException_messages["XPST0008"] = "Expression refers to an element name, attribute name, schema type name, namespace prefix, or variable name that is not defined in the static context";
oException_messages["XPST0010"] = "Axis not supported";
oException_messages["XPST0017"] = "Expanded QName and number of arguments in a function call do not match the name and arity of a function signature";
oException_messages["XPTY0018"] = "The result of the last step in a path expression contains both nodes and atomic values";
oException_messages["XPTY0019"] = "The result of a step (other than the last step) in a path expression contains an atomic value.";
oException_messages["XPTY0020"] = "In an axis step, the context item is not a node.";
oException_messages["XPST0051"] = "It is a static error if a QName that is used as an AtomicType in a SequenceType is not defined in the in-scope schema types as an atomic type.";
oException_messages["XPST0081"] = "A QName used in an expression contains a namespace prefix that cannot be expanded into a namespace URI by using the statically known namespaces.";
oException_messages["FORG0001"] = "Invalid value for cast/constructor.";
oException_messages["FORG0003"] = "fn:zero-or-one called with a sequence containing more than one item.";
oException_messages["FORG0004"] = "fn:one-or-more called with a sequence containing no items.";
oException_messages["FORG0005"] = "fn:exactly-one called with a sequence containing zero or more than one item.";
oException_messages["FORG0006"] = "Invalid argument type.";
oException_messages["FODC0001"] = "No context document.";
oException_messages["FORX0001"] = "Invalid regular expression flags.";
oException_messages["FOCA0002"] = "Invalid lexical value.";
oException_messages["FOCH0002"] = "Unsupported collation.";
oException_messages["FONS0004"] = "No namespace found for prefix.";
function cLexer(sValue) {
var aMatch = sValue.match(/\$?(?:(?![0-9-])(?:[\w-]+|\*):)?(?![0-9-])(?:[\w-]+|\*)|\(:|:\)|\/\/|\.\.|::|\d+(?:\.\d*)?(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?|"[^"]*(?:""[^"]*)*"|'[^']*(?:''[^']*)*'|<<|>>|[!<>]=|(?![0-9-])[\w-]+:\*|\s+|./g);
if (aMatch) {
var nStack = 0;
for (var nIndex = 0, nLength = aMatch.length; nIndex < nLength; nIndex++)
if (aMatch[nIndex] == '(:')
nStack++;
else
if (aMatch[nIndex] == ':)' && nStack)
nStack--;
else
if (!nStack && !/^\s/.test(aMatch[nIndex]))
this[this.length++] = aMatch[nIndex];
if (nStack)
throw new cException("XPST0003"
, "Unclosed comment"
);
}
};
cLexer.prototype.index = 0;
cLexer.prototype.length = 0;
cLexer.prototype.reset = function() {
this.index = 0;
};
cLexer.prototype.peek = function(nOffset) {
return this[this.index +(nOffset || 0)] || '';
};
cLexer.prototype.next = function(nOffset) {
return(this.index+= nOffset || 1) < this.length;
};
cLexer.prototype.back = function(nOffset) {
return(this.index-= nOffset || 1) > 0;
};
cLexer.prototype.eof = function() {
return this.index >= this.length;
};
function cDOMAdapter() {
};
cDOMAdapter.prototype.isNode = function(oNode) {
return oNode &&!!oNode.nodeType;
};
cDOMAdapter.prototype.getProperty = function(oNode, sName) {
return oNode[sName];
};
cDOMAdapter.prototype.isSameNode = function(oNode, oNode2) {
return oNode == oNode2;
};
cDOMAdapter.prototype.compareDocumentPosition = function(oNode, oNode2) {
return oNode.compareDocumentPosition(oNode2);
};
cDOMAdapter.prototype.lookupNamespaceURI = function(oNode, sPrefix) {
return oNode.lookupNamespaceURI(sPrefix);
};
cDOMAdapter.prototype.getElementById = function(oNode, sId) {
return oNode.getElementById(sId);
};
cDOMAdapter.prototype.getElementsByTagNameNS = function(oNode, sNameSpaceURI, sLocalName) {
return oNode.getElementsByTagNameNS(sNameSpaceURI, sLocalName);
};
function cDynamicContext(oStaticContext, vItem, oScope, oDOMAdapter) {
this.staticContext = oStaticContext;
this.item = vItem;
this.scope = oScope || {};
this.stack = {};
this.DOMAdapter = oDOMAdapter || new cDOMAdapter;
var oDate = new cDate,
nOffset = oDate.getTimezoneOffset();
this.dateTime = new cXSDateTime(oDate.getFullYear(), oDate.getMonth() + 1, oDate.getDate(), oDate.getHours(), oDate.getMinutes(), oDate.getSeconds() + oDate.getMilliseconds() / 1000, -nOffset);
this.timezone = new cXSDayTimeDuration(0, cMath.abs(~~(nOffset / 60)), cMath.abs(nOffset % 60), 0, nOffset > 0);
};
cDynamicContext.prototype.item = null;
cDynamicContext.prototype.position = 0;
cDynamicContext.prototype.size = 0;
cDynamicContext.prototype.scope = null;
cDynamicContext.prototype.stack = null; cDynamicContext.prototype.dateTime = null;
cDynamicContext.prototype.timezone = null;
cDynamicContext.prototype.staticContext = null;
cDynamicContext.prototype.pushVariable = function(sName, vValue) {
if (!this.stack.hasOwnProperty(sName))
this.stack[sName] = [];
this.stack[sName].push(this.scope[sName]);
this.scope[sName] = vValue;
};
cDynamicContext.prototype.popVariable = function(sName) {
if (this.stack.hasOwnProperty(sName)) {
this.scope[sName] = this.stack[sName].pop();
if (!this.stack[sName].length) {
delete this.stack[sName];
if (typeof this.scope[sName] == "undefined")
delete this.scope[sName];
}
}
};
function cStaticContext() {
this.dataTypes = {};
this.documents = {};
this.functions = {};
this.collations = {};
this.collections= {};
};
cStaticContext.prototype.baseURI = null;
cStaticContext.prototype.dataTypes = null;
cStaticContext.prototype.documents = null;
cStaticContext.prototype.functions = null;
cStaticContext.prototype.defaultFunctionNamespace = null;
cStaticContext.prototype.collations = null;
cStaticContext.prototype.defaultCollationName = sNS_XPF + "/collation/codepoint";
cStaticContext.prototype.collections = null;
cStaticContext.prototype.namespaceResolver = null;
cStaticContext.prototype.defaultElementNamespace = null;
var rStaticContext_uri = /^(?:\{([^\}]+)\})?(.+)$/;
cStaticContext.prototype.setDataType = function(sUri, fFunction) {
var aMatch = sUri.match(rStaticContext_uri);
if (aMatch)
if (aMatch[1] != sNS_XSD)
this.dataTypes[sUri] = fFunction;
};
cStaticContext.prototype.getDataType = function(sUri) {
var aMatch = sUri.match(rStaticContext_uri);
if (aMatch)
return aMatch[1] == sNS_XSD ? hStaticContext_dataTypes[cRegExp.$2] : this.dataTypes[sUri];
};
cStaticContext.prototype.setDocument = function(sUri, fFunction) {
this.documents[sUri] = fFunction;
};
cStaticContext.prototype.setFunction = function(sUri, fFunction) {
var aMatch = sUri.match(rStaticContext_uri);
if (aMatch)
if (aMatch[1] != sNS_XPF)
this.functions[sUri] = fFunction;
};
cStaticContext.prototype.getFunction = function(sUri) {
var aMatch = sUri.match(rStaticContext_uri);
if (aMatch)
return aMatch[1] == sNS_XPF ? hStaticContext_functions[cRegExp.$2] : this.functions[sUri];
};
cStaticContext.prototype.setCollation = function(sUri, fFunction) {
this.collations[sUri] = fFunction;
};
cStaticContext.prototype.getCollation = function(sUri) {
return this.collations[sUri];
};
cStaticContext.prototype.setCollection = function(sUri, fFunction) {
this.collections[sUri] = fFunction;
};
cStaticContext.prototype.getURIForPrefix = function(sPrefix) {
var oResolver = this.namespaceResolver,
fResolver = oResolver && oResolver.lookupNamespaceURI ? oResolver.lookupNamespaceURI : oResolver,
sNameSpaceURI;
if (fResolver instanceof cFunction && (sNameSpaceURI = fResolver.call(oResolver, sPrefix)))
return sNameSpaceURI;
if (sPrefix == 'fn')
return sNS_XPF;
if (sPrefix == 'xs')
return sNS_XSD;
if (sPrefix == "xml")
return sNS_XML;
if (sPrefix == "xmlns")
return sNS_XNS;
throw new cException("XPST0081"
, "Prefix '" + sPrefix + "' has not been declared"
);
};
cStaticContext.js2xs = function(vItem) {
if (typeof vItem == "boolean")
vItem = new cXSBoolean(vItem);
else
if (typeof vItem == "number")
vItem =(fIsNaN(vItem) ||!fIsFinite(vItem)) ? new cXSDouble(vItem) : fNumericLiteral_parseValue(cString(vItem));
else
vItem = new cXSString(cString(vItem));
return vItem;
};
cStaticContext.xs2js = function(vItem) {
if (vItem instanceof cXSBoolean)
vItem = vItem.valueOf();
else
if (fXSAnyAtomicType_isNumeric(vItem))
vItem = vItem.valueOf();
else
vItem = vItem.toString();
return vItem;
};
var hStaticContext_functions = {},
hStaticContext_signatures = {},
hStaticContext_dataTypes = {},
hStaticContext_operators = {};
function fStaticContext_defineSystemFunction(sName, aParameters, fFunction) {
hStaticContext_functions[sName] = fFunction;
hStaticContext_signatures[sName] = aParameters;
};
function fStaticContext_defineSystemDataType(sName, fFunction) {
hStaticContext_dataTypes[sName] = fFunction;
};
function cExpression(sExpression, oStaticContext) {
var oLexer = new cLexer(sExpression),
oExpr = fExpr_parse(oLexer, oStaticContext);
if (!oLexer.eof())
throw new cException("XPST0003"
, "Unexpected token beyond end of query"
);
if (!oExpr)
throw new cException("XPST0003"
, "Expected expression"
);
this.internalExpression = oExpr;
};
cExpression.prototype.internalExpression = null;
cExpression.prototype.evaluate = function(oContext) {
return this.internalExpression.evaluate(oContext);
};
function cStringCollator() {
};
cStringCollator.prototype.equals = function(sValue1, sValue2) {
throw "Not implemented";
};
cStringCollator.prototype.compare = function(sValue1, sValue2) {
throw "Not implemented";
};
function cXSConstants(){};
cXSConstants.ANYSIMPLETYPE_DT = 1;
cXSConstants.STRING_DT = 2;
cXSConstants.BOOLEAN_DT = 3;
cXSConstants.DECIMAL_DT = 4;
cXSConstants.FLOAT_DT = 5;
cXSConstants.DOUBLE_DT = 6;
cXSConstants.DURATION_DT = 7;
cXSConstants.DATETIME_DT = 8;
cXSConstants.TIME_DT = 9;
cXSConstants.DATE_DT = 10;
cXSConstants.GYEARMONTH_DT = 11;
cXSConstants.GYEAR_DT = 12;
cXSConstants.GMONTHDAY_DT = 13;
cXSConstants.GDAY_DT = 14;
cXSConstants.GMONTH_DT = 15;
cXSConstants.HEXBINARY_DT = 16;
cXSConstants.BASE64BINARY_DT = 17;
cXSConstants.ANYURI_DT = 18;
cXSConstants.QNAME_DT = 19;
cXSConstants.NOTATION_DT = 20;
cXSConstants.NORMALIZEDSTRING_DT = 21;
cXSConstants.TOKEN_DT = 22;
cXSConstants.LANGUAGE_DT = 23;
cXSConstants.NMTOKEN_DT = 24;
cXSConstants.NAME_DT = 25;
cXSConstants.NCNAME_DT = 26;
cXSConstants.ID_DT = 27;
cXSConstants.IDREF_DT = 28;
cXSConstants.ENTITY_DT = 29;
cXSConstants.INTEGER_DT = 30;
cXSConstants.NONPOSITIVEINTEGER_DT = 31;
cXSConstants.NEGATIVEINTEGER_DT = 32;
cXSConstants.LONG_DT = 33;
cXSConstants.INT_DT = 34;
cXSConstants.SHORT_DT = 35;
cXSConstants.BYTE_DT = 36;
cXSConstants.NONNEGATIVEINTEGER_DT = 37;
cXSConstants.UNSIGNEDLONG_DT = 38;
cXSConstants.UNSIGNEDINT_DT = 39;
cXSConstants.UNSIGNEDSHORT_DT = 40;
cXSConstants.UNSIGNEDBYTE_DT = 41;
cXSConstants.POSITIVEINTEGER_DT = 42;
cXSConstants.LISTOFUNION_DT = 43;
cXSConstants.LIST_DT = 44;
cXSConstants.UNAVAILABLE_DT = 45;
cXSConstants.DATETIMESTAMP_DT = 46;
cXSConstants.DAYMONTHDURATION_DT = 47;
cXSConstants.DAYTIMEDURATION_DT = 48;
cXSConstants.PRECISIONDECIMAL_DT = 49;
cXSConstants.ANYATOMICTYPE_DT = 50;
cXSConstants.ANYTYPE_DT = 51;
cXSConstants.XT_YEARMONTHDURATION_DT=-1;
cXSConstants.XT_UNTYPEDATOMIC_DT =-2;
function cExpr() {
this.items = [];
};
cExpr.prototype.items = null;
function fExpr_parse (oLexer, oStaticContext) {
var oItem;
if (oLexer.eof() ||!(oItem = fExprSingle_parse(oLexer, oStaticContext)))
return;
var oExpr = new cExpr;
oExpr.items.push(oItem);
while (oLexer.peek() == ',') {
oLexer.next();
if (oLexer.eof() ||!(oItem = fExprSingle_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected expression"
);
oExpr.items.push(oItem);
}
return oExpr;
};
cExpr.prototype.evaluate = function(oContext) {
var oSequence = [];
for (var nIndex = 0, nLength = this.items.length; nIndex < nLength; nIndex++)
oSequence = hStaticContext_operators["concatenate"].call(oContext, oSequence, this.items[nIndex].evaluate(oContext));
return oSequence;
};
function cExprSingle() {
};
function fExprSingle_parse (oLexer, oStaticContext) {
if (!oLexer.eof())
return fIfExpr_parse(oLexer, oStaticContext)
|| fForExpr_parse(oLexer, oStaticContext)
|| fQuantifiedExpr_parse(oLexer, oStaticContext)
|| fOrExpr_parse(oLexer, oStaticContext);
};
function cForExpr() {
this.bindings = [];
this.returnExpr = null;
};
cForExpr.prototype.bindings = null;
cForExpr.prototype.returnExpr = null;
function fForExpr_parse (oLexer, oStaticContext) {
if (oLexer.peek() == "for" && oLexer.peek(1).substr(0, 1) == '$') {
oLexer.next();
var oForExpr = new cForExpr,
oExpr;
do {
oForExpr.bindings.push(fSimpleForBinding_parse(oLexer, oStaticContext));
}
while (oLexer.peek() == ',' && oLexer.next());
if (oLexer.peek() != "return")
throw new cException("XPST0003"
, "Expected 'return' token in for expression"
);
oLexer.next();
if (oLexer.eof() ||!(oExpr = fExprSingle_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected return statement operand in for expression"
);
oForExpr.returnExpr = oExpr;
return oForExpr;
}
};
cForExpr.prototype.evaluate = function (oContext) {
var oSequence = [];
(function(oSelf, nBinding) {
var oBinding = oSelf.bindings[nBinding++],
oSequence1 = oBinding.inExpr.evaluate(oContext),
sUri = (oBinding.namespaceURI ? '{' + oBinding.namespaceURI + '}' : '') + oBinding.localName;
for (var nIndex = 0, nLength = oSequence1.length; nIndex < nLength; nIndex++) {
oContext.pushVariable(sUri, oSequence1[nIndex]);
if (nBinding < oSelf.bindings.length)
arguments.callee(oSelf, nBinding);
else
oSequence = oSequence.concat(oSelf.returnExpr.evaluate(oContext));
oContext.popVariable(sUri);
}
})(this, 0);
return oSequence;
};
function cSimpleForBinding(sPrefix, sLocalName, sNameSpaceURI, oInExpr) {
this.prefix = sPrefix;
this.localName = sLocalName;
this.namespaceURI = sNameSpaceURI;
this.inExpr = oInExpr;
};
cSimpleForBinding.prototype.prefix = null;
cSimpleForBinding.prototype.localName = null;
cSimpleForBinding.prototype.namespaceURI = null;
cSimpleForBinding.prototype.inExpr = null;
function fSimpleForBinding_parse (oLexer, oStaticContext) {
var aMatch = oLexer.peek().substr(1).match(rNameTest);
if (!aMatch)
throw new cException("XPST0003"
, "Expected binding in for expression"
);
if (aMatch[1] == '*' || aMatch[2] == '*')
throw new cException("XPST0003"
, "Illegal use of wildcard in for expression binding variable name"
);
oLexer.next();
if (oLexer.peek() != "in")
throw new cException("XPST0003"
, "Expected 'in' token in for expression binding"
);
oLexer.next();
var oExpr;
if (oLexer.eof() ||!(oExpr = fExprSingle_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected in statement operand in for expression binding"
);
return new cSimpleForBinding(aMatch[1] || null, aMatch[2], aMatch[1] ? oStaticContext.getURIForPrefix(aMatch[1]) : null, oExpr);
};
function cIfExpr(oCondExpr, oThenExpr, oElseExpr) {
this.condExpr = oCondExpr;
this.thenExpr = oThenExpr;
this.elseExpr = oElseExpr;
};
cIfExpr.prototype.condExpr = null;
cIfExpr.prototype.thenExpr = null;
cIfExpr.prototype.elseExpr = null;
function fIfExpr_parse (oLexer, oStaticContext) {
var oCondExpr,
oThenExpr,
oElseExpr;
if (oLexer.peek() == "if" && oLexer.peek(1) == '(') {
oLexer.next(2);
if (oLexer.eof() ||!(oCondExpr = fExpr_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected if statement operand in conditional expression"
);
if (oLexer.peek() != ')')
throw new cException("XPST0003"
, "Expected ')' token in for expression"
);
oLexer.next();
if (oLexer.peek() != "then")
throw new cException("XPST0003"
, "Expected 'then' token in conditional if expression"
);
oLexer.next();
if (oLexer.eof() ||!(oThenExpr = fExprSingle_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected then statement operand in condional expression"
);
if (oLexer.peek() != "else")
throw new cException("XPST0003"
, "Expected 'else' token in conditional if expression"
);
oLexer.next();
if (oLexer.eof() ||!(oElseExpr = fExprSingle_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected else statement operand in condional expression"
);
return new cIfExpr(oCondExpr, oThenExpr, oElseExpr);
}
};
cIfExpr.prototype.evaluate = function (oContext) {
return this[fFunction_sequence_toEBV(this.condExpr.evaluate(oContext), oContext) ? "thenExpr" : "elseExpr"].evaluate(oContext);
};
function cQuantifiedExpr(sQuantifier) {
this.quantifier = sQuantifier;
this.bindings = [];
this.satisfiesExpr = null;
};
cQuantifiedExpr.prototype.bindings = null;
cQuantifiedExpr.prototype.quantifier = null;
cQuantifiedExpr.prototype.satisfiesExpr = null;
function fQuantifiedExpr_parse (oLexer, oStaticContext) {
var sQuantifier = oLexer.peek();
if ((sQuantifier == "some" || sQuantifier == "every") && oLexer.peek(1).substr(0, 1) == '$') {
oLexer.next();
var oQuantifiedExpr = new cQuantifiedExpr(sQuantifier),
oExpr;
do {
oQuantifiedExpr.bindings.push(fSimpleQuantifiedBinding_parse(oLexer, oStaticContext));
}
while (oLexer.peek() == ',' && oLexer.next());
if (oLexer.peek() != "satisfies")
throw new cException("XPST0003"
, "Expected 'satisfies' token in quantified expression"
);
oLexer.next();
if (oLexer.eof() ||!(oExpr = fExprSingle_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected satisfies statement operand in quantified expression"
);
oQuantifiedExpr.satisfiesExpr = oExpr;
return oQuantifiedExpr;
}
};
cQuantifiedExpr.prototype.evaluate = function (oContext) {
var bEvery = this.quantifier == "every",
bResult = bEvery ? true : false;
(function(oSelf, nBinding) {
var oBinding = oSelf.bindings[nBinding++],
oSequence1 = oBinding.inExpr.evaluate(oContext),
sUri = (oBinding.namespaceURI ? '{' + oBinding.namespaceURI + '}' : '') + oBinding.localName;
for (var nIndex = 0, nLength = oSequence1.length; (nIndex < nLength) && (bEvery ? bResult :!bResult); nIndex++) {
oContext.pushVariable(sUri, oSequence1[nIndex]);
if (nBinding < oSelf.bindings.length)
arguments.callee(oSelf, nBinding);
else
bResult = fFunction_sequence_toEBV(oSelf.satisfiesExpr.evaluate(oContext), oContext);
oContext.popVariable(sUri);
}
})(this, 0);
return [new cXSBoolean(bResult)];
};
function cSimpleQuantifiedBinding(sPrefix, sLocalName, sNameSpaceURI, oInExpr) {
this.prefix = sPrefix;
this.localName = sLocalName;
this.namespaceURI = sNameSpaceURI;
this.inExpr = oInExpr;
};
cSimpleQuantifiedBinding.prototype.prefix = null;
cSimpleQuantifiedBinding.prototype.localName = null;
cSimpleQuantifiedBinding.prototype.namespaceURI = null;
cSimpleQuantifiedBinding.prototype.inExpr = null;
function fSimpleQuantifiedBinding_parse (oLexer, oStaticContext) {
var aMatch = oLexer.peek().substr(1).match(rNameTest);
if (!aMatch)
throw new cException("XPST0003"
, "Expected binding in quantified expression"
);
if (aMatch[1] == '*' || aMatch[2] == '*')
throw new cException("XPST0003"
, "Illegal use of wildcard in quantified expression binding variable name"
);
oLexer.next();
if (oLexer.peek() != "in")
throw new cException("XPST0003"
, "Expected 'in' token in quantified expression binding"
);
oLexer.next();
var oExpr;
if (oLexer.eof() ||!(oExpr = fExprSingle_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected in statement operand in quantified expression binding"
);
return new cSimpleQuantifiedBinding(aMatch[1] || null, aMatch[2], aMatch[1] ? oStaticContext.getURIForPrefix(aMatch[1]) : null, oExpr);
};
function cComparisonExpr(oLeft, oRight, sOperator) {
this.left = oLeft;
this.right = oRight;
this.operator = sOperator;
};
cComparisonExpr.prototype.left = null;
cComparisonExpr.prototype.right = null;
cComparisonExpr.prototype.operator = null;
function fComparisonExpr_parse (oLexer, oStaticContext) {
var oExpr,
oRight;
if (oLexer.eof() ||!(oExpr = fRangeExpr_parse(oLexer, oStaticContext)))
return;
if (!(oLexer.peek() in hComparisonExpr_operators))
return oExpr;
var sOperator = oLexer.peek();
oLexer.next();
if (oLexer.eof() ||!(oRight = fRangeExpr_parse(oLexer, oStaticContext)))
throw new cException("XPST0003"
, "Expected second operand in comparison expression"
);
return new cComparisonExpr(oExpr, oRight, sOperator);
};
cComparisonExpr.prototype.evaluate = function (oContext) {
var oResult = hComparisonExpr_operators[this.operator](this, oContext);
return oResult == null ? [] : [oResult];
};
function fComparisonExpr_GeneralComp(oExpr, oContext) {
var oLeft = fFunction_sequence_atomize(oExpr.left.evaluate(oContext), oContext);
if (!oLeft.length)
return new cXSBoolean(false);
var oRight = fFunction_sequence_atomize(oExpr.right.evaluate(oContext), oContext);
if (!oRight.length)
return new cXSBoolean(false);
var bResult = false;
for (var nLeftIndex = 0, nLeftLength = oLeft.length, bLeft, vLeft; (nLeftIndex < nLeftLength) &&!bResult; nLeftIndex++) {
for (var nRightIndex = 0, nRightLength = oRight.length, bRight, vRight; (nRightIndex < nRightLength) &&!bResult; nRightIndex++) {
vLeft = oLeft[nLeftIndex];
vRight = oRight[nRightIndex];
bLeft = vLeft instanceof cXSUntypedAtomic;
bRight = vRight instanceof cXSUntypedAtomic;
if (bLeft && bRight) {
vLeft = cXSString.cast(vLeft);
vRight = cXSString.cast(vRight);
}
else {
if (bLeft) {
if (vRight instanceof cXSDayTimeDuration)
vLeft = cXSDayTimeDuration.cast(vLeft);
else
if (vRight instanceof cXSYearMonthDuration)
vLeft = cXSYearMonthDuration.cast(vLeft);
else
if (vRight.primitiveKind)
vLeft = hStaticContext_dataTypes[vRight.primitiveKind].cast(vLeft);
}
else
if (bRight) {
if (vLeft instanceof cXSDayTimeDuration)
vRight = cXSDayTimeDuration.cast(vRight);
else
if (vLeft instanceof cXSYearMonthDuration)
vRight = cXSYearMonthDuration.cast(vRight);
else
if (vLeft.primitiveKind)
vRight = hStaticContext_dataTypes[vLeft.primitiveKind].cast(vRight);
}
if (vLeft instanceof cXSAnyURI)
vLeft = cXSString.cast(vLeft);
if (vRight instanceof cXSAnyURI)
vRight = cXSString.cast(vRight);
}
bResult = hComparisonExpr_ValueComp_operators[hComparisonExpr_GeneralComp_map[oExpr.operator]](vLeft, vRight, oContext).valueOf();
}
}
return new cXSBoolean(bResult);
};
var hComparisonExpr_GeneralComp_map = {
'=': 'eq',
'!=': 'ne',
'>': 'gt',
'<': 'lt',
'>=': 'ge',
'<=': 'le'
};
function fComparisonExpr_ValueComp(oExpr, oContext) {
var oLeft = fFunction_sequence_atomize(oExpr.left.evaluate(oContext), oContext);
if (!oLeft.length)
return null;
fFunctionCall_assertSequenceCardinality(oContext, oLeft, '?'
, "first operand of '" + oExpr.operator + "'"
);
var oRight = fFunction_sequence_atomize(oExpr.right.evaluate(oContext), oContext);
if (!oRight.length)
return null;
fFunctionCall_assertSequenceCardinality(oContext, oRight, '?'
, "second operand of '" + oExpr.operator + "'"
);
var vLeft = oLeft[0],
vRight = oRight[0];
if (vLeft instanceof cXSUntypedAtomic)
vLeft = cXSString.cast(vLeft);
if (vRight instanceof cXSUntypedAtomic)
vRight = cXSString.cast(vRight);
if (vLeft instanceof cXSAnyURI)
vLeft = cXSString.cast(vLeft);
if (vRight instanceof cXSAnyURI)
vRight = cXSString.cast(vRight);
return hComparisonExpr_ValueComp_operators[oExpr.operator](vLeft, vRight, oContext);
};
var hComparisonExpr_ValueComp_operators = {};
hComparisonExpr_ValueComp_operators['eq'] = function(oLeft, oRight, oContext) {
var sOperator = '';
if (fXSAnyAtomicType_isNumeric(oLeft)) {
if (fXSAnyAtomicType_isNumeric(oRight))
sOperator = "numeric-equal";
}
else
if (oLeft instanceof cXSBoolean) {
if (oRight instanceof cXSBoolean)
sOperator = "boolean-equal";
}
else
if (oLeft instanceof cXSString) {
if (oRight instanceof cXSString)
return hStaticContext_operators["numeric-equal"].call(oContext, hStaticContext_functions["compare"].call(oContext, oLeft, oRight), new cXSInteger(0));
}
else
if (oLeft instanceof cXSDate) {
if (oRight instanceof cXSDate)
sOperator = "date-equal";
}
else
if (oLeft instanceof cXSTime) {
if (oRight instanceof cXSTime)
sOperator = "time-equal";
}
else
if (oLeft instanceof cXSDateTime) {
if (oRight instanceof cXSDateTime)
sOperator = "dateTime-equal";
}
else
if (oLeft instanceof cXSDuration) {
if (oRight instanceof cXSDuration)
sOperator = "duration-equal";
}
else
if (oLeft instanceof cXSGYearMonth) {
if (oRight instanceof cXSGYearMonth)
sOperator = "gYearMonth-equal";
}
else
if (oLeft instanceof cXSGYear) {
if (oRight instanceof cXSGYear)
sOperator = "gYear-equal";
}
else
if (oLeft instanceof cXSGMonthDay) {
if (oRight instanceof cXSGMonthDay)
sOperator = "gMonthDay-equal";
}
else
if (oLeft instanceof cXSGMonth) {
if (oRight instanceof cXSGMonth)
sOperator = "gMonth-equal";
}
else
if (oLeft instanceof cXSGDay) {
if (oRight instanceof cXSGDay)
sOperator = "gDay-equal";
}
else
if (oLeft instanceof cXSQName) {
if (oRight instanceof cXSQName)
sOperator = "QName-equal";
}
else
if (oLeft instanceof cXSHexBinary) {
if (oRight instanceof cXSHexBinary)
sOperator = "hexBinary-equal";
}
else
if (oLeft instanceof cXSBase64Binary) {
if (oRight instanceof cXSBase64Binary)
sOperator = "base64Binary-equal";
}
if (sOperator)
return hStaticContext_operators[sOperator].call(oContext, oLeft, oRight);
throw new cException("XPTY0004"
, "Cannot compare values of given types"
); };
hComparisonExpr_ValueComp_operators['ne'] = function(oLeft, oRight, oContext) {
return new cXSBoolean(!hComparisonExpr_ValueComp_operators['eq'](oLeft, oRight, oContext).valueOf());
};
hComparisonExpr_ValueComp_operators['gt'] = function(oLeft, oRight, oContext) {
var sOperator = '';
if (fXSAnyAtomicType_isNumeric(oLeft)) {
if (fXSAnyAtomicType_isNumeric(oRight))
sOperator = "numeric-greater-than";
}
else
if (oLeft instanceof cXSBoolean) {
if (oRight instanceof cXSBoolean)
sOperator = "boolean-greater-than";
}
else
if (oLeft instanceof cXSString) {
if (oRight instanceof cXSString)
return hStaticContext_operators["numeric-greater-than"].call(oContext, hStaticContext_functions["compare"].call(oContext, oLeft, oRight), new cXSInteger(0));
}
else
if (oLeft instanceof cXSDate) {
if (oRight instanceof cXSDate)
sOperator = "date-greater-than";
}
else
if (oLeft instanceof cXSTime) {
if (oRight instanceof cXSTime)
sOperator = "time-greater-than";
}
else
if (oLeft instanceof cXSDateTime) {
if (oRight instanceof cXSDateTime)
sOperator = "dateTime-greater-than";
}
else
if (oLeft instanceof cXSYearMonthDuration) {
if (oRight instanceof cXSYearMonthDuration)
sOperator = "yearMonthDuration-greater-than";
}
else
if (oLeft instanceof cXSDayTimeDuration) {
if (oRight instanceof cXSDayTimeDuration)
sOperator = "dayTimeDuration-greater-than";
}
if (sOperator)
return hStaticContext_operators[sOperator].call(oContext, oLeft, oRight);
throw new cException("XPTY0004"
, "Cannot compare values of given types"
); };
hComparisonExpr_ValueComp_operators['lt'] = function(oLeft, oRight, oContext) {
var sOperator = '';
if (fXSAnyAtomicType_isNumeric(oLeft)) {
if (fXSAnyAtomicType_isNumeric(oRight))
sOperator = "numeric-less-than";
}
else
if (oLeft instanceof cXSBoolean) {
if (oRight instanceof cXSBoolean)
sOperator = "boolean-less-than";
}
else
if (oLeft instanceof cXSString) {
if (oRight instanceof cXSString)
return hStaticContext_operators["numeric-less-than"].call(oContext, hStaticContext_functions["compare"].call(oContext, oLeft, oRight), new cXSInteger(0));
}
else
if (oLeft instanceof cXSDate) {