/
index.html
6726 lines (6719 loc) · 416 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="EN" xml:lang="EN">
<head>
<title>XML Path Language (XPath) 2.0</title>
<style type="text/css">
/*<![CDATA[*/
code { font-family: monospace; }
div.constraint,
div.issue,
div.note,
div.notice { margin-left: 2em; }
ol.enumar { list-style-type: decimal; }
ol.enumla { list-style-type: lower-alpha; }
ol.enumlr { list-style-type: lower-roman; }
ol.enumua { list-style-type: upper-alpha; }
ol.enumur { list-style-type: upper-roman; }
li p { margin-top: 0.3em;
margin-bottom: 0.3em; }
sup small { font-style: italic;
color: #8F8F8F;
}
div.exampleInner pre { margin-left: 1em;
margin-top: 0em; margin-bottom: 0em}
div.exampleOuter {border: 4px double gray;
margin: 0em; padding: 0em}
div.exampleInner { background-color: #d5dee3;
border-top-width: 4px;
border-top-style: double;
border-top-color: #d3d3d3;
border-bottom-width: 4px;
border-bottom-style: double;
border-bottom-color: #d3d3d3;
padding: 4px; margin: 0em }
div.exampleWrapper { margin: 4px }
div.exampleHeader { font-weight: bold;
margin: 4px}
table.small { font-size: x-small; }
/*]]>*/
</style>
<link type="text/css" rel="stylesheet" href="http://www.w3.org/StyleSheets/TR/W3C-WD.css" />
</head>
<body>
<div class="head">
<p><a href="http://www.w3.org/"><img width="72" height="48" alt="W3C" src="http://www.w3.org/Icons/w3c_home" /></a></p>
<h1><a id="title" name="title"></a>XML Path Language (XPath) 2.0</h1>
<h2><a id="w3c-doctype" name="w3c-doctype"></a>W3C Working Draft 23 July 2004</h2>
<dl>
<dt>This version:</dt>
<dd><span class="xpath"><a href="http://www.w3.org/TR/2004/WD-xpath20-20040723">http://www.w3.org/TR/2004/WD-xpath20-20040723</a></span></dd>
<dt>Latest version:</dt>
<dd><span class="xpath"><a href="http://www.w3.org/TR/xpath20/">http://www.w3.org/TR/xpath20/</a></span></dd>
<dt>Previous versions:</dt>
<dd><span class="xpath"><a href="http://www.w3.org/TR/2003/WD-xpath20-20031112/">http://www.w3.org/TR/2003/WD-xpath20-20031112/</a></span> <span class="xpath"><a href="http://www.w3.org/TR/2003/WD-xpath20-20030822/">http://www.w3.org/TR/2003/WD-xpath20-20030822/</a></span> <span class="xpath"><a href="http://www.w3.org/TR/2003/WD-xpath20-20030502/">http://www.w3.org/TR/2003/WD-xpath20-20030502/</a></span></dd>
<dt>Editors:</dt>
<dd class="xpath">Anders Berglund (XSL WG), IBM Research <a href="mailto:%20alrb@us.ibm.com"><alrb@us.ibm.com></a></dd>
<dd>Scott Boag (XSL WG), IBM Research <a href="mailto:%20scott_boag@us.ibm.com"><scott_boag@us.ibm.com></a></dd>
<dd>Don Chamberlin (XML Query WG), IBM Almaden Research Center <a href="mailto:%20chamberlin@almaden.ibm.com"><chamberlin@almaden.ibm.com></a></dd>
<dd>Mary F. Fernández (XML Query WG), AT&T Labs <a href="mailto:%20mff@research.att.com"><mff@research.att.com></a></dd>
<dd class="xpath">Michael Kay (XSL WG), Invited Expert, via <a href="http://www.saxonica.com/">http://www.saxonica.com/</a></dd>
<dd>Jonathan Robie (XML Query WG), DataDirect Technologies <a href="mailto:jonathan.robie@datadirect-technologies.com"><jonathan.robie@datadirect-technologies.com></a></dd>
<dd>Jérôme Siméon (XML Query WG), IBM T.J. Watson Research Center <a href="mailto:simeon@us.ibm.com"><simeon@us.ibm.com></a></dd>
</dl>
<p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a> © 2004 <a href="http://www.w3.org/"><acronym title="World Wide Web Consortium">W3C</acronym></a><sup>®</sup> (<a href="http://www.csail.mit.edu/"><acronym title="Massachusetts Institute of Technology">MIT</acronym></a>, <a href="http://www.ercim.org/"><acronym title="European Research Consortium for Informatics and Mathematics">ERCIM</acronym></a>, <a href="http://www.keio.ac.jp/">Keio</a>),
All Rights Reserved. W3C <a href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>, <a href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>, <a href="http://www.w3.org/Consortium/Legal/copyright-documents">document use</a> and <a href="http://www.w3.org/Consortium/Legal/copyright-software">software licensing</a> rules apply.</p>
</div>
<hr />
<div>
<h2><a id="abstract" name="abstract"></a>Abstract</h2>
<div class="xpath">
<p class="xpath">XPath 2.0 is an expression language that allows the processing of values conforming to the data model defined in <a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a>. The data model provides a tree representation of XML documents as well as atomic values such as integers, strings, and booleans, and sequences that may contain both references to nodes in an XML document and atomic values. The result of an XPath expression may be a selection of nodes from the input documents, or an
atomic value, or more generally, any sequence allowed by the data model. The name of the language derives from its most distinctive feature, the path expression, which provides a means of hierarchic addressing of the nodes in an XML tree. XPath 2.0 is a superset of <a href="#XPath">[XPath 1.0]</a>, with the added capability to support a richer set of data types, and to take advantage of the type information that becomes available when documents are validated using XML Schema. A backwards compatibility mode
is provided to ensure that nearly all XPath 1.0 expressions continue to deliver the same result with XPath 2.0; exceptions to this policy are noted in [<a href="#id-backwards-compatibility"><b>H Backwards Compatibility with XPath 1.0</b></a>].</p>
</div>
</div>
<div>
<h2><a id="status" name="status"></a>Status of this Document</h2>
<p><em>This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the <a href="http://www.w3.org/TR/">W3C technical reports index</a> at http://www.w3.org/TR/.</em></p>
<p>This is a public W3C Working Draft for review by W3C Members and other interested parties. Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.</p>
<p>XPath 2.0 has been defined jointly by the <a href="http://www.w3.org/XML/Query">XML Query Working Group</a> and the <a href="http://www.w3.org/Style/XSL/">XSL Working Group</a> (both part of the <a href="http://www.w3.org/XML/Activity.html">XML Activity</a>). The XPath 2.0 and XQuery 1.0 Working Drafts are generated from a common source. These languages are closely related, sharing much of the same expression syntax and semantics, and much of the text found in the two Working Drafts is identical.</p>
<p>This working draft includes a number of changes made in response to comments received during the Last Call period that ended on Feb. 15, 2004. The working group is continuing to process these comments, and additional changes are expected. A list of changes introduced by this draft can be found in <a href="#id-revisions-log"><b>J Revision Log</b></a>.</p>
<p>This document reflects decisions taken up to and including the face-to-face meeting in Cambridge, MA during the week of June 21, 2004. These decisions are recorded in the Last Call <span class="xpath"><span class="xpath"><a href="http://www.w3.org/2004/07/xpath-issues.html">issues list</a> (http://www.w3.org/2004/07/xpath-issues.html).</span></span> However, some of these decisions may not yet have been made in this document.</p>
<p>Public comments on this document and its open issues are invited. Comments should be sent to the W3C XPath/XQuery mailing list, <a href="mailto:public-qt-comments@w3.org">public-qt-comments@w3.org</a> (archived at <a href="http://lists.w3.org/Archives/Public/public-qt-comments/">http://lists.w3.org/Archives/Public/public-qt-comments/</a>), <span class="xpath"><span class="xpath">with “[XPath]”</span></span> at the beginning of the subject field.</p>
<p>The patent policy for this document is expected to become the <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February 2004 W3C Patent Policy</a>, pending the Advisory Committee review of the renewal of the XML Query Working Group. Patent disclosures relevant to this specification may be found on the <a href="http://www.w3.org/2002/08/xmlquery-IPR-statements">XML Query Working Group's patent disclosure page</a> and the <a href="http://www.w3.org/Style/XSL/Disclosures">XSL Working
Group's patent disclosure page</a>. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) with respect to this specification should disclose the information in accordance with <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section 6 of the W3C Patent Policy</a>.</p>
</div>
<div class="toc">
<h2><a id="contents" name="contents"></a>Table of Contents</h2>
<p class="toc">1 <a href="#id-introduction">Introduction</a><br />
2 <a href="#id-basics">Basics</a><br />
    2.1 <a href="#context">Expression Context</a><br />
        2.1.1 <a href="#static_context">Static Context</a><br />
        2.1.2 <a href="#eval_context">Dynamic Context</a><br />
    2.2 <a href="#id-processing-model">Processing Model</a><br />
        2.2.1 <a href="#id-data-model-generation">Data Model Generation</a><br />
        2.2.2 <a href="#id-schema-import-processing">Schema Import Processing</a><br />
        2.2.3 <a href="#id-expression-processing">Expression Processing</a><br />
            2.2.3.1 <a href="#id-static-analysis">Static Analysis Phase</a><br />
            2.2.3.2 <a href="#id-dynamic-evaluation">Dynamic Evaluation Phase</a><br />
        2.2.4 <a href="#id-serialization">Serialization</a><br />
        2.2.5 <a href="#id-consistency-constraints">Consistency Constraints</a><br />
    2.3 <a href="#id-important-concepts">Documents</a><br />
        2.3.1 <a href="#N1081F">Document Order</a><br />
        2.3.2 <a href="#id-atomization">Atomization</a><br />
        2.3.3 <a href="#id-ebv">Effective Boolean Value</a><br />
        2.3.4 <a href="#id-input-sources">Input Sources</a><br />
    2.4 <a href="#id-types">Types</a><br />
        2.4.1 <a href="#id-predefined-types">Predefined Types</a><br />
        2.4.2 <a href="#id-typed-value">Typed Value and String Value</a><br />
        2.4.3 <a href="#id-sequencetype-syntax">SequenceType Syntax</a><br />
        2.4.4 <a href="#id-sequencetype-matching">SequenceType Matching</a><br />
            2.4.4.1 <a href="#id-matching-value">Matching a SequenceType and a Value</a><br />
            2.4.4.2 <a href="#id-matching-item">Matching an ItemType and an Item</a><br />
            2.4.4.3 <a href="#id-element-test">Element Test</a><br />
            2.4.4.4 <a href="#id-schema-element-test">Schema Element Test</a><br />
            2.4.4.5 <a href="#id-attribute-test">Attribute Test</a><br />
            2.4.4.6 <a href="#id-schema-attribute-test">Schema Attribute Test</a><br />
    2.5 <a href="#errors">Error Handling</a><br />
        2.5.1 <a href="#id-kinds-of-errors">Kinds of Errors</a><br />
        2.5.2 <a href="#id-handling-dynamic">Handling Dynamic Errors</a><br />
        2.5.3 <a href="#id-errors-and-opt">Errors and Optimization</a><br />
    2.6 <a href="#id-optional-features">Optional Features</a><br />
    2.7 <a href="#comments">Comments</a><br />
3 <a href="#id-expressions">Expressions</a><br />
    3.1 <a href="#id-primary-expressions">Primary Expressions</a><br />
        3.1.1 <a href="#id-literals">Literals</a><br />
        3.1.2 <a href="#id-variables">Variable References</a><br />
        3.1.3 <a href="#id-paren-expressions">Parenthesized Expressions</a><br />
        3.1.4 <a href="#id-context-item-expression">Context Item Expression</a><br />
        3.1.5 <a href="#id-function-calls">Function Calls</a><br />
    3.2 <a href="#id-path-expressions">Path Expressions</a><br />
        3.2.1 <a href="#id-steps">Steps</a><br />
            3.2.1.1 <a href="#axes">Axes</a><br />
            3.2.1.2 <a href="#node-tests">Node Tests</a><br />
        3.2.2 <a href="#id-predicates">Predicates</a><br />
        3.2.3 <a href="#unabbrev">Unabbreviated Syntax</a><br />
        3.2.4 <a href="#abbrev">Abbreviated Syntax</a><br />
    3.3 <a href="#id-sequence-expressions">Sequence Expressions</a><br />
        3.3.1 <a href="#construct_seq">Constructing Sequences</a><br />
        3.3.2 <a href="#id-filter-expr">Filter Expressions</a><br />
        3.3.3 <a href="#combining_seq">Combining Node Sequences</a><br />
    3.4 <a href="#id-arithmetic">Arithmetic Expressions</a><br />
    3.5 <a href="#id-comparisons">Comparison Expressions</a><br />
        3.5.1 <a href="#id-value-comparisons">Value Comparisons</a><br />
        3.5.2 <a href="#id-general-comparisons">General Comparisons</a><br />
        3.5.3 <a href="#id-node-comparisons">Node Comparisons</a><br />
    3.6 <a href="#id-logical-expressions">Logical Expressions</a><br />
    3.7 <a href="#id-for-expressions">For Expressions</a><br />
    3.8 <a href="#id-conditionals">Conditional Expressions</a><br />
    3.9 <a href="#id-quantified-expressions">Quantified Expressions</a><br />
    3.10 <a href="#id-expressions-on-datatypes">Expressions on SequenceTypes</a><br />
        3.10.1 <a href="#id-instance-of">Instance Of</a><br />
        3.10.2 <a href="#id-cast">Cast</a><br />
        3.10.3 <a href="#id-castable">Castable</a><br />
        3.10.4 <a href="#id-constructor-functions">Constructor Functions</a><br />
        3.10.5 <a href="#id-treat">Treat</a><br /></p>
<h3><a id="appendices" name="appendices"></a>Appendices</h3>
<p class="toc">A <a href="#nt-bnf">XPath Grammar</a><br />
    A.1 <a href="#id-grammar">EBNF</a><br />
        A.1.1 <a href="#notes-on-parsing">Grammar Notes</a><br />
    A.2 <a href="#lexical-structure">Lexical structure</a><br />
        A.2.1 <a href="#N132F9">Terminal Types</a><br />
        A.2.2 <a href="#whitespace-rules">Whitespace Rules</a><br />
            A.2.2.1 <a href="#DefaultWhitespaceHandling">Default Whitespace Handling</a><br />
            A.2.2.2 <a href="#ExplicitWhitespaceHandling">ExplicitSpecial Whitespace Handling</a><br />
        A.2.3 <a href="#CommentsPragmasExtensions">Comments</a><br />
        A.2.4 <a href="#id-lexical-states">Lexical Rules</a><br />
    A.3 <a href="#id-reserved-fn-names">Reserved Function Names</a><br />
    A.4 <a href="#id-precedence-order">Precedence Order</a><br />
B <a href="#N13BF6">Type Promotion and Operator Mapping</a><br />
    B.1 <a href="#promotion">Type Promotion</a><br />
    B.2 <a href="#mapping">Operator Mapping</a><br />
C <a href="#id-xp-context-components">Context Components</a><br />
    C.1 <a href="#id-xp-static-context-components">Static Context Components</a><br />
    C.2 <a href="#id-xp-evaluation-context-components">Dynamic Context Components</a><br />
D <a href="#id-references">References</a><br />
    D.1 <a href="#id-normative-references">Normative References</a><br />
    D.2 <a href="#id-non-normative-references">Non-normative References</a><br />
    D.3 <a href="#id-informative-material">Non-normative Informative Material</a><br />
E <a href="#N14916">Conformance</a><br />
F <a href="#id-glossary">Glossary</a><br />
G <a href="#id-errors">Summary of Error Conditions</a> (Non-Normative)<br />
H <a href="#id-backwards-compatibility">Backwards Compatibility with XPath 1.0</a> (Non-Normative)<br />
    H.1 <a href="#N14A6B">Incompatibilities when Compatibility Mode is true</a><br />
    H.2 <a href="#N14B07">Incompatibilities when Compatibility Mode is false</a><br />
    H.3 <a href="#N14B38">Incompatibilities when using a Schema</a><br />
I <a href="#id-issues">XPath 2.0 and XQuery 1.0 Issues</a> (Non-Normative)<br />
J <a href="#id-revisions-log">Revision Log</a> (Non-Normative)<br />
    J.1 <a href="#N14B73">23 July 2004</a><br /></p>
</div>
<hr />
<div class="body">
<div class="div1">
<h2><a id="id-introduction" name="id-introduction"></a>1 Introduction</h2>
<div class="xpath">
<p class="xpath">The primary purpose of XPath is to address the nodes of <a href="#XML">[XML 1.0]</a> trees. XPath gets its name from its use of a path notation for navigating through the hierarchical structure of an XML document. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values.</p>
</div>
<p>[<a title="data model" id="dt-datamodel" name="dt-datamodel">Definition</a>: XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax. This logical structure is known as the <b>data model</b>, which is defined in the <a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a> document.]</p>
<div class="xpath">
<p class="xpath">XPath is designed to be embedded in a <b>host language</b> such as <a href="#XSLT">[XSLT 2.0]</a> or <a href="#xquery">[XQuery]</a>. XPath has a natural subset that can be used for matching (testing whether or not a node matches a pattern); this use of XPath is described in <a href="#XSLT">[XSLT 2.0]</a>.</p>
</div>
<p>XQuery Version 1.0 is an extension of XPath Version 2.0. Any expression that is syntactically valid and executes successfully in both XPath 2.0 and XQuery 1.0 will return the same result in both languages. Since these languages are so closely related, their grammars and language descriptions are generated from a common source to ensure consistency, and the editors of these specifications work together closely.</p>
<p>XPath also depends on and is closely related to the following specifications:</p>
<ul>
<li>
<p><a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a> defines the data model that underlies all XPath expressions.</p>
</li>
<li>
<p><a href="#XQueryFormalSemantics">[XQuery 1.0 and XPath 2.0 Formal Semantics]</a> defines the static semantics of XPath and also contains a formal but non-normative description of the dynamic semantics that may be useful for implementors and others who require a formal definition.</p>
</li>
<li>
<p>The type system of XPath is based on <a href="#XMLSchema">[XML Schema]</a>.</p>
</li>
<li>
<p>The default function library and the operators supported by XPath are defined in <a href="#FunctionsAndOperators">[XQuery 1.0 and XPath 2.0 Functions and Operators]</a>.</p>
</li>
</ul>
<p>This document specifies a grammar for XPath, using the same Basic EBNF notation used in <a href="#XML">[XML 1.0]</a>. Unless otherwise noted (see <a href="#lexical-structure"><b>A.2 Lexical structure</b></a>), whitespace is not significant in the grammar. Grammar productions are introduced together with the features that they describe, and a complete grammar is also presented in the appendix [<a href="#nt-bnf"><b>A XPath Grammar</b></a>]. The appendix is the normative version.</p>
<p>In the grammar productions in this document, nonterminal symbols are underlined and literal text is enclosed in double quotes. Certain productions (including the productions that define DecimalLiteral, DoubleLiteral, and StringLiteral) employ a regular-expression notation. The following example production describes the syntax of a function call:</p>
<table summary="Scrap" class="scrap">
<tbody>
<tr valign="baseline">
<td><a id="fakeid_doc-xpath-FunctionCall" name="fakeid_doc-xpath-FunctionCall"></a>[47]   </td>
<td><code>FunctionCall</code></td>
<td>   ::=   </td>
<td><code><a href="#prod-xpath-QName">QName</a> "(" (<a href="#doc-xpath-ExprSingle">ExprSingle</a> ("," <a href="#doc-xpath-ExprSingle">ExprSingle</a>)*)? ")"</code></td>
</tr>
</tbody>
</table>
<p>The production should be read as follows: A function call consists of a QName followed by an open-parenthesis. The open-parenthesis is followed by an optional argument list. The argument list (if present) consists of one or more expressions, separated by commas. The optional argument list is followed by a close-parenthesis.</p>
<p>Certain aspects of language processing are described in this specification as <b>implementation-defined</b> or <b>implementation-dependent</b>.</p>
<ul>
<li>
<p>[<a title="implementation defined" id="dt-implementation-defined" name="dt-implementation-defined">Definition</a>: <b>Implementation-defined</b> indicates an aspect that may differ between implementations, but must be specified by the implementor for each particular implementation.]</p>
</li>
<li>
<p>[<a title="implementation dependent" id="dt-implementation-dependent" name="dt-implementation-dependent">Definition</a>: <b>Implementation-dependent</b> indicates an aspect that may differ between implementations, is not specified by this or any W3C specification, and is not required to be specified by the implementor for any particular implementation.]</p>
</li>
</ul>
<div class="xpath">
<p class="xpath">A language aspect described in this specification as <b>implementation-defined</b> or <b>implementation dependent</b> may be further constrained by the specifications of a host language in which XPath is embedded.</p>
</div>
<p>This document normatively defines the dynamic semantics of XPath. The static semantics of XPath are normatively defined in <a href="#XQueryFormalSemantics">[XQuery 1.0 and XPath 2.0 Formal Semantics]</a>. In this document, examples and material labeled as "Note" are provided for explanatory purposes and are not normative.</p>
</div>
<div class="div1">
<h2><a id="id-basics" name="id-basics"></a>2 Basics</h2>
<p>The basic building block of XPath is the <b>expression</b>, which is a string of Unicode characters. This specification makes no assumptions or requirements regarding the character set encoding of strings of Unicode characters. The language provides several kinds of expressions which may be constructed from keywords, symbols, and operands. In general, the operands of an expression are other expressions. [<a title="functional langauge" id="dt-functional-language" name=
"dt-functional-language">Definition</a>: XPath is a <b>functional language</b>, which means that expressions can be nested with full generality. ] [<a title="strongly typed" id="dt-strongly-typed" name="dt-strongly-typed">Definition</a>: XPath is also a <b>strongly-typed language</b> in which the operands of various expressions, operators, and functions must conform to the expected types.]</p>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>This specification contains no assumptions or requirements regarding the character set encoding of strings of Unicode characters.</p>
</div>
<p>Like XML, XPath is a case-sensitive language. Keywords in XPath use lower-case characters and are not reserved—that is, names in XPath expressions are allowed to be the same as language keywords—except for the list of reserved function-names in <a href="#id-reserved-fn-names"><b>A.3 Reserved Function Names</b></a>.</p>
<p>The value of an expression is always a <a title="sequence" href="#dt-sequence">sequence</a>. [<a title="sequence" id="dt-sequence" name="dt-sequence">Definition</a>: A <b>sequence</b> is an ordered collection of zero or more <a title="item" href="#dt-item">items</a>.] [<a title="item" id="dt-item" name="dt-item">Definition</a>: An <b>item</b> is either an <a title="atomic value" href="#dt-atomic-value">atomic value</a> or a <a title="node" href="#dt-node">node</a>.] [<a title="atomic value" id=
"dt-atomic-value" name="dt-atomic-value">Definition</a>: An <b>atomic value</b> is a value in the value space of an <b>atomic type</b>, including all the atomic types defined in <a href="#XMLSchema">[XML Schema]</a> and <a title="xdt:untypedAtomic" href="#dt-untypedAtomic">xdt:untypedAtomic</a>.] [<a title="node" id="dt-node" name="dt-node">Definition</a>: A <b>node</b> is an instance of one of the <b>node kinds</b> defined in <a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a>.] Each node has a
unique <b>node identity</b>. Some kinds of nodes have typed values, string values, and names, which can be extracted from the node. The <b>typed value</b> of a node is a sequence of zero or more atomic values. The <b>string value</b> of a node is a value of type <code>xs:string</code>. The <b>name</b> of a node is a value of type <code>xs:QName</code>.</p>
<p>[<a title="singleton sequence" id="dt-singleton-sequence" name="dt-singleton-sequence">Definition</a>: A sequence containing exactly one item is called a <b>singleton sequence</b>.] An item is identical to a singleton sequence containing that item. Sequences are never nested—for example, combining the values 1, (2, 3), and ( ) into a single sequence results in the sequence (1, 2, 3). [<a title="empty sequence" id="dt-empty-sequence" name="dt-empty-sequence">Definition</a>: A sequence containing
zero items is called an <b>empty sequence</b>.]</p>
<p>Names in XPath are called <b>QNames</b>, and conform to the syntax in <a href="#XMLNAMES">[XML Names]</a>. [<a title="QName" id="dt-qname" name="dt-qname">Definition</a>: Lexically, a <b>QName</b> consists of an optional namespace prefix and a local name.] A lexical QName can be converted into an <b>expanded QName</b> by resolving its namespace prefix, using algorithms described later in this document. [<a title="expanded QName" id="dt-expanded-qname" name="dt-expanded-qname">Definition</a>: An
<b>expanded QName</b> consists of an optional namespace URI and a local name.] Two QNames are considered equal if their namespace URIs are equal and their local names are equal (even if the prefixes in their lexical forms are not equal). Namespace URIs and local names are compared on a codepoint basis, without normalization.</p>
<p>This document uses the following <span class="xpath"><span class="xpath">namespace prefixes (these prefix bindings are illustrative rather than normative):</span></span></p>
<ul>
<li>
<p><code>xs = http://www.w3.org/2001/XMLSchema</code></p>
</li>
<li>
<p><code>xsi = http://www.w3.org/2001/XMLSchema-instance</code></p>
</li>
<li>
<p><code>fn = http://www.w3.org/2004/07/xpath-functions</code></p>
</li>
<li>
<p><code>xdt = http://www.w3.org/2004/07/xpath-datatypes</code></p>
</li>
</ul>
<p>In some cases, where the meaning is clear and namespaces are not important to the discussion, built-in XML Schema typenames such as <code>integer</code> and <code>string</code> are used without a namespace prefix.</p>
<p>Element nodes have a property called <b>in-scope namespaces</b>. [<a title="in-scope namespaces" id="dt-in-scope-namespaces" name="dt-in-scope-namespaces">Definition</a>: The <b>in-scope namespaces</b> property of an element node is a set of <b>namespace bindings</b>, each of which associates a namespace prefix with a URI, thus defining the set of namespace prefixes that are available for interpreting QNames within the scope of the element. For a given element, one namespace binding may have an empty
prefix; the URI of this namespace binding is the default namespace within the scope of the element.]</p>
<div class="xpath">
<p class="xpath">In <a href="#XPath">[XPath 1.0]</a>, the in-scope namespaces of an element node are represented by a collection of <b>namespace nodes</b> arranged on a <b>namespace axis</b>. In XPath Version 2.0, the namespace axis is deprecated and need not be supported by a host language. A host language that does not support the namespace axis need not represent namespace bindings in the form of nodes.</p>
</div>
<div class="div2">
<h3><a id="context" name="context"></a>2.1 Expression Context</h3>
<p>[<a title="expression context" id="dt-expression-context" name="dt-expression-context">Definition</a>: The <b>expression context</b> for a given expression consists of all the information that can affect the result of the expression.] This information is organized into two categories called the <a title="static context" href="#dt-static-context">static context</a> and the <a title="dynamic context" href="#dt-dynamic-context">dynamic context</a>.</p>
<div class="div3">
<h4><a id="static_context" name="static_context"></a>2.1.1 Static Context</h4>
<p>[<a title="static context" id="dt-static-context" name="dt-static-context">Definition</a>: The <b>static context</b> of an expression is the information that is available during static analysis of the expression, prior to its evaluation.] This information can be used to decide whether the expression contains a <a title="static error" href="#dt-static-error">static error</a>. If analysis of an expression relies on some component of the <a title="static context" href="#dt-static-context">static
context</a> that has not been assigned a value, a <a title="static error" href="#dt-static-error">static error</a> is raised.[<a title="err:XP0001" href="#ERRXP0001">err:XP0001</a>]</p>
<p>The individual components of the <a title="static context" href="#dt-static-context">static context</a> are summarized below. Further rules governing the semantics of these components can be found in <a href="#id-xp-static-context-components"><b>C.1 Static Context Components</b></a>.</p>
<ul>
<li>
<p>[<a title="XPath 1.0 compatibility mode" id="dt-xpath-compat-mode" name="dt-xpath-compat-mode">Definition</a>: <b>XPath 1.0 compatibility mode.</b> <span class="xpath"><span class="xpath">This value is <code>true</code> if rules for backward compatibility with XPath Version 1.0 are in effect; otherwise it is <code>false</code>.</span></span>]</p>
</li>
<li>
<p>[<a title="statically known namespaces" id="dt-static-namespaces" name="dt-static-namespaces">Definition</a>: <b>Statically known namespaces.</b> This is a set of (prefix, URI) pairs that define all the namespaces that are known during static processing of a given expression.] Note the difference between <a title="in-scope namespaces" href="#dt-in-scope-namespaces">in-scope namespaces</a>, which is a dynamic property of an element node, and <a title="statically known namespaces" href=
"#dt-static-namespaces">statically known namespaces</a>, which is a static property of an expression.</p>
</li>
<li>
<p>[<a title="default element/type namespace" id="dt-def-elemtype-ns" name="dt-def-elemtype-ns">Definition</a>: <b>Default element/type namespace.</b> This is a namespace URI or "none". The namespace URI, if present, is used for any unprefixed QName appearing in a position where an element or type name is expected.] The initial default element/type namespace may be provided by the external environment.</p>
</li>
<li>
<p>[<a title="default function namespace" id="dt-def-fn-ns" name="dt-def-fn-ns">Definition</a>: <b>Default function namespace.</b> This is a namespace URI that is used for any unprefixed QName appearing as the function name in a function call. The initial default function namespace may be provided by the external environment.]</p>
</li>
<li>
<p>[<a title="in-scope schema definitions" id="dt-issd" name="dt-issd">Definition</a>: <b>In-scope schema definitions.</b> This is a generic term for all the element, attribute, and type definitions that are in scope during processing of an expression.] It includes the following three parts:</p>
<ul>
<li>
<p>[<a title="in-scope type definitions" id="dt-is-types" name="dt-is-types">Definition</a>: <b>In-scope type definitions.</b> Each named type definition is identified either by an <a title="expanded QName" href="#dt-expanded-qname">expanded QName</a> (for a <b>named type</b>) or by an <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a> type identifier (for an <b>anonymous type</b>). The in-scope type definitions include the predefined types described in
<a href="#id-predefined-types"><b>2.4.1 Predefined Types</b></a>. ]</p>
</li>
<li>
<p>[<a title="in-scope element declarations" id="dt-is-elems" name="dt-is-elems">Definition</a>: <b>In-scope element declarations.</b> Each element declaration is identified either by an <a title="expanded QName" href="#dt-expanded-qname">expanded QName</a> (for a top-level element declaration) or by an <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a> element identifier (for a local element declaration). ] An element declaration includes information about
the element's <a title="substitution group" href="#dt-substitution-group">substitution group</a> affiliation.</p>
<p>[<a title="substitution group" id="dt-substitution-group" name="dt-substitution-group">Definition</a>: <b>Substitution groups</b> are defined in <a href="#XMLSchema">[XML Schema]</a> Part 1, Section 2.2.2.2. Informally, the substitution group headed by a given element (called the <b>head element</b>) consists of the set of elements that can be substituted for the head element without affecting the outcome of schema validation.]</p>
</li>
<li>
<p>[<a title="in-scope attribute declarations" id="dt-is-attrs" name="dt-is-attrs">Definition</a>: <b>In-scope attribute declarations.</b> Each attribute declaration is identified either by an <a title="expanded QName" href="#dt-expanded-qname">expanded QName</a> (for a top-level attribute declaration) or by an <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a> attribute identifier (for a local attribute declaration). ]</p>
</li>
</ul>
</li>
<li>
<p>[<a title="in-scope variables" id="dt-in-scope-variables" name="dt-in-scope-variables">Definition</a>: <b>In-scope variables.</b> This is a set of (expanded QName, type) pairs. It defines the set of variables that are available for reference within an expression. The <a title="expanded QName" href="#dt-expanded-qname">expanded QName</a> is the name of the variable, and the type is the <a title="static type" href="#dt-static-type">static type</a> of the variable.]</p>
<p>An expression that binds a variable (such as a <code>for</code>, <code>some</code>, or <code>every</code> expression) extends the <a title="in-scope variables" href="#dt-in-scope-variables">in-scope variables</a> of its subexpressions with the new bound variable and its type.</p>
</li>
<li>
<p><b>Context item static type.</b> This component defines the static type of the context item within the scope of a given expression.</p>
</li>
<li>
<p>[<a title="function signature" id="dt-function-signature" name="dt-function-signature">Definition</a>: <b>Function signatures.</b> This component defines the set of functions that are available to be called from within an expression. Each function is uniquely identified by its <a title="expanded QName" href="#dt-expanded-qname">expanded QName</a> and its arity (number of parameters).] In addition to the name and arity, each function signature specifies the <a title="static type" href=
"#dt-static-type">static types</a> of the function parameters and result.</p>
<p>The <a title="function signature" href="#dt-function-signature">function signatures</a> include the signatures of constructor functions, which are discussed in <a href="#id-constructor-functions"><b>3.10.4 Constructor Functions</b></a>.</p>
</li>
<li>
<p>[<a title="statically known collations" id="dt-static-collations" name="dt-static-collations">Definition</a>: <b>Statically known collations.</b> This is a set of (URI, collation) pairs. It defines the names of the collations that are available for use in function calls that take a collation name as an argument.] A collation may be regarded as an object that supports two functions: a function that given a set of strings, returns a sequence containing those strings in sorted order; and a function that
given two strings, returns true if they are considered equal, and false if not.</p>
</li>
<li>
<p>[<a title="default collation" id="dt-def-collation" name="dt-def-collation">Definition</a>: <b>Default collation.</b> This identifies one of the collations in <a title="statically known collations" href="#dt-static-collations">statically known collations</a> as the collation to be used by string comparison functions and operators when no explicit collation is specified.]</p>
</li>
<li>
<p>[<a title="base URI" id="dt-base-uri" name="dt-base-uri">Definition</a>: <b>Base URI.</b> This is an absolute URI, used when necessary in the resolution of relative URIs (for example, by the <code>fn:resolve-uri</code> function.)]</p>
</li>
<li>
<p>[<a title="statically known documents" id="dt-known-docs" name="dt-known-docs">Definition</a>: <b>Statically known documents.</b> This is a mapping from strings onto types. The string represents the absolute URI of a resource that is potentially available using the <code>fn:doc</code> function. The type is the static type of a call to <code>fn:doc</code> with the given URI as its literal argument. ] If the argument to <code>fn:doc</code> is not a string literal that is present in statically known
documents, then the <a title="static type" href="#dt-static-type">static type</a> of <code>fn:doc</code> is <code>document-node()?</code>.</p>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>The purpose of the <b>statically known documents</b> is to provide static type information, not to determine which documents are available. A URI need not be found in the <b>statically known documents</b> to be accessed using <code>fn:doc</code>.</p>
</div>
</li>
<li>
<p>[<a title="statically known collections" id="dt-known-collections" name="dt-known-collections">Definition</a>: <b>Statically known collections.</b> This is a mapping from strings onto types. The string represents the absolute URI of a resource that is potentially available using the <code>fn:collection</code> function. The type is the type of the sequence of nodes that would result from calling the <code>fn:collection</code> function with this URI as its argument.] If the argument to
<code>fn:collection</code> is not a string literal that is present in statically known collections, then the <a title="static type" href="#dt-static-type">static type</a> of <code>fn:collection</code> is <code>node()*</code>.</p>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>The purpose of the <b>statically known collections</b> is to provide static type information, not to determine which collections are available. A URI need not be found in the <b>statically known collections</b> to be accessed using <code>fn:collection</code>.</p>
</div>
</li>
</ul>
</div>
<div class="div3">
<h4><a id="eval_context" name="eval_context"></a>2.1.2 Dynamic Context</h4>
<p>[<a title="dynamic context" id="dt-dynamic-context" name="dt-dynamic-context">Definition</a>: The <b>dynamic context</b> of an expression is defined as information that is available at the time the expression is evaluated.] If evaluation of an expression relies on some part of the <a title="dynamic context" href="#dt-dynamic-context">dynamic context</a> that has not been assigned a value, a <a title="dynamic error" href="#dt-dynamic-error">dynamic error</a> is raised.[<a title="err:XP0002" href=
"#ERRXP0002">err:XP0002</a>]</p>
<p>The individual components of the <a title="dynamic context" href="#dt-dynamic-context">dynamic context</a> are summarized below. Further rules governing the semantics of these components can be found in <a href="#id-xp-evaluation-context-components"><b>C.2 Dynamic Context Components</b></a>.</p>
<p>The <a title="dynamic context" href="#dt-dynamic-context">dynamic context</a> consists of all the components of the <a title="static context" href="#dt-static-context">static context</a>, and the additional components listed below.</p>
<p>[<a title="focus" id="dt-focus" name="dt-focus">Definition</a>: The first three components of the <a title="dynamic context" href="#dt-dynamic-context">dynamic context</a> (context item, context position, and context size) are called the <b>focus</b> of the expression. ] The focus enables the processor to keep track of which nodes are being processed by the expression.</p>
<p>Certain language constructs, notably the path expression <code>E1/E2</code> and the filter expression <code>E1[E2]</code>, create a new focus for the evaluation of a sub-expression. In these constructs, <code>E2</code> is evaluated once for each item in the sequence that results from evaluating <code>E1</code>. Each time <code>E2</code> is evaluated, it is evaluated with a different focus. The focus for evaluating <code>E2</code> is referred to below as the <b>inner focus</b>, while the focus for
evaluating <code>E1</code> is referred to as the <b>outer focus</b>. The inner focus exists only while <code>E2</code> is being evaluated. When this evaluation is complete, evaluation of the containing expression continues with its original focus unchanged.</p>
<ul>
<li>
<p>[<a title="context item" id="dt-context-item" name="dt-context-item">Definition</a>: The <b>context item</b> is the item currently being processed. An item is either an atomic value or a node.][<a title="context node" id="dt-context-node" name="dt-context-node">Definition</a>: When the context item is a node, it can also be referred to as the <b>context node</b>.] The context item is returned by the expression "<code>.</code>". When an expression <code>E1/E2</code> or <code>E1[E2]</code> is evaluated,
each item in the sequence obtained by evaluating <code>E1</code> becomes the context item in the inner focus for an evaluation of <code>E2</code>.</p>
</li>
<li>
<p>[<a title="context position" id="dt-context-position" name="dt-context-position">Definition</a>: The <b>context position</b> is the position of the context item within the sequence of items currently being processed.] It changes whenever the context item changes. Its value is always an integer greater than zero. The context position is returned by the expression <code>fn:position()</code>. When an expression <code>E1/E2</code> or <code>E1[E2]</code> is evaluated, the context position in the inner focus
for an evaluation of <code>E2</code> is the position of the context item in the sequence obtained by evaluating <code>E1</code>. The position of the first item in a sequence is always 1 (one). The context position is always less than or equal to the context size.</p>
</li>
<li>
<p>[<a title="context size" id="dt-context-size" name="dt-context-size">Definition</a>: The <b>context size</b> is the number of items in the sequence of items currently being processed.] Its value is always an integer greater than zero. The context size is returned by the expression <code>fn:last()</code>. When an expression <code>E1/E2</code> or <code>E1[E2]</code> is evaluated, the context size in the inner focus for an evaluation of <code>E2</code> is the number of items in the sequence obtained by
evaluating <code>E1</code>.</p>
</li>
<li>
<p>[<a title="variable values" id="dt-variable-values" name="dt-variable-values">Definition</a>: <b>Variable values</b>. This is a set of (expanded QName, value) pairs. It contains the same <a title="expanded QName" href="#dt-expanded-qname">expanded QNames</a> as the <a title="in-scope variables" href="#dt-in-scope-variables">in-scope variables</a> in the <a title="static context" href="#dt-static-context">static context</a> for the expression. The QName is the name of the variable and the value is the
dynamic value of the variable.]</p>
</li>
<li>
<p>[<a title="function implementation" id="dt-function-implementation" name="dt-function-implementation">Definition</a>: <b>Function implementations</b>. Each function in <a title="function signature" href="#dt-function-signature">function signatures</a> has a function implementation that enables the function to map instances of its parameter types into an instance of its result type. ]</p>
</li>
<li>
<p>[<a title="current date and time" id="dt-date-time" name="dt-date-time">Definition</a>: <b>Current date and time.</b> This information represents an <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a> point in time during processing of a query or transformation. It can be retrieved by the <code>fn:current-date</code>, <code>fn:current-time</code>, and <code>fn:current-dateTime</code> functions. If invoked multiple times during the execution of a query or
transformation, these functions always return the same result.]</p>
</li>
<li>
<p>[<a title="implicit timezone" id="dt-timezone" name="dt-timezone">Definition</a>: <b>Implicit timezone.</b> This is the timezone to be used when a date, time, or dateTime value that does not have a timezone is used in a comparison or in any other operation. This value is an instance of <code>xdt:dayTimeDuration</code> that is <span class="xpath"><span class="xpath">determined by the host language</span></span>. See <a href="#ISO8601">[ISO 8601]</a> for the range of legal values of a timezone.]</p>
</li>
<li>
<p>[<a title="available documents" id="dt-available-docs" name="dt-available-docs">Definition</a>: <b>Available documents.</b> This is a mapping of strings onto document nodes. The string represents the absolute URI of a resource. The document node is the root of a tree that represents that resource using the <a title="data model" href="#dt-datamodel">data model</a>. The document node is returned by the <code>fn:doc</code> function when applied to that URI.] The set of available documents is not
constrained by the set of <a title="statically known documents" href="#dt-known-docs">statically known documents</a>, and it may be empty.</p>
</li>
<li>
<p>[<a title="available collections" id="dt-available-collections" name="dt-available-collections">Definition</a>: <b>Available collections.</b> This is a mapping of strings onto sequences of nodes. The string represents the absolute URI of a resource. The sequence of nodes represents the result of the <code>fn:collection</code> function when that URI is supplied as the argument. ] The set of available collections is not constrained by the set of <a title="statically known collections" href=
"#dt-known-collections">statically known collections</a>, and it may be empty.</p>
</li>
</ul>
</div>
</div>
<div class="div2">
<h3><a id="id-processing-model" name="id-processing-model"></a>2.2 Processing Model</h3>
<p>XPath is defined in terms of the <a title="data model" href="#dt-datamodel">data model</a> and in terms of the <a title="expression context" href="#dt-expression-context">expression context</a>.</p>
<img src="ProcMod-XPath.gif" alt="Processing Model Overview" />
<p>Figure 1: Processing Model Overview</p>
<p>Figure 1 provides a schematic overview of the processing steps that are discussed in detail below. Some of these steps are completely outside the domain of XPath; in Figure 1, these are depicted outside the line that represents the boundaries of the language, an area labeled the <b>external processing domain</b>. The external processing domain includes generation of the <a title="data model" href="#dt-datamodel">data model</a> (see <a href="#id-data-model-generation"><b>2.2.1 Data Model
Generation</b></a>), schema import processing (see <a href="#id-schema-import-processing"><b>2.2.2 Schema Import Processing</b></a>) and serialization (see <a href="#id-serialization"><b>2.2.4 Serialization</b></a>). The area inside the boundaries of the language is known as the <span class="xpath"><span class="xpath"><b>XPath processing domain</b></span></span>, which includes the static analysis and dynamic evaluation phases (see <a href="#id-expression-processing"><b>2.2.3 Expression
Processing</b></a>). Consistency constraints on the <span class="xpath"><span class="xpath">XPath</span></span> processing domain are defined in <a href="#id-consistency-constraints"><b>2.2.5 Consistency Constraints</b></a>.</p>
<div class="div3">
<h4><a id="id-data-model-generation" name="id-data-model-generation"></a>2.2.1 Data Model Generation</h4>
<p>Before an expression can be processed, the input documents to be accessed by the expression must be represented in the <a title="data model" href="#dt-datamodel">data model</a>. This process occurs outside the domain of XPath, which is why Figure 1 represents it in the external processing domain. Here are some steps by which an XML document might be converted to the <a title="data model" href="#dt-datamodel">data model</a>:</p>
<ol class="enumar">
<li>
<p>A document may be parsed using an XML parser that generates an <b>XML Information Set</b> (see <a href="#XINFO">[XML Infoset]</a>). The parsed document may then be validated against one or more schemas. This process, which is described in <a href="#XMLSchema">[XML Schema]</a>, results in an abstract information structure called the <b>Post-Schema Validation Infoset</b> (PSVI). If a document has no associated schema, its Information Set is preserved. (See DM1 in Fig. 1.)</p>
</li>
<li>
<p>The Information Set or PSVI may be transformed into the <a title="data model" href="#dt-datamodel">data model</a> by a process described in <a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a>. (See DM2 in Fig. 1.)</p>
</li>
</ol>
<p>The above steps provide an example of how a document in the <a title="data model" href="#dt-datamodel">data model</a> might be constructed. A document or fragment might also be synthesized directly from a relational database, or constructed in some other way (see DM3 in Fig. 1.) XPath is defined in terms of operations on the <a title="data model" href="#dt-datamodel">data model</a>, but it does not place any constraints on how documents and instances in the <a title="data model" href=
"#dt-datamodel">data model</a> are constructed.</p>
<p>Each atomic value, element node, and attribute node in the <a title="data model" href="#dt-datamodel">data model</a> is annotated with its <a title="dynamic type" href="#dt-dynamic-type">dynamic type</a>. The dynamic type specifies a range of values—for example, an attribute named <code>version</code> might have the dynamic type <code>xs:decimal</code>, indicating that it contains a decimal value. For example, if the <a title="data model" href="#dt-datamodel">data model</a> was derived from an
input XML document, the dynamic types of the elements and attributes are derived from schema validation.</p>
<p>The value of an attribute is represented directly within the attribute node. An attribute node whose type is unknown (such as might occur in a schemaless document) is annotated with the dynamic type <code>xdt:untypedAtomic</code>.</p>
<p>The value of an element is represented by the children of the element node, which may include text nodes and other element nodes. The dynamic type of an element node indicates how the values in its child text nodes are to be interpreted. An element that has not been validated (such as might occur in a schemaless document) is annotated with the type <code>xdt:untyped</code>. An element that has been validated and found to be partially valid is annotated with the type <code>xs:anyType</code>. If an
element node is annotated <code>xdt:untyped</code>, all its descendant element nodes are also annotated <code>xdt:untyped</code>. However, if an element node is annotated <code>xs:anyType</code>, some of its descendant element nodes may have a more specific type annotation.</p>
<p>An atomic value of unknown type is annotated with the type <code>xdt:untypedAtomic</code>.</p>
</div>
<div class="div3">
<h4><a id="id-schema-import-processing" name="id-schema-import-processing"></a>2.2.2 Schema Import Processing</h4>
<div class="xpath">
<p class="xpath">The <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a> in the <a title="static context" href="#dt-static-context">static context</a> are provided by the host language (see step SI1 in Figure 1) and must satisfy the consistency constraints defined in <a href="#id-consistency-constraints"><b>2.2.5 Consistency Constraints</b></a>.</p>
</div>
</div>
<div class="div3">
<h4><a id="id-expression-processing" name="id-expression-processing"></a>2.2.3 Expression Processing</h4>
<p>XPath defines two phases of processing called the <a title="static analysis phase" href="#dt-static-analysis">static analysis phase</a> and the <a title="dynamic evaluation phase" href="#dt-dynamic-evaluation">dynamic evaluation phase</a> (see Fig. 1). An implementation is free to use any strategy or algorithm whose result conforms to these specifications.</p>
<div class="div4">
<h5><a id="id-static-analysis" name="id-static-analysis"></a>2.2.3.1 Static Analysis Phase</h5>
<p>[<a title="static analysis phase" id="dt-static-analysis" name="dt-static-analysis">Definition</a>: The <b>static analysis phase</b> depends on the expression itself and on the <a title="static context" href="#dt-static-context">static context</a>. The <b>static analysis phase</b> does not depend on input data (other than schemas).]</p>
<p>During the static analysis phase, the <span class="xpath"><span class="xpath">XPath expression</span></span> is parsed into an internal representation called the <b>operation tree</b> (step SQ1 in Figure 1). A parse error is raised as a <a title="static error" href="#dt-static-error">static error</a>.[<a title="err:XP0003" href="#ERRXP0003">err:XP0003</a>] The <a title="static context" href="#dt-static-context">static context</a> is initialized by the implementation (step SQ2). The <a title=
"static context" href="#dt-static-context">static context</a> is used to resolve type names, function names, namespace prefixes and variable names (step SQ4). If a name in the <b>operation tree</b> is not found in the <a title="static context" href="#dt-static-context">static context</a>, a <a title="static error" href="#dt-static-error">static error</a> [<a title="err:XP0008" href="#ERRXP0008">err:XP0008</a>] is raised (however, see exceptions to this rule in <a href="#id-element-test"><b>2.4.4.3 Element
Test</b></a> and <a href="#id-attribute-test"><b>2.4.4.5 Attribute Test</b></a>.</p>
<p>The <b>operation tree</b> is then <b>normalized</b> by making explicit the implicit operations such as <a title="atomization" href="#dt-atomization">atomization</a>, <a title="type promotion" href="#dt-type-promotion">type promotion</a>, and extraction of <a title="effective boolean value" href="#dt-ebv">Effective Boolean Values</a> (step SQ5). The normalization process is described in <a href="#XQueryFormalSemantics">[XQuery 1.0 and XPath 2.0 Formal Semantics]</a>.</p>
<p>If the <a title="Static Typing Feature" href="#dt-static-typing-feature">Static Typing Feature</a> is supported, each expression is assigned a <a title="static type" href="#dt-static-type">static type</a> (step SQ6). [<a title="static type" id="dt-static-type" name="dt-static-type">Definition</a>: The <b>static type</b> of an expression may be either a named type or a structural description—for example, <code>xs:boolean?</code> denotes an optional occurrence of the <code>xs:boolean</code> type.
The rules for inferring the <a title="static type" href="#dt-static-type">static types</a> of various expressions are described in <a href="#XQueryFormalSemantics">[XQuery 1.0 and XPath 2.0 Formal Semantics]</a>.] In some cases, the <a title="static type" href="#dt-static-type">static type</a> is derived from the lexical form of the expression; for example, the <a title="static type" href="#dt-static-type">static type</a> of the literal <code>5</code> is <code>xs:integer</code>. In other cases, the
<a title="static type" href="#dt-static-type">static type</a> of an expression is inferred according to rules based on the static types of its operands; for example, the <a title="static type" href="#dt-static-type">static type</a> of the expression <code>5 + 1.2</code> is <code>xs:decimal</code>.</p>
<p>During the <a title="static analysis phase" href="#dt-static-analysis">static analysis phase</a>, if the <a title="Static Typing Feature" href="#dt-static-typing-feature">Static Typing Feature</a> is in effect and an operand of an expression is found to have a <a title="static type" href="#dt-static-type">static type</a> that is not appropriate for that operand, a <a title="type error" href="#dt-type-error">type error</a> is raised.[<a title="err:XP0004" href="#ERRXP0004">err:XP0004</a>] If static type
checking raises no errors and assigns a <a title="static type" href="#dt-static-type">static type</a> T to an expression, then execution of the expression on valid input data is guaranteed either to produce a value of type T or to raise a <a title="dynamic error" href="#dt-dynamic-error">dynamic error</a>.</p>
<p>During the <a title="static analysis phase" href="#dt-static-analysis">static analysis phase</a>, if the <a title="Static Typing Feature" href="#dt-static-typing-feature">Static Typing Feature</a> is in effect and the <a title="static type" href="#dt-static-type">static type</a> assigned to an expression other than <code>()</code> or <code>data(())</code> is <code>empty()</code>, a <a title="static error" href="#dt-static-error">static error</a> is raised.[<a title="err:XP0005" href=
"#ERRXP0005">err:XP0005</a>] This catches cases in which a query refers to an element or attribute that is not present in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a>, possibly because of a spelling error.</p>
<p>The purpose of type-checking during the <a title="static analysis phase" href="#dt-static-analysis">static analysis phase</a> is to provide early detection of <a title="type error" href="#dt-type-error">type errors</a> and to infer type information that may be useful in optimizing the evaluation of an expression.</p>
</div>
<div class="div4">
<h5><a id="id-dynamic-evaluation" name="id-dynamic-evaluation"></a>2.2.3.2 Dynamic Evaluation Phase</h5>
<p>[<a title="dynamic evaluation phase" id="dt-dynamic-evaluation" name="dt-dynamic-evaluation">Definition</a>: The <b>dynamic evaluation phase</b> occurs after completion of the <a title="static analysis phase" href="#dt-static-analysis">static analysis phase</a>. During the dynamic evaluation phase, the value of the <span class="xpath"><span class="xpath">expression</span></span> is computed.]</p>
<p>The dynamic evaluation phase can occur only if no errors were detected during the <a title="static analysis phase" href="#dt-static-analysis">static analysis phase</a>. If the <a title="Static Typing Feature" href="#dt-static-typing-feature">Static Typing Feature</a> is in effect, all <a title="type error" href="#dt-type-error">type errors</a> are detected during static analysis and serve to inhibit the dynamic evaluation phase. If the <a title="Static Typing Feature" href=
"#dt-static-typing-feature">Static Typing Feature</a> is not in effect, an implementation is allowed to raise type-related warnings during the <a title="static analysis phase" href="#dt-static-analysis">static analysis phase</a>. It may then proceed with the dynamic evaluation phase; in this case, <a title="type error" href="#dt-type-error">type errors</a> must be detected and raised during dynamic evaluation.</p>
<p>The dynamic evaluation phase depends on the <b>operation tree</b> of the expression being evaluated (step DQ1), on the input data (step DQ4), and on the <a title="dynamic context" href="#dt-dynamic-context">dynamic context</a> (step DQ5), which in turn draws information from the external environment (step DQ3) and the <a title="static context" href="#dt-static-context">static context</a> (step DQ2). Execution of the evaluation phase may create new data-model values (step DQ4) and it may extend the
<a title="dynamic context" href="#dt-dynamic-context">dynamic context</a> (step DQ5)—for example, by binding values to variables.</p>
<p>[<a title="dynamic type" id="dt-dynamic-type" name="dt-dynamic-type">Definition</a>: A <b>dynamic type</b> is associated with each value as it is computed. The dynamic type of a value may be either a structural description (such as "sequence of integers") or a named type.] The dynamic type of a value may be more specific than the <a title="static type" href="#dt-static-type">static type</a> of the expression that computed it (for example, the <a title="static type" href="#dt-static-type">static type</a>
of an expression might be "zero or more integers or strings," but at evaluation time its value may have the dynamic type "integer.")</p>
<p>If an operand of an expression is found to have a <a title="dynamic type" href="#dt-dynamic-type">dynamic type</a> that is not appropriate for that operand, a <a title="type error" href="#dt-type-error">type error</a> is raised.[<a title="err:XP0006" href="#ERRXP0006">err:XP0006</a>]</p>
<p>Even though static typing can catch many <a title="type error" href="#dt-type-error">type errors</a> before an expression is executed, it is possible for an expression to raise an error during evaluation that was not detected by static analysis. For example, an expression may contain a cast of a string into an integer, which is statically valid. However, if the actual value of the string at run time cannot be cast into an integer, a <a title="dynamic error" href="#dt-dynamic-error">dynamic error</a>
will result. Similarly, an expression may apply an arithmetic operator to a value whose <a title="static type" href="#dt-static-type">static type</a> is <code>xdt:untypedAtomic</code>. This is not a <a title="static error" href="#dt-static-error">static error</a>, but at run time, if the value cannot be successfully cast to a numeric type, a <a title="dynamic error" href="#dt-dynamic-error">dynamic error</a> will be raised.</p>
<p>When the <a title="Static Typing Feature" href="#dt-static-typing-feature">Static Typing Feature</a> is in effect, it is also possible for static analysis of an expression to raise a <a title="type error" href="#dt-type-error">type error</a>, even though execution of the expression on certain inputs would be successful. For example, an expression might contain a function that requires an element as its parameter, and the static analysis phase might infer the <a title="static type" href=
"#dt-static-type">static type</a> of the function parameter to be an optional element. This case is treated as a <a title="type error" href="#dt-type-error">type error</a> and inhibits evaluation, even though the function call would have been successful for input data in which the optional element is present.</p>
</div>
</div>
<div class="div3">
<h4><a id="id-serialization" name="id-serialization"></a>2.2.4 Serialization</h4>
<p>[<a title="serialization" id="dt-serialization" name="dt-serialization">Definition</a>: <b>Serialization</b> is the process of converting a sequence of nodes and atomic values from the <a title="data model" href="#dt-datamodel">data model</a> into a sequence of octets (step DM4 in Figure 1.) ] The general framework for serialization of the <a title="data model" href="#dt-datamodel">data model</a> is described in <a href="#serialization">[XSLT 2.0 and XQuery 1.0 Serialization]</a>.</p>
<div class="xpath">
<p class="xpath">The host language may provide a serialization option based on this framework.</p>
</div>
</div>
<div class="div3">
<h4><a id="id-consistency-constraints" name="id-consistency-constraints"></a>2.2.5 Consistency Constraints</h4>
<p>In order for XPath to be well defined, the <a title="data model" href="#dt-datamodel">data model</a>, the <a title="static context" href="#dt-static-context">static context</a>, and the <a title="dynamic context" href="#dt-dynamic-context">dynamic context</a> must be mutually consistent. The consistency constraints listed below are prerequisites for correct functioning of an XPath implementation. Enforcement of these consistency constraints is beyond the scope of this specification. This specification
does not define the result of <span class="xpath"><span class="xpath">an expression</span></span> under any condition in which one or more of these constraints is not satisfied.</p>
<p>Some of the consistency constraints use the term <b>data model schema</b>. [<a title="data model schema" id="dt-data-model-schema" name="dt-data-model-schema">Definition</a>: For a given node in the <a title="data model" href="#dt-datamodel">data model</a>, the <b>data model schema</b> is defined as the schema from which the type annotation of that node was derived.] For a node that was constructed by some process other than schema validation, the <b>data model schema</b> consists simply of the type
definition that is represented by the type annotation of the node.</p>
<ul>
<li>
<p>For every data model node that has a type annotation, if that type annotation is found in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a> (ISSD), then its definition in the ISSD must be the same as its definition in the <a title="data model schema" href="#dt-data-model-schema">data model schema</a>. Furthermore, all types that are derived by extension from the given type in the <a title="data model schema" href="#dt-data-model-schema">data model schema</a> must
also be known by equivalent definitions in the ISSD.</p>
</li>
<li>
<p>For every element name <em>EN</em> that is found both in a data model node and in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a> (ISSD), all elements that are known in the <a title="data model schema" href="#dt-data-model-schema">data model schema</a> to be in the <a title="substitution group" href="#dt-substitution-group">substitution group</a> headed by <em>EN</em> must also be known in the ISSD to be in the <a title="substitution group" href=
"#dt-substitution-group">substitution group</a> headed by <em>EN</em>.</p>
</li>
<li>
<p>Every item type (i.e., every element, attribute, or type name) referenced in <a title="in-scope variables" href="#dt-in-scope-variables">in-scope variables</a> or <a title="function signature" href="#dt-function-signature">function signatures</a> must be in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a>.</p>
</li>
<li>
<p>For each mapping of a string to a document node in <a title="available documents" href="#dt-available-docs">available documents</a>, if there exists a mapping of the same string to a document type in <a title="statically known documents" href="#dt-known-docs">statically known documents</a>, the document node must match the document type, using the matching rules in <a href="#id-sequencetype-matching"><b>2.4.4 SequenceType Matching</b></a>.</p>
</li>
<li>
<p>For each mapping of a string to a sequence of nodes in <a title="available collections" href="#dt-available-collections">available collections</a>, if there exists a mapping of the same string to a type in <a title="statically known collections" href="#dt-known-collections">statically known collections</a>, the sequence of nodes must match the type, using the matching rules in <a href="#id-sequencetype-matching"><b>2.4.4 SequenceType Matching</b></a>.</p>
</li>
<li>
<p>For each (variable, type) pair in <a title="in-scope variables" href="#dt-in-scope-variables">in-scope variables</a> and the corresponding (variable, value) pair in <a title="variable values" href="#dt-variable-values">variable values</a> such that the variable names are equal, the value must match the type, using the matching rules in <a href="#id-sequencetype-matching"><b>2.4.4 SequenceType Matching</b></a>.</p>
</li>
</ul>
</div>
</div>
<div class="div2">
<h3><a id="id-important-concepts" name="id-important-concepts"></a>2.3 Documents</h3>
<p>XPath is generally used to process <b>documents</b>. The representation of a document is normatively defined in <a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a>. The functions used to access documents and collections are normatively defined in <a href="#FunctionsAndOperators">[XQuery 1.0 and XPath 2.0 Functions and Operators]</a>. Because documents are centrally important in XPath processing, we provide a summary of some key concepts here.</p>
<div class="div3">
<h4><a id="N1081F" name="N1081F"></a>2.3.1 Document Order</h4>
<p>An ordering called <b>document order</b> is defined among all the nodes used during a given query or transformation, which may consist of one or more <b>trees</b> (documents or fragments). Document order is defined in <a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a>, and its definition is repeated here for convenience. [<a title="reverse document order" id="dt-reverse-document-order" name="dt-reverse-document-order">Definition</a>: The node ordering that is the reverse of document order is
called <b>reverse document order</b>.]</p>
<p>Document order is a total ordering, although the relative order of some nodes is <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a>. [<a title="document order" id="dt-document-order" name="dt-document-order">Definition</a>: Informally, <b>document order</b> is the order defined by a pre-order, depth-first traversal of the nodes in the data model.] Document order is <b>stable</b>, which means that the relative order of two nodes will not change during the
processing of a given query or transformation, even if this order is <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a>.</p>
<p>Within a tree, document order satisfies the following constraints:</p>
<ol class="enumar">
<li>
<p>The root node is the first node.</p>
</li>
<li>
<p>The relative order of siblings is determined by their order in the XML representation of the tree. A node N1 occurs before a node N2 in document order if and only if the start of N1 occurs before the start of N2 in the XML representation.</p>
</li>
<li class="xpath">
<p>Namespace nodes immediately follow the element node with which they are associated. The relative order of namespace nodes is stable but <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a>.</p>
</li>
<li>
<p>Attribute nodes immediately follow the <span class="xpath"><span class="xpath">namespace nodes of the</span></span> element node with which they are associated. The relative order of attribute nodes is stable but <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a>.</p>
</li>
<li>
<p>Element nodes occur before their children; children occur before following-siblings.</p>
</li>
</ol>
<p>The relative order of nodes in distinct trees is stable but <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a>, subject to the following constraint: If any node in tree T1 is before any node in tree T2, then all nodes in tree T1 are before all nodes in tree T2.</p>
</div>
<div class="div3">
<h4><a id="id-atomization" name="id-atomization"></a>2.3.2 Atomization</h4>
<p>The semantics of some XPath operators depend on a process called <a title="atomization" href="#dt-atomization">atomization</a>. [<a title="atomization" id="dt-atomization" name="dt-atomization">Definition</a>: <b>Atomization</b> is applied to a value when the value is used in a context in which a sequence of atomic values is required. The result of atomization is either a sequence of atomic values or a <a title="type error" href="#dt-type-error">type error</a>. Atomization of a sequence is defined as
the result of invoking the <code>fn:data</code> function on the sequence, as defined in <a href="#FunctionsAndOperators">[XQuery 1.0 and XPath 2.0 Functions and Operators]</a>.]</p>
<p>The semantics of <code>fn:data</code> are repeated here for convenience. The result of <code>fn:data</code> is the sequence of atomic values produced by applying the following rules to each item in the input sequence:</p>
<ul>
<li>
<p>If the item is an atomic value, it is returned.</p>
</li>
<li>
<p>If the item is a node, its typed value is returned.</p>
</li>
</ul>
<p>Atomization is used in processing the following types of expressions:</p>
<ul>
<li>
<p>Arithmetic expressions</p>
</li>
<li>
<p>Comparison expressions</p>
</li>
<li>
<p>Function calls and returns</p>
</li>
<li>
<p>Cast expressions</p>
</li>
</ul>
</div>
<div class="div3">
<h4><a id="id-ebv" name="id-ebv"></a>2.3.3 Effective Boolean Value</h4>
<p>Under certain circumstances (listed below), it is necessary to find the <a title="effective boolean value" href="#dt-ebv">effective boolean value</a> of a value. [<a title="effective boolean value" id="dt-ebv" name="dt-ebv">Definition</a>: The <b>effective boolean value</b> of a value is defined as the result of applying the <code>fn:boolean</code> function to the value, as defined in <a href="#FunctionsAndOperators">[XQuery 1.0 and XPath 2.0 Functions and Operators]</a>.]</p>
<p>The semantics of <code>fn:boolean</code> are repeated here for convenience. <code>fn:boolean</code> returns <code>false</code> if its operand is any of the following:</p>
<ul>
<li>
<p>An empty sequence</p>
</li>
<li>
<p>The boolean value <code>false</code></p>
</li>
<li>
<p>A zero-length value of type <code>xs:string</code> or <code>xdt:untypedAtomic</code></p>
</li>
<li>
<p>A numeric value that is equal to zero</p>
</li>
<li>
<p>The <code>xs:double</code> or <code>xs:float</code> value <code>NaN</code></p>
</li>
</ul>
<p>Otherwise, <code>fn:boolean</code> returns <code>true</code>.</p>
<p>The <a title="effective boolean value" href="#dt-ebv">effective boolean value</a> of a sequence is computed implicitly during processing of the following types of expressions:</p>
<ul>
<li>
<p>Logical expressions (<code>and</code>, <code>or</code>)</p>
</li>
<li>
<p>The <code>fn:not</code> function</p>
</li>
<li>
<p>Certain types of predicates, such as <code>a[b]</code></p>
</li>
<li>
<p>Conditional expressions (<code>if</code>)</p>
</li>
<li>
<p>Quantified expressions (<code>some</code>, <code>every</code>)</p>
</li>
</ul>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>Note that the definition of <a title="effective boolean value" href="#dt-ebv">effective boolean value</a> is not used when casting a value to the type <code>xs:boolean</code>.</p>
</div>
</div>
<div class="div3">
<h4><a id="id-input-sources" name="id-input-sources"></a>2.3.4 Input Sources</h4>
<p>XPath has a set of functions that provide access to input data. These functions are of particular importance because they provide a way in which an expression can reference a document or a collection of documents. The input functions are described informally here; they are defined in <a href="#FunctionsAndOperators">[XQuery 1.0 and XPath 2.0 Functions and Operators]</a>.</p>
<p>An expression can access input data either by calling one of the input functions or by referencing some part of the expression context that is initialized by the external environment, such as a <b>variable</b> or a <a title="context item" href="#dt-context-item">context item</a>.</p>
<p>The input functions supported by XPath are as follows:</p>
<ul>
<li>
<p>The <code>fn:doc</code> function takes a string containing a URI that refers to an XML document, and returns a document node whose content is the <a title="data model" href="#dt-datamodel">data model</a> representation of the given document.</p>
</li>
<li>
<p>The <code>fn:collection</code> function takes a string containing a URI, and returns the data model representation of the collection identified by the URI. A collection may be any sequence of nodes. For example, the expression <code>fn:collection("http://example.org")//customer</code> identifies all the <code>customer</code> elements that are descendants of nodes found in the collection whose URI is <code>http://example.org</code>.</p>
</li>
</ul>
<p>If a given input function is invoked repeatedly with arguments that resolve to the same absolute URI during the scope of a single query or transformation, each invocation returns the same result.</p>
</div>
</div>
<div class="div2">
<h3><a id="id-types" name="id-types"></a>2.4 Types</h3>
<p>XPath is a strongly typed language with a type system based on <a href="#XMLSchema">[XML Schema]</a>. The XPath type system is formally defined in <a href="#XQueryFormalSemantics">[XQuery 1.0 and XPath 2.0 Formal Semantics]</a>.</p>
<div class="div3">
<h4><a id="id-predefined-types" name="id-predefined-types"></a>2.4.1 Predefined Types</h4>
<div class="xpath">
<p class="xpath">The <a title="in-scope type definitions" href="#dt-is-types">in-scope type definitions</a> in the <a title="static context" href="#dt-static-context">static context</a> are initialized with a set of predefined types that is determined by the host language. This set may include some or all of the types defined by <a href="#XMLSchema">[XML Schema]</a> in the namespace <code>http://www.w3.org/2001/XMLSchema</code>, represented in this document by the namespace prefix <code>xs</code>. It may
also include the types defined in the namespace <code>http://www.w3.org/2004/07/xpath-datatypes</code>, represented in this document by the namespace prefix <code>xdt</code>. The types in this namespace are defined in <a href="#FunctionsAndOperators">[XQuery 1.0 and XPath 2.0 Functions and Operators]</a> and are summarized below.</p>
</div>
<ol class="enumar">
<li>
<p>[<a title="xdt:untyped" id="dt-untyped" name="dt-untyped">Definition</a>: <code>xdt:untyped</code> is used to denote the dynamic type of an element node that has not been validated, or has been validated in <code>skip</code> mode.] It has no subtypes.</p>
</li>
<li>
<p>[<a title="xdt:untypedAtomic" id="dt-untypedAtomic" name="dt-untypedAtomic">Definition</a>: <code>xdt:untypedAtomic</code> is used to denote untyped atomic data, such as text that has not been assigned a more specific type.] It has no subtypes. An attribute that has been validated in <code>skip</code> mode, or that has a PSVI property of <code>xs:anySimpleType</code>, is represented in the Data Model by an attribute node with the type <code>xdt:untypedAtomic</code>.</p>
</li>
<li>
<p>[<a title="xdt:dayTimeDuration" id="dt-dayTimeDuration" name="dt-dayTimeDuration">Definition</a>: <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code> whose lexical representation is restricted to contain only day, hour, minute, and second components.]</p>
</li>
<li>
<p>[<a title="xdt:yearMonthDuration" id="dt-yearMonthDuration" name="dt-yearMonthDuration">Definition</a>: <code>xdt:yearMonthDuration</code> is a subtype of <code>xs:duration</code> whose lexical representation is restricted to contain only year and month components.]</p>
</li>
<li>
<p>[<a title="xdt:anyAtomicType" id="dt-anyAtomicType" name="dt-anyAtomicType">Definition</a>: <code>xdt:anyAtomicType</code> includes all atomic values (and no values that are not atomic).] It is a subtype of <code>xs:anySimpleType</code>, which is the base type for all simple types, including atomic, list, and union types. All specific atomic types such as <code>xs:integer</code>, <code>xs:string</code>, and <code>xdt:untypedAtomic</code>, are subtypes of <code>xdt:anyAtomicType</code>.</p>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p><code>xdt:anyAtomicType</code> will not appear as the type of an actual value in the Data Model.</p>
</div>
</li>
</ol>
<p>The relationships among the types in the <code>xs</code> and <code>xdt</code> namespaces are illustrated in Figure 2. A more complete description of the XPath type hierarchy can be found in <a href="#FunctionsAndOperators">[XQuery 1.0 and XPath 2.0 Functions and Operators]</a>.</p>
<img src="types.jpg" alt="Type Hierarchy Diagram" />
<p>Figure 2: Summary of XPath Type Hierarchy</p>
</div>
<div class="div3">
<h4><a id="id-typed-value" name="id-typed-value"></a>2.4.2 Typed Value and String Value</h4>
<p>In the <a title="data model" href="#dt-datamodel">data model</a>, every node has a <b>typed value</b> and a <b>string value</b>. The <b>typed value</b> of a node is a sequence of atomic values and can be extracted by applying the <code>fn:data</code> function to the node. The typed value for each kind of node is defined by the <code>dm:typed-value</code> accessor in <a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a>. The <b>string value</b> of a node is a string and can be extracted by
applying the <code>fn:string</code> function to the node. The string value for each kind of node is defined by the <code>dm:string-value</code> accessor in <a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a>. Element and attribute nodes have a <b>type annotation</b>, which represents (in an <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a> way) the <a title="dynamic type" href="#dt-dynamic-type">dynamic (run-time) type</a> of the node. In the
<a href="#datamodel">[XQuery 1.0 and XPath 2.0 Data Model]</a>, type annotation is defined by the <code>dm:type-name</code> accessor; however, XPath does not provide a way to directly access the type annotation of an element or attribute node.</p>
<p>The relationship between the typed value and the string value for various kinds of nodes is described and illustrated by examples below.</p>
<ol class="enumar">
<li>
<p>For text and document nodes, the typed value of the node is the same as its string value, as an instance of the type <code>xdt:untypedAtomic</code>. (The string value of a document node is formed by concatenating the string values of all its descendant text nodes, in <a title="document order" href="#dt-document-order">document order</a>.)</p>
</li>
<li>
<p>The typed value of a comment<span class="xpath"><span class="xpath">, namespace,</span></span> or processing instruction node is the same as its string value. It is an instance of the type <code>xs:string</code>.</p>
</li>
<li>
<p>The typed value of an attribute node with the type annotation <code>xdt:untypedAtomic</code> is the same as its string value, as an instance of <code>xdt:untypedAtomic</code>. The typed value of an attribute node with any other type annotation is derived from its string value and type annotation using the lexical-to-value-space mapping defined in <a href="#XMLSchema">[XML Schema]</a> Part 2 for the relevant type.</p>
<p>Example: A1 is an attribute having string value <code>"3.14E-2"</code> and type annotation <code>xs:double</code>. The typed value of A1 is the <code>xs:double</code> value whose lexical representation is <code>3.14E-2</code>.</p>
<p>Example: A2 is an attribute with type annotation <code>xs:IDREFS</code>, which is a list datatype derived from the atomic datatype <code>xs:IDREF</code>. Its string value is "<code>bar baz faz</code>". The typed value of A2 is a sequence of three atomic values ("<code>bar</code>", "<code>baz</code>", "<code>faz</code>"), each of type <code>xs:IDREF</code>. The typed value of a node is never treated as an instance of a named list type. Instead, if the type annotation of a node is a list type (such as
<code>xs:IDREFS</code>), its typed value is treated as a sequence of the atomic type from which it is derived (such as <code>xs:IDREF</code>).</p>
</li>
<li>
<p>For an element node, the relationship between typed value and string value depends on the node's type annotation, as follows:</p>
<ol class="enumla">
<li>
<p>If the type annotation is <code>xdt:untyped</code> or denotes a complex type with mixed content (including <code>xs:anyType</code>), then the typed value of the node is equal to its string value, as an instance of <code>xdt:untypedAtomic</code>.</p>
<p>Example: E1 is an element node having type annotation <code>xdt:untyped</code> and string value "<code>1999-05-31</code>". The typed value of E1 is "<code>1999-05-31</code>", as an instance of <code>xdt:untypedAtomic</code>.</p>
<p>Example: E2 is an element node with the type annotation <code>formula</code>, which is a complex type with mixed content. The content of E2 consists of the character "<code>H</code>", a child element named <code>subscript</code> with string value "<code>2</code>", and the character "<code>O</code>". The typed value of E2 is "<code>H2O</code>" as an instance of <code>xdt:untypedAtomic</code>.</p>
</li>
<li>
<p>If the type annotation denotes a simple type or a complex type with simple content, then the typed value of the node is derived from its string value and its type annotation in a way that is consistent with schema validation.</p>
<p>Example: E3 is an element node with the type annotation <code>cost</code>, which is a complex type that has several attributes and a simple content type of <code>xs:decimal</code>. The string value of E3 is "<code>74.95</code>". The typed value of E3 is <code>74.95</code>, as an instance of <code>xs:decimal</code>.</p>
<p>Example: E4 is an element node with the type annotation <code>hatsizelist</code>, which is a simple type derived from the atomic type <code>hatsize</code>, which in turn is derived from <code>xs:integer</code>. The string value of E4 is "<code>7 8 9</code>". The typed value of E4 is a sequence of three values (<code>7</code>, <code>8</code>, <code>9</code>), each of type <code>hatsize</code>.</p>
</li>
<li>
<p>If the type annotation denotes a complex type with empty content, then the typed value of the node is the empty sequence and its string value is the zero-length string.</p>
</li>
<li>
<p>If the type annotation denotes a complex type with element-only content, then the typed value of the node is undefined. The <code>fn:data</code> function raises a <a title="type error" href="#dt-type-error">type error</a> [<a title="err:XP0007" href="#ERRXP0007">err:XP0007</a>] when applied to such a node.</p>
<p>Example: E5 is an element node with the type annotation <code>weather</code>, which is a complex type whose content type specifies <code>element-only</code>. E5 has two child elements named <code>temperature</code> and <code>precipitation</code>. The typed value of E5 is undefined, and the <code>fn:data</code> function applied to E5 raises an error.</p>
</li>
</ol>
</li>
</ol>
</div>
<div class="div3">
<h4><a id="id-sequencetype-syntax" name="id-sequencetype-syntax"></a>2.4.3 SequenceType Syntax</h4>
<p>[<a title="SequenceType" id="dt-sequence-type" name="dt-sequence-type">Definition</a>: When it is necessary to refer to a type in an XPath expression, the <a href="#doc-xpath-SequenceType">SequenceType</a> syntax is used. The name <a href="#doc-xpath-SequenceType">SequenceType</a> suggests that this syntax is used to describe the type of an XPath value, which is always a sequence.]</p>
<table summary="Scrap" class="scrap">
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-SequenceType" name="doc-xpath-SequenceType"></a>[49]   </td>
<td><code><a href="#prod-xpath-SequenceType">SequenceType</a></code></td>
<td>   ::=   </td>
<td><code>(<a href="#doc-xpath-ItemType">ItemType</a> <a href="#doc-xpath-OccurrenceIndicator">OccurrenceIndicator</a>?)<br />
| ("empty" "(" ")")</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-ItemType" name="doc-xpath-ItemType"></a>[51]   </td>
<td><code><a href="#prod-xpath-ItemType">ItemType</a></code></td>
<td>   ::=   </td>
<td><code><a href="#doc-xpath-AtomicType">AtomicType</a> | <a href="#doc-xpath-KindTest">KindTest</a> | ("item" "(" ")")</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-OccurrenceIndicator" name="doc-xpath-OccurrenceIndicator"></a>[50]   </td>
<td><code><a href="#prod-xpath-OccurrenceIndicator">OccurrenceIndicator</a></code></td>
<td>   ::=   </td>
<td><code>"?" | "*" | "+"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-AtomicType" name="doc-xpath-AtomicType"></a>[52]   </td>
<td><code><a href="#prod-xpath-AtomicType">AtomicType</a></code></td>
<td>   ::=   </td>
<td><code><a href="#prod-xpath-QName">QName</a></code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-KindTest" name="doc-xpath-KindTest"></a>[53]   </td>
<td><code><a href="#prod-xpath-KindTest">KindTest</a></code></td>
<td>   ::=   </td>
<td><code><a href="#doc-xpath-DocumentTest">DocumentTest</a><br />
| <a href="#doc-xpath-ElementTest">ElementTest</a><br />
| <a href="#doc-xpath-AttributeTest">AttributeTest</a><br />
| <a href="#doc-xpath-SchemaElementTest">SchemaElementTest</a><br />
| <a href="#doc-xpath-SchemaAttributeTest">SchemaAttributeTest</a><br />
| <a href="#doc-xpath-PITest">PITest</a><br />
| <a href="#doc-xpath-CommentTest">CommentTest</a><br />
| <a href="#doc-xpath-TextTest">TextTest</a><br />
| <a href="#doc-xpath-AnyKindTest">AnyKindTest</a></code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-DocumentTest" name="doc-xpath-DocumentTest"></a>[55]   </td>
<td><code><a href="#prod-xpath-DocumentTest">DocumentTest</a></code></td>
<td>   ::=   </td>
<td><code>"document-node" "(" (<a href="#doc-xpath-ElementTest">ElementTest</a> | <a href="#doc-xpath-SchemaElementTest">SchemaElementTest</a>)? ")"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-ElementTest" name="doc-xpath-ElementTest"></a>[63]   </td>
<td><code><a href="#prod-xpath-ElementTest">ElementTest</a></code></td>
<td>   ::=   </td>
<td><code>"element" "(" (<a href="#doc-xpath-ElementNameOrWildcard">ElementNameOrWildcard</a> ("," <a href="#doc-xpath-TypeName">TypeName</a> "?"?)?)? ")"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-SchemaElementTest" name="doc-xpath-SchemaElementTest"></a>[65]   </td>
<td><code><a href="#prod-xpath-SchemaElementTest">SchemaElementTest</a></code></td>
<td>   ::=   </td>
<td><code>"schema-element" "(" <a href="#doc-xpath-ElementDeclaration">ElementDeclaration</a> ")"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-ElementDeclaration" name="doc-xpath-ElementDeclaration"></a>[66]   </td>
<td><code><a href="#prod-xpath-ElementDeclaration">ElementDeclaration</a></code></td>
<td>   ::=   </td>
<td><code><a href="#doc-xpath-ElementName">ElementName</a></code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-AttributeTest" name="doc-xpath-AttributeTest"></a>[59]   </td>
<td><code><a href="#prod-xpath-AttributeTest">AttributeTest</a></code></td>
<td>   ::=   </td>
<td><code>"attribute" "(" (<a href="#doc-xpath-AttribNameOrWildcard">AttribNameOrWildcard</a> ("," <a href="#doc-xpath-TypeName">TypeName</a>)?)? ")"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-SchemaAttributeTest" name="doc-xpath-SchemaAttributeTest"></a>[61]   </td>
<td><code><a href="#prod-xpath-SchemaAttributeTest">SchemaAttributeTest</a></code></td>
<td>   ::=   </td>
<td><code>"schema-attribute" "(" <a href="#doc-xpath-AttributeDeclaration">AttributeDeclaration</a> ")"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-AttributeDeclaration" name="doc-xpath-AttributeDeclaration"></a>[62]   </td>
<td><code><a href="#prod-xpath-AttributeDeclaration">AttributeDeclaration</a></code></td>
<td>   ::=   </td>
<td><code><a href="#doc-xpath-AttributeName">AttributeName</a></code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-ElementNameOrWildcard" name="doc-xpath-ElementNameOrWildcard"></a>[64]   </td>
<td><code><a href="#prod-xpath-ElementNameOrWildcard">ElementNameOrWildcard</a></code></td>
<td>   ::=   </td>
<td><code><a href="#doc-xpath-ElementName">ElementName</a> | "*"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-ElementName" name="doc-xpath-ElementName"></a>[68]   </td>
<td><code><a href="#prod-xpath-ElementName">ElementName</a></code></td>
<td>   ::=   </td>
<td><code><a href="#prod-xpath-QName">QName</a></code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-AttribNameOrWildcard" name="doc-xpath-AttribNameOrWildcard"></a>[60]   </td>
<td><code><a href="#prod-xpath-AttribNameOrWildcard">AttribNameOrWildcard</a></code></td>
<td>   ::=   </td>
<td><code><a href="#doc-xpath-AttributeName">AttributeName</a> | "*"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-AttributeName" name="doc-xpath-AttributeName"></a>[67]   </td>
<td><code><a href="#prod-xpath-AttributeName">AttributeName</a></code></td>
<td>   ::=   </td>
<td><code><a href="#prod-xpath-QName">QName</a></code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-TypeName" name="doc-xpath-TypeName"></a>[69]   </td>
<td><code><a href="#prod-xpath-TypeName">TypeName</a></code></td>
<td>   ::=   </td>
<td><code><a href="#prod-xpath-QName">QName</a></code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-PITest" name="doc-xpath-PITest"></a>[58]   </td>
<td><code><a href="#prod-xpath-PITest">PITest</a></code></td>
<td>   ::=   </td>
<td><code>"processing-instruction" "(" (<a href="#prod-xpath-NCName">NCName</a> | <a href="#doc-xpath-StringLiteral">StringLiteral</a>)? ")"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-CommentTest" name="doc-xpath-CommentTest"></a>[57]   </td>
<td><code><a href="#prod-xpath-CommentTest">CommentTest</a></code></td>
<td>   ::=   </td>
<td><code>"comment" "(" ")"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-TextTest" name="doc-xpath-TextTest"></a>[56]   </td>
<td><code><a href="#prod-xpath-TextTest">TextTest</a></code></td>
<td>   ::=   </td>
<td><code>"text" "(" ")"</code></td>
</tr>
</tbody>
<tbody>
<tr valign="baseline">
<td><a id="doc-xpath-AnyKindTest" name="doc-xpath-AnyKindTest"></a>[54]   </td>
<td><code><a href="#prod-xpath-AnyKindTest">AnyKindTest</a></code></td>
<td>   ::=   </td>
<td><code>"node" "(" ")"</code></td>
</tr>
</tbody>
</table>
<p>Here are some examples of <a href="#doc-xpath-SequenceType">SequenceTypes</a> that might be used in XPath expressions:</p>
<ul>
<li>
<p><code>xs:date</code> refers to the built-in atomic Schema type named <code>xs:date</code></p>
</li>
<li>
<p><code>attribute()?</code> refers to an optional attribute</p>
</li>
<li>
<p><code>element()</code> refers to any element</p>
</li>
<li>
<p><code>element(po:shipto, po:address)</code> refers to an element that has the name <code>po:shipto</code> and has the type annotation <code>po:address</code> (or a type derived from <code>po:address</code>)</p>
</li>
<li>
<p><code>element(*, po:address)</code> refers to an element of any name that has the type annotation <code>po:address</code> (or a type derived from <code>po:address</code>)</p>
</li>
<li>
<p><code>element(customer)</code> refers to an element named <code>customer</code> of any type</p>
</li>
<li>
<p><code>schema-element(customer)</code> refers to an element named <code>customer</code> whose type annotation matches the type declared for a <code>customer</code> element in the <a title="in-scope element declarations" href="#dt-is-elems">in-scope element declarations</a></p>
</li>
<li>
<p><code>node()*</code> refers to a sequence of zero or more nodes of any type</p>
</li>
<li>
<p><code>item()+</code> refers to a sequence of one or more nodes or atomic values</p>
</li>
</ul>
</div>
<div class="div3">
<h4><a id="id-sequencetype-matching" name="id-sequencetype-matching"></a>2.4.4 SequenceType Matching</h4>
<p>[<a title="SequenceType matching" id="dt-sequencetype-matching" name="dt-sequencetype-matching">Definition</a>: During evaluation of an expression, it is sometimes necessary to determine whether a value with a known type "matches" an expected type, expressed in the <a href="#doc-xpath-SequenceType">SequenceType</a> syntax. This process is known as <b>SequenceType matching</b>.] For example, an <code>instance of</code> expression returns <code>true</code> if the actual type of a given value matches a
given type, or <code>false</code> if it does not.</p>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>In this specification, the word "type", when used without modification, represents a type that can be expressed using the <a href="#doc-xpath-SequenceType">SequenceType</a> syntax. When we refer specifically to XML Schema simple or complex types, appropriate modifiers are used to make this clear.</p>
</div>
<p>QNames appearing in a <a href="#doc-xpath-SequenceType">SequenceType</a> have their prefixes expanded to namespace URIs by means of the <a title="statically known namespaces" href="#dt-static-namespaces">statically known namespaces</a> and the <a title="default element/type namespace" href="#dt-def-elemtype-ns">default element/type namespace</a>. As usual, two QNames are considered to be equal if their local parts are the same and their namespace URI's are the same.</p>
<p>[<a title="subtype substitution" id="dt-subtype-substitution" name="dt-subtype-substitution">Definition</a>: The use of a value whose actual type is derived from the expected type is known as <b>subtype substitution</b>.] Subtype substitution does not change the actual type of a value. For example, if an <code>xs:integer</code> value is used where an <code>xs:decimal</code> value is expected, the value retains its type as <code>xs:integer</code>.</p>
<p>The rules for <a title="SequenceType matching" href="#dt-sequencetype-matching">SequenceType matching</a> compare the actual type of a value with an expected type. These rules are a subset of the formal rules that match a value with an expected type defined in <a href="#XQueryFormalSemantics">[XQuery 1.0 and XPath 2.0 Formal Semantics]</a>, because the Formal Semantics must be able to match a value with any XML Schema type, whereas the rules below only match values against those types expressible by the
<a href="#doc-xpath-SequenceType">SequenceType</a> syntax.</p>
<p>Some of the rules for <a title="SequenceType matching" href="#dt-sequencetype-matching">SequenceType matching</a> require determining whether a given type name is the same as or derived from an expected type name. The given type name may be "known" (defined in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a>), or "unknown" (not defined in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a>). An unknown type name might be
encountered, for example, if a source document has been validated using a schema that was not imported into the <a title="static context" href="#dt-static-context">static context</a>. In this case, an implementation is allowed (but is not required) to provide an <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a> mechanism for determining whether the unknown type name is derived from the expected type name. For example, an implementation might maintain a
data dictionary containing information about type hierarchies.</p>
<p>The definition of <a title="SequenceType matching" href="#dt-sequencetype-matching">SequenceType matching</a> relies on a pseudo-function named <code>derives-from(</code><em>AT, ET</em><code>)</code>, which takes an an actual simple or complex type name <em>AT</em> and an expected simple or complex type name <em>ET</em>, and either returns a boolean value or raises a type error. [<a title="err:XP0004" href="#ERRXP0004">err:XP0004</a>][<a title="err:XP0006" href="#ERRXP0006">err:XP0006</a>] The
pseudo-function <code>derives-from</code> is defined below and is defined formally in <a href="#XQueryFormalSemantics">[XQuery 1.0 and XPath 2.0 Formal Semantics]</a>.</p>
<ul>
<li>
<p><code>derives-from(</code><em>AT</em>, <em>ET</em><code>)</code> returns <code>true</code> if any of the following three conditions is true:</p>
<ol class="enumar">
<li>
<p><em>AT</em> is a type name found in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a>, and is the same as <em>ET</em> or is derived by restriction or extension from <em>ET</em></p>
</li>
<li>
<p><em>AT</em> is a type name not found in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a>, and an <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a> mechanism is able to determine that <em>AT</em> is derived by restriction from <em>ET</em></p>
</li>
<li>
<p>There exists some type name <em>IT</em> such that <code>derives-from(</code><em>IT, ET</em><code>)</code> and <code>derives-from(</code><em>AT, IT</em><code>)</code> are true.</p>
</li>
</ol>
</li>
<li>
<p><code>derives-from(</code><em>AT</em>, <em>ET</em><code>)</code> returns <code>false</code> if either the first and third or the second and third of the following conditions are true:</p>
<ol class="enumar">
<li>
<p><em>AT</em> is a type name found in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a>, and is not the same as <em>ET</em>, and is not is derived by restriction or extension from <em>ET</em></p>
</li>
<li>
<p><em>AT</em> is a type name not found in the <a title="in-scope schema definitions" href="#dt-issd">in-scope schema definitions</a>, and an <a title="implementation dependent" href="#dt-implementation-dependent">implementation-dependent</a> mechanism is able to determine that <em>AT</em> is not derived by restriction from <em>ET</em></p>
</li>
<li>
<p>No type name <em>IT</em> exists such that <code>derives-from(</code><em>IT, ET</em><code>)</code> and <code>derives-from(</code><em>AT, IT</em><code>)</code> are true.</p>
</li>
</ol>
</li>
<li>
<p><code>derives-from(</code><em>AT</em>, <em>ET</em><code>)</code> raises a <a title="type error" href="#dt-type-error">type error</a> [<a title="err:XP0004" href="#ERRXP0004">err:XP0004</a>][<a title="err:XP0006" href="#ERRXP0006">err:XP0006</a>] if:</p>
<ol class="enumar">
<li>
<p><em>ET</em> is an unknown type, or</p>
</li>
<li>
<p><em>AT</em> is an unknown type, and the implementation is not able to determine whether <em>AT</em> is derived by restriction from <em>ET</em>.</p>
</li>
</ol>
</li>
</ul>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>The <code>derives-from</code> pseudo-function cannot be written as a real XQuery function, because types are not valid function parameters.</p>
</div>
<p>The rules for <a title="SequenceType matching" href="#dt-sequencetype-matching">SequenceType matching</a> are given below, with examples (the examples are for purposes of illustration, and do not cover all possible cases).</p>
<div class="div4">
<h5><a id="id-matching-value" name="id-matching-value"></a>2.4.4.1 Matching a SequenceType and a Value</h5>
<ul>
<li>
<p>The SequenceType <code>empty()</code> matches a value that is the empty sequence.</p>
</li>
<li>