<?xml version="1.0" encoding="utf-8"?><!--{xml-fix} XSLT Processor: Saxonica--><!--This document was created at (not provided)--><html xmlns="http://www.w3.org/1999/xhtml" lang="EN"><head><title>XML Path Language (XPath) 4.0 WG Review Draft</title><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/><link rel="stylesheet" type="text/css" href="css/w3c-base.css"/><link rel="stylesheet" href="css/qtspecs.css"/><link rel="stylesheet" href="css/xpath-40.css"/></head><body><div class="head"><p><a href="https://www.w3.org/"><img src="https://www.w3.org/StyleSheets/TR/2016/logos/W3C" alt="W3C" height="48" width="72"/></a></p>
<h1><a id="title"/>XML Path Language (XPath) 4.0 WG Review Draft</h1>
<h2><a id="w3c-doctype"/>W3C Editor's Draft 23 May 2025</h2><dl><dt>This version:</dt><dd><a href="https://qt4cg.org/pr/2019/xpath-40//">https://qt4cg.org/pr/2019/xpath-40/</a></dd><dt>Most recent version of XPath:</dt><dd>
         <a href="https://qt4cg.org/specifications/xpath-40/">https://qt4cg.org/specifications/xpath-40/</a>
      </dd><dt>Most recent Recommendation of XPath:</dt><dd>
         <a href="https://www.w3.org/TR/2017/REC-xpath-31-20170321/">https://www.w3.org/TR/2017/REC-xpath-31-20170321/</a>
      </dd><dt>Editor:</dt><dd>Michael Kay, Saxonica <a href="mailto:mike@saxonica.com">&lt;mike@saxonica.com&gt;</a></dd></dl><p>Please check the <a href="https://www.w3.org/XML/2017/qt-errata/xquery-31-errata.html"><strong>errata</strong></a> for any errors or issues reported since publication.</p><p>See also <a href="http://www.w3.org/2003/03/Translations/byTechnology?technology=xquery-31"><strong>translations</strong></a>.</p><p>This document is also available in these non-normative formats: <a href="xpath-40.xml">XML</a>.</p><p class="copyright"><a href="https://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a> © 2000 <a href="https://www.w3.org/"><abbr title="World Wide Web Consortium">W3C</abbr></a><sup>®</sup> (<a href="https://www.csail.mit.edu/"><abbr title="Massachusetts Institute of Technology">MIT</abbr></a>, <a href="https://www.ercim.eu/"><abbr title="European Research Consortium for Informatics and Mathematics">ERCIM</abbr></a>, <a href="https://www.keio.ac.jp/">Keio</a>, <a href="http://ev.buaa.edu.cn/">Beihang</a>). W3C <a href="https://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>, <a href="https://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a> and <a href="https://www.w3.org/Consortium/Legal/copyright-documents">document use</a> rules apply.</p><hr/></div><div>
<h2><a id="abstract"/>Abstract</h2><p class="xpath">

XPath 4.0 is an expression language that allows the processing of
values conforming to the data model defined in
<a href="#xpath-datamodel-40">[XQuery and XPath Data Model (XDM) 4.0]</a>. 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.  As well
as modeling the tree structure of XML, the data model also includes
atomic items, function items, maps, arrays, and sequences.  This version of XPath
supports JSON as well as XML, and adds many new functions in <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.</p><p class="xpath">XPath 4.0 is a superset of XPath 3.1. A detailed list of changes
made since XPath 3.1 can be found in <a href="#id-revision-log"><b>I Change Log</b></a>. 
</p></div><div>
<h2><a id="status"/>Status of this Document</h2><p>This is a draft prepared by the QT4CG (officially registered in W3C as the
      XSLT Extensions Community Group). Comments are invited.</p><div class="dedication" id="dedication"><h3>Dedication</h3><p class="xpath">The publications of this community group are dedicated to our co-chair,
Michael Sperberg-McQueen (1954–2024).</p><p class="xpath">Michael was central to the development of XML and many related technologies.
He brought a polymathic breadth of knowledge and experience to everything he
did. This, combined with his indefatigable curiosity and appetite for learning,
made him an invaluable contributor to our project, along with many others. We
have lost a brilliant thinker, a patient teacher, and a loyal friend.</p></div></div><nav id="toc">
<h2><a id="contents"/>Table of Contents<span class="expalltoc collapsed"> ▶</span></h2><ol class="toc"><li><details><summary><a href="#id-introduction"><span class="secno">1 </span><span class="content toc-chg">Introduction</span></a></summary></details></li><li><details><summary><a href="#id-basics"><span class="secno">2 </span><span class="content">Basics</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-terminology"><span class="secno">2.1 </span><span class="content toc-chg">Terminology</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-ebnf-introduction"><span class="secno">2.1.1 </span><span class="content toc-chg">Grammar Notation</span></a></summary></details></li><li><details><summary><a href="#id-values"><span class="secno">2.1.2 </span><span class="content toc-chg">Values</span></a></summary></details></li><li><details><summary><a href="#id-namespaces-and-qnames"><span class="secno">2.1.3 </span><span class="content">Namespaces and QNames</span></a></summary></details></li><li><details><summary><a href="#id-expanding-unprefixed-qnames"><span class="secno">2.1.4 </span><span class="content">Expanding Lexical QNames</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#context"><span class="secno">2.2 </span><span class="content">Expression Context</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#static_context"><span class="secno">2.2.1 </span><span class="content toc-chg">Static Context</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-function-definitions"><span class="secno">2.2.1.1 </span><span class="content">Function Definitions</span></a></summary></details></li><li><details><summary><a href="#id-decimal-formats"><span class="secno">2.2.1.2 </span><span class="content toc-chg">Decimal Formats</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#eval_context"><span class="secno">2.2.2 </span><span class="content toc-chg">Dynamic Context</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-processing-model"><span class="secno">2.3 </span><span class="content toc-chg">Processing Model</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-data-model-generation"><span class="secno">2.3.1 </span><span class="content">Data Model Generation</span></a></summary></details></li><li><details><summary><a href="#id-schema-import-processing"><span class="secno">2.3.2 </span><span class="content">Schema Import Processing</span></a></summary></details></li><li><details><summary><a href="#id-expression-processing"><span class="secno">2.3.3 </span><span class="content">Expression
Processing</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-static-analysis"><span class="secno">2.3.3.1 </span><span class="content">Static Analysis Phase</span></a></summary></details></li><li><details><summary><a href="#id-dynamic-evaluation"><span class="secno">2.3.3.2 </span><span class="content">Dynamic Evaluation Phase</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-input-sources"><span class="secno">2.3.4 </span><span class="content">Input Sources</span></a></summary></details></li><li><details><summary><a href="#id-serialization"><span class="secno">2.3.5 </span><span class="content">Serialization</span></a></summary></details></li><li><details><summary><a href="#id-consistency-constraints"><span class="secno">2.3.6 </span><span class="content">Consistency Constraints</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#errors"><span class="secno">2.4 </span><span class="content">Error Handling</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-kinds-of-errors"><span class="secno">2.4.1 </span><span class="content">Kinds of Errors</span></a></summary></details></li><li><details><summary><a href="#id-identifying-errors"><span class="secno">2.4.2 </span><span class="content">Identifying and Reporting Errors</span></a></summary></details></li><li><details><summary><a href="#id-handling-dynamic"><span class="secno">2.4.3 </span><span class="content">Handling Dynamic Errors</span></a></summary></details></li><li><details><summary><a href="#id-errors-and-opt"><span class="secno">2.4.4 </span><span class="content">Errors and
      Optimization</span></a></summary></details></li><li><details><summary><a href="#id-guarded-expressions"><span class="secno">2.4.5 </span><span class="content toc-chg">Guarded Expressions</span></a></summary></details></li><li><details><summary><a href="#id-implausible-expressions"><span class="secno">2.4.6 </span><span class="content toc-chg">Implausible Expressions</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-important-concepts"><span class="secno">2.5 </span><span class="content">Concepts</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-document-order"><span class="secno">2.5.1 </span><span class="content">Document Order</span></a></summary></details></li><li><details><summary><a href="#id-typed-value"><span class="secno">2.5.2 </span><span class="content">Typed Value and String Value</span></a></summary></details></li><li><details><summary><a href="#id-atomization"><span class="secno">2.5.3 </span><span class="content">Atomization</span></a></summary></details></li><li><details><summary><a href="#id-ebv"><span class="secno">2.5.4 </span><span class="content">Effective Boolean Value</span></a></summary></details></li><li><details><summary><a href="#id-uri-literals"><span class="secno">2.5.5 </span><span class="content">URI Literals</span></a></summary></details></li><li><details><summary><a href="#id-resolve-relative-uri"><span class="secno">2.5.6 </span><span class="content">Resolving a Relative URI Reference</span></a></summary></details></li></ol></details></li></ol></details></li><li><details><summary><a href="#id-types"><span class="secno">3 </span><span class="content">Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-sequencetype-syntax"><span class="secno">3.1 </span><span class="content">Sequence Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-sequence-type-examples"><span class="secno">3.1.1 </span><span class="content">Examples of Sequence Types</span></a></summary></details></li><li><details><summary><a href="#id-sequencetype-matching"><span class="secno">3.1.2 </span><span class="content">Sequence Type Matching</span></a></summary></details></li><li><details><summary><a href="#id-schema-type-derivation"><span class="secno">3.1.3 </span><span class="content">Schema Type Relationships</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-matching-item"><span class="secno">3.2 </span><span class="content">Item Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#general-item-types"><span class="secno">3.2.1 </span><span class="content">General item types</span></a></summary></details></li><li><details><summary><a href="#id-atomic-types"><span class="secno">3.2.2 </span><span class="content">Atomic Types</span></a></summary></details></li><li><details><summary><a href="#id-union-types"><span class="secno">3.2.3 </span><span class="content">Union Types</span></a></summary></details></li><li><details><summary><a href="#id-namespace-sensitive"><span class="secno">3.2.4 </span><span class="content">Namespace-sensitive Types</span></a></summary></details></li><li><details><summary><a href="#id-choice-item-types"><span class="secno">3.2.5 </span><span class="content toc-chg">Choice Item Types</span></a></summary></details></li><li><details><summary><a href="#id-enumeration-types"><span class="secno">3.2.6 </span><span class="content toc-chg">Enumeration Types</span></a></summary></details></li><li><details><summary><a href="#node-types"><span class="secno">3.2.7 </span><span class="content toc-chg">Node Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-simple-node-tests"><span class="secno">3.2.7.1 </span><span class="content">Simple Node Types</span></a></summary></details></li><li><details><summary><a href="#id-element-test"><span class="secno">3.2.7.2 </span><span class="content toc-chg">Element Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-schema-element-test"><span class="secno">3.2.7.2.1 </span><span class="content">Schema Element Types</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-attribute-test"><span class="secno">3.2.7.3 </span><span class="content toc-chg">Attribute Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-schema-attribute-test"><span class="secno">3.2.7.3.1 </span><span class="content">Schema Attribute Types</span></a></summary></details></li></ol></details></li></ol></details></li><li><details><summary><a href="#id-function-map-array-tests"><span class="secno">3.2.8 </span><span class="content">Function, Map, and Array Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-function-test"><span class="secno">3.2.8.1 </span><span class="content toc-chg">Function Types</span></a></summary></details></li><li><details><summary><a href="#id-map-test"><span class="secno">3.2.8.2 </span><span class="content">Map Types</span></a></summary></details></li><li><details><summary><a href="#id-record-test"><span class="secno">3.2.8.3 </span><span class="content toc-chg">Record Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-recursive-record-tests"><span class="secno">3.2.8.3.1 </span><span class="content">Recursive Record Types</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-array-test"><span class="secno">3.2.8.4 </span><span class="content">Array Types</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-xs-error"><span class="secno">3.2.9 </span><span class="content">The type xs:error</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-sequencetype-subtype"><span class="secno">3.3 </span><span class="content toc-chg">Subtype Relationships</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-seqtype-subtype"><span class="secno">3.3.1 </span><span class="content">Subtypes of Sequence Types</span></a></summary></details></li><li><details><summary><a href="#id-itemtype-subtype"><span class="secno">3.3.2 </span><span class="content">Subtypes of Item Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-item-subtype-general"><span class="secno">3.3.2.1 </span><span class="content">General Subtyping Rules</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-choice"><span class="secno">3.3.2.2 </span><span class="content">Subtyping of Choice Item Types</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-atomic"><span class="secno">3.3.2.3 </span><span class="content">Subtyping of Atomic and Union Types</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-nodes"><span class="secno">3.3.2.4 </span><span class="content">Subtyping of Node Types</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-item-subtype-nodes-general"><span class="secno">3.3.2.4.1 </span><span class="content">Subtyping Nodes: General Rules</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-documents"><span class="secno">3.3.2.4.2 </span><span class="content toc-chg">Subtyping Nodes: Document Nodes</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-elements"><span class="secno">3.3.2.4.3 </span><span class="content">Subtyping Nodes: Elements</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-attributes"><span class="secno">3.3.2.4.4 </span><span class="content">Subtyping Nodes: Attributes</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-item-subtype-functions"><span class="secno">3.3.2.5 </span><span class="content">Subtyping Functions</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-maps"><span class="secno">3.3.2.6 </span><span class="content">Subtyping Maps</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-arrays"><span class="secno">3.3.2.7 </span><span class="content">Subtyping Arrays</span></a></summary></details></li><li><details><summary><a href="#id-item-subtype-records"><span class="secno">3.3.2.8 </span><span class="content">Subtyping Records</span></a></summary></details></li><li><details><summary><a href="#id-itemtype-subtype-aliases"><span class="secno">3.3.2.9 </span><span class="content">Subtyping of Named Item Types</span></a></summary></details></li></ol></details></li></ol></details></li><li><details><summary><a href="#id-coercion-rules"><span class="secno">3.4 </span><span class="content toc-chg">Coercion Rules</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-xpath-10-compatibility"><span class="secno">3.4.1 </span><span class="content xpath">XPath 1.0 Compatibility Rules</span></a></summary></details></li><li><details><summary><a href="#id-item-coercion-rules"><span class="secno">3.4.2 </span><span class="content">Item Coercion Rules</span></a></summary></details></li><li><details><summary><a href="#id-implausible-coercions"><span class="secno">3.4.3 </span><span class="content">Implausible Coercions</span></a></summary></details></li><li><details><summary><a href="#id-function-coercion"><span class="secno">3.4.4 </span><span class="content toc-chg">Function Coercion</span></a></summary></details></li><li><details><summary><a href="#id-coercion-examples"><span class="secno">3.4.5 </span><span class="content">Examples of Coercions</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-predefined-types"><span class="secno">3.5 </span><span class="content">Schema Types</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-expressions"><span class="secno">4 </span><span class="content">Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#comments"><span class="secno">4.1 </span><span class="content">Comments</span></a></summary></details></li><li><details><summary><a href="#id-primary-expressions"><span class="secno">4.2 </span><span class="content">Primary Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-literals"><span class="secno">4.2.1 </span><span class="content">Literals</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-numeric-literals"><span class="secno">4.2.1.1 </span><span class="content toc-chg">Numeric Literals</span></a></summary></details></li><li><details><summary><a href="#id-string-literal"><span class="secno">4.2.1.2 </span><span class="content">String Literals</span></a></summary></details></li><li><details><summary><a href="#id-qname-literals"><span class="secno">4.2.1.3 </span><span class="content toc-chg">QName Literals</span></a></summary></details></li><li><details><summary><a href="#id-constants-other-types"><span class="secno">4.2.1.4 </span><span class="content">Constants of Other Types</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-variables"><span class="secno">4.2.2 </span><span class="content">Variable References</span></a></summary></details></li><li><details><summary><a href="#id-context-value-references"><span class="secno">4.2.3 </span><span class="content">Context Value References</span></a></summary></details></li><li><details><summary><a href="#id-paren-expressions"><span class="secno">4.2.4 </span><span class="content">Parenthesized Expressions</span></a></summary></details></li><li><details><summary><a href="#id-enclosed-expr"><span class="secno">4.2.5 </span><span class="content">Enclosed Expressions</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-postfix-expression"><span class="secno">4.3 </span><span class="content">Postfix Expressions</span></a></summary></details></li><li><details><summary><a href="#id-filter-expression"><span class="secno">4.4 </span><span class="content toc-chg">Filter Expressions</span></a></summary></details></li><li><details><summary><a href="#id-functions"><span class="secno">4.5 </span><span class="content">Functions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-static-functions"><span class="secno">4.5.1 </span><span class="content">Static Function Calls</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-function-calls"><span class="secno">4.5.1.1 </span><span class="content toc-chg">Static Function Call Syntax</span></a></summary></details></li><li><details><summary><a href="#id-eval-static-function-call"><span class="secno">4.5.1.2 </span><span class="content">Evaluating Static Function Calls</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-dynamic-functions"><span class="secno">4.5.2 </span><span class="content">Function Items</span></a></summary></details></li><li><details><summary><a href="#id-dynamic-function-invocation"><span class="secno">4.5.3 </span><span class="content">Dynamic Function Calls</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-eval-dynamic-function-call"><span class="secno">4.5.3.1 </span><span class="content toc-chg">Evaluating Dynamic Function Calls</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-partial-function-application"><span class="secno">4.5.4 </span><span class="content">Partial Function Application</span></a></summary></details></li><li><details><summary><a href="#id-named-function-ref"><span class="secno">4.5.5 </span><span class="content">Named Function References</span></a></summary></details></li><li><details><summary><a href="#id-inline-func"><span class="secno">4.5.6 </span><span class="content toc-chg">Inline Function Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-methods"><span class="secno">4.5.6.1 </span><span class="content toc-chg">Methods</span></a></summary></details></li><li><details><summary><a href="#id-focus-functions"><span class="secno">4.5.6.2 </span><span class="content">Focus Functions</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-function-identity"><span class="secno">4.5.7 </span><span class="content">Function Identity</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-path-expressions"><span class="secno">4.6 </span><span class="content">Path Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-absolute-path-expressions"><span class="secno">4.6.1 </span><span class="content">Absolute Path Expressions</span></a></summary></details></li><li><details><summary><a href="#id-relative-path-expressions"><span class="secno">4.6.2 </span><span class="content">Relative Path Expressions</span></a></summary></details></li><li><details><summary><a href="#id-path-operator"><span class="secno">4.6.3 </span><span class="content">Path operator (/)</span></a></summary></details></li><li><details><summary><a href="#id-steps"><span class="secno">4.6.4 </span><span class="content">Steps</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#axes"><span class="secno">4.6.4.1 </span><span class="content toc-chg">Axes</span></a></summary></details></li><li><details><summary><a href="#node-tests"><span class="secno">4.6.4.2 </span><span class="content toc-chg">Node Tests</span></a></summary></details></li><li><details><summary><a href="#implausible-axis-steps"><span class="secno">4.6.4.3 </span><span class="content toc-chg">Implausible Axis Steps</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-predicate"><span class="secno">4.6.5 </span><span class="content">Predicates within Steps</span></a></summary></details></li><li><details><summary><a href="#unabbrev"><span class="secno">4.6.6 </span><span class="content">Unabbreviated Syntax</span></a></summary></details></li><li><details><summary><a href="#abbrev"><span class="secno">4.6.7 </span><span class="content">Abbreviated Syntax</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-sequence-expressions"><span class="secno">4.7 </span><span class="content">Sequence Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#construct_seq"><span class="secno">4.7.1 </span><span class="content">Sequence Concatenation</span></a></summary></details></li><li><details><summary><a href="#id-range-expressions"><span class="secno">4.7.2 </span><span class="content">Range Expressions</span></a></summary></details></li><li><details><summary><a href="#combining_seq"><span class="secno">4.7.3 </span><span class="content">Combining Node Sequences</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-arithmetic"><span class="secno">4.8 </span><span class="content toc-chg">Arithmetic Expressions</span></a></summary></details></li><li><details><summary><a href="#id-string-expr"><span class="secno">4.9 </span><span class="content">String Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-string-concat-expr"><span class="secno">4.9.1 </span><span class="content">String Concatenation Expressions</span></a></summary></details></li><li><details><summary><a href="#id-string-templates"><span class="secno">4.9.2 </span><span class="content toc-chg">String Templates</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-comparisons"><span class="secno">4.10 </span><span class="content">Comparison Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-value-comparisons"><span class="secno">4.10.1 </span><span class="content toc-chg">Value Comparisons</span></a></summary></details></li><li><details><summary><a href="#id-general-comparisons"><span class="secno">4.10.2 </span><span class="content toc-chg">General Comparisons</span></a></summary></details></li><li><details><summary><a href="#id-node-comparisons"><span class="secno">4.10.3 </span><span class="content">Node Comparisons</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-logical-expressions"><span class="secno">4.11 </span><span class="content">Logical Expressions</span></a></summary></details></li><li><details><summary><a href="#id-flwor-expressions"><span class="secno">4.12 </span><span class="content">For and Let Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-for-expressions"><span class="secno">4.12.1 </span><span class="content xpath toc-chg">For Expressions</span></a></summary></details></li><li><details><summary><a href="#id-let-expressions"><span class="secno">4.12.2 </span><span class="content xpath toc-chg">Let Expressions</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-maps-and-arrays"><span class="secno">4.13 </span><span class="content">Maps and Arrays</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-maps"><span class="secno">4.13.1 </span><span class="content toc-chg">Maps</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-map-constructors"><span class="secno">4.13.1.1 </span><span class="content toc-chg">Map Constructors</span></a></summary></details></li><li><details><summary><a href="#id-map-lookup"><span class="secno">4.13.1.2 </span><span class="content">Maps as Functions</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-arrays"><span class="secno">4.13.2 </span><span class="content">Arrays</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-array-constructors"><span class="secno">4.13.2.1 </span><span class="content">Array Constructors</span></a></summary></details></li><li><details><summary><a href="#id-array-lookup"><span class="secno">4.13.2.2 </span><span class="content">Arrays as Functions</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-lookup"><span class="secno">4.13.3 </span><span class="content toc-chg">Lookup Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-postfix-lookup"><span class="secno">4.13.3.1 </span><span class="content">Postfix Lookup Expressions</span></a></summary></details></li><li><details><summary><a href="#id-unary-lookup"><span class="secno">4.13.3.2 </span><span class="content">Unary Lookup</span></a></summary></details></li><li><details><summary><a href="#id-deep-lookup"><span class="secno">4.13.3.3 </span><span class="content">Deep Lookup</span></a></summary></details></li><li><details><summary><a href="#id-implausible-lookup-expressions"><span class="secno">4.13.3.4 </span><span class="content">Implausible Lookup Expressions</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-filter-maps-and-arrays"><span class="secno">4.13.4 </span><span class="content toc-chg">Filter Expressions for Maps and Arrays</span></a></summary></details></li><li><details><summary><a href="#id-pinned-maps-and-arrays"><span class="secno">4.13.5 </span><span class="content">Pinned Maps and Arrays</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-conditionals"><span class="secno">4.14 </span><span class="content toc-chg">Conditional Expressions</span></a></summary></details></li><li><details><summary><a href="#id-otherwise"><span class="secno">4.15 </span><span class="content toc-chg">Otherwise Expressions</span></a></summary></details></li><li><details><summary><a href="#id-quantified-expressions"><span class="secno">4.16 </span><span class="content toc-chg">Quantified Expressions</span></a></summary></details></li><li><details><summary><a href="#id-expressions-on-datatypes"><span class="secno">4.17 </span><span class="content">Expressions on SequenceTypes</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-instance-of"><span class="secno">4.17.1 </span><span class="content">Instance Of</span></a></summary></details></li><li><details><summary><a href="#id-cast"><span class="secno">4.17.2 </span><span class="content">Cast</span></a></summary></details></li><li><details><summary><a href="#id-castable"><span class="secno">4.17.3 </span><span class="content">Castable</span></a></summary></details></li><li><details><summary><a href="#id-constructor-functions"><span class="secno">4.17.4 </span><span class="content">Constructor Functions</span></a></summary></details></li><li><details><summary><a href="#id-treat"><span class="secno">4.17.5 </span><span class="content">Treat</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-pipeline-operator"><span class="secno">4.18 </span><span class="content toc-chg">Pipeline operator</span></a></summary></details></li><li><details><summary><a href="#id-map-operator"><span class="secno">4.19 </span><span class="content">Simple map operator (!)</span></a></summary></details></li><li><details><summary><a href="#id-arrow-operator"><span class="secno">4.20 </span><span class="content toc-chg">Arrow Expressions</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-sequence-arrow-expression"><span class="secno">4.20.1 </span><span class="content">Sequence Arrow Expressions</span></a></summary></details></li><li><details><summary><a href="#id-mapping-arrow-expression"><span class="secno">4.20.2 </span><span class="content toc-chg">Mapping Arrow Expressions</span></a></summary></details></li></ol></details></li></ol></details></li><li><details><summary><a href="#id-conformance"><span class="secno">5 </span><span class="content toc-chg">Conformance</span></a></summary></details></li><li><details><summary><a href="#nt-bnf"><span class="secno">A </span><span class="content">XPath 4.0 Grammar</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-grammar"><span class="secno">A.1 </span><span class="content toc-chg">EBNF</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#EBNFNotation"><span class="secno">A.1.1 </span><span class="content toc-chg">Notation</span></a></summary></details></li><li><details><summary><a href="#extra-grammatical-constraints"><span class="secno">A.1.2 </span><span class="content">Extra-grammatical Constraints</span></a></summary></details></li><li><details><summary><a href="#notes-on-parsing"><span class="secno">A.1.3 </span><span class="content">Grammar Notes</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#productions-derived-from-XML"><span class="secno">A.2 </span><span class="content">Productions Derived from XML</span></a></summary></details></li><li><details><summary><a href="#lexical-structure"><span class="secno">A.3 </span><span class="content toc-chg">Lexical structure</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#terminal-symbols"><span class="secno">A.3.1 </span><span class="content">Terminal Symbols</span></a></summary></details></li><li><details><summary><a href="#id-terminal-delimitation"><span class="secno">A.3.2 </span><span class="content">Terminal Delimitation</span></a></summary></details></li><li><details><summary><a href="#id-lt-and-gt-characters"><span class="secno">A.3.3 </span><span class="content">Less-Than and Greater-Than Characters</span></a></summary></details></li><li><details><summary><a href="#id-eol-handling"><span class="secno">A.3.4 </span><span class="content">End-of-Line Handling</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-xml10-eol-handling"><span class="secno">A.3.4.1 </span><span class="content">XML 1.0 End-of-Line Handling</span></a></summary></details></li><li><details><summary><a href="#id-xml11-eol-handling"><span class="secno">A.3.4.2 </span><span class="content">XML 1.1 End-of-Line Handling</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#whitespace-rules"><span class="secno">A.3.5 </span><span class="content">Whitespace Rules</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#DefaultWhitespaceHandling"><span class="secno">A.3.5.1 </span><span class="content">Default Whitespace Handling</span></a></summary></details></li><li><details><summary><a href="#ExplicitWhitespaceHandling"><span class="secno">A.3.5.2 </span><span class="content">Explicit Whitespace Handling</span></a></summary></details></li></ol></details></li></ol></details></li><li><details><summary><a href="#id-reserved-fn-names"><span class="secno">A.4 </span><span class="content toc-chg">Reserved Function Names</span></a></summary></details></li><li><details><summary><a href="#id-precedence-order"><span class="secno">A.5 </span><span class="content">Precedence Order (Non-Normative)</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-xp-context-components"><span class="secno">B </span><span class="content xpath">Context Components</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-xp-static-context-components"><span class="secno">B.1 </span><span class="content">Static Context
Components</span></a></summary></details></li><li><details><summary><a href="#id-xp-evaluation-context-components"><span class="secno">B.2 </span><span class="content">Dynamic Context Components</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-impl-defined-items"><span class="secno">C </span><span class="content">Implementation-Defined Items</span></a></summary></details></li><li><details><summary><a href="#id-references"><span class="secno">D </span><span class="content">References</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-normative-references"><span class="secno">D.1 </span><span class="content">Normative References</span></a></summary></details></li><li><details><summary><a href="#id-non-normative-references"><span class="secno">D.2 </span><span class="content">Non-normative References</span></a></summary></details></li><li><details><summary><a href="#id-background-material"><span class="secno">D.3 </span><span class="content">Background Material</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-errors"><span class="secno">E </span><span class="content">Error Conditions</span></a></summary></details></li><li><details><summary><a href="#id-glossary"><span class="secno">F </span><span class="content">Glossary</span></a></summary></details></li><li><details><summary><a href="#id-atomic-comparisons"><span class="secno">G </span><span class="content">Atomic Comparisons: An Overview</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-equality-comparison"><span class="secno">G.1 </span><span class="content">Equality Comparisons</span></a></summary></details></li><li><details><summary><a href="#id-ordering-comparison"><span class="secno">G.2 </span><span class="content">Ordering Comparisons</span></a></summary></details></li></ol></details></li><li><details><summary><a href="#id-incompatibilities"><span class="secno">H </span><span class="content">Backwards Compatibility</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-incompatibilities-31"><span class="secno">H.1 </span><span class="content">Incompatibilities relative to XPath 3.1</span></a></summary></details></li><li><details><summary><a href="#id-incompatibilities-30"><span class="secno">H.2 </span><span class="content">Incompatibilities relative to XPath 3.0</span></a></summary></details></li><li><details><summary><a href="#id-incompatibilities-10"><span class="secno">H.3 </span><span class="content">Incompatibilities relative to XPath 2.0</span></a></summary></details></li><li><details><summary><a href="#id-backwards-compatibility"><span class="secno">H.4 </span><span class="content xpath">Incompatibilities relative to XPath 1.0</span></a><span class="exptoc collapsed"> ▶</span></summary><ol class="toc"><li><details><summary><a href="#id-incompat-in-true-mode"><span class="secno">H.4.1 </span><span class="content">Incompatibilities when Compatibility Mode is true</span></a></summary></details></li><li><details><summary><a href="#id-incompat-in-false-mode"><span class="secno">H.4.2 </span><span class="content">Incompatibilities when Compatibility Mode is false</span></a></summary></details></li><li><details><summary><a href="#id-incompat-when-using-schema"><span class="secno">H.4.3 </span><span class="content">Incompatibilities when using a Schema</span></a></summary></details></li></ol></details></li></ol></details></li><li><details><summary><a href="#id-revision-log"><span class="secno">I </span><span class="content">Change Log</span></a></summary></details></li></ol></nav><hr/><div class="body"><div class="div1">
<h2><a id="id-introduction"/>1 <a href="#id-introduction" style="text-decoration: none">Introduction</a></h2><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-terminology">⬇</a></p><ol><li><p>Use the arrows to browse significant changes since the 3.1 version of this specification.</p></li><li><p>Sections with significant changes are marked Δ in the table of contents.</p></li></ol></div><p class="xpath">XPath was originally designed as an expression language 
		to address the nodes of XML trees; it has since been extended to address a variety of non-XML data sources. 
		XPath gets its name from its use of a path notation for
		navigating through the hierarchical structure of an XML document; similar capabilities for
		navigating JSON structures were added in versions 3.0 and 3.1. XPath uses a compact,
		non-XML syntax, allowing XPath expressions to be embedded within URIs and to be used as 
		XML attribute values. XPath is designed to be embedded in (or invoked from) other
		host languages, including both languages specialized towards XML processing (such as <a href="#xslt-40">[XSL Transformations (XSLT) Version 4.0]</a> and XSD),
	   and general purpose programming languages such as Java, C#, Python, and Javascript. The interface
	   between XPath and its host language is formalized with an abstract definition of a static and dynamic
	   context, made available by the host language to the XPath processor.</p><p class="xpath">
            [<a id="dt-host-language" title="host language">Definition</a>: 
		A <b>host language</b> for XPath is any environment that provides capabilities
		for XPath expressions to be defined and evaluated, and that supplies a static and dynamic
		context for their evaluation.
	]
         </p><p>
            [<a id="dt-datamodel" title="data model">Definition</a>: XPath 4.0 operates on the abstract, logical
			structure of an XML document or JSON object rather than its surface syntax. This logical structure,
			known as the <b>data model</b>, is defined in <a href="#xpath-datamodel-40">[XQuery and XPath Data Model (XDM) 4.0]</a>.]
         </p><p>
            <span class="xpath">XPath 4.0 is a subset of XQuery 4.0.</span>

 In general, any expression that is
		syntactically valid and executes successfully in both XPath 4.0 and XQuery 4.0 will return
		the same result in both languages. There are a few exceptions to this rule: </p><ul><li><p>Because XQuery expands 
					<span class="xpath"> predefined entity references and character references
					</span> and XPath does not, expressions containing these produce different
					results in the two languages. For instance, the value of the string literal
						<code>"&amp;amp;"</code> is <code>&amp;</code> in XQuery,
					and <code>&amp;amp;</code> in XPath. (A host language may expand predefined entity references or character references
					before the XPath expression is evaluated.)</p></li><li><p>If XPath 1.0 compatibility mode is enabled, XPath behaves differently from
					XQuery in a number of ways, <span class="xpath">which are noted throughout this
						document, and listed in <a href="#id-incompat-in-false-mode"><b>H.4.2 Incompatibilities when Compatibility Mode is false</b></a>.</span>
                  </p></li></ul><p>Because these languages are so closely related, their grammars and language descriptions are
		generated from a common source to ensure consistency.</p><p>XPath 4.0 also depends on and is closely related to the following specifications:</p><ul><li><p>
                  <a href="#xpath-datamodel-40">[XQuery and XPath Data Model (XDM) 4.0]</a> defines the data model that underlies all
				XPath 4.0 expressions.</p></li><li><p>The type system of XPath 4.0 is based on XML Schema. It is implementation-defined
				whether the type system is based on <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a>.</p></li><li><p>The system function library and the operators supported by XPath 4.0 are defined
				in <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The XML-based syntax for XQuery known as XQueryX
		is no longer maintained.</p></div><p>This document specifies a grammar for XPath 4.0, 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.3 Lexical structure</b></a>),
		whitespace is not significant in <span class="xpath">expressions</span>. 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 4.0 Grammar</b></a>]. The appendix is the normative version.</p><p>In the grammar productions in this document, named symbols are underlined and literal text is
		enclosed in double quotes. For example, the following productions describe the syntax of a
		static function call:</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall"/><code>FunctionCall</code></td><td>::=</td><td><code>
                  <a href="#example-example-doc-xpath40-FunctionCall-EQName">EQName</a>
                  <a href="#example-example-doc-xpath40-FunctionCall-ArgumentList">ArgumentList</a>
               </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                  <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                */</td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                  <a href="#parse-note-parens">gn: parens</a>
                */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall-EQName"/><code>EQName</code></td><td>::=</td><td><code>
                  <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
               </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall-ArgumentList"/><code>ArgumentList</code></td><td>::=</td><td><code>"("  ((<a href="#example-example-doc-xpath40-FunctionCall-PositionalArguments">PositionalArguments</a>  (","  <a href="#example-example-doc-xpath40-FunctionCall-KeywordArguments">KeywordArguments</a>)?)  |  <a href="#example-example-doc-xpath40-FunctionCall-KeywordArguments">KeywordArguments</a>)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall-PositionalArguments"/><code>PositionalArguments</code></td><td>::=</td><td><code>(<a href="#example-example-doc-xpath40-FunctionCall-Argument">Argument</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall-Argument"/><code>Argument</code></td><td>::=</td><td><code>
                  <a href="#example-example-doc-xpath40-FunctionCall-ExprSingle">ExprSingle</a>  |  <a href="#example-example-doc-xpath40-FunctionCall-ArgumentPlaceholder">ArgumentPlaceholder</a>
               </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall-ExprSingle"/><code>ExprSingle</code></td><td>::=</td><td><code>
                  <a href="#doc-xpath40-ForExpr">ForExpr</a>
                  <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                  <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                  <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                  <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
               </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall-ArgumentPlaceholder"/><code>ArgumentPlaceholder</code></td><td>::=</td><td><code>"?"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall-KeywordArguments"/><code>KeywordArguments</code></td><td>::=</td><td><code>(<a href="#example-example-doc-xpath40-FunctionCall-KeywordArgument">KeywordArgument</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-FunctionCall-KeywordArgument"/><code>KeywordArgument</code></td><td>::=</td><td><code>
                  <a href="#example-example-doc-xpath40-FunctionCall-EQName">EQName</a>  ":="  <a href="#example-example-doc-xpath40-FunctionCall-Argument">Argument</a>
               </code></td></tr></tbody></table><p>The productions should be read as follows: A function call consists of an <a href="#doc-xpath40-EQName">EQName</a> followed by an <a href="#prod-xpath40-ArgumentList">ArgumentList</a>. The
		argument list consists of an opening parenthesis, an optional list of one or more arguments
		(separated by commas), and a closing parenthesis.</p><p>This document normatively defines the static and dynamic semantics of XPath 4.0. 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"/>2 <a href="#id-basics" style="text-decoration: none">Basics</a></h2><div class="div2">
<h3><a id="id-terminology"/>2.1 <a href="#id-terminology" style="text-decoration: none">Terminology</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-ebnf-introduction">⬇</a> <a href="#id-introduction">⬆</a></p><ol><li><p>The EBNF operators <code>++</code> and <code>**</code>
      have been introduced, for more concise representation of sequences using a character
      such as <code>","</code> as a separator. The notation is borrowed from Invisible XML.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1366">1366</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1498">1498</a>]</i></p></li></ol></div><p>The basic building block of XPath 4.0 is the
	 <b>expression</b>, which is a string of <a href="#Unicode">[Unicode]</a> characters; the version of Unicode to be used is <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>.
	 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. XPath 4.0 allows expressions to be nested with full
generality. 
      </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 <a href="#Unicode">[Unicode]</a> characters.</p></div><p>Like XML, XPath 4.0 is a case-sensitive language. Keywords in
	 XPath 4.0 use lower-case characters and are not reserved—that is, names in XPath 4.0 expressions are allowed to be the same as language keywords, except for certain unprefixed function-names listed in <a href="#id-reserved-fn-names"><b>A.4 Reserved Function Names</b></a>.</p><p>In this specification the phrases <span class="verb">must</span>, <span class="verb">must
                  not</span>, <span class="verb">should</span>, <span class="verb">should not</span>,
                  <span class="verb">may</span>, <span class="verb">required</span>, and
                  <span class="verb">recommended</span>, when used in normative
                  text and rendered in small capitals, are to be interpreted as described in
                  <a href="#RFC2119">[RFC2119]</a>.</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 id="dt-implementation-defined" title="implementation defined">Definition</a>: 
                        <b>Implementation-defined</b> indicates an aspect that may differ
					between implementations, but must be specified by the implementer for each
					particular implementation.]
                  </p></li><li><p>
                     [<a id="dt-implementation-dependent" title="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 implementer for any particular
					implementation.]
                  </p></li></ul><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 class="div3">
<h4><a id="id-ebnf-introduction"/>2.1.1 <a href="#id-ebnf-introduction" style="text-decoration: none">Grammar Notation</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-values">⬇</a> <a href="#id-terminology">⬆</a></p><ol><li><p>
                  The EBNF notation has been extended to allow the constructs <code>(A ++ ",")</code>
                  (one or more occurrences of <code>A</code>, comma-separated, and <code>(A ** ",")</code>
                  (zero or more occurrences of <code>A</code>, comma-separated.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1366">1366</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1498">1498</a> 30 October 2024]</i></p></li></ol></div><p>The grammar of XPath 4.0 is defined using a version of EBNF defined
            in <a href="#EBNFNotation"><b>A.1.1 Notation</b></a>. The notation is based on the EBNF dialect used in the
            XML specification, with two notable additions derived from the Invisible XML grammar:</p><p>
                  <code>(A ++ ",")</code> represents a sequence of one or more comma-separated
               occurrences of <code>A</code>.</p><p>
                  <code>(A ** ",")</code> represents a sequence of zero or more comma-separated
               occurrences of <code>A</code>.</p><p>For example the following production rule indicates that an <code>Expr</code>
            consists of one or more occurrences of <code>ExprSingle</code>, separated by commas:</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-Expr"/><code>Expr</code></td><td>::=</td><td><code>(<a href="#example-example-doc-xpath40-Expr-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="example-example-doc-xpath40-Expr-ExprSingle"/><code>ExprSingle</code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr">ForExpr</a>
                        <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                        <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                        <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                        <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                     </code></td></tr></tbody></table><p>In principle any production can be used for the separator, but in practice this notation
            is only used in cases where the separator is a simple constant string.</p><p>EBNF grammar rules appear throughout the specification for ease of reference, and the
            entire grammar is summarized in <a href="#id-grammar"><b>A.1 EBNF</b></a>.
            <span class="xpath">For XPath 4.0, the top-level production rule is 
            <a href="#doc-xpath40-XPath">XPath</a>, representing an XPath expression.</span>
               </p></div><div class="div3">
<h4><a id="id-values"/>2.1.2 <a href="#id-values" style="text-decoration: none">Values</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#static_context">⬇</a> <a href="#id-ebnf-introduction">⬆</a></p><ol><li><p>
               The term <b>atomic value</b> has been replaced by <a title="atomic item" class="termref" href="#dt-atomic-item">atomic item</a>.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1337">1337</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1361">1361</a> 2 August 2024]</i></p></li></ol></div><p>
                  [<a id="dt-value" title="value">Definition</a>: In the <a title="data model" class="termref" href="#dt-datamodel">data model</a>, a <b>value</b> is always a <a title="sequence" class="termref" href="#dt-sequence">sequence</a>.]
               </p><p>
                  [<a id="dt-sequence" title="sequence">Definition</a>: A
<b>sequence</b> is an ordered collection of zero or more
<a title="item" class="termref" href="#dt-item">items</a>.]
               </p><p>
                  [<a id="dt-item" title="item">Definition</a>: 
  An <b>item</b> is either an <a title="atomic item" class="termref" href="#dt-atomic-item">atomic item</a>, a <a title="node" class="termref" href="#dt-node">node</a>,
or a <a title="function item" class="termref" href="#dt-function-item">function item</a>.]
               </p><p>
                  [<a id="dt-atomic-item" title="atomic item">Definition</a>: An <b>atomic
	 item</b> is a value in the value space of an <b>atomic
	 type</b>, as defined in <a href="#XMLSchema10">[XML Schema 1.0]</a>  or <a href="#XMLSchema11">[XML Schema 1.1]</a>.]
               </p><p>
                  [<a id="dt-node" title="node">Definition</a>: A <b>node</b> is an instance of one of the
	  <b>node kinds</b> defined in <span class="markup-error">[TITLE OF DM40 SPEC, TITLE OF Node SECTION]<sup><small>DM40</small></sup></span>.]
Each node has a unique <b>node identity</b>, a <b>typed value</b>, and a <b>string value</b>. In addition, some nodes have a <b>name</b>. The <b>typed value</b> of a node is a sequence
	 of zero or more atomic items. 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 id="dt-function-item" title="function item">Definition</a>: A <b>function item</b> is an item that can
         be called using a <a title="dynamic function call" class="termref" href="#dt-dynamic-function-call">dynamic function call</a>.]
               </p><p>Maps (see <a href="#id-maps"><b>4.13.1 Maps</b></a>) and arrays (see <a href="#id-arrays"><b>4.13.2 Arrays</b></a>) are
         specific kinds of <a title="function item" class="termref" href="#dt-function-item">function item</a>s.</p><p>
                  [<a id="dt-singleton" title="singleton">Definition</a>: A sequence containing exactly one item is called a
	 <b>singleton</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 id="dt-empty-sequence" title="empty sequence">Definition</a>: A sequence containing zero items is called an <b>empty sequence</b>.]
               </p><p>
                  [<a id="dt-data-model-instance" title="XDM instance">Definition</a>: The term <b>XDM instance</b> is used,
    synonymously with the term <a title="value" class="termref" href="#dt-value">value</a>, to denote an unconstrained
    <a title="sequence" class="termref" href="#dt-sequence">sequence</a> of <a title="item" class="termref" href="#dt-item">items</a>.]
               </p></div><div class="div3">
<h4><a id="id-namespaces-and-qnames"/>2.1.3 <a href="#id-namespaces-and-qnames" style="text-decoration: none">Namespaces and QNames</a></h4><p>
                  [<a id="dt-namespace-binding" title="namespace binding">Definition</a>: A <b>namespace binding</b>
         is a pair comprising a namespace prefix (which is either an <code>xs:NCName</code> or empty),
         and a namespace URI.]
               </p><p>Element nodes have a property called <b>in-scope namespaces</b>. [<a id="dt-in-scope-namespaces" title="in-scope namespaces">Definition</a>: The <b>in-scope namespaces</b> property of an element node is a set of 
         <a title="namespace binding" class="termref" href="#dt-namespace-binding">namespace bindings</a>, each of which associates a namespace prefix with a URI.]
For a given element, one namespace binding may have an empty prefix; the URI of this namespace binding is 
referred to as the <a title="default in-scope namespace" class="termref" href="#dt-default-in-scope-namespace">default in-scope namespace</a> for the element.</p><p class="xpath">In <a href="#xpath">[XML Path Language (XPath) Version 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>. 
As of XPath 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><p>
                  [<a id="dt-default-in-scope-namespace" title="default in-scope namespace">Definition</a>: The <b>default in-scope namespace</b>
         of an element node] is the namespace URI to which the empty prefix is bound in the element’s 
         <a title="in-scope namespaces" class="termref" href="#dt-in-scope-namespaces">in-scope namespaces</a>. In the absence of an explicit binding for the empty prefix, the
         default in-scope namespace is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>.</p><p>
                  [<a id="dt-expanded-qname" title="expanded QName">Definition</a>: An <b>expanded QName</b> is a
      triple: its components are a prefix, a local name, and a
      namespace URI. In the case of a name in no namespace, the
      namespace URI and prefix are both absent. In the case of a name
      in the default namespace, the prefix is absent.] When
      comparing two expanded QNames, the prefixes are ignored: the
      local name parts must be equal under the Unicode codepoint
      collation (<a href="https://qt4cg.org/specifications/xpath-functions-40/#collations">Section 5.3.1 Collations</a><sup><small>FO</small></sup>), 
      and the namespace URI parts must either both be
      absent, or must be equal under the Unicode codepoint
      collation.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-datum">datum</a><sup><small>DM</small></sup> of 
            an <a title="atomic item" class="termref" href="#dt-atomic-item">atomic item</a> of type <code>xs:QName</code>
            is an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>. However, the
            term <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> is also used for
            the names of constructs such as functions, variables, and types
            that are not themselves atomic items.</p></div><p>In the XPath 4.0
      grammar, QNames representing the names of
      elements, attributes, functions, variables, types, or other such
      constructs are written as instances of the grammatical
      production <a href="#doc-xpath40-EQName">EQName</a>.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#doc-xpath40-EQName-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-EQName-URIQualifiedName"/><code><a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>
                        <a href="#prod-xpath40-NCName">NCName</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody></table><p>The <a href="#doc-xpath40-EQName">EQName</a> production allows a QName to
      be written in one of three ways:
      </p><ul><li><p>local-name only (for example, <code>invoice</code>).</p><p>A name written in this form has no prefix, and the rules
        for determining the namespace depend on the context in which
        the name appears: the various rules used are summarized
        in <a href="#id-expanding-unprefixed-qnames"><b>2.1.4 Expanding Lexical QNames</b></a>.
                  This form is a <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a>.</p></li><li><p>prefix plus local-name (for example, <code>my:invoice</code>).</p><p>In this case the prefix and local name of the QName are as
        written, and the namespace URI is inferred from the prefix by
        examining the <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a> in the static context where
        the QName appears; the context must include a binding for the
        prefix. This form is a <a title="lexical QName" class="termref" href="#dt-qname">lexical
        QName</a>.</p></li><li><p>URI plus local-name (for example,
        <code>Q{http://example.com/ns}invoice</code>).</p><p>In this case the local name and namespace URI are as
        written, and the prefix is absent. This way of writing a QName
        is context-free, which makes it particularly suitable for use
        in  
            <span class="xpath">expressions</span>
        that are generated by software. This
        form is a <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>.  
        If the <a href="#prod-xpath40-BracedURILiteral">
        BracedURILiteral</a> has no content (for example, <code>Q{}invoice</code>) 
        then the namespace URI of the QName is absent.</p></li></ul><p>
                  [<a id="dt-qname" title="lexical QName">Definition</a>: A
          <b>lexical QName</b> is a name that conforms to the syntax of the
          <a href="#prod-xpath40-QName">QName</a> production].
          </p><p>
	  The namespace URI value in a <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a> is whitespace normalized according
	  to the rules for the <code>xs:anyURI</code> type in
          <a href="https://www.w3.org/TR/xmlschema-2/#anyURI">Section 
               
                  3.2.17 anyURI
            </a><sup><small>XS1-2</small></sup> or 
          <a href="https://www.w3.org/TR/xmlschema11-2/#anyURI">Section 
               
                  3.3.17 anyURI
            </a><sup><small>XS11-2</small></sup>. 

          It is a <a title="static error" class="termref" href="#dt-static-error">static
          error</a>
                  [<a href="#ERRXQST0070" title="err:XQST0070">err:XQST0070</a>] if the
          namespace URI for an EQName is
          <code>http://www.w3.org/2000/xmlns/</code>.
          </p><p>Here are some examples of <a href="#doc-xpath40-EQName">EQName</a>s:</p><ul><li><p>
                        <code>pi</code> is a <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a> without a namespace prefix.</p></li><li><p>
                        <code>math:pi</code> is a <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a> with a namespace prefix.</p></li><li><p>
                        <code>Q{http://www.w3.org/2005/xpath-functions/math}pi</code> specifies the namespace URI using a  <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>; it is not a  <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a>.</p></li></ul><p>This document uses the following namespace prefixes to represent the namespace URIs with which they are listed. Although these prefixes are used within this specification to refer to the corresponding namespaces, not all of these bindings will necessarily be present in the static context of every expression, and authors are free to use different prefixes for these namespaces, or to bind these prefixes to different namespaces.</p><ul><li><p>
                        <code>xs</code>: <code>http://www.w3.org/2001/XMLSchema</code>
                     </p></li><li><p>
                        <code>fn</code>: <code>http://www.w3.org/2005/xpath-functions</code>
                     </p></li><li><p>
                        <code>array</code>: <code>http://www.w3.org/2005/xpath-functions/array</code>
                     </p></li><li><p>
                        <code>map</code>: <code>http://www.w3.org/2005/xpath-functions/map</code>
                     </p></li><li><p>
                        <code>math</code>: <code>http://www.w3.org/2005/xpath-functions/math</code>
                     </p></li><li><p>
                        <code>err</code>: <code>http://www.w3.org/2005/xqt-errors</code>
                  (see <a href="#id-identifying-errors"><b>2.4.2 Identifying and Reporting Errors</b></a>).</p></li><li><p>
                        <code>output</code>: <code>http://www.w3.org/2010/xslt-xquery-serialization</code>
                     </p></li></ul><p>
                  [<a id="dt-URI" title="URI">Definition</a>: Within this specification, the term <b>URI</b> refers to a Universal Resource Identifier as defined in <a href="#RFC3986">[RFC3986]</a> and extended in <a href="#RFC3987">[RFC3987]</a> with the new name <b>IRI</b>.]
The term URI has been retained in preference to IRI to avoid introducing new names for concepts such as “Base URI” that are defined or referenced across the whole family of XML specifications.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>In most contexts, processors are not required to raise errors if a URI is not lexically valid according to  <a href="#RFC3986">[RFC3986]</a> and  <a href="#RFC3987">[RFC3987]</a>. See <a href="#id-uri-literals"><b>2.5.5 URI Literals</b></a>
             for details.</p></div></div><div class="div3">
<h4><a id="id-expanding-unprefixed-qnames"/>2.1.4 <a href="#id-expanding-unprefixed-qnames" style="text-decoration: none">Expanding Lexical QNames</a></h4><p>When a <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a> appearing in an XPath 4.0 expression is expanded, the rules are as follows.</p><p>If the name contains a colon, then the prefix (the substring before the colon) is used to establish the 
               corresponding URI by reference to the <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a>
            in the <a title="static context" class="termref" href="#dt-static-context">static context</a>. If there
                  is no binding for the prefix in the <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a> then
                  a static error is raised [<a href="#ERRXPST0081" title="err:XPST0081">err:XPST0081</a>].
            </p><p>If the <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a> contains no colon (and therefore no prefix),
            the namespace URI part of the QName is inferred in one of a number of ways, depending on the syntactic context
            in which the name appears. The various rules are defined below, and are referred to throughout the specification.</p><ul><li><p>
                        [<a id="dt-no-namespace-rule" title="no-namespace rule">Definition</a>: When an unprefixed lexical QName
               is expanded using the <b>no-namespace rule</b>, it is interpreted as having an absent namespace URI.]
                     </p></li><li><p>
                        [<a id="dt-default-function-namespace-rule" title="default function namespace rule">Definition</a>: When 
                  an unprefixed lexical QName
               is expanded using the <b>default function namespace rule</b>, it uses the <a title="default function namespace" class="termref" href="#dt-default-function-namespace">default function namespace</a>
                  from the <a title="static context" class="termref" href="#dt-static-context">static context</a>.]
                     </p></li><li><p>
                        [<a id="dt-default-type-namespace-rule" title="default type namespace rule">Definition</a>: When 
                  an unprefixed lexical QName
               is expanded using the <b>default type namespace rule</b>, it uses the 
                  <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>. If this is absent, the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>
                  is used. If the <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a> has the special value <code>##any</code>,
                  then the lexical QName refers to a name in the namespace <code>http://www.w3.org/2001/XMLSchema</code>.]
                     </p></li><li><p>
                        [<a id="dt-default-element-namespace-rule" title="default element namespace rule">Definition</a>: When 
                  an unprefixed lexical QName
               is expanded using the <b>default element namespace rule</b>, then it uses the 
                  <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>. If this is absent, or if it takes
                  the special value <code>##any</code>, then the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>
                  is used.]
                     </p></li><li><p>
                        [<a id="dt-element-name-matching-rule" title="element name matching rule">Definition</a>: When 
                  an unprefixed lexical QName
               is expanded using the <b>element name matching rule</b> rule, then it uses the 
                  <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>. If this is absent, then 
                  it uses the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>. But if it takes the special value <code>##any</code>,
                  then the name is taken as matching any <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> with the corresponding local part,
                  regardless of namespace: that is, the  unprefixed name <code>local</code> is interpreted
                  as <code>*:local</code>.]
                     </p></li></ul></div></div><div class="div2">
<h3><a id="context"/>2.2 <a href="#context" style="text-decoration: none">Expression Context</a></h3><p>
               [<a id="dt-expression-context" title="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.]
            </p><p>This information is organized into two categories
		called the <a title="static context" class="termref" href="#dt-static-context">static
		context</a> and the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic
		context</a>.</p><div class="div3">
<h4><a id="static_context"/>2.2.1 <a href="#static_context" style="text-decoration: none">Static Context</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-decimal-formats">⬇</a> <a href="#id-values">⬆</a></p><ol><li><p>
                  The <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a> can be set to the value <code>##any</code>,
                  allowing unprefixed names in axis steps to match elements with a given local name in any namespace.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/296">296</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1181">1181</a> 30 April 2024]</i></p></li><li><p>

                  Parts of the static context that were there purely to assist in static typing, such as the statically
                  known documents, were no longer referenced and have therefore been dropped.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1343">1343</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1344">1344</a> 23 September 2024]</i></p></li><li><p>
                  The context value static type, which was there purely to assist in static typing, has been dropped.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1495">1495</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1496">1496</a> 29 October 2024]</i></p></li><li><p>
                  Named record types used in the signatures of built-in functions are now available as standard
                  in the static context.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/835">835</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1991">1991</a> 11 May 2025]</i></p></li></ol></div><p>
                  [<a id="dt-static-context" title="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" class="termref" href="#dt-static-error">static error</a>. </p><p>The individual components of the <a title="static context" class="termref" href="#dt-static-context">static context</a> are described below.</p><p class="xpath">In XPath 4.0, the static context for an expression is largely defined
               by the host language, that is, by the calling environment that causes an XPath
               expression to be evaluated. Most of the static context components are constant
               throughout an expression; the only exception is <a title="in-scope variables" class="termref" href="#dt-in-scope-variables">in-scope variables</a>.
               (There are constructs in the language, such as the <a href="#doc-xpath40-ForExpr">ForExpr</a>
               and <a href="#doc-xpath40-LetExpr">LetExpr</a>, that add additional variables to the static context
               of their subexpressions.)</p><p>Some components of the static context, but not all, also affect the dynamic semantics
            of expressions. For example, casting of a string such as <code>"xbrl:xbrl"</code> to
            an <code>xs:QName</code> might expand the prefix <code>xbrl</code> to the namespace
            URI <code>http://www.xbrl.org/2003/instance</code> using the <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a>
               from the static context;
            since the input string <code>"xbrl:xbrl"</code> is in general not known until execution time (it
            might be read from a source document), this means that the values of the 
            <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a> must be available at execution time.</p><ul><li><p>
                        [<a id="dt-xpath-compat-mode" title="XPath 1.0 compatibility     mode">Definition</a>: 
                           <b>XPath 1.0 compatibility
			 mode.</b>
                           <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>
                        ]
                     </p></li><li><p>
                        [<a id="dt-static-namespaces" title="statically known namespaces">Definition</a>: 
                           <b>Statically known namespaces.</b> 
        This is a mapping from prefix to namespace URI that defines all the namespaces 
        that are known during static processing of a given expression.]
                     </p><p>The URI value is whitespace normalized according to the rules for the
                     <code>xs:anyURI</code> type in <a href="https://www.w3.org/TR/xmlschema-2/#anyURI">Section 
               
                  3.2.17 anyURI
            </a><sup><small>XS1-2</small></sup> or 
                     <a href="https://www.w3.org/TR/xmlschema11-2/#anyURI">Section 
               
                  3.3.17 anyURI
            </a><sup><small>XS11-2</small></sup>.</p><p>Note the difference between <a title="in-scope namespaces" class="termref" href="#dt-in-scope-namespaces">in-scope namespaces</a>, which is a dynamic property of an element node, and <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a>, which is a static property of an expression.</p></li><li><p>
                        [<a id="dt-default-namespace-elements-and-types" title="default namespace for elements and types">Definition</a>: 
                           <b>Default namespace for elements and types.</b> This is either a
				namespace URI, or the special value <code>"##any"</code>, or <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>. This indicates how unprefixed QNames are interpreted when
                        they appear in a position  where an element name or type name is expected.]
                     </p><ul><li><p>If the value is set to a namespace URI, 
                        this namespace is used for any such unprefixed QName. The URI value is
               whitespace-normalized according to the rules for the <code>xs:anyURI</code> type in <a href="https://www.w3.org/TR/xmlschema-2/#anyURI">Section 
               
                  3.2.17 anyURI
            </a><sup><small>XS1-2</small></sup> or <a href="https://www.w3.org/TR/xmlschema11-2/#anyURI">Section 
               
                  3.3.17 anyURI
            </a><sup><small>XS11-2</small></sup>.</p></li><li><p>The special value <code>"##any"</code> indicates that:</p><ul><li><p>When an unprefixed QName is used as a <a title="name test" class="termref" href="#dt-name-test">name test</a> for selecting
                              named elements in an <a title="axis step" class="termref" href="#dt-axis-step">axis step</a>, the <a title="name test" class="termref" href="#dt-name-test">name test</a>
                           will match an element having the specified local name, in any namespace or none.</p></li><li><p>When an unprefixed QName is used in a context where a type name is expected
                           (but not as a function name), the default namespace is the <code>xs</code>
                           namespace, <code>http://www.w3.org/2001/XMLSchema</code>.</p></li><li><p>In any other context, an unprefixed QName represents a name in no namespace.</p></li></ul></li><li><p>If the value is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>,
                        an unprefixed QName representing an element or type
                        name is interpreted as being in no namespace.</p></li></ul></li><li><p>
                        [<a id="dt-default-function-namespace" title="default function namespace">Definition</a>: 
                           <b>Default function namespace.</b> This is either a namespace URI, or <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>. The namespace URI, if present, is used for any unprefixed QName appearing
                     in a position where a function name is expected.] The URI value is whitespace-normalized according
                     to the rules for the <code>xs:anyURI</code> type in <a href="https://www.w3.org/TR/xmlschema-2/#anyURI">Section 
               
                  3.2.17 anyURI
            </a><sup><small>XS1-2</small></sup> or <a href="https://www.w3.org/TR/xmlschema11-2/#anyURI">Section 
               
                  3.3.17 anyURI
            </a><sup><small>XS11-2</small></sup>
                     </p><p>
                        In its simplest form its value is simply a whitespace-normalized <code>xs:anyURI</code>
                        value (most commonly, the URI <code>http://www.w3.org/2005/xpath-functions</code>)
                        to be used as the default namespace for unprefixed function names. However, the use of a more
                        complex algorithm is not precluded, for example an algorithm which searches multiple namespaces for
                        a matching name.
                  </p></li><li><p>
                        [<a id="dt-issd" title="in-scope schema definitions">Definition</a>: 
                           <b>In-scope schema
			 definitions</b> is a generic term
			 for all the element declarations, attribute declarations, and schema type
			 definitions that are in scope during
			 static analysis of an expression.] It includes the
			 following three
			 parts:</p><ul><li><p>
                              [<a id="dt-is-types" title="in-scope schema type">Definition</a>: 
                                 <b>In-scope schema types.</b> Each schema type
			        definition is identified either by an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded
			        QName</a> (for a <b>named type</b>)
			        or by an <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> type
			        identifier (for an <b>anonymous
			        type</b>). The in-scope schema types include the predefined schema types described in <a href="#id-predefined-types"><b>3.5 Schema Types</b></a>.

                                
                           ]
                           </p></li><li><p>
                              [<a id="dt-is-elems" title="in-scope element declarations">Definition</a>: 
                                 <b>In-scope element declarations.</b> Each element
declaration is identified either by an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> (for a top-level element
declaration) or by an <a title="implementation   dependent" class="termref" 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" class="termref" href="#dt-substitution-group">substitution group</a> affiliation.</p><p>
                              [<a id="dt-substitution-group" title="substitution group">Definition</a>: 
                                 <b>Substitution groups</b> are defined in <a href="https://www.w3.org/TR/xmlschema-1/#Element_Equivalence_Class">Section 
                  
                     2.2.2.2 Element Substitution Group
               </a><sup><small>XS1-1</small></sup> and 
<a href="https://www.w3.org/TR/xmlschema11-1/#Element_Equivalence_Class">Section 
                  
                     2.2.2.2 Element Substitution Group
               </a><sup><small>XS11-1</small></sup>. 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 id="dt-is-attrs" title="in-scope attribute declarations">Definition</a>: 
                                 <b>In-scope attribute
declarations.</b> Each attribute declaration is identified either
by an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> (for a top-level attribute declaration) or by an
<a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> attribute identifier (for a local attribute
declaration).  
                           ]
                           </p></li></ul></li><li><p>
                        [<a id="dt-in-scope-variables" title="in-scope variables">Definition</a>: 
                           <b>In-scope variables.</b> 
                           This is a mapping from <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a> to sequence types. It defines the
                           set of variables that are available for reference within an
                           expression. The <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> is the name of the variable, and the type is the
                           <a title="static type" class="termref" href="#dt-static-type">static type</a> of the
                           variable.]
                     </p><p>
                     
An expression that binds a variable extends the <a title="in-scope variables" class="termref" href="#dt-in-scope-variables">in-scope variables</a>, within the scope of the variable, with the variable and its type. 
Within the body of an
<a title="inline function expression" class="termref" href="#dt-inline-func">inline function expression</a>, the
<a title="in-scope variables" class="termref" href="#dt-in-scope-variables">in-scope variables</a> are extended
by the names and types of the <b>function
parameters</b>.</p></li><li><p>
                        [<a id="dt-in-scope-named-item-types" title="in-scope named item types">Definition</a>: 
                           <b>In-scope named item types.</b> This is a mapping from 
                        <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a> to 
                        <a title="named item type" class="termref" href="#dt-named-item-type">named item types</a>.]
                     </p><p>
                        [<a id="dt-named-item-type" title="named item type">Definition</a>: A <b>named item type</b>
                      is an <code>ItemType</code> identified by an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>.]
                     </p><p>Named item types serve two purposes:</p><ul><li><p>They allow frequently used item types, especially complex item types such as
                        record types, to be given simple names, to avoid repeating the definition 
                        every time it is used.</p></li><li><p>They allow the definition of recursive types, which are useful for
                     describing recursive data structures such as lists and trees. For details see
                     <a href="#id-recursive-record-tests"><b>3.2.8.3.1 Recursive Record Types</b></a>. </p></li></ul><p>Certain named item types (typically item types used in the signatures of built-in functions)
                  are always available in the static context. These are defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#id-built-in-named-record-types">Section C Built-in named record types</a><sup><small>FO</small></sup>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p class="xpath">Named item types can be defined in a <a title="host language" class="termref" href="#dt-host-language">host language</a> 
                        such as XQuery 4.0 and in XSLT 4.0, but not in XPath 4.0 itself. They are available in XPath
                        only if the host language provides the ability to define them.</p></div></li><li><p>
                        [<a id="dt-statically-known-function-definitions" title="statically known function definitions">Definition</a>: 
                           <b>Statically known function definitions.</b> This is a set of 
                           <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a>.]
                     </p><p>Function definitions are described in <a href="#id-function-definitions"><b>2.2.1.1 Function Definitions</b></a>.</p></li><li><p>
                        [<a id="dt-static-collations" title="statically known collations">Definition</a>: 
                           <b>Statically known collations.</b> This is an <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>
        mapping from URI to collation. It defines the names of the collations that are available for
				use in processing  expressions.]
                        [<a id="dt-collation" title="collation">Definition</a>: A <b>collation</b> is a specification of the manner in which strings and URIs are compared and, by extension, ordered. For a more complete definition of collation, see <a href="https://qt4cg.org/specifications/xpath-functions-40/#string-compare">Section 5.3 Comparison of strings</a><sup><small>FO</small></sup>.]
                     </p></li><li><p>
                        [<a id="dt-static-base-uri" title="Static Base URI">Definition</a>: 
                           <b>Static Base URI.</b>
        This is an absolute URI, used to resolve relative URIs during static analysis.
                     ]
                     For example, it is used to resolve module location URIs in XQuery, and 
                     the URIs in <code>xsl:import</code> and <code>xsl:include</code> in XSLT.
                     
                     <span class="xpath">
        If <var>E</var> is a subexpression of <var>F</var> then the Static 
        Base URI of <var>E</var> is the same as the Static Base URI of <var>F</var>.
        There are no constructs in XPath that require resolution of relative URI references 
        during static analysis.
      </span>
                     </p><p>
                        Relative URI references are 
      resolved as described in <a href="#id-resolve-relative-uri"><b>2.5.6 Resolving a Relative URI Reference</b></a>.</p><p>At execution time,
                  relative URIs supplied to functions such as <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-doc"><code>fn:doc</code></a>
                  are resolved against the <a title="Executable Base URI" class="termref" href="#dt-executable-base-uri">Executable Base URI</a>,
                  which may or may not be the same as the Static Base URI.</p></li><li><p>
                        [<a id="dt-static-decimal-formats" title="statically known decimal formats">Definition</a>: 
                           <b>Statically known decimal
		      formats.</b> This is a mapping from QNames to decimal formats, with one default format that has no visible name,
		      referred to as the unnamed decimal format. Each
		      format is available for use when formatting numbers using the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-number"><code>fn:format-number</code></a> function.]
                     </p><p>Decimal formats are described in <a href="#id-decimal-formats"><b>2.2.1.2 Decimal Formats</b></a>.</p></li></ul><div class="div4">
<h5><a id="id-function-definitions"/>2.2.1.1 <a href="#id-function-definitions" style="text-decoration: none">Function Definitions</a></h5><p>
                     [<a id="dt-function-definition" title="function definition">Definition</a>: A <b>function definition</b>
                  contains information used to evaluate a static function call, including the name, parameters,
                  and return type of the function.]
                  </p><p>The properties of a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> include:</p><ul><li><p>The function name, which is an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>.</p></li><li><p>Parameter definitions, specifically:</p><ul><li><p>A (possibly empty) list of required parameters, each having:</p><ul><li><p>a parameter name (an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>)</p></li><li><p>a required type (a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>)</p></li></ul></li><li><p>A (possibly empty) list of optional parameters, each having:</p><ul><li><p>a parameter name (an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>)</p></li><li><p>a required type (a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>)</p></li><li><p>a default value expression (an <b>expression</b>: see <a href="#id-expressions"><b>4 Expressions</b></a>)</p></li></ul></li></ul><p>The names of the parameters must be distinct.</p><p>
                           [<a id="dt-arity-range" title="arity range">Definition</a>: A <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> has an <b>arity range</b>,
                        which is a range of consecutive non-negative integers. If the function definition has <var>M</var> required parameters
                        and <var>N</var> optional parameters, then its arity range is from <var>M</var> to <var>M</var>+<var>N</var>
                        inclusive.]
                        </p><p>The static context may contain several <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a> with the
                        same name, but the <a title="arity range" class="termref" href="#dt-arity-range">arity ranges</a> of two such function definitions must not 
                        overlap. For example, if two function definitions <var>A</var> and <var>B</var> have the same function name, then:</p><ul><li><p>It is acceptable for <var>A</var> to have two required parameters and no optional
                           parameters, while <var>B</var> has three required parameters and one optional
                           parameter.</p></li><li><p>It is not acceptable for <var>A</var> to have one required parameter while <var>B</var>
                           has three optional parameters.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>Implementations must ensure that no two <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a> 
                        have the same <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> and overlapping
                        arity ranges (even if the signatures are consistent).</p><p>XQuery and XSLT enforce this rule by defining a static error if the rule is violated; but further constraints
                           may be needed if an API allows external functions to be added to the static context.</p></div></li><li><p>A return type (a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>)</p></li><li><p>The function category, which is one of application, system, or external:</p><ul><li><p>
                                 [<a id="dt-application-function" title="application function">Definition</a>: 
                                    <b>Application functions</b> are function definitions written in a 
                           host language such as XQuery or XSLT whose syntax and
                           semantics are defined in this family of specifications. Their behavior 
                           (including the rules determining the static and dynamic context) follows the
                           rules for such functions in the relevant host language specification.]
                           The most common application functions are functions written by users in XQuery or XSLT.
                        </p></li><li><p>
                                 [<a id="dt-system-function" title="system function">Definition</a>: 
                                    <b>System functions</b> include the functions defined in <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>, functions defined by the specifications 
                           of a host language, <a title="constructor function" class="termref" href="#dt-constructor-function">constructor functions</a> 
                           for atomic types, and any additional functions provided
                           by the implementation. System functions are sometimes called built-in
                           functions.]
                              </p><p>The behavior of system functions follows the rules given for the individual
                              function in this family of specifications, or in the specification of the
                              particular processor implementation. A system function may have behavior that depends on the
                              static or dynamic context of the caller (for example, comparing strings
                              using the default collation from the <span>dynamic</span> context of the caller). Such
                              functions are said to be <a title="context dependent" class="termref" href="#dt-context-dependent">context dependent</a>.</p></li><li><p>
                                 [<a id="dt-external-function" title="external function">Definition</a>: 
                                    <b>External functions</b> 
                           can be characterized as functions that are neither
                           part of the processor implementation, nor written in a language whose semantics
                           are under the control of this family of specifications. The semantics of external
                           functions, including any context dependencies, are entirely implementation-defined. 
                           In XSLT, external functions are called 
                           <a href="https://www.w3.org/TR/xslt-30/#extension-functions">Section 
            
               24.1 Extension Functions
         </a><sup><small>XT30</small></sup>. ]
                              </p><p>For example, an implementation might provide a mechanism allowing
                              external functions to be written in a language such as Java or Python.
                              The way in which argument and return values are converted between
                              the XDM type system and the type system of the external language is
                              implementation-defined.
                           </p></li></ul><p>
                           [<a id="dt-context-dependent" title="context dependent">Definition</a>: A 
                        <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> is said to be <b>context dependent</b>
                        if its result depends on the static or dynamic context of its caller.
                        A function definition may
                        be context-dependent for some arities in its arity range, and context-independent
                        for others: for example <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-name"><code>fn:name#0</code></a> is context-dependent
                        while <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-name"><code>fn:name#1</code></a> is context-independent.]
                        </p><div class="note"><p class="prefix"><b>Note:</b></p><p>Some system functions, such as <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-position"><code>fn:position</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-last"><code>fn:last</code></a>,
                        and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-static-base-uri"><code>fn:static-base-uri</code></a>, exist for the sole purpose of providing information
                        about the static or dynamic context of their caller.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>
                              <a title="application function" class="termref" href="#dt-application-function">Application functions</a> 
                        are context dependent only to the extent that they define optional parameters with default
                        values that are context dependent.</p></div></li><li><p>A (possibly empty) set of <b>function annotations</b>
                        </p></li><li><p>A body. The function 
                     body contains the logic that enables the function
                     result to be computed from the supplied arguments and information in the static and dynamic context.</p></li></ul><p>The <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a> 
                  present in the static context are available for reference from a 
                  <a title="static function call" class="termref" href="#dt-static-function-call">static function call</a>,
                  or from a 
                  <a title="named function reference" class="termref" href="#dt-named-function-ref">named function reference</a>.
                  
               </p></div><div class="div4">
<h5><a id="id-decimal-formats"/>2.2.1.2 <a href="#id-decimal-formats" style="text-decoration: none">Decimal Formats</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#eval_context">⬇</a> <a href="#static_context">⬆</a></p><ol><li><p>
                           Several decimal format properties, including minus sign, exponent separator, percent, and per-mille,
                           can now be rendered as arbitrary strings rather than being confined to a single
                           character.
                        <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1048">1048</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1250">1250</a> 3 June 2024]</i></p></li></ol></div><p>Each decimal format defines a set of properties, which control the interpretation of characters
                        in the picture string supplied to the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-number"><code>fn:format-number</code></a>
                        function, and also specify characters to be used in the result
                        of formatting the number.</p><p>Each property potentially has two parts: a marker character <var>M</var> used
                     in the picture string to mark an insertion position, and a rendition string <var>R</var>
                     to indicate how the relevant property is to be rendered in the output of the 
                     <code>fn:format-number</code> function. In the list below properties are annotated
                     with <em>(<var>M</var>)</em>, <em>(<var>R</var>)</em>, or 
                     <em>(<var>M</var>, <var>R</var>)</em> to indicate whether the property includes a
                     marker character, a rendition string, or both.</p><p>In XQuery and XSLT declarations
                     defining the values of properties, a property where the marker character and the
                     rendition differ is indicated using the syntax <code>
                        <var>M</var>:<var>R</var>
                     </code>. For
                     example the <code>percent</code> property may be expressed as <code>%:pc</code>
                     to indicate that the character <code>%</code> will be used in the picture string,
                     and the string <code>pc</code> will be used in the function output.
                     In this example, the value <code>0.10</code>, formatted with the picture string 
                      <code>#0%</code>, results in the output <code>10pc</code>
                  </p><ul><li><p>
                           [<a id="id-static-decimal-format-decimal-separator" title="decimal-separator">Definition</a>: 
                              <b>decimal-separator</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is
                                 used to separate the integer part of the number from the fractional part.
                                 The default value for both the marker and the rendition is <span class="unicode-codepoint">U+002E</span> (<span class="unicode-name">FULL STOP, PERIOD</span>, <code>.</code>) .]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-exponent-separator" title="exponent-separator">Definition</a>: 
                              <b>exponent-separator</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is
                                 used to separate the mantissa from the exponent in
                                 scientific notation. The default value 
                                 for both the marker and the rendition is <span class="unicode-codepoint">U+0065</span> (<span class="unicode-name">LATIN SMALL LETTER E</span>, <code>e</code>) .]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-grouping-separator" title="grouping-separator">Definition</a>: 
                              <b>grouping-separator</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is used to 
                                 separate groups of digits (for example as a thousands separator).
                                 The default value for both the marker and the rendition is <span class="unicode-codepoint">U+002C</span> (<span class="unicode-name">COMMA</span>, <code>,</code>) .]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-percent" title="percent">Definition</a>: 
                              <b>percent</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is used to 
                                 indicate that the number is written as a per-hundred fraction; the default
                                 value for both the marker and the rendition is <span class="unicode-codepoint">U+0025</span> (<span class="unicode-name">PERCENT SIGN</span>, <code>%</code>) .]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-per-mille" title="per-mille">Definition</a>: 
                              <b>per-mille</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is used to 
                                 indicate that the number is written as a per-thousand fraction; the default
                                 value for both the marker and the rendition is <span class="unicode-codepoint">U+2030</span> (<span class="unicode-name">PER MILLE SIGN</span>, <code>‰</code>) .]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-zero-digit" title="zero-digit">Definition</a>: 
                              <b>zero-digit</b>
                              <em>(<var>M</var>)</em>
                                 is the character used in the picture string to represent the digit zero; the default
                                 value is <span class="unicode-codepoint">U+0030</span> (<span class="unicode-name">DIGIT ZERO</span>, <code>0</code>) . This character must be a digit
                                 (category Nd in the Unicode property database), and it must have
                                 the numeric value zero. This property implicitly defines the
                                 ten Unicode characters that are used to represent the values 0
                                 to 9 in the function output: Unicode is organized so that each
                                 set of decimal digits forms a contiguous block of characters in
                                 numerical sequence. Within the picture string any of these ten character 
                                 can be used (interchangeably) as a place-holder for a mandatory digit.
                                 Within the final result string, these ten characters are used to represent
                                 the digits zero to nine.]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-digit" title="digit">Definition</a>: 
                              <b>digit</b>
                              <em>(<var>M</var>)</em>
                                 is a character used in the picture string to represent an optional digit; 
                                 the default value is <span class="unicode-codepoint">U+0023</span> (<span class="unicode-name">NUMBER SIGN</span>, <code>#</code>) .]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-pattern-separator" title="pattern-separator">Definition</a>: 
                              <b>pattern-separator</b>
                              <em>(<var>M</var>)</em> is a character used
                                 to separate positive and negative sub-pictures
                                 in a picture string; the default value is <span class="unicode-codepoint">U+003B</span> (<span class="unicode-name">SEMICOLON</span>, <code>;</code>) .]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-infinity" title="infinity">Definition</a>: 
                              <b>infinity</b>
                              <em>(<var>R</var>)</em>
                                 is the string used to represent the double value infinity (<code>INF</code>); the
                                 default value is the string <code>"Infinity"</code>
                           ]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-NaN" title="NaN">Definition</a>: 
                              <b>NaN</b>
                              <em>(<var>R</var>)</em>
                                 is the string used to
                                 represent the double value <code>NaN</code> (not a number); the default value is the string <code>"NaN"</code>
                           ]
                        </p></li><li><p>
                           [<a id="id-static-decimal-format-minus-sign" title="minus-sign">Definition</a>: 
                              <b>minus-sign</b>
                              <em>(<var>R</var>)</em> is the string used to mark negative numbers; the
                                 default value is <span class="unicode-codepoint">U+002D</span> (<span class="unicode-name">HYPHEN-MINUS</span>, <code>-</code>) .]
                        </p></li></ul></div></div><div class="div3">
<h4><a id="eval_context"/>2.2.2 <a href="#eval_context" style="text-decoration: none">Dynamic Context</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-processing-model">⬇</a> <a href="#id-decimal-formats">⬆</a></p><ol><li><p>
                  The concept of the context item has been generalized, so it is now a context value. That is,
                  it is no longer constrained to be a single item.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/129">129</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/368">368</a> 14 September 2023]</i></p></li><li><p>
                  The rules regarding the <code>document-uri</code> property of nodes returned by the
                  <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a> function have been relaxed.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1161">1161</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1265">1265</a> 11 June 2024]</i></p></li></ol></div><p>
                  [<a id="dt-dynamic-context" title="dynamic context">Definition</a>: The <b>dynamic
context</b> of an expression is defined as information that is needed for the dynamic evaluation of an expression,
                  beyond any information that is needed from the <a title="static context" class="termref" href="#dt-static-context">static context</a>.] If
evaluation of an expression relies on some part of the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a> that 
is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>, a <a title="type error" class="termref" href="#dt-type-error">type error</a> 
               is raised [<a href="#ERRXPDY0002" title="err:XPDY0002">err:XPDY0002</a>].</p><div class="note"><p class="prefix"><b>Note:</b></p><p>In previous versions of the specification, this was classified as a
            <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>. The change allows the error to be raised during
            static analysis when possible; for example a function written as
            <code>fn($x) { @code }</code> can now be reported as an error whether or not
            the function is actually evaluated. The actual error code remains unchanged
            for backwards compatibility reasons.</p><p>There are other cases where static detection of the error is not possible.</p></div><p>The individual
components of the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a> are described below.</p><p>In general, the dynamic context for the outermost expression is supplied externally,
               often by some kind of application programming interface (API) allowing XPath 4.0
               expressions to be invoked from a host language. Application Programming Interfaces
               are outside the scope of this specification.  The dynamic context for inner subexpressions
               may be set by their containing expressions: for example in a mapping expression 
               <code>
                     <var>E<sub>1</sub></var>!<var>E<sub>2</sub></var>
                  </code>,
               the value of the <a title="focus" class="termref" href="#dt-focus">focus</a> (part of the dynamic context) for evaluation
               of <var>E<sub>2</sub></var> is defined by the evaluation of <var>E<sub>1</sub></var>.</p><p>Some aspects of the dynamic context are outside the direct control of the query author;
               they are defined by the implementation, which may or may not allow them to be configured by users.
               An example is <a title="available documents" class="termref" href="#dt-available-docs">available documents</a>, which is an abstraction for the set of XML
               documents that can be retrieved by URI from within an expression. In some environments this may be
               the entire contents of the web; in others it may be constrained to documents that satisfy
               particular security constraints; and in some environments the set of available documents
               might even be empty. These components of the dynamic context are generally treated as being constant for
               the duration of the execution.</p><p>
                  <span class="xpath">Further rules governing the semantics of these components can be found in <a href="#id-xp-evaluation-context-components"><b>B.2 Dynamic Context Components</b></a>.</span>
               </p><p>The components of the 
           <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a> are listed below.</p><p>
                  [<a id="dt-focus" title="focus">Definition</a>: The first three components of
the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a>
(context value, context position, and context size) are called the
<b>focus</b> of the expression. ] The focus enables the
processor to keep track of which items are being processed by the
expression.



<span class="xpath">If any component in the focus is defined, both the context value and context position are known.</span>
               </p><div class="note"><p class="prefix"><b>Note:</b></p><p>If any component in the focus is defined, context size is usually defined as well.  However, when streaming, 
the context size cannot be determined without lookahead, so it may be undefined.  If so, expressions like <code>last()</code> will 
                     raise a dynamic error because the context size is undefined.</p></div><p>
                  [<a id="dt-fixed-focus" title="fixed focus">Definition</a>: A <b>fixed focus</b> is a focus for an expression that is evaluated once, 
                  rather than being applied to a series of values; in a fixed focus, 
                  the context value is set to one specific value, the context position is 1, and the context size is 1.]
               </p><p>
                  [<a id="dt-singleton-focus" title="singleton focus">Definition</a>: A <b>singleton focus</b> is a <a title="fixed focus" class="termref" href="#dt-fixed-focus">fixed focus</a> in which the
                  <a title="context value" class="termref" href="#dt-context-value">context value</a> is a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> item.].
               With a singleton focus, the context value is a single item, the context position is 1, and the context size is 1.
               </p><p>Certain language constructs, notably the <a title="path expression" class="termref" href="#dt-path-expression">path operator</a>
                  <code>
                     <var>E<sub>1</sub></var>/<var>E<sub>2</sub></var>
                  </code>, the <a href="#doc-xpath40-SimpleMapExpr">simple map operator</a>
                  <code>
                     <var>E<sub>1</sub></var>!<var>E<sub>2</sub></var>
                  </code>, and the <a title="" class="termref" href="#dt-predicate">predicate</a>
                  <code>
                     <var>E<sub>1</sub></var>[<var>E<sub>2</sub></var>]</code>, create a new focus
for the evaluation of a sub-expression. In these constructs, <var>E<sub>2</sub></var> is evaluated once for each item in the
sequence that results from evaluating <var>E<sub>1</sub></var>. Each time <var>E<sub>2</sub></var> is evaluated, it is evaluated with a
different focus. The focus for evaluating <var>E<sub>2</sub></var> is referred to below as the <b>inner
focus</b>, while the focus for evaluating <var>E<sub>1</sub></var> is referred to as the <b>outer
focus</b>. The inner focus is used only for the evaluation of <var>E<sub>2</sub></var>. Evaluation of <var>E<sub>1</sub></var> continues with its original focus unchanged.</p><ul><li><p>
                        [<a id="dt-context-value" title="context value">Definition</a>: The <b>context value</b>
is the <a title="value" class="termref" href="#dt-value">value</a> currently being processed.]
                     In many cases (but not always), the context value will be a single item.
                     [<a id="dt-context-node" title="context node">Definition</a>: When the context value is a single item, it can also be referred
                        to as the <b>context item</b>; when it is a single node,
it can also be referred to as the <b>context
node</b>.] The context value is returned by an expression
consisting of a single dot (<code>.</code>). When an expression <code>
                           <var>E<sub>1</sub></var>/<var>E<sub>2</sub></var>
                        </code> or <code>
                           <var>E<sub>1</sub></var>[<var>E<sub>2</sub></var>]</code> is evaluated, each item in the
sequence obtained by evaluating <code>
                           <var>E<sub>1</sub></var>
                        </code>
becomes the context value in the inner focus for an evaluation of <var>E<sub>2</sub></var>. </p></li><li><p>
                        [<a id="dt-context-position" title="context position">Definition</a>: The <b>context
position</b> is the position of the context value within the
series of values currently being processed.] It changes whenever the context value
changes. When the focus is defined, the value of the context position is an integer greater than zero. The context
position is returned by the expression <code>fn:position()</code>. When an expression <code>
                           <var>E<sub>1</sub></var>/<var>E<sub>2</sub></var>
                        </code> or <code>
                           <var>E<sub>1</sub></var>[<var>E<sub>2</sub></var>]</code> is evaluated, the context position in
the inner focus for an evaluation of <code>E2</code>
is the position of the context value in the sequence obtained by
evaluating <var>E<sub>1</sub></var>. 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 id="dt-context-size" title="context size">Definition</a>: The <b>context
size</b> is the number of values in the series of values 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>
                           <var>E<sub>1</sub></var>/<var>E<sub>2</sub></var>
                        </code> or <code>
                           <var>E<sub>1</sub></var>[<var>E<sub>2</sub></var>]</code> is evaluated, the context size in the
inner focus for an evaluation of <var>E<sub>2</sub></var> is
the number of items in the sequence obtained by evaluating <var>E<sub>1</sub></var>. </p></li><li><p>
                        [<a id="dt-variable-values" title="variable values">Definition</a>: 
                           <b>Variable values</b>. 
        This is a mapping from <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a> to values. 
        It contains the
				same <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a> as the <a title="in-scope variables" class="termref" href="#dt-in-scope-variables">in-scope variables</a> in the
				<a title="static context" class="termref" href="#dt-static-context">static context</a> for the expression. The <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> is the name of the variable and the value is the dynamic value of the variable, which includes its <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a>.]
                     </p></li><li><p>
                        [<a id="dt-dynamically-known-function-definitions" title="dynamically known function definitions">Definition</a>: 
                           <b>Dynamically known function definitions</b>.
        This is a set of <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a>. It includes the
                        <a title="statically known function definitions" class="termref" href="#dt-statically-known-function-definitions">statically known function definitions</a> as a subset, but may include
                        other function definitions that are not known statically.
      ]
                     </p><p>The function definitions in the dynamic context are used primarily by the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-function-lookup"><code>fn:function-lookup</code></a>
                  function.</p><p>If two function definitions in the <a title="dynamically known function definitions" class="termref" href="#dt-dynamically-known-function-definitions">dynamically known function definitions</a> have the same
                     name, then their <a title="arity range" class="termref" href="#dt-arity-range">arity ranges</a> must not overlap.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The reason for allowing named functions to be available dynamically beyond those that are
                  available statically is primarily to allow for cases where the run-time execution
                  environment is significantly different from the compile-time environment. This could happen, for example,
                  if a stylesheet or query is compiled within a web server and then executed in the web browser.
                  The <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-function-lookup"><code>fn:function-lookup</code></a> function allows dynamic discovery of resources that were not
                  available statically.</p></div></li><li><p>
                        [<a id="dt-date-time" title="current dateTime">Definition</a>: 
                           <b>Current dateTime.</b> This information represents
				an <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> point in time during the processing of 
                        <span class="xpath">an expression</span>, and includes an explicit timezone. It can be retrieved by the  <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-current-dateTime"><code>fn:current-dateTime</code></a> function. 
                        If called multiple times during the execution of 
                        <span class="xpath">an expression</span>,
				this function always returns the same result.]
                     </p></li><li><p>
                        [<a id="dt-timezone" title="implicit 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 arithmetic operation. The implicit timezone is an  <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a> value of type
<code>xs:dayTimeDuration</code>. See <a href="https://www.w3.org/TR/xmlschema-2/#dateTime-timezones">Section 
                  
                     3.2.7.3 Timezones
               </a><sup><small>XS1-2</small></sup> or
<a href="https://www.w3.org/TR/xmlschema11-2/#dateTime">Section 
               
                  3.3.7 dateTime
            </a><sup><small>XS11-2</small></sup> for the range of valid values of a timezone.]
                     </p></li><li><p>
                        [<a id="dt-executable-base-uri" title="Executable Base URI">Definition</a>: 
                           <b>Executable Base URI.</b> This is an absolute URI used
                     to resolve relative URIs during the evaluation of expressions;
                     it is used, for example, to resolve a relative URI supplied
                     to the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-doc"><code>fn:doc</code></a> or <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-unparsed-text"><code>fn:unparsed-text</code></a>
                     functions.
                  ]
                     </p><p>URIs are resolved as described in <a href="#id-resolve-relative-uri"><b>2.5.6 Resolving a Relative URI Reference</b></a>.</p><p>The function <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-static-base-uri"><code>fn:static-base-uri</code></a>, despite its name, returns the
                     value of the <a title="Executable Base URI" class="termref" href="#dt-executable-base-uri">Executable Base URI</a>.</p><p>In many straightforward processing scenarios, the <a title="Executable Base URI" class="termref" href="#dt-executable-base-uri">Executable Base URI</a>
                  in the dynamic context will be the same as the <a title="Static Base URI" class="termref" href="#dt-static-base-uri">Static Base URI</a> for the
                     corresponding expression in the static context. There are situations, however, where they may differ:</p><ul><li><p>Some processors may allow the static analysis of a query or stylesheet
                     to take place on a development machine, while execution of the query or stylesheet
                     happens on a test or production server. In this situation, resources needed during
                     static analysis (such as other modules of the query or stylesheet) will be located
                     on the development machine, by reference to the <a title="Static Base URI" class="termref" href="#dt-static-base-uri">Static Base URI</a>,
                     while resources needed during execution (such as reference data files) will be located
                     on the production machine, accessed via the <a title="Executable Base URI" class="termref" href="#dt-executable-base-uri">Executable Base URI</a>.</p></li><li><p>When the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-static-base-uri"><code>fn:static-base-uri</code></a> function is called within
                     the initializing expression of an optional parameter in a function declaration,
                     it returns the executable base URI of the relevant function call. This allows a user-written
                     function to accept two parameters: a required parameter containing a relative URI, and an
                     optional parameter containing a base URI. The optional parameter can be given a default
                     value of <code>fn:static-base-uri()</code>, allowing the code in the function body
                     to resolve the relative URI against the executable base URI of the caller.</p></li></ul></li><li><p>
                        [<a id="dt-def-collation" title="default collation">Definition</a>: 
                           <b>Default
                           collation.</b> This identifies one of the collations in <a title="statically known collations" class="termref" href="#dt-static-collations">statically known collations</a> as the  collation to be
                        used by functions and operators for comparing and ordering values of type <code>xs:string</code> and <code>xs:anyURI</code> (and types derived from them) when no
                        explicit collation is
                        specified.]
                     </p><div class="note"><p class="prefix"><b>Note:</b></p><p>Although the default collation is defined (in 4.0) as a property of the
                     dynamic context, its value will in nearly all cases be known statically. The reason it is defined in the
                     dynamic context is to allow a call on the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-default-collation"><code>fn:default-collation</code></a> function to be used when defining
                     the default value of an optional parameter to a user-defined function. In this situation,
                     the actual value supplied for the parameter is taken from the dynamic context of the relevant function call.</p></div></li><li><p>
                        [<a id="dt-default-language" title="default language">Definition</a>: 
                           <b>Default language.</b>
  This is the natural language used when creating human-readable output
  (for example, by the functions <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a> and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-integer"><code>fn:format-integer</code></a>)
  if no other language is requested. 
  The value is a language code as defined by the type <code>xs:language</code>.]
                     </p></li><li><p>
                        [<a id="dt-default-calendar" title="default calendar">Definition</a>: 
                           <b>Default calendar.</b>
    This is the calendar used when formatting dates in human-readable output
    (for example, by the functions <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a> and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-dateTime"><code>fn:format-dateTime</code></a>)
    if no other calendar is requested. 
    The value is a string.]
                     </p></li><li><p>
                        [<a id="dt-default-place" title="default place">Definition</a>: 
                           <b>Default place.</b>
    This is a geographical location used to identify the place where events happened (or will happen) when
    processing dates and times using functions such as <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-dateTime"><code>fn:format-dateTime</code></a>,
     and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-civil-timezone"><code>fn:civil-timezone</code></a>,
    if no other place is specified. It is used when translating timezone offsets to civil timezone names,
    and when using calendars where the translation from ISO dates/times to a local representation is dependent
    on geographical location. Possible representations of this information are an ISO country code or an
    Olson timezone name, but implementations are free to use other representations from which the above
    information can be derived. The only requirement is that it should uniquely identify a civil timezone,
    which means that country codes for countries with multiple timezones, such as the United States,
    are inadequate.]
                     </p></li><li><p>
                        [<a id="dt-available-docs" title="available documents">Definition</a>: 
                           <b>Available
    documents.</b> This is a mapping of strings to document nodes.  Each 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" class="termref" href="#dt-datamodel">data model</a>. The document node is returned by the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-doc"><code>fn:doc</code></a> 
    function when applied to that URI.] The set of available documents may be empty.</p></li><li><p>
                        [<a id="dt-available-text-resources" title="available text resources">Definition</a>: 
                           <b>Available text resources</b>. 
  This is a mapping of strings to text resources. Each string
  represents the absolute URI of a resource. The resource is returned
  by the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-unparsed-text"><code>fn:unparsed-text</code></a> function when applied to that
  URI.] The set of available text resources may be empty.</p></li><li><p>
                        [<a id="dt-available-binary-resources" title="available binary resources">Definition</a>: 
                           <b>Available binary resources</b>. 
  This is a mapping of strings to binary resources. Each string
  represents the absolute URI of a resource. The resource is returned
  by the <code>fn:unparsed-binary</code> function when applied to that
  URI.] The set of available binary resources may be empty.</p></li><li><p>
                        [<a id="dt-available-collections" title="available item collections">Definition</a>: 
                           <b>Available
                           collections.</b> This is a mapping of
                         strings to sequences of items. Each string
                         represents the absolute URI of a
                         resource. The sequence of items represents
                         the result of the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a>
                         function when that URI is supplied as the
                         argument. ] The set of available
                            collections may be empty.</p><p>Ideally, for every document node <code>D</code> that is in the target of a 
                     mapping in <a title="available item collections" class="termref" href="#dt-available-collections">available item collections</a>, or that is the root of a tree containing 
                     such a node, the document-uri property of <code>D</code> should either be absent, 
                     or should be a URI <code>U</code> such that <a title="available documents" class="termref" href="#dt-available-docs">available documents</a> contains a mapping from <code>U</code> to <code>D</code>.

</p><div class="note"><p class="prefix"><b>Note:</b></p><p>That is to say, the <code>document-uri</code> property of nodes returned
                        by the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a> function should be such that
                        calling <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-doc"><code>fn:doc</code></a> with that URI returns the relevant node.</p><p>It is not always possible to ensure this, especially in cases where 
                        dereferencing of document or collection URIs is configurable using
                        configuration files or user-supplied resolver code.</p></div></li><li><p>
                        [<a id="dt-default-collection" title="default collection">Definition</a>: 
                           <b>Default  collection.</b>
    This is the sequence of items  that would result from calling the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a> function
    with no arguments.] The value of <b>default   collection</b> may be initialized by the
    implementation.</p></li><li><p>
                        [<a id="dt-available-uri-collections" title="available uri collections">Definition</a>: 
                           <b>Available
    URI collections.</b> This is a mapping of
    strings to sequences of URIs. The string
    represents the absolute URI of a
    resource which can be interpreted as an aggregation of a number of individual resources each of which
    has its own URI. The sequence of URIs represents
    the result of the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-uri-collection"><code>fn:uri-collection</code></a>
    function when that URI is supplied as the
    argument. ] There is no implication that the URIs in this sequence
    can be successfully dereferenced, or that the resources they refer to have any particular media type.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>An implementation <span class="verb">may</span> maintain some consistent relationship between the available
    collections and the available URI collections, for example by ensuring that the result of
  <code>fn:uri-collection(X)!fn:doc(.)</code> is the same as the result of <code>fn:collection(X)</code>.
    However, this is not required. The <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-uri-collection"><code>fn:uri-collection</code></a> function is more 
    general than <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a> in that 
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a> allows access to 
    nodes that might lack individual URIs, for example nodes corresponding 
    to XML fragments stored in the rows of a relational database.</p></div></li><li><p>
                        [<a id="dt-default-uri-collection" title="default URI collection">Definition</a>: 
                           <b>Default URI collection.</b>
    This is the sequence of URIs that would result from calling the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-uri-collection"><code>fn:uri-collection</code></a> function
    with no arguments.] The value of <b>default URI collection</b> may be initialized by the
    implementation.</p></li><li><p>
                        [<a id="dt-environment-variables" title="environment variables">Definition</a>: 
                           <b>Environment variables.</b>
  This is a mapping from names to values. 
    Both the names and the values are strings. The names are compared using an
    <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a> collation, and are unique under this collation. The set of environment variables is
  <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a> and <span class="verb">may</span> be empty.]
                     </p><div class="note"><p class="prefix"><b>Note:</b></p><p>A possible implementation is to provide the set of POSIX environment variables (or their equivalent on other
      operating systems) appropriate to the process in which the 
                        <span class="xpath">expression is evaluated</span>.</p></div></li></ul></div></div><div class="div2">
<h3><a id="id-processing-model"/>2.3 <a href="#id-processing-model" style="text-decoration: none">Processing Model</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-guarded-expressions">⬇</a> <a href="#eval_context">⬆</a></p><ol><li><p>
             The static typing option has been dropped.
           <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1343">1343</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1344">1344</a> 3 September 2024]</i></p></li></ol></div><p>The semantics of XPath 4.0 are defined in terms
                         of the <a title="data model" class="termref" href="#dt-datamodel">data
                         model</a> and the <a title="expression context" class="termref" href="#dt-expression-context">expression
                         context</a>.</p><div class="svg-image"><!-- Generated by graphviz version 12.2.1 (20241206.2353)
 --><!-- Title: Processing_Model Pages: 1 --><svg width="592pt" height="667pt" viewBox="0.00 0.00 591.62 666.50" preserveAspectRatio="xMidYMid meet" zoomAndPan="magnify" version="1.1" contentScriptType="text/ecmascript" contentStyleType="text/css"><g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 662.5)"><title>Processing_Model</title><g id="a_graph0"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="XPath Processing Model" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="white" stroke="none" points="-4,4 -4,-662.5 587.62,-662.5 587.62,4 -4,4"/>
</a></g><g id="clust1" class="cluster"><title>clusterQT4</title><g id="a_clust1"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="XPath Expression Processing" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="none" stroke="black" points="260,-80.5 260,-575 488,-575 488,-80.5 260,-80.5"/>
<text text-anchor="middle" x="374" y="-557.7" font-family="Times,serif" font-size="14.00">XPath Expression Processing</text>
</a></g></g><g id="clust2" class="cluster"><title>clusterEP</title><g id="a_clust2"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="External processing" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="none" stroke="black" points="8,-174 8,-349 252,-349 252,-174 8,-174"/>
<text text-anchor="middle" x="130" y="-331.7" font-family="Times,serif" font-size="14.00">External processing</text>
</a></g></g><!-- Exec --><g id="node1" class="node"><title>Exec</title><g id="a_node1"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Execution Engine" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M332.38,-223C332.38,-223 285.62,-223 285.62,-223 279.62,-223 273.62,-217 273.62,-211 273.62,-211 273.62,-194 273.62,-194 273.62,-188 279.62,-182 285.62,-182 285.62,-182 332.38,-182 332.38,-182 338.38,-182 344.38,-188 344.38,-194 344.38,-194 344.38,-211 344.38,-211 344.38,-217 338.38,-223 332.38,-223"/>
<text text-anchor="middle" x="309" y="-205.7" font-family="Times,serif" font-size="14.00">Execution</text>
<text text-anchor="middle" x="309" y="-189.2" font-family="Times,serif" font-size="14.00">Engine</text>
</a></g></g><!-- XDM --><g id="node2" class="node"><title>XDM</title><g id="a_node2"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#dt-data-model-instance" xlink:title="XPath Data Model" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="none" stroke="black" points="347.5,-129.5 268.5,-129.5 268.5,-88.5 347.5,-88.5 347.5,-129.5"/>
<text text-anchor="middle" x="308" y="-112.2" font-family="Times,serif" font-size="14.00" fill="blue">XPath Data</text>
<text text-anchor="middle" x="308" y="-95.7" font-family="Times,serif" font-size="14.00" fill="blue">Model</text>
</a></g></g><!-- Exec&#45;&gt;XDM --><g id="edge8" class="edge"><title>Exec-&gt;XDM</title><g id="a_edge8"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title=" DQ4" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M308.66,-170.46C308.56,-161.01 308.44,-150.62 308.34,-141.16"/>
<polygon fill="black" stroke="black" points="305.16,-170.17 308.77,-180.13 312.16,-170.09 305.16,-170.17"/>
<polygon fill="black" stroke="black" points="311.84,-141.29 308.23,-131.33 304.84,-141.37 311.84,-141.29"/>
</a></g><g id="a_edge8-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title="Access and create" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="323.59" y="-150.7" font-family="Times,serif" font-size="14.00" fill="blue"> DQ4</text>
</a></g></g><!-- Serialize --><g id="node15" class="node"><title>Serialize</title><polygon fill="none" stroke="black" points="333.62,-36 276.38,-36 276.38,0 339.62,0 339.62,-30 333.62,-36"/><polyline fill="none" stroke="black" points="333.62,-36 333.62,-30"/><polyline fill="none" stroke="black" points="339.62,-30 333.62,-30"/><text text-anchor="middle" x="308" y="-12.95" font-family="Times,serif" font-size="14.00">Serialize</text></g><!-- XDM&#45;&gt;Serialize --><g id="edge17" class="edge"><title>XDM-&gt;Serialize</title><g id="a_edge17"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#id-serialization" xlink:title=" DM4" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M308,-88.25C308,-76.33 308,-60.94 308,-47.69"/>
<polygon fill="black" stroke="black" points="311.5,-47.81 308,-37.81 304.5,-47.81 311.5,-47.81"/>
</a></g><g id="a_edge17-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#id-serialization" xlink:title="Serialize" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="324.5" y="-57.2" font-family="Times,serif" font-size="14.00" fill="blue"> DM4</text>
</a></g></g><!-- AST --><g id="node3" class="node"><title>AST</title><g id="a_node3"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ5" xlink:title="Abstract Syntax Tree (AST)" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M337.62,-316.5C337.62,-316.5 280.38,-316.5 280.38,-316.5 274.38,-316.5 268.38,-310.5 268.38,-304.5 268.38,-304.5 268.38,-287.5 268.38,-287.5 268.38,-281.5 274.38,-275.5 280.38,-275.5 280.38,-275.5 337.62,-275.5 337.62,-275.5 343.62,-275.5 349.62,-281.5 349.62,-287.5 349.62,-287.5 349.62,-304.5 349.62,-304.5 349.62,-310.5 343.62,-316.5 337.62,-316.5"/>
<text text-anchor="middle" x="309" y="-299.2" font-family="Times,serif" font-size="14.00" fill="blue">Abstract</text>
<text text-anchor="middle" x="309" y="-282.7" font-family="Times,serif" font-size="14.00" fill="blue">Syntax Tree</text>
</a></g></g><!-- AST&#45;&gt;Exec --><g id="edge3" class="edge"><title>AST-&gt;Exec</title><g id="a_edge3"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title=" DQ1" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M309,-275.15C309,-263.28 309,-247.94 309,-234.5"/>
<polygon fill="black" stroke="black" points="312.5,-234.83 309,-224.83 305.5,-234.83 312.5,-234.83"/>
</a></g><g id="a_edge3-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title="Access AST" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="324" y="-244.2" font-family="Times,serif" font-size="14.00" fill="blue"> DQ1</text>
</a></g></g><!-- AST&#45;&gt;AST --><g id="edge2" class="edge"><title>AST-&gt;AST</title><g id="a_edge2"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ5" xlink:title=" SQ5" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M350.09,-304.63C360.14,-303.99 367.62,-301.11 367.62,-296 367.62,-293.12 365.26,-290.96 361.4,-289.49"/>
<polygon fill="black" stroke="black" points="362.05,-286.06 351.58,-287.65 360.76,-292.94 362.05,-286.06"/>
</a></g><g id="a_edge2-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ5" xlink:title="Normalize" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="381.5" y="-290.95" font-family="Times,serif" font-size="14.00" fill="blue"> SQ5</text>
</a></g></g><!-- Static --><g id="node4" class="node"><title>Static</title><g id="a_node4"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#dt-static-context" xlink:title="Static Context" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="none" stroke="black" points="473.38,-432.5 418.62,-432.5 414.62,-428.5 414.62,-391.5 469.38,-391.5 473.38,-395.5 473.38,-432.5"/>
<polyline fill="none" stroke="black" points="469.38,-428.5 414.62,-428.5"/>
<polyline fill="none" stroke="black" points="469.38,-428.5 469.38,-391.5"/>
<polyline fill="none" stroke="black" points="469.38,-428.5 473.38,-432.5"/>
<text text-anchor="middle" x="444" y="-415.2" font-family="Times,serif" font-size="14.00" fill="blue">Static</text>
<text text-anchor="middle" x="444" y="-398.7" font-family="Times,serif" font-size="14.00" fill="blue">Context</text>
</a></g></g><!-- Static&#45;&gt;AST --><g id="edge5" class="edge"><title>Static-&gt;AST</title><g id="a_edge5"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ1234" xlink:title=" SQ4" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M420.48,-391.14C398.42,-372.51 365.24,-344.49 340.81,-323.86"/>
<polygon fill="black" stroke="black" points="343.3,-321.38 333.4,-317.61 338.78,-326.73 343.3,-321.38"/>
</a></g><g id="a_edge5-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ1234" xlink:title="Resolve names" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="410.36" y="-360.2" font-family="Times,serif" font-size="14.00" fill="blue"> SQ4</text>
</a></g></g><!-- Dynamic --><g id="node5" class="node"><title>Dynamic</title><g id="a_node5"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#dt-dynamic-context" xlink:title="Dynamic Context" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="none" stroke="black" points="480.12,-316.5 417.88,-316.5 413.88,-312.5 413.88,-275.5 476.12,-275.5 480.12,-279.5 480.12,-316.5"/>
<polyline fill="none" stroke="black" points="476.12,-312.5 413.88,-312.5"/>
<polyline fill="none" stroke="black" points="476.12,-312.5 476.12,-275.5"/>
<polyline fill="none" stroke="black" points="476.12,-312.5 480.12,-316.5"/>
<text text-anchor="middle" x="447" y="-299.2" font-family="Times,serif" font-size="14.00" fill="blue">Dynamic</text>
<text text-anchor="middle" x="447" y="-282.7" font-family="Times,serif" font-size="14.00" fill="blue">Context</text>
</a></g></g><!-- Static&#45;&gt;Dynamic --><g id="edge6" class="edge"><title>Static-&gt;Dynamic</title><g id="a_edge6"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title=" DQ2" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M444.52,-391.14C444.98,-373.73 445.65,-348.14 446.18,-328.03"/>
<polygon fill="black" stroke="black" points="449.68,-328.23 446.44,-318.14 442.68,-328.05 449.68,-328.23"/>
</a></g><g id="a_edge6-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title="Provide access" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="460.41" y="-360.2" font-family="Times,serif" font-size="14.00" fill="blue"> DQ2</text>
</a></g></g><!-- Dynamic&#45;&gt;Exec --><g id="edge7" class="edge"><title>Dynamic-&gt;Exec</title><g id="a_edge7"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title=" DQ5" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M407.71,-268.95C389.08,-256.6 366.88,-241.88 348.26,-229.53"/>
<polygon fill="black" stroke="black" points="405.53,-271.7 415.79,-274.31 409.39,-265.87 405.53,-271.7"/>
<polygon fill="black" stroke="black" points="350.42,-226.76 340.15,-224.15 346.55,-232.59 350.42,-226.76"/>
</a></g><g id="a_edge7-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title="Access and change" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="405.36" y="-244.2" font-family="Times,serif" font-size="14.00" fill="blue"> DQ5</text>
</a></g></g><!-- Schema --><g id="node6" class="node"><title>Schema</title><g id="a_node6"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#dt-issd" xlink:title="In-Scope Schema Definitions" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="none" stroke="black" points="478.38,-542.5 401.62,-542.5 401.62,-485 478.38,-485 478.38,-542.5"/>
<text text-anchor="middle" x="440" y="-525.2" font-family="Times,serif" font-size="14.00" fill="blue">In-Scope</text>
<text text-anchor="middle" x="440" y="-508.7" font-family="Times,serif" font-size="14.00" fill="blue">Schema</text>
<text text-anchor="middle" x="440" y="-492.2" font-family="Times,serif" font-size="14.00" fill="blue">Definitions</text>
</a></g></g><!-- Schema&#45;&gt;Static --><g id="edge4" class="edge"><title>Schema-&gt;Static</title><path fill="none" stroke="black" d="M441.12,-484.7C441.63,-472.08 442.23,-457.14 442.75,-444.17"/><polygon fill="black" stroke="black" points="446.24,-444.59 443.14,-434.46 439.24,-444.31 446.24,-444.59"/></g><!-- XPath --><g id="node7" class="node"><title>XPath</title><path fill="none" stroke="black" d="M324,-430C324,-430 294,-430 294,-430 288,-430 282,-424 282,-418 282,-418 282,-406 282,-406 282,-400 288,-394 294,-394 294,-394 324,-394 324,-394 330,-394 336,-400 336,-406 336,-406 336,-418 336,-418 336,-424 330,-430 324,-430"/><text text-anchor="middle" x="309" y="-406.95" font-family="Times,serif" font-size="14.00">XPath</text></g><!-- XPath&#45;&gt;AST --><g id="edge1" class="edge"><title>XPath-&gt;AST</title><g id="a_edge1"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ1234" xlink:title=" SQ1" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M309,-393.69C309,-376.32 309,-349.19 309,-328.09"/>
<polygon fill="black" stroke="black" points="312.5,-328.29 309,-318.29 305.5,-328.29 312.5,-328.29"/>
</a></g><g id="a_edge1-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ1234" xlink:title="Parse expression" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="322.88" y="-360.2" font-family="Times,serif" font-size="14.00" fill="blue"> SQ1</text>
</a></g></g><!-- XML --><g id="node8" class="node"><title>XML</title><polygon fill="none" stroke="black" points="64,-314 16,-314 16,-278 70,-278 70,-308 64,-314"/><polyline fill="none" stroke="black" points="64,-314 64,-308"/><polyline fill="none" stroke="black" points="70,-308 64,-308"/><text text-anchor="middle" x="43" y="-290.95" font-family="Times,serif" font-size="14.00">XML</text></g><!-- PSVI --><g id="node10" class="node"><title>PSVI</title><g id="a_node10"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#id-data-model-generation" xlink:title="Infoset/PSVI" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="none" stroke="black" points="84.62,-223 33.38,-223 33.38,-182 90.62,-182 90.62,-217 84.62,-223"/>
<polyline fill="none" stroke="black" points="84.62,-223 84.62,-217"/>
<polyline fill="none" stroke="black" points="90.62,-217 84.62,-217"/>
<text text-anchor="middle" x="62" y="-205.7" font-family="Times,serif" font-size="14.00" fill="blue">Infoset/</text>
<text text-anchor="middle" x="62" y="-189.2" font-family="Times,serif" font-size="14.00" fill="blue">PSVI</text>
</a></g></g><!-- XML&#45;&gt;PSVI --><g id="edge9" class="edge"><title>XML-&gt;PSVI</title><g id="a_edge9"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DM1" xlink:title=" DM1" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M46.57,-277.79C49.12,-265.54 52.61,-248.73 55.62,-234.21"/>
<polygon fill="black" stroke="black" points="59,-235.16 57.61,-224.66 52.15,-233.74 59,-235.16"/>
</a></g><g id="a_edge9-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DM1" xlink:title="Parse and optionally validate" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="70.7" y="-244.2" font-family="Times,serif" font-size="14.00" fill="blue"> DM1</text>
</a></g></g><!-- JSON --><g id="node9" class="node"><title>JSON</title><polygon fill="none" stroke="black" points="136,-314 88,-314 88,-278 142,-278 142,-308 136,-314"/><polyline fill="none" stroke="black" points="136,-314 136,-308"/><polyline fill="none" stroke="black" points="142,-308 136,-308"/><text text-anchor="middle" x="115" y="-290.95" font-family="Times,serif" font-size="14.00">JSON</text></g><!-- JSON&#45;&gt;XDM --><g id="edge10" class="edge"><title>JSON-&gt;XDM</title><g id="a_edge10"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DM2" xlink:title=" DM2" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M119.66,-277.61C127.32,-251.97 144.81,-203.83 175,-174 197.93,-151.34 230.44,-135.5 257.58,-125.24"/>
<polygon fill="black" stroke="black" points="258.43,-128.66 266.64,-121.96 256.05,-122.07 258.43,-128.66"/>
</a></g><g id="a_edge10-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DM2" xlink:title="Generate Data Model" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="191.5" y="-197.45" font-family="Times,serif" font-size="14.00" fill="blue"> DM2</text>
</a></g></g><!-- PSVI&#45;&gt;XDM --><g id="edge11" class="edge"><title>PSVI-&gt;XDM</title><g id="a_edge11"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DM2" xlink:title=" DM2" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M64.37,-181.68C66.83,-170 71.82,-155.9 82,-147.5 108.02,-126.01 198.84,-116.54 257.12,-112.59"/>
<polygon fill="black" stroke="black" points="257.05,-116.1 266.81,-111.97 256.61,-109.11 257.05,-116.1"/>
</a></g><g id="a_edge11-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DM2" xlink:title="Generate Data Model" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="98.5" y="-150.7" font-family="Times,serif" font-size="14.00" fill="blue"> DM2</text>
</a></g></g><!-- Direct --><g id="node11" class="node"><title>Direct</title><g id="a_node11"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Other/Direct Generation" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M232.12,-316.5C232.12,-316.5 171.88,-316.5 171.88,-316.5 165.88,-316.5 159.88,-310.5 159.88,-304.5 159.88,-304.5 159.88,-287.5 159.88,-287.5 159.88,-281.5 165.88,-275.5 171.88,-275.5 171.88,-275.5 232.12,-275.5 232.12,-275.5 238.12,-275.5 244.12,-281.5 244.12,-287.5 244.12,-287.5 244.12,-304.5 244.12,-304.5 244.12,-310.5 238.12,-316.5 232.12,-316.5"/>
<text text-anchor="middle" x="202" y="-299.2" font-family="Times,serif" font-size="14.00">Other/Direct</text>
<text text-anchor="middle" x="202" y="-282.7" font-family="Times,serif" font-size="14.00">Generation</text>
</a></g></g><!-- Direct&#45;&gt;XDM --><g id="edge12" class="edge"><title>Direct-&gt;XDM</title><g id="a_edge12"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DM3" xlink:title=" DM3" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M203.17,-275.11C205.47,-249.87 212.21,-205.78 232,-174 240.96,-159.6 254.25,-146.83 267.22,-136.55"/>
<polygon fill="black" stroke="black" points="269.02,-139.58 274.88,-130.75 264.8,-133.99 269.02,-139.58"/>
</a></g><g id="a_edge12-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DM3" xlink:title="Other/Direct Generation" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="248.5" y="-197.45" font-family="Times,serif" font-size="14.00" fill="blue"> DM3</text>
</a></g></g><!-- XSD --><g id="node12" class="node"><title>XSD</title><g id="a_node12"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="XML Schema" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<polygon fill="none" stroke="black" points="418.75,-658.5 365.25,-658.5 365.25,-617.5 424.75,-617.5 424.75,-652.5 418.75,-658.5"/>
<polyline fill="none" stroke="black" points="418.75,-658.5 418.75,-652.5"/>
<polyline fill="none" stroke="black" points="424.75,-652.5 418.75,-652.5"/>
<text text-anchor="middle" x="395" y="-641.2" font-family="Times,serif" font-size="14.00">XML</text>
<text text-anchor="middle" x="395" y="-624.7" font-family="Times,serif" font-size="14.00">Schema</text>
</a></g></g><!-- XSD&#45;&gt;Schema --><g id="edge13" class="edge"><title>XSD-&gt;Schema</title><g id="a_edge13"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#id-schema-import-processing" xlink:title=" SI1" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M402.33,-617.09C408.65,-599.92 417.96,-574.63 425.74,-553.49"/>
<polygon fill="black" stroke="black" points="428.96,-554.89 429.13,-544.29 422.39,-552.47 428.96,-554.89"/>
</a></g><g id="a_edge13-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#id-schema-import-processing" xlink:title="XML Schema Import" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="426.06" y="-586.2" font-family="Times,serif" font-size="14.00" fill="blue"> SI1</text>
</a></g></g><!-- DirectXSD --><g id="node13" class="node"><title>DirectXSD</title><g id="a_node13"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Other/Direct Generation" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M515.12,-658.5C515.12,-658.5 454.88,-658.5 454.88,-658.5 448.88,-658.5 442.88,-652.5 442.88,-646.5 442.88,-646.5 442.88,-629.5 442.88,-629.5 442.88,-623.5 448.88,-617.5 454.88,-617.5 454.88,-617.5 515.12,-617.5 515.12,-617.5 521.12,-617.5 527.12,-623.5 527.12,-629.5 527.12,-629.5 527.12,-646.5 527.12,-646.5 527.12,-652.5 521.12,-658.5 515.12,-658.5"/>
<text text-anchor="middle" x="485" y="-641.2" font-family="Times,serif" font-size="14.00">Other/Direct</text>
<text text-anchor="middle" x="485" y="-624.7" font-family="Times,serif" font-size="14.00">Generation</text>
</a></g></g><!-- DirectXSD&#45;&gt;Schema --><g id="edge14" class="edge"><title>DirectXSD-&gt;Schema</title><g id="a_edge14"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SI2" xlink:title=" SI2" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M477.67,-617.09C471.35,-599.92 462.04,-574.63 454.26,-553.49"/>
<polygon fill="black" stroke="black" points="457.61,-552.47 450.87,-544.29 451.04,-554.89 457.61,-552.47"/>
</a></g><g id="a_edge14-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SI2" xlink:title="Other/Direct Generation" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="481.69" y="-586.2" font-family="Times,serif" font-size="14.00" fill="blue"> SI2</text>
</a></g></g><!-- Host --><g id="node14" class="node"><title>Host</title><g id="a_node14"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Host Environment" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M571.62,-534.25C571.62,-534.25 508.38,-534.25 508.38,-534.25 502.38,-534.25 496.38,-528.25 496.38,-522.25 496.38,-522.25 496.38,-505.25 496.38,-505.25 496.38,-499.25 502.38,-493.25 508.38,-493.25 508.38,-493.25 571.62,-493.25 571.62,-493.25 577.62,-493.25 583.62,-499.25 583.62,-505.25 583.62,-505.25 583.62,-522.25 583.62,-522.25 583.62,-528.25 577.62,-534.25 571.62,-534.25"/>
<text text-anchor="middle" x="540" y="-516.95" font-family="Times,serif" font-size="14.00">Host</text>
<text text-anchor="middle" x="540" y="-500.45" font-family="Times,serif" font-size="14.00">Environment</text>
</a></g></g><!-- Host&#45;&gt;Static --><g id="edge15" class="edge"><title>Host-&gt;Static</title><g id="a_edge15"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ1234" xlink:title=" SQ2" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M505.43,-492.87C490.4,-483.73 474.92,-473.5 469.25,-467 463.24,-460.11 458.38,-451.55 454.58,-443.29"/>
<polygon fill="black" stroke="black" points="457.85,-442.05 450.78,-434.17 451.39,-444.74 457.85,-442.05"/>
</a></g><g id="a_edge15-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#SQ1234" xlink:title="Initialize from host environment" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="483.12" y="-453.7" font-family="Times,serif" font-size="14.00" fill="blue"> SQ2</text>
</a></g></g><!-- Host&#45;&gt;Dynamic --><g id="edge16" class="edge"><title>Host-&gt;Dynamic</title><g id="a_edge16"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title=" DQ3" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<path fill="none" stroke="black" d="M533.02,-492.77C522.38,-462.83 500.99,-404.74 479,-357 474.39,-347 468.9,-336.32 463.77,-326.79"/>
<polygon fill="black" stroke="black" points="467,-325.4 459.14,-318.31 460.86,-328.76 467,-325.4"/>
</a></g><g id="a_edge16-label"><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#DQ12345" xlink:title="Initialize from host environment" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
<text text-anchor="middle" x="525.43" y="-406.95" font-family="Times,serif" font-size="14.00" fill="blue"> DQ3</text>
</a></g></g></g></svg></div><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 4.0; in Figure 1, these are depicted
outside the line that represents the boundaries of the language, an
area labeled <b>external processing</b>. The external processing
domain includes generation of <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instances</a> that represent the data to be queried (see <a href="#id-data-model-generation"><b>2.3.1 Data Model Generation</b></a>), schema import processing (see
<a href="#id-schema-import-processing"><b>2.3.2 Schema Import Processing</b></a>), and serialization. The area inside the boundaries of
the language is known as the <span class="xpath">
                  <b>XPath processing domain</b>
               </span>, which includes the static
analysis and dynamic evaluation phases (see <a href="#id-expression-processing"><b>2.3.3 Expression
Processing</b></a>).  Consistency constraints on the

            <span class="xpath">XPath</span> processing domain are defined in <a href="#id-consistency-constraints"><b>2.3.6 Consistency Constraints</b></a>.</p><div class="div3">
<h4><a id="id-data-model-generation"/>2.3.1 <a href="#id-data-model-generation" style="text-decoration: none">Data Model Generation</a></h4><p>The input data for 
               <span class="xpath">an expression</span> must be represented as one or more <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instances</a>. This process occurs outside
the domain of XPath 4.0, which is why Figure 1 represents it in the
external processing domain.</p><p>In many cases the input data might originate as XML.
               Here are some steps by which an XML
document might be converted to an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instance</a>:</p><ol class="enumar"><li><p id="DM1">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="http://www.w3.org/TR/xmlschema-1/">[XML Schema 1.0 Part 1]</a> or
<a href="http://www.w3.org/TR/xmlschema11-1/">[XML Schema 1.1 Part 1]</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 Figure 1)</p></li><li><p id="DM2">The Information Set or PSVI may be
transformed into an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instance</a>
by a process described in <a href="#xpath-datamodel-40">[XQuery and XPath Data Model (XDM) 4.0]</a>. (See DM2 in
Figure 1)</p></li></ol><p id="DM3">The above steps provide an example of how an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instance</a> might be constructed. An XDM instance might
also be constructed in some other way (see DM3 in Figure 1), for example it might be 
synthesized directly from a relational database, or
derived by parsing a JSON text or a CSV file. Whatever the origin, XPath 4.0 is defined in terms
of the <a title="data model" class="termref" href="#dt-datamodel">data model</a>,
but it does not place any constraints on how XDM instances are constructed.</p><p>The remainder of this section is concerned with the common case where XML data is being processed.</p><p>
                  [<a id="dt-type-annotation" title="type annotation">Definition</a>: Each element node and attribute node in an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instance</a> has a <b>type annotation</b> (described in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#types">Section 4.1 Schema Information</a><sup><small>DM</small></sup>). 
The type annotation of a node is a reference to a <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>. 
]  The <code>type-name</code> of a node is the name of the type referenced by its <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> (but note that the
               type annotation can be a reference to an anonymous type). 
If the <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instance</a> was derived from a validated XML document as described in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#const-psvi">Section 6.5.3 Construction from a PSVI</a><sup><small>DM</small></sup>, the type annotations of the element and attribute nodes are derived from schema
validation. XPath 4.0 does
not provide a way to directly access the type annotation of an element
or attribute node.</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 given the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>
                  <code>xs: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 <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> 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 <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>
                  <code>xs:untyped</code>. An element that has been validated and found to be partially valid is annotated with the schema type <code>xs:anyType</code>. If an element node is annotated as <code>xs:untyped</code>, all its descendant element nodes are also annotated as <code>xs:untyped</code>. However, if an element node is annotated as <code>xs:anyType</code>, some of its descendant element nodes may have a more specific <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>.</p></div><div class="div3">
<h4><a id="id-schema-import-processing"/>2.3.2 <a href="#id-schema-import-processing" style="text-decoration: none">Schema Import Processing</a></h4><p class="xpath">The <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema
definitions</a> in the <a title="static context" class="termref" 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.3.6 Consistency Constraints</b></a>.

</p></div><div class="div3">
<h4><a id="id-expression-processing"/>2.3.3 <a href="#id-expression-processing" style="text-decoration: none">Expression
Processing</a></h4><p>XPath 4.0 defines two phases of processing called
the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis phase</a>
and the <a title="dynamic evaluation phase" class="termref" href="#dt-dynamic-evaluation">dynamic evaluation
phase</a> (see Figure 1).  During the static analysis phase, <a title="static error" class="termref" href="#dt-static-error">static errors</a>,  <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic errors</a>, or <a title="type error" class="termref" href="#dt-type-error">type errors</a> may be raised. During the dynamic evaluation phase, only <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic errors</a> or <a title="type error" class="termref" href="#dt-type-error">type errors</a> may be raised. These kinds of errors are defined in <a href="#id-kinds-of-errors"><b>2.4.1 Kinds of Errors</b></a>.  </p><p>Within each phase, an implementation is free to use any
strategy or algorithm whose result conforms to the
specifications in this document.</p><div class="div4">
<h5><a id="id-static-analysis"/>2.3.3.1 <a href="#id-static-analysis" style="text-decoration: none">Static Analysis Phase</a></h5><p>
                     [<a id="dt-static-analysis" title="static analysis phase">Definition</a>: The
<b>static analysis phase</b> depends on the expression itself
and on the <a title="static context" class="termref" href="#dt-static-context">static context</a>. The <b>static analysis phase</b> does
not depend on input data (other than schemas).]
                  </p><p id="SQ1234">During the static analysis phase, the 
                  <span class="xpath">XPath expression</span> is typically 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" class="termref" href="#dt-static-error">static error</a>
                     [<a href="#ERRXPST0003" title="err:XPST0003">err:XPST0003</a>]. The <a title="static context" class="termref" href="#dt-static-context">static context</a> is initialized by the implementation (step SQ2).  The <a title="static context" class="termref" href="#dt-static-context">static context</a> is used to resolve schema type names, function names, namespace prefixes, and variable names (step
SQ4).
If a name of one of these kinds in the <b>operation tree</b> is
not found in the <a title="static context" class="termref" href="#dt-static-context">static context</a>, a <a title="static error" class="termref" href="#dt-static-error">static error</a> ([<a href="#ERRXPST0008" title="err:XPST0008">err:XPST0008</a>] or [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>]) is raised (however, see exceptions to this rule in <a href="#id-element-test"><b>3.2.7.2 Element Types</b></a> and <a href="#id-attribute-test"><b>3.2.7.3 Attribute Types</b></a>.)</p><p id="SQ5">The <b>operation tree</b> is then typically
normalized by making explicit the implicit operations
such as <a title="atomization" class="termref" href="#dt-atomization">atomization</a> and extraction of <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean values</a> (step SQ5).</p><p>During the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis
phase</a>, a processor may perform type analysis. The
effect of type analysis is to assign a <a title="static type" class="termref" href="#dt-static-type">static type</a> to each expression in the
operation tree. [<a id="dt-static-type" title="static type">Definition</a>: The
<b>static type</b> of an expression is the best inference that
the processor is able to make statically about the type of the result
of the expression.] This specification does not define the
rules for type analysis nor the static types that are assigned to
particular expressions: the only constraint is that the inferred type
must match all possible values that the expression is capable of
returning.</p><p>Examples of inferred static types might be:</p><ul><li><p>For the expression <code>concat(a,b)</code> the inferred static type is <code>xs:string</code>
                        </p></li><li><p>For the expression <code>$a = $v</code> the inferred static type is <code>xs:boolean</code>
                        </p></li><li><p>For the expression <code>$s[exp]</code> the inferred static
    type has the same item type as the static type of <code>$s</code>,
    but a cardinality that allows the empty sequence even if the
    static type of <code>$s</code> does not allow an empty
    sequence.</p></li><li><p>The inferred static type of the expression <code>data($x)</code> (whether written
    explicitly or inserted into the operation tree in places where atomization
    is implicit) depends on the inferred static type of <code>$x</code>: for example, if <code>$x</code>
    has type <code>element(*, xs:integer)</code> then <code>data($x)</code> has static type <code>xs:integer</code>.</p></li></ul><p>In XQuery 1.0 and XPath 2.0, rules for static type inferencing were published
normatively in <a href="#xquery-semantics">[XQuery 1.0 and XPath 2.0 Formal Semantics]</a>, but implementations were allowed to
refine these rules to infer a more precise type where possible. In
subsequent versions, the rules for static type inferencing are entirely implementation-dependent.</p><p>Every kind of expression also imposes requirements on the type of its
operands. For example, with the expression <code>substring($a, $b, $c)</code>, <code>$a</code> must be
of type <code>xs:string</code> (or something that can be converted to <code>xs:string</code> by the
function calling rules), while <code>$b</code> and <code>$c</code> must be numeric.</p><p>A processor <span class="verb">may</span> raise a type
                  error during static analysis if the inferred static type of an
                     expression has no overlap (intersection) with the required type, and cannot be converted
                     to the required type using the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>. For example,
                     given the call <code>fn:upper-case($s)</code>, the processor <span class="verb">may</span> raise an error if the
                     declared or inferred type of <code>$s</code> is <code>xs:integer</code>, 
                     but not if it is <code>xs:anyAtomicType</code>.
                  </p><p>In addition, type analysis <span class="verb">may</span> conclude that an expression is <a title="implausible" class="termref" href="#dt-implausible">implausible</a>.
               Implausible expressions may be considered erroneous unless such checks have been disabled.
               For example, the expression <code>round(tokenize($input))</code> is implausible because 
                  the required type for <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-round"><code>fn:round</code></a> is <code>xs:numeric?</code>,
                  while the static type of <code>tokenize($input)</code> is <code>xs:string*</code>,
                  and these two sequence types are <a title="substantively disjoint" class="termref" href="#dt-substantively-disjoint">substantively disjoint</a>.
               This topic is described further in <a href="#id-implausible-expressions"><b>2.4.6 Implausible Expressions</b></a>.</p><p>Alternatively, the processor
                  <span class="verb">may</span> defer all type checking until the dynamic evaluation phase.</p></div><div class="div4">
<h5><a id="id-dynamic-evaluation"/>2.3.3.2 <a href="#id-dynamic-evaluation" style="text-decoration: none">Dynamic Evaluation Phase</a></h5><p id="DQ12345">
                     [<a id="dt-dynamic-evaluation" title="dynamic evaluation phase">Definition</a>: The <b>dynamic evaluation phase</b> is the phase during which the value of an expression is computed.] It is dependent on successful completion of the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis phase</a>.</p><p>The dynamic evaluation phase can occur only if no errors were detected during the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis phase</a>.</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" class="termref" 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" class="termref" href="#dt-static-context">static context</a> (step DQ2). The dynamic evaluation phase may create new data-model values (step DQ4) and it may extend the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a> (step DQ5)—for example, by binding values to variables.</p><p>
                     [<a id="dt-dynamic-type" title="dynamic type">Definition</a>: 
                  Every value matches one or more <a title="sequence type" class="termref" href="#dt-sequence-type">sequence types</a>. 
                     A value is said to have a <b>dynamic type</b>
                        <var>T</var> if it matches (or <b>is an instance of</b>) 
                     the sequence type <var>T</var>.]
                  </p><p>In many cases (but not all), one of the dynamic types that a value matches will 
                     be a subtype of all the others, in which case it makes sense to speak of “the dynamic type” of the value as 
                     meaning this single most specific type. In other cases (examples are 
                     <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-empty-map">empty maps</a><sup><small>DM</small></sup> and 
                     <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-empty-array">empty arrays</a><sup><small>DM</small></sup>) none of the 
                     dynamic types is more specific than all the others.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>An atomic item has a <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> which will always be
                  a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of all the other types that it matches; we can therefore refer to 
                  this as the <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> of the atomic item without ambiguity.</p></div><p>A value may match a dynamic type that is more specific than the <a title="static type" class="termref" href="#dt-static-type">static type</a> 
                  of the expression that computed it (for example, the static type of an expression might be <code>xs:integer*</code>, 
                  denoting a sequence of zero or more integers, but at evaluation time its value may be an instance of <code>xs:integer</code>, 
                  denoting exactly one integer).
               </p><p>If an operand of an expression does not have a <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> that is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of the <a title="static type" class="termref" href="#dt-static-type">static type</a>
                  required for that operand, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is
                  raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p><p>Even though static typing can catch many <a title="type error" class="termref" 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" class="termref" 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" class="termref" href="#dt-static-type">static type</a> is <code>xs:untypedAtomic</code>. This is not a <a title="static error" class="termref" href="#dt-static-error">static error</a>, but at run time, if the value cannot be successfully cast to a <a title="numeric" class="termref" href="#dt-numeric">numeric</a> type, a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> will be raised.</p></div></div><div class="div3">
<h4><a id="id-input-sources"/>2.3.4 <a href="#id-input-sources" style="text-decoration: none">Input Sources</a></h4><p>XPath 4.0 has a set of functions that provide access to XML documents (<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-doc"><code>fn:doc</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-doc-available"><code>fn:doc-available</code></a>), collections (<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-uri-collection"><code>fn:uri-collection</code></a>), text files (<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-unparsed-text"><code>fn:unparsed-text</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-unparsed-text-lines"><code>fn:unparsed-text-lines</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-unparsed-text-available"><code>fn:unparsed-text-available</code></a>), and environment variables (<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-environment-variable"><code>fn:environment-variable</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-available-environment-variables"><code>fn:available-environment-variables</code></a>).  These functions are defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#fns-on-docs">Section 14.6 Functions giving access to external information</a><sup><small>FO</small></sup>.</p><p>An expression can access input data either by calling one
               of these input functions or by referencing some part of the
               <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a> that is initialized by the external
               environment, such as a <a title="variable values" class="termref" href="#dt-variable-values">variable</a> or
               <a title="context value" class="termref" href="#dt-context-value">context value</a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The <a href="http://expath.org">EXPath Community Group</a> has developed a <a href="http://expath.org/spec/file">File Module</a>, which some implementations use to perform file system related operations 
                  such as reading or writing files and directories. Multiple files can be read or written 
                  from a single query.</p></div></div><div class="div3">
<h4><a id="id-serialization"/>2.3.5 <a href="#id-serialization" style="text-decoration: none">Serialization</a></h4><p>
                  [<a id="dt-serialization" title="serialization">Definition</a>: 
                     <b>Serialization</b> is the process of
converting an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM
instance</a> to a sequence of octets (step DM4 in Figure 1.),
as described in <a href="#xslt-xquery-serialization-40">[XSLT and XQuery Serialization 4.0]</a>.]
               </p><div class="note"><p class="prefix"><b>Note:</b></p><p>This definition of serialization is the definition
used in this specification. Any form of serialization that is
not based on <a href="#xslt-xquery-serialization-40">[XSLT and XQuery Serialization 4.0]</a> is outside
the scope of the XPath 4.0 specification.</p></div><p class="xpath">Serialization is outside the scope of the XPath specification, except
               to the extent that there is a function <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-serialize"><code>fn:serialize</code></a> that enables
               serialization to be invoked.</p></div><div class="div3">
<h4><a id="id-consistency-constraints"/>2.3.6 <a href="#id-consistency-constraints" style="text-decoration: none">Consistency Constraints</a></h4><p>In order for XPath 4.0 to
be well defined, the input <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instances</a>, the <a title="static context" class="termref" href="#dt-static-context">static context</a>, and the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a> must be mutually
consistent. The consistency constraints listed below are prerequisites
for correct functioning of an XPath 4.0 implementation. Enforcement
of these consistency constraints is beyond the scope of this
specification. This specification does not
define the result of  
               <span class="xpath">an expression</span> under any condition in which one
or more of these constraints is not satisfied.</p><ul><li><p>For every  node that has a type annotation, if that type annotation is found in the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a> (ISSD), then its definition in the ISSD must be 
                     <span>
                           <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-schema-compatible">compatible</a><sup><small>DM</small></sup>
                        </span> with its definition 
                     in the <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-schema">schema</a><sup><small>DM</small></sup> that was used to validate the node. 
                  </p></li><li><p>Every element name, attribute name, or schema type name referenced in <a title="in-scope variables" class="termref" href="#dt-in-scope-variables">in-scope variables</a> or <a title="statically known function definitions" class="termref" href="#dt-statically-known-function-definitions">statically known function definitions</a>
                         must be in the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a>, unless it is an element name referenced as part of an <a href="#doc-xpath40-ElementTest">ElementTest</a> or an attribute name referenced as part of an <a href="#doc-xpath40-AttributeTest">AttributeTest</a>.</p></li><li><p>Any reference to a global element, attribute, or type name in
the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a> must have a corresponding element, attribute or type
definition in the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a>.</p></li><li><p>For each (variable, type) pair in <a title="in-scope variables" class="termref" href="#dt-in-scope-variables">in-scope variables</a> and the corresponding (variable, value) pair in <a title="variable values" class="termref" 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>3.1.2 Sequence Type Matching</b></a>.</p></li><li><p>In the <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a>, the prefix <code>xml</code> must not be bound to any namespace URI other than <code>http://www.w3.org/XML/1998/namespace</code>, and no prefix other than <code>xml</code> may be bound to this namespace URI.
The prefix <code>xmlns</code> must not be bound to any namespace URI, and no prefix may be bound to the namespace URI <code>http://www.w3.org/2000/xmlns/</code>.</p></li></ul></div></div><div class="div2">
<h3><a id="errors"/>2.4 <a href="#errors" style="text-decoration: none">Error Handling</a></h3><div class="div3">
<h4><a id="id-kinds-of-errors"/>2.4.1 <a href="#id-kinds-of-errors" style="text-decoration: none">Kinds of Errors</a></h4><p>
As described in <a href="#id-expression-processing"><b>2.3.3 Expression
Processing</b></a>, XPath 4.0
defines a <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis phase</a>, which does not depend on input
data, and a <a title="dynamic evaluation phase" class="termref" href="#dt-dynamic-evaluation">dynamic evaluation
phase</a>, which does depend on input
data.  Errors may be raised during each phase.</p><p>
                  [<a id="dt-static-error" title="static error">Definition</a>: 
An error that can be detected during the static analysis phase, and is not a type error, is a <b>static error</b>.] A syntax error is an example of a <a title="static error" class="termref" href="#dt-static-error">static error</a>.</p><p>
                  [<a id="dt-dynamic-error" title="dynamic error">Definition</a>: A <b>dynamic
error</b> is an error that
must be detected during the dynamic evaluation phase and may be detected
during the static analysis phase.]
Numeric overflow is an example of a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>.
            </p><p>
                  [<a id="dt-type-error" title="type error">Definition</a>: A <b>type
error</b> may be raised during the static analysis phase or the dynamic evaluation phase.
During the static analysis phase, a <a title="type error" class="termref" href="#dt-type-error">type error</a> occurs
when the <a title="static type" class="termref" href="#dt-static-type">static type</a> of an expression does not match the expected type
of the context in which the expression occurs.
During the dynamic evaluation phase, a <a title="type error" class="termref" href="#dt-type-error">type error</a> occurs
when the <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> of a value does not match the expected type of
the context in which the value occurs.]
               </p><p>The outcome of the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis
phase</a> is either success or one or more <a title="type error" class="termref" href="#dt-type-error">type errors</a>, <a title="static error" class="termref" href="#dt-static-error">static errors</a>, or statically detected <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic errors</a>. The result of the <a title="dynamic evaluation phase" class="termref" href="#dt-dynamic-evaluation">dynamic evaluation
phase</a> is either a result value, a <a title="type error" class="termref" href="#dt-type-error">type
error</a>, or a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>.</p><p>If more than one error is present, or if an error condition comes within the
scope of more than one error defined in this specification, then any non-empty
subset of these errors may be reported.</p><p>If an implementation can determine during the
<a title="static analysis phase" class="termref" href="#dt-static-analysis">static
analysis phase</a> that 
               <span class="xpath">an XPath expression</span>, if evaluated, would necessarily
raise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> or that an expression, if evaluated, would necessarily raise a <a title="type error" class="termref" href="#dt-type-error">type error</a>, the implementation may (but is not required to) report that
error during the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static
analysis phase</a>.</p><p>An implementation can raise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> for 
               <span class="xpath">an XPath expression</span> statically only if the 
               <span class="xpath">expression</span> can never execute without raising that error, as in the following example:</p><div class="exampleInner"><pre>
error()</pre></div><p>The following example contains a type error, which can be reported statically even if the implementation can not prove that the expression will actually be evaluated.</p><div class="exampleInner"><pre>
if (empty($arg))
then "cat" * 2
else 0</pre></div><p>
                  [<a id="dt-warning" title="warning">Definition</a>: In addition to <a title="static error" class="termref" href="#dt-static-error">static errors</a>, <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic errors</a>, and <a title="type error" class="termref" href="#dt-type-error">type
errors</a>, an XPath 4.0
implementation may raise <b>warnings</b>, either during the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis
phase</a> or the
<a title="dynamic evaluation phase" class="termref" href="#dt-dynamic-evaluation">dynamic evaluation
phase</a>. The circumstances in which warnings are raised, and
the ways in which warnings are handled, are <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>.]
               </p><p>In addition to the errors defined in this
specification, an implementation may raise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> for a reason beyond the scope of this specification. For
example, limitations may exist on the maximum
numbers or sizes of various objects. 
<span>An error must be raised if such a limitation is exceeded  [<a href="#ERRXPDY0130" title="err:XPDY0130">err:XPDY0130</a>].</span>
               </p></div><div class="div3">
<h4><a id="id-identifying-errors"/>2.4.2 <a href="#id-identifying-errors" style="text-decoration: none">Identifying and Reporting Errors</a></h4><p>The errors defined in this specification are identified by QNames that have the form <code>err:XPYYnnnn</code>
               , where:</p><ul><li><p>
                        <code>err</code> denotes the namespace for XPath and XQuery errors, <code>http://www.w3.org/2005/xqt-errors</code>. This binding of the namespace prefix <code>err</code> is used for convenience in this document, and is not normative.</p></li><li class="xpath"><p>
                        <code>XP</code> identifies the error as an XPath error (some errors, originally defined by XQuery and later added to XPath, use the code <code>XQ</code> instead).</p></li><li><p>
                        <code>YY</code> denotes the error category, using the following encoding:</p><ul><li><p>
                              <code>ST</code> denotes a static error.</p></li><li><p>
                              <code>DY</code> denotes a dynamic error.</p></li><li><p>
                              <code>TY</code> denotes a type error.</p></li></ul></li><li><p>
                        <code>nnnn</code> is a unique numeric code.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The namespace URI for  XPath and XQuery errors is not expected to
change from one version of 
                  <span class="xpath">XPath</span> to another. However, the contents of this
namespace may be extended to include additional error definitions.</p></div></div><div class="div3">
<h4><a id="id-handling-dynamic"/>2.4.3 <a href="#id-handling-dynamic" style="text-decoration: none">Handling Dynamic Errors</a></h4><p>Except as noted in this document, if any operand of an expression
raises a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>, the expression also raises a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>.
If an expression can validly return a value or raise a dynamic
error, the implementation may choose to return the value or raise
the dynamic error (see <a href="#id-errors-and-opt"><b>2.4.4 Errors and
      Optimization</b></a>).  For example, the logical expression
<code>expr1 and expr2</code> may return the value <code>false</code>
if either operand returns <code>false</code>,
or may raise a dynamic error if either operand raises a dynamic
error.</p><p> If more than one operand of an expression raises
an error, the
implementation may choose which error is raised by the expression.
For example, in this expression:

</p><div class="exampleInner"><pre>($x div $y) + xs:decimal($z)</pre></div><p>

both the sub-expressions <code>($x div $y)</code> and <code>xs:decimal($z)</code> may
raise an error.  The
implementation may choose which error is raised by the <code>+</code>
expression.  Once one operand raises an error, the implementation is
not required, but is permitted, to evaluate any other operands.</p><p>
                  [<a id="dt-error-value" title="error value">Definition</a>: In addition to its identifying QName, a dynamic error may also carry a descriptive string and one or more additional values called <b>error values</b>.] An implementation may provide a mechanism whereby an application-defined error handler can process error values and produce diagnostic messages. 
  
  <span class="xpath">The host language may also provide error handling mechanisms.</span>
               </p><p>A dynamic error may be raised by a <a title="system function" class="termref" href="#dt-system-function">system
function</a> or operator.  For example,
the <code>div</code> operator raises an error if its operands are <code>xs:decimal</code> values and its second operand
is equal to zero. Errors raised by system functions and operators are defined in <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a> or the host language.</p><p>A dynamic error can also be raised explicitly by calling the
<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-error"><code>fn:error</code></a> function, which always raises a dynamic error and never
returns a value.  This function is defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-error">Section 3.1.1 fn:error</a><sup><small>FO</small></sup>. For example, the following
function call raises a dynamic
error, providing a QName that identifies the error, a descriptive string, and a diagnostic value (assuming that the prefix <code>app</code> is bound to a namespace containing application-defined error codes):</p><div class="exampleInner"><pre>error( #app:err057, "Unexpected value", string($v) )</pre></div></div><div class="div3">
<h4><a id="id-errors-and-opt"/>2.4.4 <a href="#id-errors-and-opt" style="text-decoration: none">Errors and
      Optimization</a></h4><p>Because different implementations may
      choose to evaluate or optimize an expression in different ways,
      certain aspects of raising <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic errors</a> are <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>, as described in this section.</p><p>An implementation is always free to evaluate the operands of an operator in any order.</p><p>In some cases,  a processor can determine the result of an expression without accessing all the data that would be implied by the formal expression semantics. For example, the formal description of <a title="filter expression" class="termref" href="#dt-filter-expression">filter expressions</a> suggests that <code>$s[1]</code> should be evaluated by examining all the items in sequence <code>$s</code>, and selecting all those that satisfy the predicate <code>position()=1</code>. In practice, many implementations will recognize that they can evaluate this expression by taking the first item in the sequence and then exiting. If <code>$s</code> is defined by an expression such as <code>//book[author eq 'Berners-Lee']</code>, then this strategy may avoid a complete scan of a large document and may therefore greatly improve performance. However, a consequence of this strategy is that a dynamic error or type error that would be detected if the expression semantics were followed literally might not be detected at all if the evaluation exits early. In this example, such an error might occur if there is a <code>book</code> element in the input data with more than one <code>author</code> subelement.</p><p>The extent to which a processor may optimize its access to data, at the cost of not raising errors, is defined by the following rules.</p><p>Consider an expression <em>Q</em> that has an operand (sub-expression) <em>E</em>. In general the value of <em>E</em> is a sequence. At an intermediate stage during evaluation of the sequence, some of its items will be known and others will be unknown. If, at such an intermediate stage of evaluation, a processor is able to establish that there are only two possible outcomes of evaluating <em>Q</em>, namely the value <em>V</em> or an error, then the processor may deliver the result <em>V</em> without evaluating further items in the operand <em>E</em>. For this purpose, two values are considered to represent the same outcome if their items are pairwise the same, where nodes are the same if they have the same identity, and values are the same if they are equal and have exactly the same type.</p><p>There is an exception to this rule: If a processor evaluates an operand <em>E</em> (wholly or in part), then it  is required to establish that the actual value of the operand <em>E</em> does not violate any constraints on its cardinality. For example, the expression <code>$e eq 0</code> results in a type error if the value of <code>$e</code> contains two or more items. A processor is not allowed to decide, after evaluating the first item in the value of <code>$e</code> and finding it equal to zero, that the only possible outcomes are the value <code>true</code> or a type error caused by the cardinality violation. It must establish that the value of <code>$e</code> contains no more than one item.</p><p>These rules apply to all the operands of an expression considered in combination: thus if an expression has two operands <em>E1</em> and <em>E2</em>, it may be evaluated using any samples of the respective sequences that satisfy the above rules.</p><p>The rules cascade: if <em>A</em> is an operand of <em>B</em> and <em>B</em> is an operand of <em>C</em>, then the processor needs to evaluate only a sufficient sample of <em>B</em> to determine the value of <em>C</em>, and needs to evaluate only a sufficient sample of <em>A</em> to determine this sample of <em>B</em>.</p><p>The effect of these rules is that the processor is free to stop examining further items in a sequence as soon as it can establish that further items would not affect the result except possibly by causing an error. For example, the processor may return <code>true</code> as the result of the expression <code>S1 = S2</code> as soon as it finds a pair of equal values from the two sequences.</p><p>Another consequence of these rules is that where none of the items in a sequence contributes to the result of an expression, the processor is not obliged to evaluate any part of the sequence. Again, however, the processor cannot dispense with a required cardinality check: if an empty sequence is not permitted in the relevant context, then the processor must ensure that the operand is not an empty sequence.</p><p>Examples:</p><ul><li><p>If an implementation can find (for example, by using an index) that at
least one item returned by <code>$expr1</code> in the following example has the value <code>47</code>, it is allowed to
return <code>true</code> as the result of the <code>some</code> expression, without searching for
another item returned by <code>$expr1</code> that would raise an error if it were evaluated.
</p><div class="exampleInner"><pre>some $x in $expr1 satisfies $x = 47</pre></div></li><li><p>In the following example, if an implementation can find (for example, by using an index) the
<code>product</code> element-nodes that have an <code>id</code> child with the value <code>47</code>, it is allowed to return these nodes as the
result of the <a title="path expression" class="termref" href="#dt-path-expression">path expression</a>, without searching for another <code>product</code> node that
would raise an error because it has an <code>id</code> child whose value is not an integer.</p><div class="exampleInner"><pre>//product[id = 47]</pre></div></li></ul><p>For a variety of reasons, including optimization, implementations
          may rewrite expressions into a different
          form. There are a number of rules that limit the extent of this freedom:
          </p><ul><li><p>
                Other than the raising or not raising of errors, the result
                of evaluating a rewritten expression must
                conform to the semantics
                defined in this specification for the original expression.
             </p><div class="note"><p class="prefix"><b>Note:</b></p><p>
                This allows an implementation to return a result in cases where the
                original expression would have raised an error, or to raise an error in cases
                where the original expression would have returned a result. The main cases
                where this is likely to arise in practice are (a) where a rewrite changes the
                order of evaluation, such that a subexpression causing an error is evaluated
                when the expression is written one way and is not evaluated when the expression
                is written a different way, and (b) where intermediate results of the
                evaluation cause overflow or other out-of-range conditions.
             </p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>
                This rule does not mean that the result of the expression will always
                be the same in non-error cases as if it had not been rewritten, because there
                are many cases where the result of an expression is to some degree
                <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>
                or <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>.
             </p></div></li><li><p>
                     The rules described in <a href="#id-guarded-expressions"><b>2.4.5 Guarded Expressions</b></a> ensure that for
                     certain kinds of expression (for example conditional expressions), changing the
                     order of evaluation of subexpressions does not result in dynamic errors that
                     would not otherwise occur.</p></li><li><p>
                Expressions must not be rewritten in such a way
                as to create or remove static errors.

                The static errors in this specification are defined
                for the original expression, and must be preserved if
                the expression is rewritten.</p></li><li><p>
                     As stated earlier, an expression
                     must not be rewritten to dispense with a
                     required cardinality check: for example, <code>string-length(//title)</code>
                     must raise an
                     error if the document contains more than one title element.
                  </p></li></ul></div><div class="div3">
<h4><a id="id-guarded-expressions"/>2.4.5 <a href="#id-guarded-expressions" style="text-decoration: none">Guarded Expressions</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-implausible-expressions">⬇</a> <a href="#id-processing-model">⬆</a></p><ol><li><p>
                  The rules for “errors and optimization” have been tightened up to disallow
                  many cases of optimizations that alter error behavior. In particular
                  there are restrictions on reordering the operands of <code>and</code> and <code>or</code>,
                  and of predicates in filter expressions, in a way that might allow the processor to raise dynamic
                  errors that the author intended to prevent.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/71">71</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/230">230</a> 15 November 2022]</i></p></li></ol></div><p>
                  [<a id="dt-guarded" title="guarded">Definition</a>: An expression <var>E</var> is said to be <b>guarded</b>
               by some governing condition <var>C</var> if evaluation of <var>E</var> is not allowed to fail
               with a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> except when <var>C</var> applies.]
               </p><p>For example, in a conditional expression <code>if (P) then T else F</code>, the subexpression
               <var>T</var> is guarded by <var>P</var>, and the subexpression <var>F</var> is guarded by
               <code>not(P)</code>. One way an implementation can satisfy this rule is by not evaluating <var>T</var> unless <var>P</var>
               is true, and likewise not evaluating <var>F</var> unless <var>P</var> is false. Another
               way of satisfying the rule is for the implementation to evaluate all the subexpressions, but to catch any errors that occur 
               in a guarded subexpression so they are not propagated.
            </p><p>The existence of this rule enables errors to be prevented by writing expressions such as
            <code>if ($y eq 0) then "N/A" else ($x div $y)</code>. This example will never fail with a divide-by-zero
            error because the <code>else</code> branch of the conditional is <a title="guarded" class="termref" href="#dt-guarded">guarded</a>.</p><p>Similarly, in the mapping expression <code>
                     <var>E<sub>1</sub></var>!<var>E<sub>2</sub></var>
                  </code>, the subexpression <var>E<sub>2</sub></var> is guarded
            by the existence of an item from <var>E<sub>1</sub></var>. This means, for example, that the expression <code>(1 to $n)!doc('bad.xml')</code>
            must not raise a dynamic error if <code>$n</code> is zero. The rule governing evaluation of guarded expressions
               is phrased so as not to disallow “loop-lifting” or “constant-folding” optimizations 
               whose aim is to avoid repeated evaluation of a common subexpression;
            but such optimizations must not result in errors that would not otherwise occur.</p><p>The complete list of expressions that have guarded subexpressions is as follows:</p><ul><li><p>In a conditional expression (<a href="#doc-xpath40-IfExpr">IfExpr</a>) the <code>then</code> branch
                  is guarded by the condition being true, and the <code>else</code> branch
                  is guarded by the condition being false.</p></li><li><p>In an <code>and</code> expression (<a href="#prod-xpath40-AndExpr">AndExpr</a>), the second operand
               is guarded by the value of the first operand being true.</p></li><li><p>In an <code>or</code> expression (<a href="#doc-xpath40-OrExpr">OrExpr</a>), the second operand
                  is guarded by the value of the first operand being false.</p></li><li><p>In an <code>otherwise</code> expression (<a href="#doc-xpath40-OtherwiseExpr">OtherwiseExpr</a>), the second operand
                  is guarded by the value of the first operand being an empty sequence.</p></li><li><p>In a path expression of the form <code>
                           <var>E<sub>1</sub></var>/<var>E<sub>2</sub></var>
                        </code> 
                  or <code>
                           <var>E<sub>1</sub></var>//<var>E<sub>2</sub></var>
                        </code>, and in a mapping
               expression of the form <code>
                           <var>E<sub>1</sub></var>!<var>E<sub>2</sub></var>
                        </code>, the right-hand operand <var>E<sub>2</sub></var> is guarded by
               the existence of at least one item in the result of evaluating <var>E<sub>1</sub></var>.</p><p>This rule applies even if <var>E<sub>2</sub></var> does not reference the context value.
                     For example, no dynamic error can be thrown by the expression 
               <code>(1 to $n)!doc('bad.xml')</code> in the case where <code>$n</code> is zero.</p></li><li><p>In a filter expression of the form <code>
                           <var>E</var>[<var>P</var>]</code>, 
                  the predicate <var>P</var> is guarded by
                  the existence of at least one item in the result of evaluating <var>E</var>.</p><p>This rule has the consequence that in a filter expression with multiple predicates, 
                  such as <code>
                           <var>E</var>[<var>P<sub>1</sub></var>][<var>P<sub>2</sub></var>]</code>,
               evaluation of <var>P<sub>2</sub></var> must not raise a dynamic error unless <var>P<sub>1</sub></var> returns <code>true</code>. This rule does
               not prevent reordering of predicates (for example, to take advantage of indexes), but it does require that any
               such reordering must not result in errors that would not otherwise occur.</p></li><li><p class="xpath">In a <code>for</code> expression (<a href="#doc-xpath40-ForExpr">ForExpr</a>) such
                  as <code>for $x in <var>S</var> return <var>E</var>
                        </code>, 
                  the expression <var>E</var> is guarded by the existence of
                  an item bound to <code>$x</code>.</p><p>This means that the expression <code>for $x in 1 to $n return doc('bad.xml')</code>
               must not raise a dynamic error in the case where <code>$n</code> is zero.</p></li><li><p>In a <code>quantified</code> expression (<a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>) such
                  as <code>some $x in <var>S</var> satisfies <var>P</var>
                        </code>, the expression 
                  <var>P</var> is guarded by the existence of
                     an item bound to <code>$x</code>.</p></li></ul><p>The fact that an expression is <a title="guarded" class="termref" href="#dt-guarded">guarded</a> does not remove the obligation to report
            <a title="static error" class="termref" href="#dt-static-error">static errors</a> in the expression; nor does it remove the option
            to report statically detectable <a title="type error" class="termref" href="#dt-type-error">type errors</a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>These rules do not constrain the order of evaluation of subexpressions. For example, given an expression
                  such as <code>//person[@first = "Winston"][@last = "Churchill"]</code>, or equivalently
                  <code>//person[@first = "Winston" and @last = "Churchill"]</code>, an implementation might use an index on the value of
                  <code>@last</code> to select items that satisfy the second condition, and then filter these
                  items on the value of the first condition. Alternatively, it might evaluate both predicates in parallel.
                  Or it might interpose an additional redundant condition: 
                  <code>//person[string-length(@first) + string-length(@last) = 16][@first = "Winston"][@last = "Churchill"]</code>.
                  But implementations must ensure that
                  such rewrites do not result in dynamic errors being reported that would not occur if the predicates
                  were evaluated in order as written.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Although the rules for guarded expressions prevent optimizations resulting in spurious errors, 
                  they do not prevent optimizations whose effect is to mask errors. For example, the rules guarantee that
                  <code>("A", 3)[. instance of xs:integer][. eq 3]</code> will not raise an error caused by the comparison
                  <code>("A" eq 3)</code>, but they
                  do not guarantee the converse: the expression <code>("A", 3)[. eq 3][. instance of xs:integer]</code>
                  may or may not raise a dynamic error.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The rules in this section do not disallow all expression rewrites that might result in dynamic
                  errors. For example, rewriting <code>($x - $y + $z)</code> as <code>($x + $z - $y)</code> is permitted
               even though it might result in an arithmetic overflow.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Some implementations allow calls on external functions that have side-effects. The semantics of
               such function calls are entirely <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation defined</a>. Processors <span class="verb">may</span>
               choose to reference the rules for <a title="guarded" class="termref" href="#dt-guarded">guarded</a> expressions when defining the behavior
               of such function calls, but this is outside the scope of the language specification.</p></div></div><div class="div3">
<h4><a id="id-implausible-expressions"/>2.4.6 <a href="#id-implausible-expressions" style="text-decoration: none">Implausible Expressions</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-choice-item-types">⬇</a> <a href="#id-guarded-expressions">⬆</a></p><ol><li><p>
                  The rules for reporting type errors during static analysis have been changed
                  so that a processor has more freedom to report errors in respect of constructs that
                  are evidently wrong, such as <code>@price/@value</code>, even though dynamic evaluation
                  is defined to return an empty sequence rather than an error.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/602">602</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/603">603</a> 25 July 2023]</i></p></li></ol></div><p>
                  [<a id="dt-implausible" title="implausible">Definition</a>: Certain expressions, while not
            erroneous, are classified as being <b>implausible</b>,
            because they achieve no useful effect.]
               </p><p>An example of an implausible expression is <code>@code/text()</code>. This expression
            will always evaluate to an empty sequence, because attribute nodes cannot have text
            node children. The semantics of the expression are well defined, but it is likely
            that the user writing this expression intended something different: if they wanted to write an 
            expression that evaluated to an empty sequence, there would be easier ways to write it.</p><p>Where an expression is classified (by rules in this specification) as being
            <a title="implausible" class="termref" href="#dt-implausible">implausible</a>, a processor <span class="verb">may</span> (but is
            not <span class="verb">required</span> to) raise a static error.</p><p>For reasons of backwards compatibility and interoperability, and to facilitate
            automatic generation of XPath 4.0 code, a processor <span class="verb">must</span> 
            provide a mode of operation in which <a title="implausible" class="termref" href="#dt-implausible">implausible</a>
            expressions are not treated as static errors, but are evaluated
            with the defined semantics for the expression.</p><p>Some other examples of implausible expressions include:</p><ul><li><p>
                        <code>round(tokenize($input))</code>. The result of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-tokenize"><code>fn:tokenize</code></a>
               is a sequence of strings (<code>xs:string*</code>), while the required type for the
               first argument of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-round"><code>fn:round</code></a> is optional numeric (<code>xs:numeric?</code>).
               The expression can succeed only in the exceptional case where the result of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-tokenize"><code>fn:tokenize</code></a>
               is an empty sequence, in which case the result of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-round"><code>fn:round</code></a> will also be
               an empty sequence; it is therefore highly likely that the expression was written in error.</p></li><li><p>
                        <code>parse-csv($input)?column-names</code>. The signature of the <code>parse-csv</code>
               function declares its return type as <code>record(columns, rows)</code>. There is no field in this
               record named <code>column-names</code>, and therefore the lookup expression will always
               return an empty sequence. Again, there is no good reason that a user would write this,
               so it is likely that it was written in error.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The specification is deliberately conservative in the choice of constructs that have
            been classified as implausible. Constructs have not been classified as implausible merely because there
            are better ways of writing the same thing, but only in cases where it is considered that no user in full understanding
            of the specification would intentionally write such a construct. All these cases correspond to situations
            that would be classed as errors in a language with stricter static typing rules.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>In many cases the classification of constructs as implausible is designed to protect users
            from usability problems that have been found with earlier versions of the language. without
            introducing backwards incompatibilities.</p></div></div></div><div class="div2">
<h3><a id="id-important-concepts"/>2.5 <a href="#id-important-concepts" style="text-decoration: none">Concepts</a></h3><p>This section explains some concepts that are important to the processing of XPath 4.0 expressions.</p><div class="div3">
<h4><a id="id-document-order"/>2.5.1 <a href="#id-document-order" style="text-decoration: none">Document Order</a></h4><p>An ordering called <b>document order</b> is defined among all the nodes accessible during processing of a given 
               <span class="xpath">expression</span>, which may consist of one or more <b>trees</b> (documents or fragments). 

Document order is defined in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#document-order">Section 6.3 Document Order</a><sup><small>DM</small></sup>, and its definition is repeated here for convenience. 

Document order is a total ordering, although the relative order of some nodes is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>.  

[<a id="dt-document-order" title="document order">Definition</a>: Informally, <b>document order</b> is the order in which nodes appear in the XML serialization of a document.]
                  [<a id="stable" title="stable">Definition</a>: Document order is <b>stable</b>, which means that the relative order of two nodes will not change during the processing of a given 
                  <span class="xpath">expression</span>, even if this order is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>.]
                  [<a id="dt-reverse-document-order" title="reverse document order">Definition</a>: The node ordering that is the reverse of document order is called <b>reverse document order</b>.]
               </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>Every node occurs before all of its children and descendants.</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" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>.</p></li><li><p>Attribute nodes immediately follow the <span class="xpath">namespace nodes of the
</span> element node with which they are associated. The relative order of
attribute nodes is stable but <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>.</p></li><li><p>The relative order of siblings is the order in which they occur
in the <code>children</code> property of their parent node.</p></li><li><p>Children and descendants occur before following siblings.</p></li></ol><p>The relative order of nodes in distinct trees is stable but
<a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>,
subject to the following constraint: If any node in a given tree T1 is before
any node in a different tree T2, then all nodes in tree T1 are before all nodes in
tree T2.</p></div><div class="div3">
<h4><a id="id-typed-value"/>2.5.2 <a href="#id-typed-value" style="text-decoration: none">Typed Value and String Value</a></h4><p>Every node has a <b>typed value</b> and a <b>string value</b>, except for nodes whose value is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>.
               
               [<a id="dt-typed-value" title="typed value">Definition</a>: The <b>typed
                     value</b> of a node is a sequence of atomic items and can be
                  extracted by applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-data">Section 2.1.4 fn:data</a><sup><small>FO</small></sup> function to the
                  node.]
                  [<a id="dt-string-value" title="string value">Definition</a>: The
                  <b>string value</b> of a node is a string and can be extracted
                  by applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-string">Section 2.1.3 fn:string</a><sup><small>FO</small></sup>
                  function to the node.]
               </p><p>An implementation may store both the <a title="typed value" class="termref" href="#dt-typed-value">typed value</a> and the <a title="string value" class="termref" href="#dt-string-value">string value</a> of a node, or it may store only one of these and derive the other as needed.
               The string value of a node must be a valid lexical representation of the typed value of the node,
               but the node is not required to preserve the string representation from the original source document.
               For example, if the typed value of a node is the <code>xs:integer</code> value <code>30</code>,
               its string value might be <code>"30"</code> or <code>"0030"</code>.</p><p class="xpath">The <a title="typed value" class="termref" href="#dt-typed-value">typed value</a>, <a title="string value" class="termref" href="#dt-string-value">string value</a>, and <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of a node are closely related.  If the node was created by mapping from an Infoset or PSVI, the relationships among these properties are defined by rules in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#types">Section 4.1 Schema Information</a><sup><small>DM</small></sup>.</p><p>The relationship between <a title="typed value" class="termref" href="#dt-typed-value">typed value</a> and
               <a title="string value" class="termref" href="#dt-string-value">string value</a> for various kinds of nodes is summarized 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>xs: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" class="termref" href="#dt-document-order">document
                        order</a>.</p></li><li><p>The typed value of a comment<span class="xpath">, namespace,</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 <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>
                        <code>xs:anySimpleType</code> or <code>xs:untypedAtomic</code> is the same as its
                     string value, as an instance of <code>xs: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="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</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 whose item type is 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 items (<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 <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a> 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 <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>, as follows:</p><ol class="enumla"><li><p>If the type annotation is <code>xs:untyped</code> or <code>xs:anySimpleType</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>xs:untypedAtomic</code>.  However, if the <code>nilled</code>
                           property of the node is <code>true</code>, then its typed value is the empty sequence.</p><p>Example: E1 is an element node
                           having type annotation <code>xs: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>xs: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>xs: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. However, if the <code>nilled</code>
                           property of the node is <code>true</code>, then its typed value is the empty sequence.</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 <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>
                              <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><p>Example: E5 is an element node with the type annotation <code>my:integer-or-string</code>
                           which is a union type with member types <code>xs:integer</code> and <code>xs:string</code>.
                           The string value of E5 is <code>"47"</code>. The typed value of E5 is <code>47</code> as a
                           <code>xs:integer</code>, since <code>xs:integer</code> is the member type that validated the
                           content of E5. In general, when the type annotation of a node is a union type,
                           the typed value of the node will be an instance of one of the member types of the union.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>If an implementation stores only the string value of a node, and the type annotation of the node is a union type, the implementation must be able to deliver the typed value of the node as an instance of the appropriate member type.</p></div></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 <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>. The <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-data"><code>fn:data</code></a> function raises a
                           <a title="type error" class="termref" href="#dt-type-error">type error</a>
                              [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFOTY0012" title="err:FOTY0012">err:FOTY0012</a>]<sup><small>FO40</small></sup> when applied to such a node. The string value of such a node is equal to the concatenated string values of all its text node descendants, in document order.</p><p>Example: E6 is an
                           element node with the type annotation <code>weather</code>, which is a
                           complex type whose content type specifies
                           <code>element-only</code>. E6 has two child elements named
                           <code>temperature</code> and <code>precipitation</code>. The typed
                           value of E6 is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>, and the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-data"><code>fn:data</code></a> function
                           applied to E6 raises an error.
                        </p></li></ol></li></ol></div><div class="div3">
<h4><a id="id-atomization"/>2.5.3 <a href="#id-atomization" style="text-decoration: none">Atomization</a></h4><p>The semantics of some
XPath 4.0 operators depend on a process called <a title="atomization" class="termref" href="#dt-atomization">atomization</a>. Atomization is
applied to a value when the value is used in a context in which a
sequence of atomic items is required. The result of atomization is
either a sequence of atomic items or a <a title="type error" class="termref" href="#dt-type-error">type error</a>
                  [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFOTY0012" title="err:FOTY0012">err:FOTY0012</a>]<sup><small>FO40</small></sup>.  [<a id="dt-atomization" title="atomization">Definition</a>: 
                     <b>Atomization</b> of a sequence
is defined as the result of invoking the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-data"><code>fn:data</code></a> function, as defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-data">Section 2.1.4 fn:data</a><sup><small>FO</small></sup>.]
               </p><p> The semantics of
<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-data"><code>fn:data</code></a> are repeated here for convenience. The result of
<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-data"><code>fn:data</code></a> is the sequence of atomic items produced by
applying the following rules to each item in the input
sequence:</p><ul><li><p>If the item is an atomic item, it is
returned.</p></li><li><p>If the item is a node,
its <a title="typed value" class="termref" href="#dt-typed-value">typed value</a> is returned (a <a title="type error" class="termref" href="#dt-type-error">type error</a>
                        [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFOTY0012" title="err:FOTY0012">err:FOTY0012</a>]<sup><small>FO40</small></sup> is raised if the node has no typed value.)</p></li><li><p>If the item is a <a title="function item" class="termref" href="#dt-function-item">function item</a> (other than an array) or map a <a title="type error" class="termref" href="#dt-type-error">type error</a>
                        [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFOTY0013" title="err:FOTY0013">err:FOTY0013</a>]<sup><small>FO40</small></sup> is raised.</p></li><li><p>If the item is an array <code>$a</code>, atomization is defined as <code>$a?* ! fn:data(.)</code>, which is equivalent to atomizing the members of the array.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>This definition recursively atomizes members that are arrays. Hence, the result of atomizing the array <code>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]</code> is the sequence <code>(1, 2, 3, 4, 5, 6)</code>.</p></div></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"/>2.5.4 <a href="#id-ebv" style="text-decoration: none">Effective Boolean Value</a></h4><p>Under certain circumstances (listed below), it is necessary to find
the <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a> of a
value. [<a id="dt-ebv" title="effective boolean value">Definition</a>: The
<b>effective boolean value</b> of a value is defined as the result
of applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> function to the value, as
defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-boolean">Section 8.3.1 fn:boolean</a><sup><small>FO</small></sup>.]
               </p><p>The dynamic semantics of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> are repeated here for convenience:</p><ol class="enumar"><li><p>If its operand is an empty sequence, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> returns <code>false</code>.</p></li><li><p>If its operand is a sequence whose first item is a node, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> returns <code>true</code>.</p></li><li><p>If its operand is a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> value of 
                     type <code>xs:boolean</code> or derived from <code>xs:boolean</code>, 
                     <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> returns the value of its operand unchanged.</p></li><li><p>If its operand is a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> value of 
                     type <code>xs:string</code>, <code>xs:anyURI</code>, 
                     <code>xs:untypedAtomic</code>, or a type derived from one of these,
                     <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> returns <code>false</code> 
                     if the operand value has zero length; otherwise it returns <code>true</code>.</p></li><li><p>If its operand is a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> value of 
                     any <a title="numeric" class="termref" href="#dt-numeric">numeric</a> type or derived from a numeric
                     type, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> returns <code>false</code> 
                     if the operand value is <code>NaN</code> or is numerically 
                     equal to zero; otherwise it returns <code>true</code>.</p></li><li><p>In all other cases, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> raises a type error [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFORG0006" title="err:FORG0006">err:FORG0006</a>]<sup><small>FO40</small></sup>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>For instance, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> raises a type error if the operand is a function, a map, or an array.</p></div></li></ol><p>The <a title="effective boolean value" class="termref" 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 <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-not"><code>fn:not</code></a> function</p></li><li><p>Certain types of <a title="" class="termref" href="#dt-predicate">predicates</a>, 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><li class="xpath"><p>General comparisons, in <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0
		compatibility mode</a>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The definition of <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean
  value</a> is <em>not</em> used when casting a value to the
  type <code>xs:boolean</code>, for example in a <code>cast</code>
  expression or when passing a value to a function whose expected
  parameter is of type <code>xs:boolean</code>.</p></div></div><div class="div3">
<h4><a id="id-uri-literals"/>2.5.5 <a href="#id-uri-literals" style="text-decoration: none">URI Literals</a></h4><p>XPath 4.0 requires a statically known, valid URI in a <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>. 

      An implementation may raise a <a title="static error" class="termref" href="#dt-static-error">static error</a>
                  [<a href="#ERRXQST0046" title="err:XQST0046">err:XQST0046</a>] if the value of  a Braced URI Literal is of nonzero length
      and is neither an
      absolute URI nor a relative URI.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The <code>xs:anyURI</code>
      type is designed to anticipate the introduction of
      Internationalized Resource Identifiers (IRIs) as defined in
      <a href="#RFC3987">[RFC3987]</a>.</p></div><p>Whitespace is normalized using the whitespace normalization rules
      of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-normalize-space"><code>fn:normalize-space</code></a>. If the result of whitespace
      normalization contains only whitespace, the corresponding URI
      consists of the empty string.  
            </p><p>A Braced URI Literal or URI Literal is not
      subjected to percent-encoding
      or decoding as defined in <a href="#RFC3986">[RFC3986]</a>.</p></div><div class="div3">
<h4><a id="id-resolve-relative-uri"/>2.5.6 <a href="#id-resolve-relative-uri" style="text-decoration: none">Resolving a Relative URI Reference</a></h4><p>
                  [<a id="dt-resolve-relative-uri" title="resolve">Definition</a>: To
      <b>resolve a relative URI</b>
                     <code>$rel</code> against a
      base URI <code>$base</code> is to expand it to an absolute URI,
      as if by calling the function <code>fn:resolve-uri($rel,
      $base)</code>.] During static analysis, the base URI is
      the Static Base URI. During dynamic evaluation, the base URI
      used to resolve a relative URI reference depends on the semantics of the
      expression.</p><p>Any process that attempts to <a title="resolve" class="termref" href="#dt-resolve-relative-uri">resolve a URI</a> against a
      base URI, or to dereference the URI, may apply percent-encoding
      or decoding as defined in the relevant RFCs.</p></div></div></div><div class="div1">
<h2><a id="id-types"/>3 <a href="#id-types" style="text-decoration: none">Types</a></h2><p>As noted in <a href="#id-values"><b>2.1.2 Values</b></a>, every value in XPath 4.0 is regarded
      as a <a title="sequence" class="termref" href="#dt-sequence">sequence</a> of zero, one, or more <a title="item" class="termref" href="#dt-item">items</a>.
      The type system of XPath 4.0, described in this section, classifies the
      kinds of value that the language can handle, and the operations permitted
      on different kinds of value.</p><p>The type system of XPath 4.0 is related to the type system of
		<a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a> in two ways:</p><ul><li><p>atomic items in XPath 4.0 (which are one kind of <a title="item" class="termref" href="#dt-item">item</a>)
               have <a title="atomic type" class="termref" href="#dt-atomic-type">atomic types</a> such as <code>xs:string</code>,
               <code>xs:boolean</code>, and <code>xs:integer</code>. These types are taken directly
               from their definitions in <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a>.</p></li><li><p>Nodes (which are another kind of <a title="item" class="termref" href="#dt-item">item</a>) have a property
            called a <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> which determines the type of their content.
            The type annotation is a <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>. The type annotation of a node
            must not be confused with the item type of the node. For example, an element 
            <code>&lt;age&gt;23&lt;/age&gt;</code> might have been validated against a schema
               that defines this element as having <code>xs:integer</code> content. If this
               is the case, the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of the node will be
               <code>xs:integer</code>, and in the XPath 4.0 type system, the node will
               match the <a title="item type" class="termref" href="#dt-item-type">item type</a>
                  <code>element(age, xs:integer)</code>.
            </p></li></ul><p>This chapter of the specification starts by defining 
            <a title="sequence type" class="termref" href="#dt-sequence-type">sequence types</a> and 
         <a title="item type" class="termref" href="#dt-item-type">item types</a>, which describe the range of
            <a title="value" class="termref" href="#dt-value">values</a> that can be bound to variables, used in 
            expressions, or passed to functions. It then describes how these
            relate to <a title="schema type" class="termref" href="#dt-schema-type">schema types</a>,
            that is, the simple and complex types defined in an XSD schema.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>In many situations the terms <b>item type</b> and 
         <b>sequence type</b> are used interchangeably to refer either to the type itself,
         or to the syntactic construct that designates the type: so in the expression
         <code>$x instance of xs:string*</code>, the construct <code>xs:string*</code>
         uses the <a href="#doc-xpath40-SequenceType">SequenceType</a> syntax to designate a
         <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a> whose instances are sequences of strings.
         When more precision is required, the specification is careful to use the terms
         <a title="item type" class="termref" href="#dt-item-type">item type</a> and <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a> to
         refer to the actual types, while using the production names <a href="#doc-xpath40-ItemType">ItemType</a>
         and <a href="#doc-xpath40-SequenceType">SequenceType</a> to refer to the syntactic
         designators of these types.</p></div><div class="div2">
<h3><a id="id-sequencetype-syntax"/>3.1 <a href="#id-sequencetype-syntax" style="text-decoration: none">Sequence Types</a></h3><p>
               [<a id="dt-sequence-type" title="sequence type">Definition</a>: A <b>sequence type</b> is a type that can be expressed using the <a href="#doc-xpath40-SequenceType">SequenceType</a>
                  syntax. Sequence types are used whenever it is necessary to refer to a type in an XPath 4.0 expression. 
                  Since all values are sequences, every value matches one or more <b>sequence types</b>.]
            </p><p>Whenever it is necessary to refer to a <b>sequence type</b> 
            in an XPath 4.0 expression, the <a href="#doc-xpath40-SequenceType">SequenceType</a> syntax is used.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-SequenceType-ItemType">ItemType</a>
                     <a href="#doc-xpath40-SequenceType-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SequenceType-ItemType"/><code><a href="#prod-xpath40-ItemType">ItemType</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-AnyItemTest">AnyItemTest</a>  |  <a href="#prod-xpath40-TypeName">TypeName</a>  |  <a href="#prod-xpath40-KindTest">KindTest</a>  |  <a href="#doc-xpath40-FunctionType">FunctionType</a>  |  <a href="#doc-xpath40-MapType">MapType</a>  |  <a href="#doc-xpath40-ArrayType">ArrayType</a>  |  <a href="#doc-xpath40-RecordType">RecordType</a>  |  <a href="#doc-xpath40-EnumerationType">EnumerationType</a>  |  <a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SequenceType-OccurrenceIndicator"/><code><a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a></code></td><td>::=</td><td><code>"?"  |  "*"  |  "+"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-occurrence-indicators">xgc: occurrence-indicators</a>
                   */</td></tr></tbody></table><p>
               [<a id="dt-sequence-type-designator" title="sequence type designator">Definition</a>: A
      <b>sequence type designator</b> is a syntactic construct conforming to the grammar rule
         <a href="#doc-xpath40-SequenceType">SequenceType</a>. A sequence type designator is said
      to <b>designate</b> a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>.]
            </p><p>With the exception of the special type
<code>empty-sequence()</code>, a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a> consists of an
<a title="item type" class="termref" href="#dt-item-type">item type</a> that constrains the type of each item in the
sequence, and a <b>cardinality</b> that constrains the number of
items in the sequence. Apart from the item type <code>item()</code>,
which permits any kind of item, item types divide into <b>node
types</b> (such as <code>element()</code>), <b>generalized atomic
types</b> (such as <code>xs:integer</code>) and function types
(such as <code>function() as item()*</code>).</p><p>The cardinality of a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a> is represented
               in the <a title="sequence type designator" class="termref" href="#dt-sequence-type-designator">sequence type designator</a> syntax by
               an <a href="#prod-xpath40-OccurrenceIndicator"/>. 
               The occurrence indicators <code>+</code>, <code>*</code>, and <code>?</code> 
               bind to the last <a href="#doc-xpath40-ItemType">ItemType</a> in the <a href="#doc-xpath40-SequenceType">SequenceType</a>, as described in the <a href="#parse-note-occurrence-indicators">occurrence-indicators</a> constraint.</p><div class="div3">
<h4><a id="id-sequence-type-examples"/>3.1.1 <a href="#id-sequence-type-examples" style="text-decoration: none">Examples of Sequence Types</a></h4><p>Here are some examples of <a title="sequence type" class="termref" href="#dt-sequence-type">sequence types</a> that
		  might be used in XPath 4.0:</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 node</p></li><li><p>
                        <code>element()</code> refers to any element node</p></li><li><p>
                        <code>element(po:shipto, po:address)</code> refers to an element node that has the name <code>po:shipto</code> and has the type annotation <code>po:address</code> (or a schema type derived from <code>po:address</code>)</p></li><li><p>
                        <code>element(*, po:address)</code> refers to an element node 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 node named <code>customer</code> with any type annotation</p></li><li><p>
                        <code>schema-element(customer)</code> refers to an element node whose name is <code>customer</code> (or is in the substitution group headed by <code>customer</code>) and whose type annotation matches the schema type declared for a <code>customer</code> element in the <a title="in-scope element declarations" class="termref" 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 kind</p></li><li><p>
                        <code>item()+</code> refers to a sequence of one or more <a title="item" class="termref" href="#dt-item">items</a>
                     </p></li><li><p>
                        <code>function(*)</code> refers to any <a title="function item" class="termref" href="#dt-function-item">function item</a>, regardless of arity or type</p></li><li><p>
                        <code>function(node()) as xs:string*</code> refers to a <a title="function item" class="termref" href="#dt-function-item">function item</a> that takes a single argument whose value is a single node,
        and returns a sequence of zero or more <code>xs:string</code> values</p></li><li><p>
                        <code>(fn(node()) as xs:string)*</code> refers to a sequence of zero or more <a title="function item" class="termref" href="#dt-function-item">function items</a>, each of which takes a single
        argument whose value is a single node, and returns as its result a single <code>xs:string</code> value</p></li></ul></div><div class="div3">
<h4><a id="id-sequencetype-matching"/>3.1.2 <a href="#id-sequencetype-matching" style="text-decoration: none">Sequence Type Matching</a></h4><p>
                  [<a id="dt-sequencetype-matching" title="SequenceType matching">Definition</a>: 
                     <b>SequenceType matching</b> compares a value with an expected <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>. ] For example, an <code>instance of</code> expression 
               returns <code>true</code> if a given value matches a given <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>, and <code>false</code> if it does not.</p><p>An XPath 4.0 implementation must be able to determine relationships among the types in type annotations in an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instance</a> and the types in the  <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a> (ISSD). 
            </p><p>
                  [<a id="dt-subtype-substitution" title="subtype substitution">Definition</a>: The use of a value that has a <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a>
                      that is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of 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" class="termref" 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><ul><li><p>The <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>
                        <code>empty-sequence()</code> matches a value that is the empty sequence.</p></li><li><p>An <a href="#doc-xpath40-ItemType">ItemType</a> with no <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a> matches any value that contains exactly one item if the <a href="#doc-xpath40-ItemType">ItemType</a> matches that item (see <a href="#id-matching-item"><b>3.2 Item Types</b></a>).</p></li><li><p>An <a href="#doc-xpath40-ItemType">ItemType</a> with an <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a> matches a value if the number of items in the value matches the <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a> and the <a href="#doc-xpath40-ItemType">ItemType</a> matches each of the items in the value.</p></li></ul><p>An <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a> specifies the number of items in
		    a sequence, as follows:</p><ul><li><p>
                        <code>?</code> matches zero or one items</p></li><li><p>
                        <code>*</code> matches zero or more items</p></li><li><p>
                        <code>+</code> matches one or more items</p></li></ul><p>As a consequence of these rules, any <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a> whose
		    <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a> is <code>*</code> or <code>?</code> matches a
		    value that is an empty sequence.</p></div><div class="div3">
<h4><a id="id-schema-type-derivation"/>3.1.3 <a href="#id-schema-type-derivation" style="text-decoration: none">Schema Type Relationships</a></h4><p>Some <a title="item type" class="termref" href="#dt-item-type">item types</a> are defined in terms
            of <a title="schema type" class="termref" href="#dt-schema-type">schema types</a>, and the matching
            rules for such item types depend on the rules defining relationships between
            schema types in the XSD specification.</p><p>
                  [<a id="dt-derives-from" title="derives from">Definition</a>: A 
               <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>
                     <var>S<sub>1</sub></var> is said to <b>derive from</b>
                     <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>
                     <var>S<sub>2</sub></var> if any of the following
               conditions is true:
               </p><ul><li><p>
                        <var>S<sub>1</sub></var> is the same type as <var>S<sub>2</sub></var>.</p></li><li><p>
                        <var>S<sub>2</sub></var> is the base type of <var>S<sub>1</sub></var>.</p></li><li><p>
                        <var>S<sub>2</sub></var> is a <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a>
                     of which <var>S<sub>1</sub></var> is a member type.</p></li><li><p>There is a <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>
                        <var>M</var> such that 
                     <var>S<sub>1</sub></var>
                        <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                        <var>M</var>
                     and <var>M</var>
                        <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                        <var>S<sub>2</sub></var>.</p></li></ul><p>]</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The XML Schema specification does not completely specify the circumstances
               under which <var>S<sub>1</sub></var> and <var>S<sub>2</sub></var> are considered to be the same
               type. For example, if both are anonymous union types with the same member types,
               but defined in different places in the schema, then schema processors have
               discretion whether to treat them as the same type.</p></div></div></div><div class="div2">
<h3><a id="id-matching-item"/>3.2 <a href="#id-matching-item" style="text-decoration: none">Item Types</a></h3><p>
               [<a id="dt-item-type" title="item type">Definition</a>: An <b>item type</b> is a type that can be expressed using the <a href="#doc-xpath40-ItemType">ItemType</a> syntax, which forms part of the <a href="#doc-xpath40-SequenceType">SequenceType</a>
            syntax. Item types match individual <a title="item" class="termref" href="#dt-item">items</a>.]
            </p><div class="note"><p class="prefix"><b>Note:</b></p><p>While this definition is adequate for the purpose of defining the syntax
            of XPath 4.0, it ignores the fact that there are also item types that cannot be
            expressed using XPath 4.0 syntax: specifically, item types that reference
            an anonymous simple type or complex type defined in a schema. Such types 
            can appear as type annotations on nodes following schema validation.</p></div><p>In most cases, the set of items matched by an item type consists either
         exclusively of <a title="atomic item" class="termref" href="#dt-atomic-item">atomic items</a>,
         exclusively of <a title="node" class="termref" href="#dt-node">nodes</a>, 
         or exclusively of <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-function-item">function items</a><sup><small>DM</small></sup>.
         Exceptions include the generic types <code>item()</code>, which matches all items, <code>xs:error</code>,
         which matches no items, and <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item types</a>,
         which can match any combination of types.
      </p><p>
               [<a id="dt-item-type-designator" title="item type designator">Definition</a>: An
         <b>item type designator</b> is a syntactic construct conforming to the grammar rule
         <a href="#doc-xpath40-ItemType">ItemType</a>. An item type designator is said
         to <b>designate</b> an <a title="item type" class="termref" href="#dt-item-type">item type</a>.]
            </p><div class="note"><p class="prefix"><b>Note:</b></p><p>Two <a title="item type designator" class="termref" href="#dt-item-type-designator">item type designators</a> may designate the
      same item type. For example, <code>element()</code> and <code>element(*)</code> are equivalent,
      as are <code>attribute(A)</code> and <code>attribute(A, xs:anySimpleType)</code>.</p></div><p>
               <a title="lexical QName" class="termref" href="#dt-qname">Lexical QNames</a> appearing in an <a title="item type designator" class="termref" href="#dt-item-type-designator">item type designator</a>  are expanded using the 
                  <a title="default type namespace rule" class="termref" href="#dt-default-type-namespace-rule">default type namespace rule</a>.
      Equality of QNames is defined by the <code>eq</code> operator.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType"/><code><a href="#prod-xpath40-ItemType">ItemType</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ItemType-AnyItemTest">AnyItemTest</a>  |  <a href="#doc-xpath40-ItemType-TypeName">TypeName</a>  |  <a href="#doc-xpath40-ItemType-KindTest">KindTest</a>  |  <a href="#doc-xpath40-ItemType-FunctionType">FunctionType</a>  |  <a href="#doc-xpath40-ItemType-MapType">MapType</a>  |  <a href="#doc-xpath40-ItemType-ArrayType">ArrayType</a>  |  <a href="#doc-xpath40-ItemType-RecordType">RecordType</a>  |  <a href="#doc-xpath40-ItemType-EnumerationType">EnumerationType</a>  |  <a href="#doc-xpath40-ItemType-ChoiceItemType">ChoiceItemType</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-AnyItemTest"/><code><a href="#prod-xpath40-AnyItemTest">AnyItemTest</a></code></td><td>::=</td><td><code>"item"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-TypeName"/><code><a href="#prod-xpath40-TypeName">TypeName</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ItemType-EQName">EQName</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-KindTest"/><code><a href="#prod-xpath40-KindTest">KindTest</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ItemType-DocumentTest">DocumentTest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-ElementTest">ElementTest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-AttributeTest">AttributeTest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-SchemaElementTest">SchemaElementTest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-SchemaAttributeTest">SchemaAttributeTest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-PITest">PITest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-CommentTest">CommentTest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-TextTest">TextTest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-NamespaceNodeTest">NamespaceNodeTest</a>
                     <br/>|  <a href="#doc-xpath40-ItemType-AnyKindTest">AnyKindTest</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-DocumentTest"/><code><a href="#prod-xpath40-DocumentTest">DocumentTest</a></code></td><td>::=</td><td><code>"document-node"  "("  (<a href="#doc-xpath40-ItemType-ElementTest">ElementTest</a>  |  <a href="#doc-xpath40-ItemType-SchemaElementTest">SchemaElementTest</a>  |  <a href="#doc-xpath40-ItemType-NameTestUnion">NameTestUnion</a>)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-ElementTest"/><code><a href="#prod-xpath40-ElementTest">ElementTest</a></code></td><td>::=</td><td><code>"element"  "("  (<a href="#doc-xpath40-ItemType-NameTestUnion">NameTestUnion</a>  (","  <a href="#doc-xpath40-ItemType-TypeName">TypeName</a>  "?"?)?)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-SchemaElementTest"/><code><a href="#prod-xpath40-SchemaElementTest">SchemaElementTest</a></code></td><td>::=</td><td><code>"schema-element"  "("  <a href="#prod-xpath40-ElementName">ElementName</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-NameTestUnion"/><code><a href="#prod-xpath40-NameTestUnion">NameTestUnion</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-ItemType-NameTest">NameTest</a> ++ "|")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-NameTest"/><code><a href="#prod-xpath40-NameTest">NameTest</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ItemType-EQName">EQName</a>  |  <a href="#doc-xpath40-ItemType-Wildcard">Wildcard</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-Wildcard"/><code><a href="#prod-xpath40-Wildcard">Wildcard</a></code></td><td>::=</td><td><code>"*"<br/>|  (<a href="#prod-xpath40-NCName">NCName</a>  ":*")<br/>|  ("*:"  <a href="#prod-xpath40-NCName">NCName</a>)<br/>|  (<a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>  "*")</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#ws-explicit">ws: explicit</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-AttributeTest"/><code><a href="#prod-xpath40-AttributeTest">AttributeTest</a></code></td><td>::=</td><td><code>"attribute"  "("  (<a href="#doc-xpath40-ItemType-NameTestUnion">NameTestUnion</a>  (","  <a href="#doc-xpath40-ItemType-TypeName">TypeName</a>)?)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-SchemaAttributeTest"/><code><a href="#prod-xpath40-SchemaAttributeTest">SchemaAttributeTest</a></code></td><td>::=</td><td><code>"schema-attribute"  "("  <a href="#prod-xpath40-AttributeName">AttributeName</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-PITest"/><code><a href="#prod-xpath40-PITest">PITest</a></code></td><td>::=</td><td><code>"processing-instruction"  "("  (<a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#doc-xpath40-ItemType-StringLiteral">StringLiteral</a>)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                  </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#ws-explicit">ws: explicit</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-CommentTest"/><code><a href="#prod-xpath40-CommentTest">CommentTest</a></code></td><td>::=</td><td><code>"comment"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-TextTest"/><code><a href="#prod-xpath40-TextTest">TextTest</a></code></td><td>::=</td><td><code>"text"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-NamespaceNodeTest"/><code><a href="#prod-xpath40-NamespaceNodeTest">NamespaceNodeTest</a></code></td><td>::=</td><td><code>"namespace-node"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-AnyKindTest"/><code><a href="#prod-xpath40-AnyKindTest">AnyKindTest</a></code></td><td>::=</td><td><code>"node"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-FunctionType"/><code><a href="#prod-xpath40-FunctionType">FunctionType</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-AnyFunctionType">AnyFunctionType</a>
                     <br/>|  <a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-MapType"/><code><a href="#prod-xpath40-MapType">MapType</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-AnyMapType">AnyMapType</a>  |  <a href="#prod-xpath40-TypedMapType">TypedMapType</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-ArrayType"/><code><a href="#prod-xpath40-ArrayType">ArrayType</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-AnyArrayType">AnyArrayType</a>  |  <a href="#prod-xpath40-TypedArrayType">TypedArrayType</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-RecordType"/><code><a href="#prod-xpath40-RecordType">RecordType</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-AnyRecordType">AnyRecordType</a>  |  <a href="#prod-xpath40-TypedRecordType">TypedRecordType</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-EnumerationType"/><code><a href="#prod-xpath40-EnumerationType">EnumerationType</a></code></td><td>::=</td><td><code>"enum"  "("  (<a href="#doc-xpath40-ItemType-StringLiteral">StringLiteral</a> ++ ",")  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ItemType-ChoiceItemType"/><code><a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a></code></td><td>::=</td><td><code>"("  (<a href="#doc-xpath40-ItemType">ItemType</a> ++ "|")  ")"</code></td></tr></tbody></table><p>This section defines the syntax and semantics of different <code>ItemTypes</code>
               in terms of the values that they match.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>For an explanation of the EBNF grammar notation (and in particular, the operators
               <code>++</code> and <code>**</code>), see <a href="#id-grammar"><b>A.1 EBNF</b></a>.</p></div><p>An <a title="item type designator" class="termref" href="#dt-item-type-designator">item type designator</a> written simply 
               as an <code>EQName</code>
               (that is, a <code>TypeName</code>) is interpreted as follows:</p><ol class="enumar"><li><p>If the name is written as a lexical QName, then it is expanded using the
               <a title="default type namespace rule" class="termref" href="#dt-default-type-namespace-rule">default type namespace rule</a>.</p></li><li><p>If the expanded name matches a <a title="named item type" class="termref" href="#dt-named-item-type">named item type</a> in the <a title="static context" class="termref" href="#dt-static-context">static context</a>,
                  then it is taken as a reference to the corresponding item type. The rules that
               apply are the rules for the expanded item type definition.</p></li><li><p>Otherwise, it must match the name of a type in the <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope schema types</a>
                  in the <a title="static context" class="termref" href="#dt-static-context">static context</a>: specifically, an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>
                  or a <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a>.
                  See <a href="#id-predefined-types"><b>3.5 Schema Types</b></a> for details.
                  </p><div class="note"><p class="prefix"><b>Note:</b></p><p>A name in the <code>xs</code> namespace will always fall into this category, since the namespace
               is reserved. See <a href="#id-namespaces-and-qnames"><b>2.1.3 Namespaces and QNames</b></a>.</p></div></li><li><p>If the name cannot be resolved to a type, a <a title="static error" class="termref" href="#dt-static-error">static error</a> 
                  is raised [<a href="#ERRXPST0051" title="err:XPST0051">err:XPST0051</a>].</p></li></ol><div class="div3">
<h4><a id="general-item-types"/>3.2.1 <a href="#general-item-types" style="text-decoration: none">General item types</a></h4><ul><li><p>
                        <code>item()</code> matches
                        any single <a title="item" class="termref" href="#dt-item">item</a>.</p><p>For example, <code>item()</code> matches the atomic
                        item <code>1</code>, the element <code>&lt;a/&gt;</code>, or the function <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-concat"><code>fn:concat#3</code></a>.</p></li><li><p>A <a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a> lists a number of alternative item types in parentheses,
                        separated by <code>"|"</code>. An item matches a <code>ChoiceItemType</code>
                        it if matches any of the alternatives.</p><p>For example, <code>(map(*) | array(*))</code> matches any item that
                     is a map or an array.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>If there is only one alternative, the <code>ChoiceItemType</code>
                         designates the same <a title="item type" class="termref" href="#dt-item-type">item type</a>
                         as the <a href="#doc-xpath40-ItemType">ItemType</a> that is in parentheses.
                         A singleton choice (that is, a parenthesized item type) is used primarily 
                         when defining nested item types in a function
                      signature. For example, a sequence of functions that each return a single boolean might be denoted
                      <code>(fn() as xs:boolean)*</code>. In this example the parentheses
                      are needed to indicate where the occurrence indicator belongs.</p></div></li></ul></div><div class="div3">
<h4><a id="id-atomic-types"/>3.2.2 <a href="#id-atomic-types" style="text-decoration: none">Atomic Types</a></h4><p>Atomic types in the XPath 4.0 type system correspond directly to atomic types
                  as defined in the <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a>
                 type system.</p><p>Atomic types are either built-in atomic types such as <code>xs:integer</code>,
               or user-defined atomic types imported from a schema. Atomic types are identified
               by a QName: see <a href="#id-namespaces-and-qnames"><b>2.1.3 Namespaces and QNames</b></a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>A schema may also include anonymous atomic types. Such types are
               not usable directly in XPath 4.0, though they may appear as the values
               of <a title="type annotation" class="termref" href="#dt-type-annotation">type annotations</a> on nodes.</p></div><p>
                  [<a id="dt-generalized-atomic-type" title="generalized atomic type">Definition</a>: A <b>generalized atomic type</b> is an 
                        <a title="item type" class="termref" href="#dt-item-type">item type</a> whose instances are all
                        atomic items. Generalized atomic types include (a) 
                        <a title="atomic type" class="termref" href="#dt-atomic-type">atomic types</a>, either built-in
                        (for example <code>xs:integer</code>) or imported from a schema, 
                        (b) <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union types</a>, either built-in
                        (<code>xs:numeric</code> and <code>xs:error</code>) or imported from a schema,
                        (c) <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item types</a> if their alternatives
                        are all generalized atomic types, and 
                        (d) <a title="enumeration type" class="termref" href="#dt-enumeration-type">enumeration types</a>.
                     ].</p><p>A <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a> may be designated by 
                     an <a href="#doc-xpath40-ItemType">ItemType</a> in any of the following ways:</p><ul><li><p>Using the QName of a type in the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a> that is an 
                        <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>
                     or a <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a>.</p></li><li><p>Using a QName that identifies a <a title="named item type" class="termref" href="#dt-named-item-type">named item type</a> that resolves
                        to a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>.</p></li><li><p>Using a <a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a> where every alternative
                        is itself a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>.</p></li><li><p>Using an <a href="#doc-xpath40-EnumerationType">EnumerationType</a> as described below.</p></li></ul><p>An atomic item <var>A</var> matches the 
                     <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>
                  <var>GAT</var> 
                     if the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of <var>A</var>
                  <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                  <var>GAT</var>.</p><p>Example: The <a href="#doc-xpath40-ItemType">ItemType</a>
                  <code>xs:decimal</code> matches any value of type
    <code>xs:decimal</code>.  It also matches any value of type
                        <code>shoesize</code>, if <code>shoesize</code> is an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>
    derived by restriction from <code>xs:decimal</code>.</p><p>Example: Suppose <a href="#doc-xpath40-ItemType">ItemType</a>
                  <code>dress-size</code> is a union type that allows
    either <code>xs:decimal</code> values for numeric sizes (for example: 4, 6, 10, 12),
    or one of an enumerated set of <code>xs:strings</code>
    (for example: <code>small</code>, <code>medium</code>, <code>large</code>). The <a href="#doc-xpath40-ItemType">ItemType</a>
                  <code>dress-size</code> matches any of these values.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The names of <span>list</span>
    types such as <code>xs:IDREFS</code> are not accepted in this context,
    but can often be replaced by a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a> with an occurrence indicator, such as
    <code>xs:IDREF+</code>.</p></div></div><div class="div3">
<h4><a id="id-union-types"/>3.2.3 <a href="#id-union-types" style="text-decoration: none">Union Types</a></h4><p>Union types, as defined in XSD, are a variety of simple types. The membership of
                  a union type in XSD may include list types as well as atomic types and other union types.</p><p>
                  [<a id="dt-pure-union-type" title="pure union type">Definition</a>: A <b>pure union type</b> is a <span>
                        <b>simple type</b>
                     </span> 
                     that satisfies the following constraints:
                     (a) <a href="https://www.w3.org/TR/xmlschema11-1/#std-variety">{variety}<sup><small>XS11-1</small></sup></a> is <code>union</code>, 
                     (b) the <a href="https://www.w3.org/TR/xmlschema11-1/#std-facets">{facets}<sup><small>XS11-1</small></sup></a> property is empty, 
                     (c) no type in the transitive membership of the union type has 
                     <a href="https://www.w3.org/TR/xmlschema11-1/#std-variety">{variety}<sup><small>XS11-1</small></sup></a>
                     <code>list</code>, and 
                     (d) no type in the transitive membership of the union type is a type with 
                     <a href="https://www.w3.org/TR/xmlschema11-1/#std-variety">{variety}<sup><small>XS11-1</small></sup></a>
                     <code>union</code> having a non-empty <a href="https://www.w3.org/TR/xmlschema11-1/#std-facets">{facets}<sup><small>XS11-1</small></sup></a> property].</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The definition of <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a>
                     excludes union types derived by non-trivial restriction from other
                     union types, as well as union types that include list types in their
                     membership. Pure union types have the property that every
                     instance of an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a> defined as one of the member types of the
                     union is also a valid instance of the union type.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The current (second) edition of XML Schema 1.0 contains an
                     error in respect of the substitutability of a union type by one of its
                     members: it fails to recognize that this is unsafe if the union is
                     derived by restriction from another union.</p><p>This problem is fixed in XSD 1.1, but the effect of the resolution
                     is that an atomic item labeled with an atomic type cannot be treated
                     as being substitutable for a union type without explicit validation.
                     This specification therefore allows union types to be used as item
                     types only if they are defined directly as the union of a number of
                     atomic types.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Local union types (see <a href="#id-choice-item-types"><b>3.2.5 Choice Item Types</b></a>) and 
                  <a title="enumeration type" class="termref" href="#dt-enumeration-type">enumeration types</a> cannot be
                  used as the target for schema validation.</p></div><p>
                  [<a id="dt-numeric" title="numeric">Definition</a>: The type <code>xs:numeric</code>
               is defined as a union type with member types <code>xs:double</code>, 
               <code>xs:float</code>, and <code>xs:decimal</code>. An item that
               is an instance of any of these types is referred to as a <b>numeric value</b>,
               and a type that is a subtype of <code>xs:numeric</code> is referred to
               as a <b>numeric type</b>.]
               </p></div><div class="div3">
<h4><a id="id-namespace-sensitive"/>3.2.4 <a href="#id-namespace-sensitive" style="text-decoration: none">Namespace-sensitive Types</a></h4><p>
                  [<a id="dt-namespace-sensitive" title="namespace-sensitive">Definition</a>: The <b>namespace-sensitive</b>
                     types are <code>xs:QName</code>, <code>xs:NOTATION</code>, types
                     derived by restriction from <code>xs:QName</code> or
                     <code>xs:NOTATION</code>, list types that have a namespace-sensitive
                     item type, and union types with a namespace-sensitive type in their
                     transitive membership.]
               </p><p>It is not possible to preserve the type of a <a title="namespace-sensitive" class="termref" href="#dt-namespace-sensitive">namespace-sensitive</a> value without also preserving the <a title="namespace binding" class="termref" href="#dt-namespace-binding">namespace binding</a> 
                  that defines the meaning of each namespace prefix used in the value. Therefore, XPath 4.0 defines 
                  some error conditions that occur only with <a title="namespace-sensitive" class="termref" href="#dt-namespace-sensitive">namespace-sensitive</a> values. For instance, casting to a <a title="namespace-sensitive" class="termref" href="#dt-namespace-sensitive">namespace-sensitive</a> type raises 
                  a <a title="type error" class="termref" href="#dt-type-error">type error</a>
                  [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFONS0004" title="err:FONS0004">err:FONS0004</a>]<sup><small>FO40</small></sup> if the namespace bindings for the result cannot be determined. </p></div><div class="div3">
<h4><a id="id-choice-item-types"/>3.2.5 <a href="#id-choice-item-types" style="text-decoration: none">Choice Item Types</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-enumeration-types">⬇</a> <a href="#id-implausible-expressions">⬆</a></p><ol><li><p>
                     Choice item types (an item type allowing a set of alternative item types)
                     are introduced.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/122">122</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1132">1132</a> 9 April 2024]</i></p></li></ol></div><p>
                  [<a id="dt-choice-item-type" title="choice item type">Definition</a>: A 
                  <b>choice item type</b> defines an item type that is the union
               of a number of alternatives. For example the type 
               <code>(xs:hexBinary | xs:base64Binary)</code> defines the union of 
                  these two primitive <a title="atomic type" class="termref" href="#dt-atomic-type">atomic types</a>, 
                  while the type <code>(map(*) | array(*))</code>
               matches any item that is either a map or an array.]
               </p><p>An item matches a <code>ChoiceItemType</code> if it matches any of the 
                  alternatives listed within the parentheses.</p><p>For example, the type <code>(xs:NCName | enum(""))</code> matches any value that is either
                  an instance of <code>xs:NCName</code>, or a zero-length string. This might be a suitable type for
                  a variable that holds a namespace prefix.</p><p>If all the alternatives are <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic types</a>
                  then the <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item type</a> is itself a generalized atomic type,
               which means, for example, that it can be used as the target of a cast expression.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>A <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item type</a> in which all the alternatives are atomic
                  behaves in most respects like a schema-defined <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a>.
                     However, because it can be defined at the point of use (for example,
                     within a function signature), it may be more convenient than defining the type in an
                     imported schema.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Choice item types are particularly useful in function signatures, 
                     allowing a function to take arguments
                  of a variety of types. If the choice item type is a local union type, 
                  then the semantics are identical to using a named union type, but a local union type is more
                  convenient because it does not need to be defined in a schema, and does not require a schema-aware processor.</p><p>A local union type can also be used in a cast expression: <code>cast @when as (xs:date | xs:dateTime)</code>
                  allows the attribute <code>@when</code> to be either an <code>xs:date</code>, or an <code>xs:dateTime</code>.</p><p>An <code>instance of</code> expression can be used to test whether a value belongs to one
                  of a number of specified types: <code>$x instance of (xs:string | xs:anyURI | xs:untypedAtomic)</code>
                  returns <code>true</code> if <code>$x</code> is an instance of any of these three atomic types,
                  while <code>$x instance of (map(*) | array(*))</code> tests whether <code>$x</code> is
                  a map or array.</p></div></div><div class="div3">
<h4><a id="id-enumeration-types"/>3.2.6 <a href="#id-enumeration-types" style="text-decoration: none">Enumeration Types</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#node-types">⬇</a> <a href="#id-choice-item-types">⬆</a></p><ol><li><p>Enumeration types are added as a new kind of <code>ItemType</code>, constraining
                     the value space of strings.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/688">688</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/691">691</a> 10 October 2023]</i></p></li></ol></div><p>
                  [<a id="dt-enumeration-type" title="enumeration type">Definition</a>: An <b>EnumerationType</b>
                  accepts a fixed set of string values.]
               </p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-EnumerationType"/><code><a href="#prod-xpath40-EnumerationType">EnumerationType</a></code></td><td>::=</td><td><code>"enum"  "("  (<a href="#doc-xpath40-EnumerationType-StringLiteral">StringLiteral</a> ++ ",")  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-EnumerationType-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody></table><p>An <a title="enumeration type" class="termref" href="#dt-enumeration-type">enumeration type</a> has a value space consisting of a set of <code>xs:string</code>
                  values. When matching strings against an enumeration type, strings are always compared
               using the Unicode codepoint collation.</p><p>For example, if an argument of a function declares the required type 
                  as <code>enum("red", "green", "blue")</code>, then the string <code>"green"</code> is accepted,
                  while <code>"yellow"</code> is rejected with a type error.</p><p>Technically, enumeration types are defined as follows:</p><ul><li><p>
                        [<a id="dt-singleton-enumeration-type" title="singleton enumeration type">Definition</a>: An 
                     enumeration type with a single enumerated value (such as
                     <code>enum("red")</code>) is an anonymous <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>
                     derived from <code>xs:string</code> by restriction using an enumeration facet
                     that permits only the value <code>"red"</code>. This is referred to
                     as a <b>singleton enumeration type</b>.] It is equivalent to the XSD-defined type:</p><div class="exampleInner"><pre>
&lt;xs:simpleType&gt;
  &lt;xs:restriction base="xs:string"&gt;
    &lt;xs:enumeration value="red"/&gt;
  &lt;/xs:restriction&gt;
&lt;/xs:simpleType&gt;</pre></div></li><li><p>Two <a title="singleton enumeration type" class="termref" href="#dt-singleton-enumeration-type">singleton enumeration types</a> 
                     are the same type if and only
                  if they have the same (single) enumerated value, as determined using the Unicode
                  codepoint collation.</p></li><li><p>An enumeration type with multiple
                     enumerated values is a union of <a title="singleton enumeration type" class="termref" href="#dt-singleton-enumeration-type">singleton enumeration types</a>, 
                     so <code>enum("red", "green", "blue")</code>
                     is equivalent to <code>(enum("red") | enum("green") | enum("blue"))</code>.</p></li><li><p>In consequence, an enumeration type <var>T</var> is a subtype
                  of an enumeration type <var>U</var> if the enumerated values of <var>T</var>
                  are a subset of the enumerated values of <var>U</var>: 
                     see <a href="#id-itemtype-subtype"><b>3.3.2 Subtypes of Item Types</b></a>.</p></li></ul><p>An enumeration type is thus a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>.</p><p>It follows from these rules that an atomic item will only satisfy an <code>instance of</code>
               test if it has the correct type annotation, and this typically requires an explicit cast. 
               So the expression <code>"red" instance of enum("red", "green", "blue")</code>
               returns <code>false</code>, while <code>"red" cast as enum("red") instance of enum("red", "green", "blue")</code>
                  returns <code>true</code>.
                  However, the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a> ensure that where a variable
               or function declaration specifies an enumeration type as the required type, a string 
               (or indeed an <code>xs:untypedAtomic</code> or <code>xs:anyURI</code> value) equal
               to one of the enumerated values will be accepted.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Some consequences of these rules may not be immediately apparent.</p><p>Suppose that an XQuery query contains the declarations:</p><div class="exampleInner"><pre>
declare type my:color := enum("red", "green", "orange");
declare type my:fruit := enum("apple", "orange", "banana");
declare variable $orange-color as my:color := "orange";
declare variable $orange-fruit as my:fruit := "orange";</pre></div><p>The same applies with the equivalent XSLT syntax:</p><div class="exampleInner"><pre>
&lt;xsl:item-type name="my:color" as="enum('red', 'green', 'orange')"/&gt;
&lt;xsl:item-type name="my:fruit" as="enum('apple', 'orange', 'banana')"/&gt;
&lt;xsl:variable name="orange-color" as="my:color" select="'orange'"/&gt;
&lt;xsl:variable name="orange-fruit" as="my:fruit" select="'orange'"/&gt;</pre></div><p>Now, the value of <code>$orange-color</code> is an atomic item whose datum
                  is the string <code>"orange"</code>, and whose type annotation is the anonymous
                  type designated <code>enum("orange")</code>. Similarly, the value of 
                     <code>$orange-fruit</code> is an atomic item whose datum
                  is the string <code>"orange"</code>, and whose type annotation is the anonymous
                  type designated <code>enum("orange")</code>. That is, the values of the two
                     variables are indistinguishable and interchangeable in every way. In particular,
                  both values are instances of <code>my:color</code>,
                  and both are instances of <code>my:fruit</code>.</p><p>This way of handling enumeration values has advantages and disadvantages.
                  On the positive side, it means that enumeration subsets and supersets work
                  cleanly: a value that is an instance of <code>enum("red", "green", "orange")</code>
                  can be used where an instance of <code>enum("red", "orange", "yellow", "green", "blue", "indigo",
                     "violet")</code> is expected. The downside is that labeling a string as an instance
                  of an enumeration type does not provide type safety: a function that expects an instance
                  of <code>my:color</code> can be called with any string that matches one of the required
                  colors, whether or not it has an appropriate type annotation. A function that expects a color
                  can be successfully called passing a fruit, if they happen to have the same name.</p></div></div><div class="div3">
<h4><a id="node-types"/>3.2.7 <a href="#node-types" style="text-decoration: none">Node Types</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-element-test">⬇</a> <a href="#id-enumeration-types">⬆</a></p><ol><li><p>
                     Element and attribute tests can include alternative names: <code>element(chapter|section)</code>,
                     <code>attribute(role|class)</code>.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/107">107</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/286">286</a> 17 January 2023]</i></p></li><li><p>
                     The <code>NodeTest</code> in an <code>AxisStep</code> now allows alternatives: 
                     <code>ancestor::(section|appendix)</code>
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/107">107</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/286">286</a> 17 January 2023]</i></p></li><li><p>
                     The syntax <code>document-node(<var>N</var>)</code>, where <var>N</var> is a <code>NameTestUnion</code>,
                     is introduced as an abbreviation for <code>document-node(element(<var>N</var>))</code>. For example,
                     <code>document-node(*)</code> matches any well-formed XML document (as distinct from a document fragment).
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1593">1593</a>  24 November 2024]</i></p></li></ol></div><p>Node types are <a title="item type" class="termref" href="#dt-item-type">item types</a> whose 
                  instances are all <a title="node" class="termref" href="#dt-node">nodes</a>.</p><p>The syntax for node types is also used for <a title="node test" class="termref" href="#dt-node-test">node tests</a>
               within path expressions. This explains why the production rules have names such as
               <code>NodeTest</code> rather than <code>NodeType</code>.</p><p>Some of the constructs described in this section include a <a href="#prod-xpath40-TypeName">TypeName</a>. This appears 
                  as <var>T</var> in:</p><ul><li><p>
                        <code>element(N, T)</code>
                     </p></li><li><p>
                        <code>attribute(N, T)</code>
                     </p></li><li><p>
                        <code>document-node(element(N, T))</code>
                     </p></li></ul><p>The type name <var>T</var> is expanded using the <a title="default type namespace rule" class="termref" href="#dt-default-type-namespace-rule">default type namespace rule</a>. The resulting
               <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> must identify a type in the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a>. This can be any <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>: either a simple type,
               or (except in the case of attributes) a complex type. If it is a simple type then it can be an atomic, union, or
               list type. It can be a built-in type (such as <code>xs:integer</code>) or a user-defined type. It must however
               be the name of a type defined in a schema; it cannot be a <a title="named item type" class="termref" href="#dt-named-item-type">named item type</a>.</p><div class="div4">
<h5><a id="id-simple-node-tests"/>3.2.7.1 <a href="#id-simple-node-tests" style="text-decoration: none">Simple Node Types</a></h5><ul><li><p>
                           <code>node()</code>
    matches any node.</p></li><li><p>
                           <code>text()</code> matches any
    text node.</p></li><li><p>
                           <code>processing-instruction()</code>
    matches any processing-instruction
    node.</p></li><li><p>
                           <code>processing-instruction(</code>
                           <em>N</em>
                           <code>)</code>
    matches any processing-instruction node whose PITarget is equal to <code>fn:normalize-space(N)</code>. If the result of <code>fn:normalize-space(N)</code> is not in the lexical space of NCName, a type error is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>]
                        </p><p>Example:
    <code>processing-instruction(xml-stylesheet)</code> matches any
    processing instruction whose PITarget is
    <code>xml-stylesheet</code>.</p><p>For backward compatibility with
    XPath 1.0, the PITarget of a
    processing instruction may also be expressed as a
    string literal, as in this example:
    <code>processing-instruction("xml-stylesheet")</code>.</p><p>If the specified PITarget is not a syntactically valid NCName, a type error is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li><li><p>
                           <code>comment()</code> matches any comment node.</p></li><li><p>
                           <code>namespace-node()</code> matches any
    namespace node.</p></li><li><p>
                           <code>document-node()</code> matches any document
    node.</p></li><li><p>
                           <code>document-node(</code>
                           <em>E</em>
                           <code>)</code>, where <var>E</var> is an <a href="#doc-xpath40-ElementTest">ElementTest</a> or <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a> (see <a href="#id-element-test"><b>3.2.7.2 Element Types</b></a> and <a href="#id-schema-element-test"><b>3.2.7.2.1 Schema Element Types</b></a>)
    matches any document node whose children comprise (in any order) zero or more
    comment and processing instruction nodes, zero text nodes, and exactly one element node,
    which itself must match <var>E</var>.</p><p>Example:
    <code>document-node(element(book))</code> matches a document node
    containing
    exactly one element node that is matched by the ElementTest
    <code>element(book)</code>.</p></li><li><p>The construct <code>document-node(<var>NTU</var>)</code>, where
                  <var>NTU</var> is a <a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>, is
                  an abbreviation for <code>document-node(element(<var>NTU</var>))</code>.
                  For example, <code>document-node(*)</code> is an abbreviation for
                  <code>document-node(element(*))</code>, which matches any document node
                  corresponding to a well-formed XML document, that is, one that has
                  one element child, zero or more comment and processing-instruction children,
                  and no text node children.</p></li><li><p>An <a href="#doc-xpath40-ItemType">ItemType</a> that is an
    <a href="#doc-xpath40-ElementTest">ElementTest</a>, <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a>, <a href="#doc-xpath40-AttributeTest">AttributeTest</a>,
    <a href="#doc-xpath40-SchemaAttributeTest">SchemaAttributeTest</a>, or <a href="#doc-xpath40-FunctionType">FunctionType</a> matches an item as described in the following sections.
    </p></li></ul></div><div class="div4">
<h5><a id="id-element-test"/>3.2.7.2 <a href="#id-element-test" style="text-decoration: none">Element Types</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-attribute-test">⬇</a> <a href="#node-types">⬆</a></p><ol><li><p>
                     Element and attribute tests of the form <code>element(N)</code>
                     and <code>attribute(N)</code> now allow <code>N</code> to be any <code>NameTest</code>,
                     including a wildcard.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/107">107</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/286">286</a> 17 January 2023]</i></p></li><li><p>
                     Element and attribute tests of the form <code>element(A|B)</code>
                     and <code>attribute(A|B)</code> are now allowed.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/23">23</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/606">606</a> 17 January 2023]</i></p></li><li><p>
                     Setting the default namespace for elements and types to the special value
                     <code>##any</code> causes an unprefixed element name to act as a wildcard,
                     matching by local name regardless of namespace.
                  </p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ElementTest"/><code><a href="#prod-xpath40-ElementTest">ElementTest</a></code></td><td>::=</td><td><code>"element"  "("  (<a href="#doc-xpath40-ElementTest-NameTestUnion">NameTestUnion</a>  (","  <a href="#doc-xpath40-ElementTest-TypeName">TypeName</a>  "?"?)?)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ElementTest-NameTestUnion"/><code><a href="#prod-xpath40-NameTestUnion">NameTestUnion</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-ElementTest-NameTest">NameTest</a> ++ "|")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ElementTest-NameTest"/><code><a href="#prod-xpath40-NameTest">NameTest</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-ElementTest-EQName">EQName</a>  |  <a href="#doc-xpath40-ElementTest-Wildcard">Wildcard</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ElementTest-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ElementTest-Wildcard"/><code><a href="#prod-xpath40-Wildcard">Wildcard</a></code></td><td>::=</td><td><code>"*"<br/>|  (<a href="#prod-xpath40-NCName">NCName</a>  ":*")<br/>|  ("*:"  <a href="#prod-xpath40-NCName">NCName</a>)<br/>|  (<a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>  "*")</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ElementTest-TypeName"/><code><a href="#prod-xpath40-TypeName">TypeName</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-ElementTest-EQName">EQName</a>
                        </code></td></tr></tbody></table><p>
    An <a href="#doc-xpath40-ElementTest">ElementTest</a> is used to match an
    element node by its name and/or <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>.
  </p><p>An unprefixed <a href="#doc-xpath40-EQName">EQName</a>
                  within the <code>NameTestUnion</code> is expanded using the
                  <a title="element name matching rule" class="termref" href="#dt-element-name-matching-rule">element name matching rule</a>.
                  The name need not be present in the <a title="in-scope attribute declarations" class="termref" href="#dt-is-attrs">in-scope element declarations</a>.
               </p><div class="note"><p class="prefix"><b>Note:</b></p><p>This means that when the 
                  <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>
               has the special value <code>##any</code>, an unprefixed name
               <var>N</var> is interpreted as a wildcard <code>*:<var>N</var>
                        </code>.</p><p>It is always possible to match no-namespace names explicitly
               by using the form <code>Q{}<var>N</var>
                        </code>
                     </p></div><p>An unprefixed <a href="#prod-xpath40-TypeName">TypeName</a> is expanded using the
                  <a title="default type namespace rule" class="termref" href="#dt-default-type-namespace-rule">default type namespace rule</a>.
                  The <a href="#prod-xpath40-TypeName">TypeName</a> must be present in the <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope schema types</a>
                     [<a href="#ERRXPST0008" title="err:XPST0008">err:XPST0008</a>]
                  </p><div class="note"><p class="prefix"><b>Note:</b></p><p>If the <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>
                  has the special value <code>##any</code>, then an unprefixed type name
                  <var>T</var> is interpreted as <code>Q{http://www.w3.org/2001/XMLSchema}<var>T</var>
                        </code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>
                        <a title="substitution group" class="termref" href="#dt-substitution-group">Substitution groups</a> do not affect the semantics of <a href="#doc-xpath40-ElementTest">ElementTest</a>.</p></div><p>An <a href="#doc-xpath40-ElementTest">ElementTest</a>
                     <var>ET</var> matches an item <var>E</var> if the following conditions
                  are satisfied:</p><ol class="enumar"><li><p>
                           <var>E</var> is an element node.</p></li><li><p>If <var>ET</var> includes a <a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>,
                           then the name of the element node <var>E</var> matches one or more of
                           the <a href="#prod-xpath40-NameTest">NameTests</a> in the <a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>.
                           A name <var>N</var> matches a <a href="#prod-xpath40-NameTest">NameTest</a>
                           <var>NT</var> if one of the following
                           conditions is true:</p><ol class="enumla"><li><p>
                                 <var>NT</var> is <code>*</code>
                              </p></li><li><p>
                                 <var>NT</var> is <code>*:<em>local</em>
                                 </code> and the local part
                                 of <var>N</var> is <var>local</var>.</p></li><li><p>
                                 <var>NT</var> is <code>
                                    <em>prefix</em>:*</code> and the namespace URI
                                 of <var>N</var> matches the namespace URI bound to <var>prefix</var> in the static
                                 context.</p></li><li><p>
                                 <var>NT</var> is <code>
                                    <em>BracedURILiteral</em>*</code> and the namespace URI
                                 of <var>N</var> matches the namespace URI found in the <code>BracedURILiteral</code>.</p></li><li><p>
                                 <var>NT</var> is an <code>EQName</code> equal to <var>N</var>.</p></li></ol></li><li><p>If <var>ET</var> includes a <a href="#prod-xpath40-TypeName">TypeName</a>
                           <var>T</var>,
                           then the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of the element node <var>E</var>
                           <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                           <var>T</var>.</p></li><li><p>If <var>E</var> has the <code>nilled</code> property, then <var>ET</var>
                           either includes no <a href="#prod-xpath40-TypeName">TypeName</a>, 
                           or includes a <a href="#prod-xpath40-TypeName">TypeName</a> followed by the symbol <code>?</code>.</p></li></ol><p>Here are some examples of <a href="#doc-xpath40-ElementTest">ElementTests</a>:</p><ol class="enumar"><li><p>
                           <code>element()</code> and

<code>element(*)</code>  match any
single element node, regardless of its name or
type annotation.</p></li><li><p>
                           <code>element(person)</code> matches any element node whose name is <code>person</code>,
                        in the <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>.</p></li><li><p>
                           <code>element(doctor|nurse)</code> matches any element node whose name is 
                        <code>doctor</code> or <code>nurse</code>,
                        in the <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>.</p></li><li><p>
                           <code>element(xhtml:*)</code> matches any element node whose name is in the namespace
                        bound to the prefix <code>xhtml</code>.</p></li><li><p>
                           <code>element(xhtml:*|svg:*|mathml:*)</code> matches any element node whose name is one of the
                        three namespaces identified, specifically the namespaces bound to the prefixes
                        <code>xhtml</code>, <code>svg</code>, and <code>mathml</code>.</p></li><li><p>
                           <code>element(Q{http://www.w3.org/2000/svg}*)</code> matches any element node whose name is in the SVG namespace.</p></li><li><p>
                           <code>element(*:html)</code> matches any element node whose local name is <code>"html"</code>,
                     in any namespace.</p></li><li><p>
                           <code>element(person, surgeon)</code> matches a
non-nilled element node whose name is <code>person</code> and whose
type annotation is <code>surgeon</code> (or is derived from <code>surgeon</code>). </p></li><li><p>
                           <code>element(person, surgeon?)</code> matches a nilled or non-nilled element node whose name is <code>person</code> and whose type
annotation is <code>surgeon</code> (or is derived from <code>surgeon</code>).</p></li><li><p>
                           <code>element(*, surgeon)</code>
matches any non-nilled element node whose type annotation is
<code>surgeon</code> (or is derived from <code>surgeon</code>), regardless of its name.</p></li><li><p>
                           <code>element(*, surgeon?)</code>
matches any nilled or non-nilled element node whose type annotation is
<code>surgeon</code> (or is derived from <code>surgeon</code>), regardless of its name.</p></li></ol><p>
                  Where a <a href="#prod-xpath40-TypeName">TypeName</a> is included in an 
                  <a href="#doc-xpath40-ElementTest">ElementTest</a>
                     <var>T</var>, an element node will only
                  match the test if it has been validated against a schema that 
                  defines type <var>T</var>; furthermore, <var>T</var> must be
                  present in the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a> of the static context of the
                  <a href="#doc-xpath40-ElementTest">ElementTest</a>. Although it is guaranteed that
                  type <var>T</var> will have 
                  <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-schema-compatible">compatible</a><sup><small>DM</small></sup>
                  definitions in the schema that was used for validation and in the
                  <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a>, it is not guaranteed that revalidation
                  using the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a> would succeed. For example, if
                  substitution group membership varies between the two schemas, the element
                  node may contain children or descendants that the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a>
                  would not allow.
               </p><div class="note"><p class="prefix"><b>Note:</b></p><p>Technically, <code>element(p|q)</code> is not the same type as
               the choice item type <code>(element(p)|element(q))</code>. However, (a)
               they match exactly the same set of element nodes, and (b) each is a subtype
               of the other, so in practice they are indistinguishable.</p></div><div class="div5">
<h6><a id="id-schema-element-test"/>3.2.7.2.1 <a href="#id-schema-element-test" style="text-decoration: none">Schema Element Types</a></h6><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SchemaElementTest"/><code><a href="#prod-xpath40-SchemaElementTest">SchemaElementTest</a></code></td><td>::=</td><td><code>"schema-element"  "("  <a href="#doc-xpath40-SchemaElementTest-ElementName">ElementName</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SchemaElementTest-ElementName"/><code><a href="#prod-xpath40-ElementName">ElementName</a></code></td><td>::=</td><td><code>
                              <a href="#doc-xpath40-SchemaElementTest-EQName">EQName</a>
                           </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SchemaElementTest-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                              <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                           </code></td></tr></tbody></table><p>
    A <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a> matches an element node against a corresponding
    element declaration found in the <a title="in-scope element declarations" class="termref" href="#dt-is-elems">in-scope element declarations</a>.
  </p><p>
    The <a href="#prod-xpath40-ElementName">ElementName</a> of a <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a>
    has its prefixes expanded to a namespace URI by means of the
    <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a>, or if unprefixed, the
                  is interpreted according to the
                  <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>. If this has the special
                  value <code>"##any"</code>, an unprefixed name represents a name in no namespace.

    If the <a href="#prod-xpath40-ElementName">ElementName</a> specified in the <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a>
    is not found in the <a title="in-scope element declarations" class="termref" href="#dt-is-elems">in-scope element declarations</a>, a
    <a title="static error" class="termref" href="#dt-static-error">static error</a> is raised [<a href="#ERRXPST0008" title="err:XPST0008">err:XPST0008</a>].
  </p><p>
    A <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a> matches a candidate element node if all of the following conditions are satisfied:
  </p><ol class="enumar"><li><p>Either:</p><ol class="enumla"><li><p>The name <var>N</var> of the candidate node matches the specified <a href="#prod-xpath40-ElementName">ElementName</a>, or</p></li><li><p>The name <var>N</var> of the candidate node matches the name of an element declaration that is a member of the actual substitution group headed by the declaration of element <a href="#prod-xpath40-ElementName">ElementName</a>.</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>The term “actual substitution group” is defined in <a href="#XMLSchema11">[XML Schema 1.1]</a>. The actual substitution group of an element declaration 
                           <var>H</var> includes those element declarations 
                           <var>P</var> that are declared to have <var>H</var> as their 
                           direct or indirect substitution group head, provided that 
                           <var>P</var> is not declared as abstract, and that <var>P</var> 
                           is validly substitutable for <var>H</var>, which means that 
                           there must be no blocking constraints that prevent substitution.</p></div></li><li><p>The schema element declaration named <var>N</var> is not abstract.</p></li><li><p>
                              <var>AT</var>
                              <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                              <var>ET</var>,
                        where <var>AT</var> is the type annotation of the candidate node 
                        and <var>ET</var> is the schema type declared in the schema element 
                        declaration named <var>N</var>.</p></li><li><p>If the schema element declaration named <var>N</var> 
                        is not nillable, then the <code>nilled</code> property of the candidate node 
                        is <code>false</code>.</p></li></ol><p>Example: The <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a>
                        <code>schema-element(customer)</code> matches a candidate element node 
in the following two situations:
</p><ol class="enumar"><li><p>customer is a top-level element declaration in the in-scope element declarations; the name of the candidate node is customer; the element declaration of customer is not abstract; the type annotation of the candidate node is the same as or derived from the schema type declared in the customer element declaration; and either the candidate node is not nilled, or customer is declared to be nillable.</p></li><li><p>customer is a top-level element declaration in the in-scope element declarations; the name of the candidate node is client; client is an actual (non-abstract and non-blocked) member of the substitution group of customer; the type annotation of the candidate node is the same as or derived from the schema type declared for the client element; and either the candidate node is not nilled, or client is declared to be nillable.</p></li></ol><p>
                  In the case where the schema <var>X</var> used to validate an element node 
                  <var>E</var> (whose name is <var>N</var>)
                  differs from the schema <var>Y</var> represented by the
                  <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema definitions</a> in the static context of the 
                  <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a>, the following
                  considerations apply:</p><ul><li><p>In applying the test <code>
                                 <var>AT</var> derives-from <var>ET</var>
                              </code>,
                  note that <var>AT</var> will necessarily be present in <var>X</var>,
                  but not necessarily in <var>Y</var>. However, <var>ET</var> will 
                  necessarily be present in both; and because the two schemas
                  must be <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-schema-compatible">compatible</a><sup><small>DM</small></sup>,
                  <var>ET</var> will be the present in both schemas, will have the same
                  definition in both, and will be the declared type of <var>N</var> in both.
                  The test can therefore be applied from knowledge of type <var>AT</var>
                  as defined in schema <var>X</var>.</p></li><li><p>The test as to whether the element name <var>N</var> is a member
                  of the actual substitution group is performed entirely by reference
                  to schema <var>Y</var>. Although the two schemas are compatible,
                  substitution group membership can vary.</p></li></ul></div></div><div class="div4">
<h5><a id="id-attribute-test"/>3.2.7.3 <a href="#id-attribute-test" style="text-decoration: none">Attribute Types</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-function-test">⬇</a> <a href="#id-element-test">⬆</a></p><ol><li><p>
                     Element and attribute tests of the form <code>element(N)</code>
                     and <code>attribute(N)</code> now allow <code>N</code> to be any <code>NameTest</code>,
                     including a wildcard.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/107">107</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/286">286</a> 17 January 2023]</i></p></li><li><p>
                     Element and attribute tests of the form <code>element(A|B)</code>
                     and <code>attribute(A|B)</code> are now allowed.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/23">23</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/606">606</a> 17 January 2023]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AttributeTest"/><code><a href="#prod-xpath40-AttributeTest">AttributeTest</a></code></td><td>::=</td><td><code>"attribute"  "("  (<a href="#doc-xpath40-AttributeTest-NameTestUnion">NameTestUnion</a>  (","  <a href="#doc-xpath40-AttributeTest-TypeName">TypeName</a>)?)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AttributeTest-NameTestUnion"/><code><a href="#prod-xpath40-NameTestUnion">NameTestUnion</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-AttributeTest-NameTest">NameTest</a> ++ "|")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AttributeTest-NameTest"/><code><a href="#prod-xpath40-NameTest">NameTest</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-AttributeTest-EQName">EQName</a>  |  <a href="#doc-xpath40-AttributeTest-Wildcard">Wildcard</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AttributeTest-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AttributeTest-Wildcard"/><code><a href="#prod-xpath40-Wildcard">Wildcard</a></code></td><td>::=</td><td><code>"*"<br/>|  (<a href="#prod-xpath40-NCName">NCName</a>  ":*")<br/>|  ("*:"  <a href="#prod-xpath40-NCName">NCName</a>)<br/>|  (<a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>  "*")</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AttributeTest-TypeName"/><code><a href="#prod-xpath40-TypeName">TypeName</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-AttributeTest-EQName">EQName</a>
                        </code></td></tr></tbody></table><p>
    An <a href="#doc-xpath40-AttributeTest">AttributeTest</a> is used to match an
    attribute node by its name and/or <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>.
  </p><p>An unprefixed <a href="#doc-xpath40-EQName">EQName</a>
               within the <code>NameTestUnion</code> is expanded using the
                  <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>.
                  The name need not be present in the <a title="in-scope attribute declarations" class="termref" href="#dt-is-attrs">in-scope attribute declarations</a>.</p><p>An unprefixed <a href="#prod-xpath40-TypeName">TypeName</a> is expanded using the <a title="default type namespace rule" class="termref" href="#dt-default-type-namespace-rule">default type namespace rule</a>.
                  The <a href="#prod-xpath40-TypeName">TypeName</a> must be present in the <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope schema types</a>
                     [<a href="#ERRXPST0008" title="err:XPST0008">err:XPST0008</a>]
                  </p><p>An <a href="#doc-xpath40-AttributeTest">AttributeTest</a>
                     <var>AT</var> matches an item <var>A</var> if the following conditions
                     are satisfied:</p><ol class="enumar"><li><p>
                           <var>A</var> is an attribute node.</p></li><li><p>If <var>AT</var> includes a <a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>,
                        then the name of the attribute node <var>A</var> matches one or more of
                        the <a href="#prod-xpath40-NameTest">NameTests</a> in the <a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>.
                        A name <var>N</var> matches a <a href="#prod-xpath40-NameTest">NameTest</a>
                           <var>NT</var> if one of the following
                        conditions is true:</p><ol class="enumla"><li><p>
                                 <var>NT</var> is <code>*</code>
                              </p></li><li><p>
                                 <var>NT</var> is <code>*:<em>local</em>
                                 </code> and the local part
                           of <var>N</var> matches <var>local</var>.</p></li><li><p>
                                 <var>NT</var> is <code>
                                    <em>prefix</em>:*</code> and the namespace URI
                           of <var>N</var> matches the namespace URI bound to <var>prefix</var> in the static
                           context.</p></li><li><p>
                                 <var>NT</var> is <code>
                                    <em>BracedURILiteral</em>*</code> and the namespace URI
                              of <var>N</var> matches the namespace URI found in the <code>BracedURILiteral</code>.</p></li><li><p>
                                 <var>NT</var> is an <code>EQName</code> equal to <var>N</var>.</p></li></ol></li><li><p>If <var>AT</var> includes a <a href="#prod-xpath40-TypeName">TypeName</a>
                           <var>T</var>,
                        then the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of the attribute node <var>A</var>
                           <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                           <var>T</var>.</p></li></ol><p>Here are some examples of <a href="#doc-xpath40-AttributeTest">AttributeTests</a>:
  </p><ul><li><p>
                           <code>attribute()</code> and <code>attribute(*)</code> match any single attribute node,
regardless of its name or type annotation.</p></li><li><p>
                           <code>attribute(price)</code>
                        matches any attribute node whose name is <code>price</code>
                        (in no namespace), regardless of its type annotation.</p></li><li><p>
                           <code>attribute(price|discount)</code>
                        matches any attribute node whose name is <code>price</code> or <code>discount</code>
                        (in no namespace).</p></li><li><p>
                           <code>attribute(xlink:*)</code> matches any attribute node whose name is in the namespace
                        bound to the prefix <code>xlink</code>.</p></li><li><p>
                           <code>element(Q{http://www.w3.org/2000/svg}*)</code> matches any attribute node whose name is in the SVG namespace.</p></li><li><p>
                           <code>attribute(*:default-collation)</code> matches any attribute node
                        whose local name is <code>default-collation</code>,
                        regardless of namespace, and regardless of type annotation.</p></li><li><p>
                           <code>attribute(*:price|*:discount)</code> matches any attribute node
                        whose local name is <code>price</code> or <code>discount</code>,
                        regardless of namespace, and regardless of type annotation.</p></li><li><p>
                           <code>attribute(price, currency)</code> matches an
attribute node whose name is <code>price</code> (in no namespace) and whose type
annotation is
<code>currency</code> (or is derived from <code>currency</code>).</p></li><li><p>
                           <code>attribute(xlink:*, xs:string)</code> matches any attribute node whose name is in the namespace
                        bound to the prefix <code>xlink</code>, and whose type annotation is <code>xs:string</code>
                     or a type derived from <code>xs:string</code>.</p></li><li><p>
                           <code>attribute(*, currency)</code> matches any attribute node whose
type annotation is <code>currency</code> (or is derived from <code>currency</code>), regardless of its
name.</p></li></ul><p>
                  Unlike the situation with an <a href="#doc-xpath40-ElementTest">ElementTest</a>,
                  few problems arise if the attribute was validated using a different
                  schema. This is because simple types can never be derived by extension,
                  and attributes do not have substitution groups.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Technically, <code>attribute(p|q)</code> is not the same type as
               the choice item type <code>(attribute(p)|attribute(q))</code>. However, (a)
               they match exactly the same set of attribute nodes, and (b) each is a subtype
               of the other, so in practice they are indistinguishable.</p></div><div class="div5">
<h6><a id="id-schema-attribute-test"/>3.2.7.3.1 <a href="#id-schema-attribute-test" style="text-decoration: none">Schema Attribute Types</a></h6><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SchemaAttributeTest"/><code><a href="#prod-xpath40-SchemaAttributeTest">SchemaAttributeTest</a></code></td><td>::=</td><td><code>"schema-attribute"  "("  <a href="#doc-xpath40-SchemaAttributeTest-AttributeName">AttributeName</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SchemaAttributeTest-AttributeName"/><code><a href="#prod-xpath40-AttributeName">AttributeName</a></code></td><td>::=</td><td><code>
                              <a href="#doc-xpath40-SchemaAttributeTest-EQName">EQName</a>
                           </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SchemaAttributeTest-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                              <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                           </code></td></tr></tbody></table><p>
    A <a href="#doc-xpath40-SchemaAttributeTest">SchemaAttributeTest</a> matches an attribute node against a corresponding
    attribute declaration found in the <a title="in-scope attribute declarations" class="termref" href="#dt-is-attrs">in-scope attribute declarations</a>.
  </p><p>
    The <a href="#prod-xpath40-AttributeName">AttributeName</a> of a <a href="#doc-xpath40-SchemaAttributeTest">SchemaAttributeTest</a>
    has its prefixes expanded to a namespace URI by means of the
    <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a>. If unprefixed, an
    <a href="#prod-xpath40-AttributeName">AttributeName</a> is in no namespace.

    If the <a href="#prod-xpath40-AttributeName">AttributeName</a> specified in the <a href="#doc-xpath40-SchemaAttributeTest">SchemaAttributeTest</a>
    is not found in the <a title="in-scope attribute declarations" class="termref" href="#dt-is-attrs">in-scope attribute declarations</a>, a
    <a title="static error" class="termref" href="#dt-static-error">static error</a> is raised [<a href="#ERRXPST0008" title="err:XPST0008">err:XPST0008</a>].
  </p><p>
    A <a href="#doc-xpath40-SchemaAttributeTest">SchemaAttributeTest</a> matches a candidate attribute node if both of the
  following conditions are satisfied:
  </p><ol class="enumar"><li><p>The name of the candidate node matches the specified <a href="#prod-xpath40-AttributeName">AttributeName</a>.</p></li><li><p>
                              <var>AT</var>
                              <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                              <var>ET</var>,
                        where <var>AT</var> is the type annotation of the candidate node and 
                        <var>ET</var> is the schema type declared for attribute <a href="#prod-xpath40-AttributeName">AttributeName</a> in the <a title="in-scope attribute declarations" class="termref" href="#dt-is-attrs">in-scope attribute declarations</a>.</p></li></ol><p>Example: The <a href="#doc-xpath40-SchemaAttributeTest">SchemaAttributeTest</a>
                        <code>schema-attribute(color)</code> matches a candidate attribute node if <code>color</code> is a top-level attribute declaration in the <a title="in-scope attribute declarations" class="termref" href="#dt-is-attrs">in-scope attribute declarations</a>, the name of the candidate node is <code>color</code>, and the type annotation of the candidate node  is the same as or derived from the schema type declared for the <code>color</code> attribute.</p><p>
                  Unlike the situation with a <a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a>,
                  few problems arise if the attribute was validated using a different
                  schema. This is because attributes do not have substitution groups.</p></div></div></div><div class="div3">
<h4><a id="id-function-map-array-tests"/>3.2.8 <a href="#id-function-map-array-tests" style="text-decoration: none">Function, Map, and Array Types</a></h4><p>The following sections describe the syntax for <a title="item type" class="termref" href="#dt-item-type">item types</a>
            for functions, including arrays and maps.</p><p>The <a title="subtype" class="termref" href="#dt-subtype">subtype</a> relation among these types is described in the various subsections
            of <a href="#id-itemtype-subtype"><b>3.3.2 Subtypes of Item Types</b></a>.</p><div class="div4">
<h5><a id="id-function-test"/>3.2.8.1 <a href="#id-function-test" style="text-decoration: none">Function Types</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-record-test">⬇</a> <a href="#id-attribute-test">⬆</a></p><ol><li><p>The keyword <code>fn</code> is allowed as a synonym for <code>function</code>
                  in function types, to align with changes to inline function declarations.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1192">1192</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1197">1197</a> 21 May 2024]</i></p></li><li><p>The terms <b>FunctionType</b>, <b>ArrayType</b>,
                     <b>MapType</b>, and <b>RecordType</b> replace 
                     <b>FunctionTest</b>, <b>ArrayTest</b>,
                     <b>MapTest</b>, and <b>RecordTest</b>, with no
                  change in meaning.</p></li><li><p>
                     Parameter names may be included in a function signature; they are purely
                     documentary.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1136">1136</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1696">1696</a> 12 January 2025]</i></p></li></ol></div><p>A <a href="#doc-xpath40-FunctionType">FunctionType</a> matches selected <a title="function item" class="termref" href="#dt-function-item">function items</a>,
                  potentially checking their <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-signature">signature</a><sup><small>DM</small></sup>
                  (which includes the types of the arguments and results).</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionType"/><code><a href="#prod-xpath40-FunctionType">FunctionType</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-FunctionType-AnyFunctionType">AnyFunctionType</a>
                           <br/>|  <a href="#doc-xpath40-FunctionType-TypedFunctionType">TypedFunctionType</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionType-AnyFunctionType"/><code><a href="#prod-xpath40-AnyFunctionType">AnyFunctionType</a></code></td><td>::=</td><td><code>("function"  |  "fn")  "("  "*"  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionType-TypedFunctionType"/><code><a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a></code></td><td>::=</td><td><code>("function"  |  "fn")  "("  (<a href="#doc-xpath40-FunctionType-TypedFunctionParam">TypedFunctionParam</a> ** ",")  ")"  "as"  <a href="#doc-xpath40-FunctionType-SequenceType">SequenceType</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionType-TypedFunctionParam"/><code><a href="#prod-xpath40-TypedFunctionParam">TypedFunctionParam</a></code></td><td>::=</td><td><code>("$"  <a href="#doc-xpath40-FunctionType-EQName">EQName</a>  "as")?  <a href="#doc-xpath40-FunctionType-SequenceType">SequenceType</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionType-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionType-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                           <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody></table><p>The keywords <code>function</code> and <code>fn</code> are synonyms.</p><p>An <a href="#prod-xpath40-AnyFunctionType">AnyFunctionType</a>
    matches any <a title="function item" class="termref" href="#dt-function-item">function item</a>, including a map or an array. For example,
    the following expressions all return true:</p><ul><li><p>
                           <code>fn:name#1 instance of function(*)</code>
                        </p></li><li><p>
                           <code>fn { @id } instance of function(*)</code>
                        </p></li><li><p>
                           <code>fn:random-number-generator() instance of function(*)</code>
                        </p></li><li><p>
                           <code>[ 1, 2, 3 ] instance of fn(*)</code>
                        </p></li><li><p>
                           <code>{} instance of fn(*)</code>
                        </p></li></ul><p>A <a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a> matches
    a <a title="function item" class="termref" href="#dt-function-item">function item</a> if the function’s type signature (as defined in
    <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#function-items">Section 7.1 Function Items</a><sup><small>DM</small></sup>) is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of the <a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The keywords <code>function</code> and <code>fn</code> are synonymous.</p></div><p>If parameter names are included in a <a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a>,
               they are purely documentary and have no semantic effect. In particular, they
               play no part in deciding whether a particular function item matches the
               function type, and they never appear as keywords in function calls. 
               For example the construct
               <code>function($x as node()) as xs:string</code> designates exactly the same type
               as <code>function(node()) as xs:string</code>.</p><p>Any parameter names that are supplied must be distinct 
                  [<a href="#ERRXQST0039" title="err:XQST0039">err:XQST0039</a>].</p><p>A <a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a>
               may also match certain maps and arrays, as described in <a href="#id-map-test"><b>3.2.8.2 Map Types</b></a> and
               <a href="#id-array-test"><b>3.2.8.4 Array Types</b></a>
                  </p><p>
    Here are some examples of expressions that 
    use a <a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a>:
  </p><ul><li><p>
                           <code>fn:count#1 instance of function(item()*) as xs:integer</code> returns true,
                        because the signature of the function item <code>fn:count#1</code>
                        is <code>function(item()*) as xs:integer</code>.
                     </p></li><li><p>
                           <code>fn:count#1 instance of function(xs:string*) as item()</code> returns true,
                        because the signature of the function item <code>fn:count#1</code>
                        is a subtype of <code>function(xs:string*) as item()</code>.
                     </p><div class="note"><p class="prefix"><b>Note:</b></p><p>The same type might also be written 
                        <code>fn($x as xs:int, $y as xs:int) as xs:int</code>.</p></div></li><li><p>
                           <code>function(xs:anyAtomicType) as item()*</code> matches any map, 
                        or any other function item with the required signature.</p></li><li><p>
                           <code>function(xs:integer) as item()*</code> matches any array, 
                        or any other function item with the required signature.</p></li></ul></div><div class="div4">
<h5><a id="id-map-test"/>3.2.8.2 <a href="#id-map-test" style="text-decoration: none">Map Types</a></h5><p>A <a href="#doc-xpath40-MapType">MapType</a> designates an item type that
               either matches any map, or that matches maps whose keys and values
               are constrained to specific types.</p>
<a id="d3e8476"/><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapType"/><code><a href="#prod-xpath40-MapType">MapType</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-MapType-AnyMapType">AnyMapType</a>  |  <a href="#doc-xpath40-MapType-TypedMapType">TypedMapType</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapType-AnyMapType"/><code><a href="#prod-xpath40-AnyMapType">AnyMapType</a></code></td><td>::=</td><td><code>"map"  "("  "*"  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapType-TypedMapType"/><code><a href="#prod-xpath40-TypedMapType">TypedMapType</a></code></td><td>::=</td><td><code>"map"  "("  <a href="#doc-xpath40-MapType-ItemType">ItemType</a>  ","  <a href="#doc-xpath40-MapType-SequenceType">SequenceType</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapType-ItemType"/><code><a href="#prod-xpath40-ItemType">ItemType</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-AnyItemTest">AnyItemTest</a>  |  <a href="#prod-xpath40-TypeName">TypeName</a>  |  <a href="#prod-xpath40-KindTest">KindTest</a>  |  <a href="#doc-xpath40-FunctionType">FunctionType</a>  |  <a href="#doc-xpath40-MapType">MapType</a>  |  <a href="#doc-xpath40-ArrayType">ArrayType</a>  |  <a href="#doc-xpath40-RecordType">RecordType</a>  |  <a href="#doc-xpath40-EnumerationType">EnumerationType</a>  |  <a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapType-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-MapType-ItemType">ItemType</a>
                           <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody></table><p>An <a href="#prod-xpath40-AnyMapType"/>
                     <code>map(*)</code> matches any map.</p><p>The <a href="#doc-xpath40-MapType">MapType</a>
                     <code>map(K, V)</code> matches any map where every key
                  is an instance of <code>K</code> and every value is an
                  instance of <code>V</code>.</p><p>The <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry-order</a><sup><small>DM</small></sup>
               of a map has no effect on whether the map matches a particular
               map type.</p><p>Although the grammar for <code>TypedMapType</code>
               allows the key to be described using the full <code>ItemType</code> syntax, the item type used must be
               a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>
                     [<a href="#ERRXPST0152" title="err:XPST0152">err:XPST0152</a>].</p><p>For example, given a map <code>$M</code> whose keys are integers and whose
  results are strings, such as <code>{ 0: "no", 1: "yes" }</code>,
  the following following expressions deliver the result shown:
  </p><ul><li><p>
                           <code>$M instance of map(*)</code>  returns <code>true()</code>
                        </p></li><li><p>
                           <code>$M instance of map(xs:integer, xs:string)</code>  returns <code>true()</code>
                        </p></li><li><p>
                           <code>$M instance of map(xs:decimal, xs:anyAtomicType)</code>  returns <code>true()</code>
                        </p></li><li><p>
                           <code>$M instance of map(xs:int, xs:string)</code>  returns <code>false()</code>
                        </p></li><li><p>
                           <code>$M instance of map(xs:integer, xs:token))</code>  returns <code>false()</code>
                        </p></li></ul><p>A map is also a function item, and therefore matches certain
               function types. Specifically, a map that matches <code>map(K, V)</code> also matches a function
               type of the form <code>function(xs:anyAtomicType) as R</code> provided that both the following
                  conditions are satisfied:</p><ul><li><p>
                           <var>V</var> is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of <var>R</var>
                        </p></li><li><p>
                           <code>empty-sequence()</code> is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of <var>R</var>
                        </p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>To understand this rule, consider the use of a map <code>$M</code> in a function 
                  call <code>$M($K)</code>, which is equivalent to the function call <code>map:get($M, $K)</code>.

                  This function accepts any atomic item for the argument <code>$K</code>, and hence matches
                  a function type that requires an argument type of <code>xs:anyAtomicType</code>. If the

                  key <code>$K</code> is present in the map, the result of the function will be a value of
                  type <var>V</var>; if not, it will be an empty sequence. The map is therefore substitutable
                  for the function type provided that the function type allows both a value of type <var>V</var>
                  and the empty sequence as possible results.</p><p>The key type <var>K</var> does not enter into this rule. That is because in the function call
                     <code>$M($K)</code>, the sought key <code>$K</code> does not have to be of the same
                  type as the keys actually present in the map.</p><p>The transitivity rules for item type matching mean that if an item <var>M</var>
                     matches a type <var>T</var>, and <var>T</var> is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> 
                     of <var>U</var>, then <var>M</var> also matches type <var>U</var>. So the fact
                  that a map from integers to strings (<code>map(xs:integer, xs:string)</code>)
                  matches <code>function(xs:anyAtomicType) as xs:string?</code>
                  means that it will also match other function types such as 
                     <code>function(xs:integer) as xs:string?</code> and 
                     <code>function(xs:decimal) as xs:anyAtomicType?</code>
                     </p><p>Furthermore, the rules for
                     <a title="function coercion" class="termref" href="#dt-function-coercion">function coercion</a> mean that any map can be supplied as a value in a
                     context where it does not actually match the required function type, but
                     can be coerced to a function that does. For example a map of type 
                     <code>map(xs:integer, xs:string)</code> can be coerced to a function of
                     type <code>function(xs:integer) as xs:string</code>; in this situation a type
                     error will occur only if a call on the function actually returns an empty sequence. </p></div><p>Examples:</p><ul><li><p>
                           <code>$M instance of fn(*)</code>  returns <code>true()</code>
                        </p></li><li><p>
                           <code>$M instance of fn(xs:anyAtomicType) as item()*</code> returns <code>true()</code>
                        </p></li><li><p>
                           <code>$M instance of fn(xs:integer) as item()*</code>  returns <code>true()</code>
                        </p></li><li><p>
                           <code>$M instance of fn(xs:int) as item()*</code>  returns <code>true()</code>
                        </p></li><li><p>
                           <code>$M instance of fn(xs:string) as item()*</code>  returns <code>true()</code>
                        </p></li><li><p>
                           <code>not($M instance of fn(xs:integer) as xs:string)</code>  returns <code>true()</code>
                        </p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The last case might seem surprising; 
                     however, <a title="function coercion" class="termref" href="#dt-function-coercion">function coercion</a> ensures that <code>$M</code> can be used successfully 
  anywhere that the required type is <code>fn(xs:integer) as xs:string</code>.</p></div><p>Rules defining whether one map type is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of another
               are given in <a href="#id-item-subtype-maps"><b>3.3.2.6 Subtyping Maps</b></a>.</p></div><div class="div4">
<h5><a id="id-record-test"/>3.2.8.3 <a href="#id-record-test" style="text-decoration: none">Record Types</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-sequencetype-subtype">⬇</a> <a href="#id-function-test">⬆</a></p><ol><li><p>
                     Record types are added as a new kind of <code>ItemType</code>, constraining
                     the value space of maps.
                  </p></li><li><p>
                     The syntax <code>record(*)</code> is allowed; it matches any map.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/52">52</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/728">728</a> 10 October 2023]</i></p></li><li><p>
                     The syntax <code>record()</code> is allowed; the only thing it matches is an empty map.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1491">1491</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1577">1577</a> 17 October 2024]</i></p></li></ol></div><p>A <a href="#doc-xpath40-RecordType">RecordType</a> matches maps that meet specific criteria.</p><p>For example, the <code>RecordType</code>
                     <code>record(r as xs:double, i as xs:double)</code>
		             matches a map if the map has exactly two entries: an entry with key <code>"r"</code>
		                whose value is a <a title="singleton" class="termref" href="#dt-singleton">singleton</a>
                     <code>xs:double</code> value, and an entry with key <code>"i"</code>
		                whose value is also a <a title="singleton" class="termref" href="#dt-singleton">singleton</a>
                     <code>xs:double</code> value.</p><p>Record types describe a subset of the value space of maps. They do not define any new kinds of
		             values, or any additional operations. They are useful in many cases to describe more accurately the
		             type of a variable, function parameter, or function result, giving benefits both in the readability
		             of the code, and in the ability of the processor to detect and diagnose type errors and to optimize
		             execution.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RecordType"/><code><a href="#prod-xpath40-RecordType">RecordType</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-RecordType-AnyRecordType">AnyRecordType</a>  |  <a href="#doc-xpath40-RecordType-TypedRecordType">TypedRecordType</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RecordType-AnyRecordType"/><code><a href="#prod-xpath40-AnyRecordType">AnyRecordType</a></code></td><td>::=</td><td><code>"record"  "("  "*"  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RecordType-TypedRecordType"/><code><a href="#prod-xpath40-TypedRecordType">TypedRecordType</a></code></td><td>::=</td><td><code>"record"  "("  (<a href="#doc-xpath40-RecordType-FieldDeclaration">FieldDeclaration</a> ** ",")  <a href="#doc-xpath40-RecordType-ExtensibleFlag">ExtensibleFlag</a>?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RecordType-FieldDeclaration"/><code><a href="#prod-xpath40-FieldDeclaration">FieldDeclaration</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-RecordType-FieldName">FieldName</a>  "?"?  ("as"  <a href="#doc-xpath40-RecordType-SequenceType">SequenceType</a>)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RecordType-FieldName"/><code><a href="#prod-xpath40-FieldName">FieldName</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#doc-xpath40-RecordType-StringLiteral">StringLiteral</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RecordType-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RecordType-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                           <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RecordType-ExtensibleFlag"/><code><a href="#prod-xpath40-ExtensibleFlag">ExtensibleFlag</a></code></td><td>::=</td><td><code>","  "*"</code></td></tr></tbody></table><p>If the list of fields ends with <code>",*"</code> then the record type is said to be
		                <b>extensible</b>. For example, the <code>RecordType</code>
                     <code>record(e as element(Employee), *)</code>
		             matches a map if it has an entry with key <code>"e"</code> whose value matches <code>element(Employee)</code>,
		             regardless what other entries the map might contain.</p><p>For generality:</p><ul><li><p>The syntax <code>record()</code> defines a record type that has no explicit fields and that
                  is not extensible. The only thing it matches is an <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-empty-map">empty map</a><sup><small>DM</small></sup>.</p></li><li><p>The syntax <code>record(*)</code> defines an extensible record type that has no explicit
                  field declarations. It is equivalent to the item type
                  <code>map(*)</code>: that is, it matches any map.</p></li></ul><p>A record type can constrain only those entries whose keys are strings, but when the record
		             type is marked as extensible, then other entries may be present in the map with either string or non-string keys.
		             Entries whose key is a string can be expressed using an (unquoted) NCName if the key conforms to
		             NCName syntax, or using a (quoted) string literal otherwise.</p><p>Although constructors for named record types produce a map in which the
                  <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry order</a><sup><small>DM</small></sup> reflects
                  the order of field definitions in the record type definition,
                  the <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry order</a><sup><small>DM</small></sup>
                  of a map has no effect on whether the map matches a particular
                  record type: the entries in a map do not have to be in any particular order.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Lookup expressions have been extended in 4.0 so that non-NCName keys can be used without
		             parentheses: <code>employee?"middle name"</code>
                     </p></div><p>If the type declaration for a field is omitted, then <code>item()*</code> is assumed: that is,
		             the map entry may have any type.</p><p>If the field name is followed by a question mark,
		             then the value must have the specified type if it is present, but it may also be absent. For example,
		             the <code>RecordType</code>
                     <code>record(first as xs:string, middle? as xs:string, last as xs:string, *)</code>
		                requires the map to have string-valued entries with keys <code>"first"</code> and <code>"last"</code>;
		                it also declares that if the map has an entry with key <code>"middle"</code>, the value of that
		                entry must be a single <code>xs:string</code>. Declaring the type as 
		                <code>record(first as xs:string, middle? as xs:string?, last as xs:string, *)</code> also allows
		             the entry with key <code>"middle"</code> to be present but empty.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Within an extensible record type, a <code>FieldDeclaration</code> that is marked optional 
                     and has no declared type does not constrain the
                     map in any way, so it serves no practical purpose, but it is permitted because it may have
                     documentary value.</p></div><p>The names of the fields in a record type must be distinct [<a href="#ERRXPST0021" title="err:XPST0021">err:XPST0021</a>].</p><p>If a variable <code>$rec</code> is known to conform to a particular
		             record type, then when a lookup expression <code>$rec?field</code> is used, (a) the processor
		             can report a type error if <code>$rec</code> cannot contain an entry with name <code>field</code>
                   (see <a href="#id-implausible-lookup-expressions"><b>4.13.3.4 Implausible Lookup Expressions</b></a>),
		             and (b) the processor can make static type inferences about the type of value returned by 
		             <code>$rec?field</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>(TODO: change function signatures as suggested here!) A number of functions in the standard 
                     function library use maps as function arguments;
		                this is a useful technique where the information to be supplied across the interface is highly
		                variable. However, the type signature for such functions typically declares the argument type
		                as <code>map(*)</code>, which gives very little information (and places very few constraints)
		                on the values that are actually passed across. Using record types offers the possibility of
		                improving this: for example, the options argument of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-parse-json"><code>fn:parse-json</code></a>, previously
		                given as <code>map(*)</code>, can now be expressed as <code>record(liberal? as xs:boolean, 
		                   duplicates? as xs:string, escape? as xs:boolean, fallback as fn(xs:string) as xs:string, *)</code>.
		                In principle the <code>xs:string</code> type used to describe the <code>duplicates</code>
		                   option could also be replaced by a schema-defined subtype
		                of <code>xs:string</code> that enumerates the permitted values (<code>"reject"</code>,
		                   <code>"use-first"</code>, <code>"use-last"</code>). 
		                </p><p>The use of a record type in the signature of such a function causes the 
		                   <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>
		                to be invoked. So, for example, if the function expects an entry in the map to be an <code>xs:double</code>
		                value, it becomes possible to supply a map in which the corresponding entry has type <code>xs:integer</code>.</p><p>Greater precision in defining the types of such arguments also enables better type checking,
		                better diagnostics, better optimization, better documentation, and better syntax-directed
		                editing tools.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>One of the motivations for introducing record types is to enable better pattern matching
		             in XSLT when processing JSON input. With XML input, patterns are often based
		             around XML element names. JSON has no direct equivalent of XML’s element names; matching a JSON object
		             such as <code>{longitude: 130.2, latitude: 53.4}</code> relies instead on recognizing the property
		             names appearing in the object. XSLT 4.0, by integrating record types into pattern matching syntax,
		             allows such an object to be matched with a pattern of the form 
		                <code>match="record(longitude, latitude)"</code>
                     </p></div><p>Rules defining whether one record type is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of another
                  are given in <a href="#id-item-subtype-records"><b>3.3.2.8 Subtyping Records</b></a>.</p><div class="div5">
<h6><a id="id-recursive-record-tests"/>3.2.8.3.1 <a href="#id-recursive-record-tests" style="text-decoration: none">Recursive Record Types</a></h6><p>A named record type <var>N</var> is said to be recursive if its 
                  definition includes a direct or indirect reference to <var>N</var>. 
               </p><p>For example, the following XQuery declaration defines a linked list:</p><div class="exampleInner"><pre>declare record my:list (value as item()*, next? as my:list);</pre></div><p>The equivalent in XSLT is:</p><div class="exampleInner"><pre>&lt;xsl:record-type name="my:list"&gt;
   &lt;xsl:field name="value" as="item()*"/&gt;
   &lt;xsl:field name="next" as="my:list" required="no"/&gt;
&lt;/xsl:record-type&gt;</pre></div><p>Instances of recursive record types can be constructed and interrogated in the normal way.
               For example a list of length 3 can be constructed as:</p><div class="exampleInner"><pre>{ "value": 1, "next": { "value": 2, "next": { "value": 3 } } }</pre></div><p>and the third value in the map can be retrieved as <code>$list?next?next?value</code>.
               In practice, recursive data structures are usually manipulated using recursive functions.</p><p>It is possible to define a recursive record type that cannot be instantiated, because it
               has no finite instances: for example (in XQuery) <code>declare record X (next as X);</code>.
               Such a record declaration is <a title="implausible" class="termref" href="#dt-implausible">implausible</a>, so the processor may
               treat it as an error [<a href="#ERRXPST0023" title="err:XPST0023">err:XPST0023</a>], but it is not obliged to do so.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>For an example of a practical use of recursive record types, see the
               specification of the function <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-random-number-generator"><code>fn:random-number-generator</code></a>.</p></div><p>Recursive type definitions need to be handled specially by the subtyping rules; 
                  a naïve approach of simply replacing each reference to a named item type 
               with its definition would make the assessment of the subtype relationship non-terminating.
               For details see <a href="#id-itemtype-subtype"><b>3.3.2 Subtypes of Item Types</b></a>.</p><div class="example">
<div class="exampleHeader"><a id="e-binary-tree"/>Example: A Binary Tree</div><p>A record used to represent a node in a binary tree might be represented (using XQuery syntax) as:</p><div class="exampleInner"><pre>declare record t:binary-tree 
   ( left? as t:binary-tree, 
     value as item()*, 
     right? as t:binary-tree
   )</pre></div><p>A recursive function to walk this tree and enumerate all the values in depth-first order might be written 
                     (again using XQuery syntax) as:</p><div class="exampleInner"><pre>declare function t:values($tree as t:binary-tree?) as item()* {
  $tree ! (t:values(?left), ?value, t:values(?right))   
}</pre></div></div><p> </p><div class="example">
<div class="exampleHeader"><a id="e-arbitrary-tree"/>Example: An Arbitrary Tree</div><p>A record used to represent a node in a tree where each node has an arbitrary number
                     of children might be represented (using XQuery syntax) as:</p><div class="exampleInner"><pre>declare record t:tree as (value, children as t:tree*);</pre></div><p>A function to walk this tree and enumerate all the values in order might be written 
                     as:</p><div class="exampleInner"><pre>declare function t:flatten($tree as t:tree) as item()* {
  $tree?value, $tree?children ! t:flatten(.))   
}</pre></div></div><p> </p><div class="example">
<div class="exampleHeader"><a id="e-mutually-recursive-types"/>Example: Mutually Recursive Types</div><p>The usual textbook example of mutually-recursive types is that of a <em>forest</em>
                  consisting of a list of <em>trees</em>, where each <em>tree</em> is a record 
                  comprising a value and a <em>forest</em>.
                  As the previous example shows, this structure can be defined straightforwardly in XPath 4.0 without 
                  recourse to mutual recursion.</p><p>A more realistic example where mutual recursion is needed is for the schema component model
                     used in <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a>. Simplifying greatly,
                     the data representing an element declaration in XSD may contain references to a
                     complex type, which in turn will typically contain references to further 
                     element declarations. The structure therefore involves mutual recursion.
                  A simplified version of the schema component model might be written (in part) as:</p><div class="exampleInner"><pre>
declare record ElementDeclaration (
   name as xs:NCName,
   targetNamespace? as xs:anyURI,
   typeDefinition as (SimpleTypeDefinition | ComplexTypeDefinition),
   nillable as xs:boolean,
   abstract as xs:boolean
);
declare record SimpleTypeDefinition (
   name? as xs:NCName,
   targetNamespace? as xs:anyURI,
   baseType? as SimpleTypeDefinition,
   variety as enum("atomic", "list", "union"),
   facets as Facet*,
);
declare record ComplexTypeDefinition (
   name? as xs:NCName,
   targetNamespace? as xs:anyURI,
   baseType? as ComplexTypeDefinition,
   derivationMethod as enum("extension", "restriction"),
   contentType as record (
      variety as enum("empty", "simple", "element-only", "mixed"),
      particle? as Particle,
      simpleTypeDefinition? as SimpleTypeDefinition
   )
);
declare record Particle (
   minOccurs as xs:nonNegativeInteger,
   maxOccurs as (xs:positiveInteger | enum("unbounded")),
   term as (ElementDeclaration | Wildcard | Group)
);</pre></div></div></div></div><div class="div4">
<h5><a id="id-array-test"/>3.2.8.4 <a href="#id-array-test" style="text-decoration: none">Array Types</a></h5><p>An <a href="#doc-xpath40-ArrayType">ArrayType</a> designates an item type that
               either matches all arrays, or that matches arrays whose members
               are constrained to a specific type.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayType"/><code><a href="#prod-xpath40-ArrayType">ArrayType</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-ArrayType-AnyArrayType">AnyArrayType</a>  |  <a href="#doc-xpath40-ArrayType-TypedArrayType">TypedArrayType</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayType-AnyArrayType"/><code><a href="#prod-xpath40-AnyArrayType">AnyArrayType</a></code></td><td>::=</td><td><code>"array"  "("  "*"  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayType-TypedArrayType"/><code><a href="#prod-xpath40-TypedArrayType">TypedArrayType</a></code></td><td>::=</td><td><code>"array"  "("  <a href="#doc-xpath40-ArrayType-SequenceType">SequenceType</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayType-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                           <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody></table><p>An <a href="#prod-xpath40-AnyArrayType"/>
                     <code>array(*)</code> matches any array.</p><p>The <a href="#prod-xpath40-TypedArrayType">TypedArrayType</a>
                     <code>array(X)</code> matches any array
  in which every array member matches the <a href="#doc-xpath40-SequenceType">SequenceType</a>
                     <code>X</code>.</p><p>Examples:</p><ul><li><p>
                           <code>[ 1, 2 ] instance array(*)</code> returns <code>true()</code>
                        </p></li><li><p>
                           <code>[] instance of array(xs:string)</code> returns <code>true()</code>
                        </p></li><li><p>
                           <code>[ "foo" ] instance of array(xs:string)</code> returns <code>true()</code>
                        </p></li><li><p>
                           <code>[ "foo" ] instance of array(xs:integer)</code> returns <code>false()</code>
                        </p></li><li><p>
                           <code>[ (1, 2), (3, 4) ] instance of array(xs:integer)</code> returns <code>false()</code>
                        </p></li><li><p>
                           <code>[ (1, 2), (3, 4) ] instance of array(xs:integer+)</code> returns <code>true()</code>
                        </p></li><li><p>
                           <code>[ [ 1, 2 ], [ 3, 4 ] ] instance of array(array(xs:integer+))</code> returns <code>true()</code>
                        </p></li></ul><p>An array also matches certain other <a title="item type" class="termref" href="#dt-item-type">item types</a>, 
                  including:</p><ul><li><p>
                           <code>item()</code>
                        </p></li><li><p>
                           <code>function(*)</code>
                        </p></li><li><p>
                           <code>function(xs:integer) as item()*</code>
                        </p></li></ul><p>An array that matches <code>array(T)</code>
                  also matches the function type <code>function(xs:integer) as T</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>To understand this rule, consider the use of an array <code>$A</code> in a function 
                     call <code>$A($I)</code>, which is equivalent to the function call <code>array:get($A, $I)</code>.
                     This function accepts any integer for the argument <code>$I</code>, and the result
                     will either be an instance of <var>T</var>, or an error.</p><p>The transitivity rules for item type matching mean that if an item <var>A</var>
                     matches a type <var>T</var>, and <var>T</var> is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> 
                     of <var>U</var>, then <var>A</var> also matches type <var>U</var>. So the fact
                     that an array of strings (<code>array(xs:string)</code>)
                     matches <code>function(xs:integer) as xs:string</code>
                     means that it will also match other function types such as 
                     <code>function(xs:long) as item()*</code>.</p><p>Furthermore, the rules for
                     <a title="function coercion" class="termref" href="#dt-function-coercion">function coercion</a> mean that any array can be supplied as a value in a
                        context where it does not actually match the required function type, but
                        can be coerced to a function that does. For example an array of type 
                        <code>array(node())</code> can be coerced to a function of
                        type <code>function(xs:integer) as element()</code>; in this situation a type
                        error will occur only if a call on the function actually returns a node
                        that is not an element node.</p></div><p>Rules defining whether one array type is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of another
                  are given in <a href="#id-item-subtype-arrays"><b>3.3.2.7 Subtyping Arrays</b></a>.</p></div></div><div class="div3">
<h4><a id="id-xs-error"/>3.2.9 <a href="#id-xs-error" style="text-decoration: none">The type <code>xs:error</code>
               </a></h4><p>The item type <code>xs:error</code> has an empty value space; 
                  it never appears as a dynamic type or as the content type of a dynamic element or attribute type. 
                  
                  
                  It was defined in XML Schema in the interests of making the type system complete and closed, 
                  and it is also available in XPath 4.0 for similar reasons.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Even though it cannot occur in an instance, <code>xs:error</code> is a valid type name in a sequence type. The
                     practical uses of <code>xs:error</code> as a sequence type are limited, but they do exist. For instance, an error-handling function that always raises a dynamic error 
                     never returns a value, so <code>xs:error</code> is a good choice for the return type of the function.</p><p>The semantics of <code>xs:error</code> are well defined as a consequence of the fact that <code>xs:error</code> is defined as a union type with
                     no member types. For example:</p><ul><li><p>
                           <code>$x instance of xs:error</code> always returns <code>false</code>, regardless of the value of <code>$x</code>.</p></li><li><p>
                           <code>$x cast as xs:error</code> fails dynamically with error [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFORG0001" title="err:FORG0001">err:FORG0001</a>]<sup><small>FO40</small></sup>,  regardless of the value of <code>$x</code>.</p></li><li><p>
                           <code>$x cast as xs:error?</code> raises a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>
                           [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFORG0001" title="err:FORG0001">err:FORG0001</a>]<sup><small>FO40</small></sup> if <code>exists($x)</code> returns <code>true</code>, and evaluates to the empty sequence if <code>empty($x)</code> returns <code>true</code>.</p></li><li><p>
                           <code>xs:error($x)</code> has the same semantics as <code>$x cast as xs:error?</code> (see the previous bullet point)</p></li><li><p>
                           <code>$x castable as xs:error</code> evaluates to <code>false</code>, regardless of the value of <code>$x</code>.</p></li><li><p>
                           <code>$x treat as xs:error</code>  raises a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>
                           [<a href="#ERRXPDY0050" title="err:XPDY0050">err:XPDY0050</a>] if evaluated, regardless of the value of <code>$x</code>. It never fails statically.</p></li></ul><p>All of the above examples assume that <code>$x</code> is actually evaluated. The rules specified in <a href="#id-errors-and-opt"><b>2.4.4 Errors and
      Optimization</b></a> permit an implementation to avoid evaluating <code>$x</code> if the result of the query does not depend upon the value of <code>$x</code> and thus to avoid raising an error.</p></div></div></div><div class="div2">
<h3><a id="id-sequencetype-subtype"/>3.3 <a href="#id-sequencetype-subtype" style="text-decoration: none">Subtype Relationships</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-item-subtype-documents">⬇</a> <a href="#id-record-test">⬆</a></p><ol><li><p>The presentation of the rules for the subtype relationship between 
                  sequence types and item types has been substantially
                  rewritten to improve clarity; no change to the semantics is intended.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/196">196</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/202">202</a> 25 October 2022]</i></p></li></ol></div><p>
               [<a id="dt-subtype" title="subtype">Definition</a>: Given two 
               <a title="sequence type" class="termref" href="#dt-sequence-type">sequence types</a>
               or <a title="item type" class="termref" href="#dt-item-type">item types</a>, the rules in this section determine if one 
               is a <b>subtype</b> of the other. If a type <var>A</var> is a subtype of type <var>B</var>,
               it follows that every value matched by <var>A</var> is also matched by <var>B</var>.]
            </p><div class="note"><p class="prefix"><b>Note:</b></p><p>The relationship <code>subtype(A, A)</code> is always true:
               every type is a subtype of itself.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The converse is not necessarily true: we cannot infer that 
               if every value matched by <var>A</var> is also matched by <var>B</var>, then 
               <var>A</var> is a subtype of type <var>B</var>. For example, <var>A</var> might be
            defined as the set of strings matching the regular expression <code>[A-Z]*</code>, while <var>B</var>
            is the set of strings matching the regular expression <code>[A-Za-z]*</code>; no subtype
            relationship holds between these types.</p></div><p>The rules for deciding whether one <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a> is a subtype
               of another are given in <a href="#id-seqtype-subtype"><b>3.3.1 Subtypes of Sequence Types</b></a>. The rules for deciding whether
               one <a title="item type" class="termref" href="#dt-item-type">item type</a> is a subtype of another are given in <a href="#id-itemtype-subtype"><b>3.3.2 Subtypes of Item Types</b></a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The subtype relationship is not acyclic. There are cases where <code>subtype(A, B)</code> and
               <code>subtype(B, A)</code> are both true. This implies that <var>A</var> and <var>B</var>
               have the same value space, but they can still be different types. For example this applies when <var>A</var>
               is a union type with member types <code>xs:string</code> and <code>xs:integer</code>, while 
               <var>B</var> is a union type with member types <code>xs:integer</code> and <code>xs:string</code>.
               These are different types (<code>"23" cast as A</code> produces a string, while <code>"23" cast as B</code>
               produces an integer, because casting is attempted to each member type in order) but both types have the same value space.
            </p></div><div class="div3">
<h4><a id="id-seqtype-subtype"/>3.3.1 <a href="#id-seqtype-subtype" style="text-decoration: none">Subtypes of Sequence Types</a></h4><p>We use the notation <code>A ⊑ B</code>, or <code>subtype(A, B)</code> to indicate that
                  a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>
                  <code>A</code> is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of a sequence type <code>B</code>.
               This section defines the rules for deciding whether any two sequence types have this relationship.</p><p>To define the rules, we divide sequence types into six categories:</p><ul><li><p>The category <code>empty</code> includes the sequence types <code>empty-sequence()</code>,
                   <code>xs:error*</code> and <code>xs:error?</code>. All these sequence types
                  match the empty sequence as their only instance.</p></li><li><p>The category <code>void</code> includes the sequence types <code>xs:error</code> and <code>xs:error+</code>,
                  which have no instances (not even the empty sequence).</p></li><li><p>The categories <code>X?</code>, <code>X*</code>, <code>X</code> and <code>X+</code> includes all sequence types 
                     having an item type <code>X</code> other than <code>xs:error</code>, together with an occurrence indicator of 
                     <code>?</code> (zero or more), <code>*</code> (one or more), absent (exactly one), or <code>+</code> (one or more)
                     respectively. We use the notation <var>X<sub>i</sub></var> to indicate the item type of such a sequence type.</p></li></ul><p>The judgement <code>A ⊑ B</code> is then determined by the categories of the two sequence types, as defined
               in the table below. In many cases this depends on the relationship between the item types of <code>A</code>
                  and <code>B</code>. This is denoted using the notation <code>
                     <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                  </code>, 
                  as defined in <a href="#id-itemtype-subtype"><b>3.3.2 Subtypes of Item Types</b></a>.</p><table class="medium"><tbody><tr><th rowspan="2" colspan="2"/><th colspan="6">
                           <a title="sequence type" class="termref" href="#dt-sequence-type">Sequence type</a>
                           <var>B</var>
                        </th></tr><tr><th>
                           <code>empty</code>
                        </th><th>
                           <code>
                              <var>B<sub>i</sub></var>?</code>
                        </th><th>
                           <code>
                              <var>B<sub>i</sub></var>*</code>
                        </th><th>
                           <code>
                              <var>B<sub>i</sub></var>
                           </code>
                        </th><th>
                           <code>
                              <var>B<sub>i</sub></var>+</code>
                        </th><th>
                           <code>void</code>
                        </th></tr><tr><th rowspan="6">
                           <a title="sequence type" class="termref" href="#dt-sequence-type">Sequence type</a>
                           <var>A</var>
                        </th><th>
                           <code>empty</code>
                        </th><td>true</td><td>true</td><td>true</td><td>false</td><td>false</td><td>false</td></tr><tr><th>
                           <code>
                              <var>A<sub>i</sub></var>?</code>
                        </th><td>false</td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>false</td><td>false</td><td>false</td></tr><tr><th>
                           <code>
                              <var>A<sub>i</sub></var>*</code>
                        </th><td>false</td><td>false</td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>false</td><td>false</td><td>false</td></tr><tr><th>
                           <code>
                              <var>A<sub>i</sub></var>
                           </code>
                        </th><td>false</td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>false</td></tr><tr><th>
                           <code>
                              <var>A<sub>i</sub></var>+</code>
                        </th><td>false</td><td>false</td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>false</td><td>
                           <code>
                              <var>A<sub>i</sub></var> ⊆ <var>B<sub>i</sub></var>
                           </code>
                        </td><td>false</td></tr><tr><th>
                           <code>void</code>
                        </th><td>true</td><td>true</td><td>true</td><td>true</td><td>true</td><td>true</td></tr></tbody></table></div><div class="div3">
<h4><a id="id-itemtype-subtype"/>3.3.2 <a href="#id-itemtype-subtype" style="text-decoration: none">Subtypes of Item Types</a></h4><p>We use the notation <code>A ⊆ B</code>, or <code>itemtype-subtype(A, B)</code> to indicate that
                  an <a title="item type" class="termref" href="#dt-item-type">item type</a>
                  <code>A</code> is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of an item type <code>B</code>.
                  This section defines the rules for deciding whether any two item types have this relationship.</p><p>The rules in this section apply to 
                  <a title="item type" class="termref" href="#dt-item-type">item types</a>, not to 
                  <a title="item type designator" class="termref" href="#dt-item-type-designator">item type designators</a>.
                  For example, if the name <code>STR</code> has been defined in the
                  static context as a <a title="named item type" class="termref" href="#dt-named-item-type">named item type</a> referring to the type <code>xs:string</code>,
                  then anything said here about the type <code>xs:string</code> applies equally
                  whether it is designated as <code>xs:string</code> or as <code>STR</code>,
                  or indeed as the parenthesized forms <code>(xs:string)</code> or
                 <code>(STR)</code>.</p><p>References to <a title="named item type" class="termref" href="#dt-named-item-type">named item types</a> 
                  are handled as described in <a href="#id-itemtype-subtype-aliases"><b>3.3.2.9 Subtyping of Named Item Types</b></a>.</p><p>The relationship <code>A ⊆ B</code> is true
               if and only if at least one of the conditions listed in the following subsections applies:</p><div class="div4">
<h5><a id="id-item-subtype-general"/>3.3.2.1 <a href="#id-item-subtype-general" style="text-decoration: none">General Subtyping Rules</a></h5><p>Given <a title="item type" class="termref" href="#dt-item-type">item types</a>
                     <var>A</var> and <var>B</var>, 
                     <code>
                        <var>A</var> ⊆ <var>B</var>
                     </code> is true if any of the following apply:</p><ol class="enumar"><li><p>
                           <var>A</var> is <code>xs:error</code>.</p></li><li><p>
                           <var>B</var> is <code>item()</code>.</p></li><li><p>
                           <var>A</var> and <var>B</var> are the same <a title="item type" class="termref" href="#dt-item-type">item type</a>.</p></li><li><p>There is an <a title="item type" class="termref" href="#dt-item-type">item type</a>
                           <var>X</var> such that <code>
                              <var>A</var> ⊆ <var>X</var>
                           </code>
                              and <code>
                              <var>X</var> ⊆ <var>B</var>
                           </code>. (This is referred to below as the <b>transitivity rule</b>).</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>The first rule is technically redundant: it is implied by the second rule in <a href="#id-item-subtype-atomic"><b>3.3.2.3 Subtyping of Atomic and Union Types</b></a>.
                     The type <code>xs:error</code> is defined as a union type with no member types; therefore it is automatically true that
                        every member type <var>T</var> satisfies <code>
                           <var>T</var> ⊆ <var>B</var>
                        </code>.</p></div></div><div class="div4">
<h5><a id="id-item-subtype-choice"/>3.3.2.2 <a href="#id-item-subtype-choice" style="text-decoration: none">Subtyping of Choice Item Types</a></h5><p>The following rules determine whether <code>
                        <var>A</var> ⊆ <var>B</var>
                     </code> is true in the
                  case where either <var>A</var> or <var>B</var> or both is a <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item type</a>.</p><p>Firstly, if one of the operands is <em>not</em> a choice item type, then it is treated
                  as a choice item type with a single member type. The rule is then:</p><p>
                     <code>
                        <var>A</var> ⊆ <var>B</var>
                     </code> is true if for every member type <var>a</var> in 
                  <var>A</var>, there is a member type <var>b</var> in <var>B</var> such that <code>
                        <var>a</var> ⊆ <var>b</var>
                     </code>.</p><p>For example, <code>(xs:int | xs:long)</code> is a subtype of <code>(xs:decimal | xs:date)</code>
                  because both <code>xs:int</code> and <code>xs:long</code> are subtypes of <code>xs:decimal</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Because an <a title="enumeration type" class="termref" href="#dt-enumeration-type">enumeration type</a> is defined as a choice type
                        of singleton enumerations, these
                     rules have the consequence, for example, that <code>enum("A", "B")</code> is a subtype
                     of <code>enum("A", "B", "C")</code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The type <code>xs:int</code> is not a subtype of <code>(xs:negativeInteger | xs:nonNegativeInteger)</code>,
                     because it does not satisfy this rule. This is despite the fact that the value space of <code>xs:int</code>
                     is a subset of the value space of <code>(xs:negativeInteger | xs:nonNegativeInteger)</code>.</p></div></div><div class="div4">
<h5><a id="id-item-subtype-atomic"/>3.3.2.3 <a href="#id-item-subtype-atomic" style="text-decoration: none">Subtyping of Atomic and Union Types</a></h5><p>Given item types <var>A</var> and <var>B</var>, <code>
                        <var>A</var> ⊆ <var>B</var>
                     </code> is true if any of the following apply:</p><ol class="enumar"><li><p>
                           <var>A</var> and <var>B</var> are <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic types</a>, 
                              and <var>A</var>
                           <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                           <var>B</var>.</p><div class="example">
<div class="exampleHeader"><a id="d3e10322"/>Examples:</div><ul><li><p>
                                    <code>xs:integer ⊆ xs:decimal</code> because <code>xs:integer</code> is derived
                                 by restriction from <code>xs:decimal</code>.</p></li><li><p>
                                    <code>xs:decimal ⊆ xs:numeric</code> because <code>xs:numeric</code> is a pure union
                                    type that includes <code>xs:decimal</code> as a member type.</p></li><li><p>
                                    <code>enum("red") ⊆ xs:string</code> because the singleton
                                    enumeration type <code>enum("red")</code> is defined to be an atomic
                                    type derived from <code>xs:string</code>.</p></li><li><p>
                                    <code>enum("red") ⊆ enum("red", "green")</code> because the 
                                    enumeration type <code>enum("red", "green")</code> is defined to be a union type
                                       that has the generalized atomic type <code>enum("red")</code> as a member type.</p></li></ul></div></li><li><p>
                           <var>A</var> is a <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a>, 
                              and every type <var>T</var> in the transitive membership of <var>A</var>
                              satisfies <code>
                              <var>T</var> ⊆ <var>B</var>
                           </code>.</p><div class="example">
<div class="exampleHeader"><a id="d3e10391"/>Examples:</div><ul><li><p>
                                    <code>(xs:short | xs:long) ⊆ xs:integer</code>
                                    because <code>xs:short ⊆ xs:integer</code> and <code>xs:long ⊆ xs:integer</code>.</p></li><li><p>
                                    <code>(P | Q) ⊆ (P | Q | R)</code>
                                 because <code>P ⊆ (P | Q | R)</code> and <code>Q ⊆ (P | Q | R)</code>.</p></li><li><p>
                                    <code>enum("red", "green") ⊆ xs:string</code> because the 
                                    enumeration type <code>enum("red") ⊆ xs:string</code> 
                                    and <code>enum("green") ⊆ xs:string</code>.</p></li><li><p>
                                    <code>enum("red", "green") ⊆ enum("red", "green", "blue")</code> because 
                                    <code>enum("red") ⊆ enum("red", "green", "blue")</code> and 
                                    <code>enum("green") ⊆ enum("red", "green", "blue")</code>.</p></li><li><p>
                                    <code>enum("red", "green", "blue") ⊆ (enum("red", "green") | enum("blue"))</code> because 
                                    each of the types <code>enum("red")</code>, <code>enum("green")</code>, and <code>enum("blue")</code>
                                    is a subtype of one of the two members of the union type.</p></li></ul></div></li></ol></div><div class="div4">
<h5><a id="id-item-subtype-nodes"/>3.3.2.4 <a href="#id-item-subtype-nodes" style="text-decoration: none">Subtyping of Node Types</a></h5><p>The following subsections describe the subtype relationships
                  among node types.</p><div class="div5">
<h6><a id="id-item-subtype-nodes-general"/>3.3.2.4.1 <a href="#id-item-subtype-nodes-general" style="text-decoration: none">Subtyping Nodes: General Rules</a></h6><p>Given item types <var>A</var> and <var>B</var>, <code>
                           <var>A</var> ⊆ <var>B</var>
                        </code> is true if any of the following apply:</p><ol class="enumar"><li><p>
                              <var>A</var> is a <a href="#prod-xpath40-KindTest">KindTest</a> and <var>B</var> is <code>node()</code>.</p><div class="example">
<div class="exampleHeader"><a id="d3e10491"/>Example:</div><p>
                                 <code>comment() ⊆ node()</code>
                              </p></div></li><li><p>
                              <var>A</var> is <code>processing-instruction(<var>N</var>)</code> for any name <var>N</var>,
                              and <var>B</var> is <code>processing-instruction()</code>.</p><div class="example">
<div class="exampleHeader"><a id="d3e10515"/>Example:</div><p>
                                 <code>processing-instruction('pi') ⊆ processing-instruction()</code>
                              </p></div></li></ol></div><div class="div5">
<h6><a id="id-item-subtype-documents"/>3.3.2.4.2 <a href="#id-item-subtype-documents" style="text-decoration: none">Subtyping Nodes: Document Nodes</a></h6><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-coercion-rules">⬇</a> <a href="#id-sequencetype-subtype">⬆</a></p><ol><li><p>The rules for subtyping of document node types have been refined.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1624">1624</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1898">1898</a> 7 April 2025]</i></p></li></ol></div><p>Given item types <var>A</var> and <var>B</var>, <code>
                           <var>A</var> ⊆ <var>B</var>
                        </code> 
                     is true if any of the following rules apply.</p><p>These rules apply after expanding <code>document-node(<var>N</var>)</code>, where <var>N</var>
                  is a <a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>, to the equivalent 
                     <code>document-node(element(<var>N</var>))</code>.</p><ol class="enumar"><li><p>
                              <var>A</var> is <code>document-node(<var>E</var>)</code> for any <var>E</var>,
                              and <var>B</var> is <code>document-node()</code>.</p><div class="example">
<div class="exampleHeader"><a id="d3e10578"/>Examples:</div><p>
                                 <code>document-node(element(chap)) ⊆ document-node()</code>
                              </p><p>
                                 <code>document-node(*) ⊆ document-node()</code>
                              </p></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                    <var>A</var> is <code>document-node(<var>A<sub>e</sub></var>)</code>
                                 </p></li><li><p>
                                    <var>B</var> is <code>document-node(<var>B<sub>e</sub></var>)</code>
                                 </p></li><li><p>
                                    <code>
                                       <var>A<sub>e</sub></var> ⊆ <var>B<sub>e</sub></var>
                                    </code>
                                 </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e10623"/>Examples:</div><p>
                                 <code>document-node(element(title)) ⊆ document-node(element(*))</code>.</p><p>
                                 <code>document-node(title) ⊆ document-node(*)</code>.</p></div></li><li><p>
                              <var>A</var> is <code>document-node(element(<var>A<sub>1</sub></var>|<var>A<sub>2</sub></var>|..., <var>T</var>))</code> 
                              (where <var>T</var> may be absent),
                           and for each <var>A<sub>n</sub></var>, <code>document-node(element(<var>A<sub>n</sub></var>, <var>T</var>)) ⊆ <var>B</var>
                              </code>.</p><div class="example">
<div class="exampleHeader"><a id="d3e10662"/>Examples:</div><ul><li><p>
                                       <code>document-node(a|b) ⊆ document-node(a) | document-node(b)</code>
                                    </p></li><li><p>
                                       <code>document-node(a|b) ⊆ document-node(a|b|c)</code>
                                    </p></li></ul></div></li></ol></div><div class="div5">
<h6><a id="id-item-subtype-elements"/>3.3.2.4.3 <a href="#id-item-subtype-elements" style="text-decoration: none">Subtyping Nodes: Elements</a></h6><p>
                        [<a id="dt-wildcard-matches" title="wildcard-matches">Definition</a>: In these rules, if <var>MU</var> and <var>NU</var> 
                     are <a href="#prod-xpath40-NameTestUnion">NameTestUnions</a>, 
                     then <var>MU</var>
                           <b>wildcard-matches</b>
                           <var>NU</var> is true if every name that matches <var>MU</var>
                     also matches <var>NU</var>.]
                     </p><p>More specifically, this is the case if for every <a href="#prod-xpath40-NameTest">NameTest</a>
                        <var>M</var>
                     in <var>MU</var> there is a <a href="#prod-xpath40-NameTest">NameTest</a>
                        <var>N</var> in <var>NU</var>
                     where at least one of the following applies:</p><ol class="enumar"><li><p>
                              <var>M</var> and <var>N</var> are the same <code>NameTest</code>.</p></li><li><p>
                              <var>M</var> is an <code>EQName</code> and <var>N</var> is a 
                        <a href="#prod-xpath40-Wildcard">Wildcard</a> that matches <var>M</var>.</p></li><li><p>
                              <var>N</var> is the <a href="#prod-xpath40-Wildcard">Wildcard</a>
                              <code>*</code>.</p></li></ol><p>Given item types <var>A</var> and <var>B</var>, <code>
                           <var>A</var> ⊆ <var>B</var>
                        </code> 
                     is true if any of the following apply.</p><ol class="enumar"><li><p>
                              <var>A</var> is an <a href="#doc-xpath40-ElementTest">ElementTest</a> and
                           <var>B</var> is either <code>element()</code> or <code>element(*)</code>
                           </p></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                    <var>A</var> is either <code>element(<var>A<sub>n</sub></var>)</code> or <code>element(<var>A<sub>n</sub></var>, <var>T</var>)</code>  
                                 or <code>element(<var>A<sub>n</sub></var>, <var>T</var>?)</code> for any type <var>T</var>
                                 </p></li><li><p>
                                    <var>B</var> is either <code>element(<var>B<sub>n</sub></var>)</code> or <code>element(<var>B<sub>n</sub></var>, xs:anyType?)</code>
                                 </p></li><li><p>
                                    <var>A<sub>n</sub></var>
                                    <a title="wildcard-matches" class="termref" href="#dt-wildcard-matches">wildcard-matches</a>
                                    <var>B<sub>n</sub></var>
                                 </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e10845"/>Examples:</div><ul><li><p>
                                       <code>element(title) ⊆ element(*)</code>
                                    </p></li><li><p>
                                       <code>element(title, xs:string) ⊆ element(*)</code>
                                    </p></li><li><p>
                                       <code>element(title|heading, xs:string) ⊆ element(*)</code>
                                    </p></li><li><p>
                                       <code>element(title, xs:string) ⊆ element(title|heading)</code>
                                    </p></li><li><p>
                                       <code>element(title, xs:string?) ⊆ element(*)</code>
                                    </p></li><li><p>
                                       <code>element(title|heading, xs:string) ⊆ element(*)</code>
                                    </p></li><li><p>
                                       <code>element(title) ⊆ element(title, xs:anyType?)</code>
                                    </p></li><li><p>
                                       <code>element(title, xs:integer) ⊆ element(title|heading, xs:anyType?)</code>
                                    </p></li><li><p>
                                       <code>element(title, xs:string?) ⊆ element(title, xs:anyType?)</code>
                                    </p></li><li><p>
                                       <code>element(my:title|your:title) ⊆ element(*:title)</code>
                                    </p></li><li><p>
                                       <code>element(my:title|my:heading) ⊆ element(my:*)</code>
                                    </p></li></ul></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                    <var>A</var> is <code>element(<var>A<sub>n</sub></var>, <var>A<sub>t</sub></var>)</code>.</p></li><li><p>
                                    <var>B</var> is <code>element(<var>B<sub>n</sub></var>, <var>B<sub>t</sub></var>)</code>.</p></li><li><p>
                                    <var>A<sub>n</sub></var>
                                    <a title="wildcard-matches" class="termref" href="#dt-wildcard-matches">wildcard-matches</a>
                                    <var>B<sub>n</sub></var>.</p></li><li><p>
                                    <var>A<sub>t</sub></var>
                                    <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                                    <var>B<sub>t</sub></var>.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e10959"/>Examples:</div><ul><li><p>
                                       <code>element(size, xs:integer) ⊆ element(size, xs:decimal)</code>
                                    </p></li><li><p>
                                       <code>element(size, xs:integer) ⊆ element(size|größe, xs:decimal)</code>
                                    </p></li><li><p>
                                       <code>element(size, xs:integer) ⊆ element(*, xs:decimal)</code>
                                    </p></li><li><p>
                                       <code>element(*, xs:integer) ⊆ element(*, xs:decimal)</code>
                                    </p></li><li><p>
                                       <code>element(my:*, xs:integer) ⊆ element(*, xs:decimal)</code>
                                    </p></li></ul></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                    <var>A</var> is either <code>element(<var>A<sub>n</sub></var>, <var>A<sub>t</sub></var>)</code> or 
                                 <code>element(<var>A<sub>n</sub></var>, <var>A<sub>t</sub></var>?)</code>
                                 </p></li><li><p>
                                    <var>B</var> is <code>element(<var>B<sub>n</sub></var>, <var>B<sub>t</sub></var>?)</code>
                                 </p></li><li><p>
                                    <var>A<sub>n</sub></var>
                                    <a title="wildcard-matches" class="termref" href="#dt-wildcard-matches">wildcard-matches</a>
                                    <var>B<sub>n</sub></var>
                                 </p></li><li><p>
                                    <var>A<sub>t</sub></var>
                                    <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                                    <var>B<sub>t</sub></var>.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11044"/>Examples:</div><ul><li><p>
                                       <code>element(size, xs:integer) ⊆ element(size, xs:decimal?)</code>
                                    </p></li><li><p>
                                       <code>element(size, xs:integer?) ⊆ element(*, xs:decimal?)</code>
                                    </p></li><li><p>
                                       <code>element(*, xs:integer) ⊆ element(*, xs:decimal?)</code>
                                    </p></li><li><p>
                                       <code>element(my:*, xs:integer?) ⊆ element(*, xs:decimal?)</code>
                                    </p></li></ul></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                    <var>A</var> is <code>schema-element(<var>A<sub>n</sub></var>)</code>
                                 </p></li><li><p>
                                    <var>B</var> is <code>schema-element(<var>B<sub>n</sub></var>)</code>
                                 </p></li><li><p>Every element declaration that is an actual member of the substitution group of <var>A<sub>n</sub></var> 
                                 is also an actual member of the substitution group of <var>B<sub>n</sub></var>.</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>The fact that <var>P</var> is a member of the substitution group of <var>Q</var> 
                                   does not mean that every element declaration in the substitution group of <var>P</var> 
                                   is also in the substitution group of <var>Q</var>. For example, <var>Q</var> might 
                                   block substitution of elements whose type is derived by extension, while <var>P</var> does not.</p></div></li><li><p>
                              <var>A</var> is <code>element(<var>A<sub>1</sub></var>|<var>A<sub>2</sub></var>|..., <var>T</var>)</code> 
                              (where <var>T</var> may be absent),
                           and for each <var>A<sub>n</sub></var>, <code>element(<var>A<sub>n</sub></var>, <var>T</var>) ⊆ <var>B</var>
                              </code>.</p><div class="example">
<div class="exampleHeader"><a id="d3e11144"/>Examples:</div><ul><li><p>
                                       <code>element(a|b) ⊆ (element(a)|element(b)|element(c))</code>
                                    </p></li><li><p>
                                       <code>element(a|b, xs:integer) ⊆ (element(a, xs:decimal) | element(b, xs:integer))</code>
                                    </p></li></ul></div></li></ol></div><div class="div5">
<h6><a id="id-item-subtype-attributes"/>3.3.2.4.4 <a href="#id-item-subtype-attributes" style="text-decoration: none">Subtyping Nodes: Attributes</a></h6><p>Given item types <var>A</var> and <var>B</var>, <code>
                           <var>A</var> ⊆ <var>B</var>
                        </code> is true if any of the following apply:</p><ol class="enumar"><li><p>
                              <var>A</var> is an <a href="#doc-xpath40-AttributeTest">AttributeTest</a> and
                              <var>B</var> is either <code>attribute()</code> or <code>attribute(*)</code>
                           </p></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                    <var>A</var> is either <code>attribute(<var>A<sub>n</sub></var>)</code> or 
                                 <code>attribute(<var>A<sub>n</sub></var>, <var>T</var>)</code> 
                                 for any type <var>T</var>.</p></li><li><p>
                                    <var>B</var> is either <code>attribute(Bn)</code> or 
                                 <code>attribute(<var>B<sub>n</sub></var>, xs:anyAtomicType)</code>
                                 </p></li><li><p>
                                    <var>A<sub>n</sub></var>
                                    <a title="wildcard-matches" class="termref" href="#dt-wildcard-matches">wildcard-matches</a>
                                    <var>B<sub>n</sub></var>
                                 </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11236"/>Examples:</div><ul><li><p>
                                       <code>attribute(code) ⊆ attribute(*)</code>
                                    </p></li><li><p>
                                       <code>attribute(code|status) ⊆ attribute(*)</code>
                                    </p></li><li><p>
                                       <code>attribute(code, xs:untypedAtomic) ⊆ attribute(*)</code>
                                    </p></li><li><p>
                                       <code>attribute(code|status, xs:string) ⊆ attribute(code, xs:anyAtomicType)</code>
                                    </p></li><li><p>
                                       <code>attribute(my:code|your:code) ⊆ attribute(*:code)</code>
                                    </p></li><li><p>
                                       <code>attribute(my:code|my:status) ⊆ attribute(my:*)</code>
                                    </p></li></ul></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                    <var>A</var> is <code>attribute(<var>A<sub>n</sub></var>, <var>A<sub>t</sub></var>)</code>
                                 </p></li><li><p>
                                    <var>B</var> is <code>attribute(<var>B<sub>n</sub></var>, <var>B<sub>t</sub></var>)</code>
                                 </p></li><li><p>
                                    <var>A<sub>n</sub></var>
                                    <a title="wildcard-matches" class="termref" href="#dt-wildcard-matches">wildcard-matches</a>
                                    <var>B<sub>n</sub></var>
                                 </p></li><li><p>
                                    <var>A<sub>t</sub></var>
                                    <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                                    <var>B<sub>t</sub></var>.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11320"/>Examples:</div><ul><li><p>
                                       <code>attribute(*, xs:ID) ⊆ attribute(*, xs:string)</code>
                                    </p></li><li><p>
                                       <code>attribute(my:*, xs:ID) ⊆ attribute(*, xs:string)</code>
                                    </p></li><li><p>
                                       <code>attribute(code, xs:ID) ⊆ attribute(code|status, xs:string)</code>
                                    </p></li><li><p>
                                       <code>attribute(code, xs:ID) ⊆ attribute(*, xs:string)</code>
                                    </p></li><li><p>
                                       <code>attribute(code, xs:ID) ⊆ attribute(*:code, xs:ID)</code>
                                    </p></li><li><p>
                                       <code>attribute(my:code|my:status, xs:ID) ⊆ attribute(my:*, xs:string)</code>
                                    </p></li></ul></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                    <var>A</var> is <code>schema-attribute(<var>A<sub>n</sub></var>)</code>
                                 </p></li><li><p>
                                    <var>B</var> is <code>schema-attribute(<var>B<sub>n</sub></var>)</code>
                                 </p></li><li><p>the <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> of <var>A<sub>n</sub></var> equals the <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> of <var>B<sub>n</sub></var>
                                 </p></li></ol></li><li><p>
                              <var>A</var> is <code>attribute(<var>A<sub>1</sub></var>|<var>A<sub>2</sub></var>|..., <var>T</var>)</code> (where <var>T</var> may be absent),
                           and for each <var>A<sub>n</sub></var>, <code>attribute(<var>A<sub>n</sub></var>, <var>T</var>) ⊆ <var>B</var>
                              </code>.</p><div class="example">
<div class="exampleHeader"><a id="d3e11422"/>Examples:</div><ul><li><p>
                                       <code>attribute(a|b) ⊆ (attribute(a)|attribute(b)|attribute(c))</code>
                                    </p></li><li><p>
                                       <code>attribute(a|b, xs:integer) ⊆ (attribute(a, xs:decimal) | attribute(b))</code>
                                    </p></li></ul></div></li></ol></div></div><div class="div4">
<h5><a id="id-item-subtype-functions"/>3.3.2.5 <a href="#id-item-subtype-functions" style="text-decoration: none">Subtyping Functions</a></h5><p>Given item types <var>A</var> and <var>B</var>, <code>
                        <var>A</var> ⊆ <var>B</var>
                     </code> is true if any of the following apply:</p><ol class="enumar"><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is a <a href="#doc-xpath40-FunctionType">FunctionType</a>
                              </p></li><li><p>
                                 <var>B</var> is <code>
                                  function(*)</code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11473"/>Example:</div><p>
                              <code>function(xs:integer) as xs:string ⊆ function(*)</code>
                           </p></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>
                                    function(<var>a<sub>1</sub></var>, <var>a<sub>2</sub></var>, ... <var>a<sub>M</sub></var>) as <var>R<sub>A</sub></var>
                                 </code>
                              </p></li><li><p>
                                 <var>B</var> is <code>
                                    function(<var>b<sub>1</sub></var>, <var>b<sub>2</sub></var>, ... <var>b<sub>N</sub></var>) as <var>R<sub>B</sub></var>
                                 </code>
                              </p></li><li><p>
                                 <var>N</var> (the arity of <var>B</var>) equals <var>M</var> (the arity of <var>A</var>)
                                    </p></li><li><p>
                                 <code>
                                    <var>R<sub>A</sub></var> ⊑ <var>R<sub>B</sub></var>
                                 </code>
                              </p></li><li><p>For all values of <var>p</var> between 1 and <var>N</var>, <code>
                                    <var>b<sub>p</sub></var> ⊑ <var>a<sub>p</sub></var>
                                 </code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11550"/>Examples:</div><ul><li><p>
                                    <code>function(xs:integer) as xs:string ⊆ function(xs:long) as xs:string</code>
                                 </p></li><li><p>
                                    <code>function(xs:integer) as xs:ID ⊆ function(xs:integer) as xs:string</code>
                                 </p></li><li><p>
                                    <code>function(xs:integer) as xs:ID ⊆ function(xs:long) as xs:string</code>
                                 </p></li></ul></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Function return types are covariant because this rule requires <code>
                                 <var>R<sub>A</sub></var> ⊑ <var>R<sub>B</sub></var>
                              </code> for return types. 
                                 Function parameter types are contravariant because this rule requires <code>
                                 <var>b<sub>p</sub></var> ⊑ <var>a<sub>p</sub></var>
                              </code> for parameter types.</p></div></li></ol></div><div class="div4">
<h5><a id="id-item-subtype-maps"/>3.3.2.6 <a href="#id-item-subtype-maps" style="text-decoration: none">Subtyping Maps</a></h5><p>Given item types <var>A</var> and <var>B</var>, <code>
                        <var>A</var> ⊆ <var>B</var>
                     </code> is true if any of the following apply:</p><ol class="enumar"><li><p>Both of the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>map(<var>K</var>, <var>V</var>)</code>,
                                 for any <var>K</var> and <var>V</var>
                              </p></li><li><p>
                                 <var>B</var> is <code>map(*)</code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11631"/>Example:</div><p>
                              <code>map(xs:integer, item()*) ⊆ map(*)</code>
                           </p></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>map(<var>K<sub>a</sub></var>, <var>V<sub>a</sub></var>)</code>
                              </p></li><li><p>
                                 <var>B</var> is <code>map(<var>K<sub>b</sub></var>, <var>V<sub>b</sub></var>)</code>
                              </p></li><li><p>
                                 <code>
                                    <var>K<sub>a</sub></var> ⊆ <var>K<sub>b</sub></var>
                                 </code>
                              </p></li><li><p>
                                 <code>
                                    <var>V<sub>a</sub></var> ⊑ <var>V<sub>b</sub></var>
                                 </code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11685"/>Example:</div><p>
                              <code>map(xs:long, item()) ⊆ map(xs:integer, item()+)</code>
                           </p></div></li><li><p>Both the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>map(*)</code>
                                 (or, because of the transitivity rules, any other map type)</p></li><li><p>
                                 <var>B</var> is <code>function(*)</code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11711"/>Example:</div><p>
                              <code>map(xs:long, xs:string?) ⊆ function(*)</code>
                           </p></div></li><li><p>Both the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>map(*)</code>
                                 (or, because of the transitivity rules, any other map type)</p></li><li><p>
                                 <var>B</var> is 
                                 <code>function(xs:anyAtomicType) as item()*</code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11737"/>Example:</div><p>
                              <code>map(xs:long, xs:string?) ⊆ function(xs:anyAtomicType) as item()*</code>
                           </p></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>map(<var>K</var>, <var>V</var>)</code>
                              </p></li><li><p>
                                 <var>B</var> is <code>function(xs:anyAtomicType) as <var>R</var>
                                 </code>
                              </p></li><li><p>
                                 <var>V</var> ⊆ <var>R</var>
                              </p></li><li><p>
                                 <code>empty-sequence()</code> ⊆ <var>R</var>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11784"/>Examples:</div><ul><li><p>
                                    <code>map(xs:int, node()) ⊆ function(xs:anyAtomicType) as node()?</code>
                                 </p></li><li><p>
                                    <code>map(xs:int, node()+) ⊆ function(xs:anyAtomicType) as node()*</code>
                                 </p></li></ul><p>The function accepts type <code>xs:anyAtomicType</code> rather than <code>xs:int</code>,
                              because <code>$M("xyz")</code> is a valid call on a map (treated as a function) even
                              when all the keys in the map are integers.</p><p>The return type of the function is extended from <code>node()</code> or <code>node()+</code> to allow an empty sequence
                                 because <code>$M("xyz")</code> can return an empty sequence even if none of the entries
                              in the map contains an empty sequence.</p></div></li></ol></div><div class="div4">
<h5><a id="id-item-subtype-arrays"/>3.3.2.7 <a href="#id-item-subtype-arrays" style="text-decoration: none">Subtyping Arrays</a></h5><p>Given item types <var>A</var> and <var>B</var>, <code>
                        <var>A</var> ⊆ <var>B</var>
                     </code> is true if any of the following apply:</p><ol class="enumar"><li><p>Both the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>array(<var>X</var>)</code>
                              </p></li><li><p>
                                 <var>B</var> is <code>array(*)</code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11859"/>Example:</div><p>
                              <code>array(xs:integer) ⊆ array(*)</code>
                           </p></div></li><li><p>All the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>array(<var>X</var>)</code>
                              </p></li><li><p>
                                 <var>B</var> is <code>array(<var>Y</var>)</code>
                              </p></li><li><p>
                                 <code>
                                    <var>X</var> ⊑ <var>Y</var>
                                 </code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11899"/>Example:</div><p>
                              <code>array(xs:integer) ⊆ array(xs:decimal+)</code>
                           </p></div></li><li><p>Both the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>array(*)</code>
                                 (or, because of the transitivity rules, any other array type)</p></li><li><p>
                                 <var>B</var> is <code>function(*)</code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11925"/>Example:</div><p>
                              <code>array(xs:integer) ⊆ function(*)</code>
                           </p></div></li><li><p>Both the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>array(*)</code>
                                 (or, because of the transitivity rules, any other array type)</p></li><li><p>
                                 <var>B</var> is <code>function(xs:integer) as item()*</code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11951"/>Example:</div><p>
                              <code>array(*) ⊆ function(xs:integer) as item()*</code>
                           </p></div></li><li><p>Both the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is <code>array(<var>X</var>)</code>
                              </p></li><li><p>
                                 <var>B</var> is <code>function(xs:integer) as <var>X</var>
                                 </code>
                              </p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e11981"/>Example:</div><p>
                              <code>array(xs:string) ⊆ function(xs:integer) as xs:string</code>
                           </p></div></li></ol></div><div class="div4">
<h5><a id="id-item-subtype-records"/>3.3.2.8 <a href="#id-item-subtype-records" style="text-decoration: none">Subtyping Records</a></h5><p>Given item types <var>A</var> and <var>B</var>, <var>A</var>
                     <code>⊆</code>
                     <var>B</var> is true if any of the following apply:</p><ol class="enumar"><li><p>
                           <var>A</var> is <code>map(*)</code> and <var>B</var> is <code>record(*)</code>.</p></li><li><p>All of the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is a record type.</p></li><li><p>
                                 <var>B</var> is <code>map(*)</code> or <code>record(*)</code>.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e12036"/>Examples:</div><p>
                              <code>record(longitude, latitude)</code> ⊆ <code>map(*)</code>
                           </p><p>
                              <code>record(longitude, latitude, *)</code> ⊆ <code>record(*)</code>
                           </p><p>
                              <code>record(*)</code> ⊆ <code>map(*)</code>
                           </p></div></li><li><p>All of the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is a non-extensible record type</p></li><li><p>
                                 <var>B</var> is <code>map(<var>K</var>, <var>V</var>)</code>
                              </p></li><li><p>
                                 <var>K</var> is either <code>xs:string</code> or <code>xs:anyAtomicType</code>
                              </p></li><li><p>For every field <var>F</var> in <var>A</var>,
                              where <var>T</var> is the declared type of <var>F</var> (or its default, <code>item()*</code>),
                              <code>
                                    <var>T</var> ⊑ <var>V</var>
                                 </code>.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e12115"/>Examples:</div><ul><li><p>
                                    <code>record(x, y)</code> ⊆ <code>map(xs:string, item()*)</code>
                                 </p></li><li><p>
                                    <code>record(x as xs:double, y as xs:double)</code> ⊆ <code>map(xs:string, xs:double)</code>
                                 </p></li></ul></div></li><li><p>All of the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is a non-extensible record type.</p></li><li><p>
                                 <var>B</var> is a non-extensible record type.</p></li><li><p>Every field in <var>A</var> is also declared in <var>B</var>.</p></li><li><p>Every mandatory field in <var>B</var> is also declared  as mandatory in <var>A</var>.</p></li><li><p>For every field that is declared in both <var>A</var> and <var>B</var>,
                              where the declared type in <var>A</var> is <var>T</var> 
                              and the declared type in <var>B</var> is <var>U</var>, <code>
                                    <var>T</var> ⊑ <var>U</var>
                                 </code>.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e12186"/>Examples:</div><ul><li><p>
                                    <code>record(x, y as xs:integer) ⊆ record(x, y as xs:decimal)</code>
                                 </p></li><li><p>
                                    <code>record(x, y) ⊆ record(x, y, z?)</code>
                                 </p></li></ul></div></li><li><p>All of the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is an extensible record type</p></li><li><p>
                                 <var>B</var> is an extensible record type</p></li><li><p>Every mandatory field in <var>B</var> is also declared  as mandatory in <var>A</var>.</p></li><li><p>For every field that is declared in both <var>A</var> and <var>B</var>,
                                 where the declared type in <var>A</var> is <var>T</var> 
                                 and the declared type in <var>B</var> is <var>U</var>, <code>
                                    <var>T</var> ⊑ <var>U</var>
                                 </code>.</p></li><li><p>For every field that is declared in <var>B</var>
                                 but not in <var>A</var>, the declared type in <var>B</var> is <code>item()*</code>.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e12256"/>Examples:</div><ul><li><p>
                                    <code>record(x, y, z, *) ⊆ record(x, y, *)</code>
                                 </p></li><li><p>
                                    <code>record(x?, y?, z?, *) ⊆ record(x, y, *)</code>
                                 </p></li><li><p>
                                    <code>record(x as xs:integer, y as xs:integer, *) ⊆ record(x as xs:decimal, y as xs:integer*, *)</code>
                                 </p></li><li><p>
                                    <code>record(x as xs:integer, *) ⊆ record(x as xs:decimal, y as item(), *)</code>
                                 </p></li></ul></div></li><li><p>All of the following are true:</p><ol class="enumla"><li><p>
                                 <var>A</var> is a non-extensible record type.</p></li><li><p>
                                 <var>B</var> is an extensible record type.</p></li><li><p>Every mandatory field in <var>B</var> is also declared as mandatory in <var>A</var>.</p></li><li><p>For every field that is declared in both <var>A</var> and <var>B</var>,
                              where the declared type in <var>A</var> is <var>T</var> 
                              and the declared type in <var>B</var> is <var>U</var>, <code>
                                    <var>T</var> ⊑ <var>U</var>
                                 </code>.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e12327"/>Examples:</div><ul><li><p>
                                    <code>record(x, y as xs:integer) ⊆ record(x, y as xs:decimal, *)</code>
                                 </p></li><li><p>
                                    <code>record(y as xs:integer) ⊆ record(x?, y as xs:decimal, *)</code>
                                 </p></li></ul></div></li></ol></div><div class="div4">
<h5><a id="id-itemtype-subtype-aliases"/>3.3.2.9 <a href="#id-itemtype-subtype-aliases" style="text-decoration: none">Subtyping of Named Item Types</a></h5><p>This section describes how references to <a title="named item type" class="termref" href="#dt-named-item-type">named item types</a>
                  are handled when evaluating the subtype relationship.</p><p>Named item types can be classified as recursive or non-recursive. 
                     A recursive type is one that references itself, directly or indirectly. Only named record
                  types are allowed to be recursive.</p><p>In the case of references to non-recursive named item types, the reference
                  is fully expanded as the first step in evaluating the subtype relationship. For example
                  this means that if <var>U</var> is a named item type with the expansion 
                     <code>(xs:integer | xs:double)</code>,
                     then <code>xs:integer ⊆ U</code> is true, because 
                     <code>xs:integer ⊆ (xs:integer | xs:double)</code> is true.</p><p>Recursive record types are considered to be, in the terminology of the computer science
                  literature, <b>iso-recursive</b> (rather than <b>equi-recursive</b>). 
                     This means that a recursive type name is not
                  treated as being equivalent to its expansion (at any depth). 
                  For example, if the named item type <var>T</var>
                  has the expansion <code>record(A as item()*, B as T?)</code>, then the type 
                     <code>array(T)</code> is not considered to be equivalent to 
                     <code>array(record(A as item()*, B as T?))</code>, despite the fact
                  that the two types have exactly the same instances.</p><p>The rules are therefore defined as follows:</p><ul><li><p>If <var>B</var> is a reference to a recursive record type, then
                        <var>A</var> ⊆ <var>B</var> is true if and only if 
                        <var>A</var> and <var>B</var>
                        are references to the same named record type.</p></li><li><p>If <var>A</var> is a reference to a recursive named item type, then
                        <var>A</var> ⊆ <var>B</var> is true if either:</p><ul><li><p>
                                 <var>A</var> and <var>B</var>
                              are references to the same named record type.</p></li><li><p>
                                 <code>record(*) ⊆ B</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>This is because only record types are allowed to be recursive.</p></div></li></ul></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The decision to make recursive types iso-recursive rather than equi-recursive
                  was made largely because it saves a great deal of implementation complexity without any serious
                  adverse effects for users. In practice, problems can be avoided by using named record types
                  consistently (for example, avoiding having two named record types with
                  different names but identical definitions).</p></div></div></div></div><div class="div2">
<h3><a id="id-coercion-rules"/>3.4 <a href="#id-coercion-rules" style="text-decoration: none">Coercion Rules</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-function-coercion">⬇</a> <a href="#id-item-subtype-documents">⬆</a></p><ol><li><p>
                  The term "function conversion rules" used in 3.1 has been replaced by the term "coercion rules".
               <i>  [ PR <a href="https://github.com/qt4cg/qtspecs/pull/254">254</a> 29 November 2022]</i></p></li><li><p>
                  The coercion rules allow “relabeling” of a supplied atomic item where
                  the required type is a derived atomic type: for example, it is now permitted to supply
                  the value 3 when calling a function that expects an instance of <code>xs:positiveInteger</code>.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/117">117</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/254">254</a> 29 November 2022]</i></p></li><li><p>
                  The coercion rules now allow any numeric type to be implicitly converted to any other, for example
                  an <code>xs:double</code> is accepted where the required type is <code>xs:double</code>.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/980">980</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/911">911</a> 30 January 2024]</i></p></li><li><p>
                  The coercion rules now allow conversion in either direction between <code>xs:hexBinary</code>
                  and <code>xs:base64Binary</code>.
               <i>  [Issues <a href="https://github.com/qt4cg/qtspecs/issues/130">130</a> <a href="https://github.com/qt4cg/qtspecs/issues/480">480</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/815">815</a> 7 November 2023]</i></p></li><li><p>
                  The coercion rules now apply recursively to the members of an array and the entries in a map.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1318">1318</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1501">1501</a> 29 October 2024]</i></p></li><li><p>
                  The coercion rules now reorder the entries in a map when the required type is a record type.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1862">1862</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1874">1874</a> 25 March 2025]</i></p></li></ol></div><p>
               [<a id="dt-coercion-rules" title="coercion rules">Definition</a>: The <b>coercion rules</b> are rules used to convert a supplied value to a required type,
                     for example when converting an argument of a function call to the declared type of the function parameter.
                  ] The required type is expressed as a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>. The effect of the coercion rules may be to accept the value as supplied,
                  to convert it to a value that matches the required type, or to reject it with a type error.</p><p>This section defines how the coercion rules operate; the situations in which the rules apply
                  are defined elsewhere, by reference to this section.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>In previous versions of this specification, the coercion rules were referred to as the
                              <em>function conversion</em> rules. The terminology has changed because the rules are not exclusively
                              associated with functions or function calling.</p></div><p>If the required type is <code>empty-sequence()</code>, 
                  no coercion takes place (the supplied
               value must be an empty sequence, or a type error occurs).</p><p>In all other cases, the required 
                  <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>
               <var>T</var> comprises a required <a title="item type" class="termref" href="#dt-item-type">item type</a>
               <var>R</var> 
                  and an optional occurrence indicator. The coercion rules are then 
                  applied to a supplied value <var>V</var> 
               and the required type <var>T</var> as follows:</p><ol class="enumar"><li class="xpath"><p>If  <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath
                     1.0 compatibility mode</a> is <code>true</code> and <var>V</var>
                     is not an instance of the required type <var>T</var>, then the conversions defined in
                     <a href="#id-xpath-10-compatibility"><b>3.4.1 XPath 1.0 Compatibility Rules</b></a> are applied to <var>V</var>. Then:</p></li><li><p>Each item in <var>V</var> is processed against the required item type <var>R</var>
                     using the item coercion rules defined
                  in <a href="#id-item-coercion-rules"><b>3.4.2 Item Coercion Rules</b></a>, and the results are sequence-concatenated
                  into a single sequence <var>V′</var>.</p></li><li><p>A type error is raised if the cardinality of <var>V′</var> does not match the
                  required cardinality of <var>T</var>
                     [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li></ol><div class="div3 xpath">
<h4><a id="id-xpath-10-compatibility"/>3.4.1 <a href="#id-xpath-10-compatibility" style="text-decoration: none">XPath 1.0 Compatibility Rules</a></h4><p>These rules are used to process a value <var>V</var> against a required sequence type 
                  <var>T</var> when 
                  <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>true</code>.</p><ol class="enumar"><li><p>If the occurrence indicator of <var>T</var> is either absent or <code>?</code>
                        (examples: <code>xs:string</code>, <code>xs:string?</code>, 
                        <code>xs:untypedAtomic</code>, <code>xs:untypedAtomic?</code>, 
                        <code>node()</code>, <code>node()?</code>, <code>item()</code>, 
                        <code>item()?</code>), then <var>V</var> is effectively replaced by <code>V[1]</code>.</p></li><li><p>If <var>T</var> is <code>xs:string</code> or <code>xs:string?</code>,
                        then <var>V</var> is effectively replaced by
                        <code>fn:string(V)</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>This rule does not apply where <var>T</var> is derived from <code>xs:string</code>
                        or <code>xs:string?</code>, because derived types did not arise in XPath 1.0.</p></div></li><li><p>If <var>T</var> is <code>xs:double</code> or <code>xs:double?</code>, then 
                        <code>V</code> is effectively replaced by <code>fn:number(V)</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>This rule does not apply where <var>T</var> is derived from <code>xs:double</code>
                        or <code>xs:double?</code>, because derived types did not arise in XPath 1.0.</p></div></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>
                     The special rules for <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility
                        mode</a> are used for converting the arguments of a static function call, and
                     in certain XSLT constructs. They are not invoked in other contexts such as dynamic function calls,
                     for converting the result of an inline function to its required type,
                     for partial function application, or for implicit function calls such as
                     occur when evaluating functions such as <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-for-each"><code>fn:for-each</code></a> and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-filter"><code>fn:filter</code></a>.</p></div></div><div class="div3">
<h4><a id="id-item-coercion-rules"/>3.4.2 <a href="#id-item-coercion-rules" style="text-decoration: none">Item Coercion Rules</a></h4><p>The rules in this section are used to process each item <var>J</var> in a supplied
               sequence, given a required <a title="item type" class="termref" href="#dt-item-type">item type</a>
                  <var>R</var>.</p><ol class="enumar"><li><p>If <var>R</var> is a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>
                     (for example, if it is an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>,
                     a <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a>, or
                     an <a title="enumeration type" class="termref" href="#dt-enumeration-type">enumeration type</a>), and <var>J</var>
                     is not an atomic item, then:
                     </p><ol class="enumla"><li><p>
                                 <var>J</var> is atomized to produce a sequence of atomic items
                        <var>JJ</var>.</p></li><li><p>Each atomic item in <var>JJ</var> is coerced to the required
                        type <var>R</var> by recursive application of the item coercion rules (the 
                        rules in this section) to produce a value <var>V</var>.</p></li><li><p>The result is the sequence-concatenation of the <var>V</var> values.</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>For example, if <code>J</code> is an element with type annotation
                  <code>xs:integer</code>, and <code>R</code> is the union type <code>xs:numeric</code>,
                  then the effect is to atomize the element to an <code>xs:integer</code>, and then
                  to coerce the resulting <code>xs:integer</code> to <code>xs:numeric</code>
                  (which leaves the integer unchanged). This is not the same as attempting to
                  coerce the element to each of the alternatives of the union type in turn, which
                  would deliver an instance of <code>xs:double</code>.</p></div></li><li><p>Otherwise, if <var>R</var> is a <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item type</a> or
                     a <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a> (which includes the case where
                     it is an <a title="enumeration type" class="termref" href="#dt-enumeration-type">enumeration type</a>), then:</p><ol class="enumla"><li><p>If <var>J</var> matches (is an instance of) one of the alternatives
                           in <var>R</var>, then <var>J</var> is coerced to the first alternative in 
                           <var>R</var> that <var>J</var> matches.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>There are two situations where coercing an item to a type that
                              it already matches does not simply return the item unchanged:</p><ul><li><p>When the required type is a typed function type
                                      (see <a href="#id-function-test"><b>3.2.8.1 Function Types</b></a>),
                                    then function coercion is applied to coerce <var>J</var> to that
                                    function type, as described in <a href="#id-function-coercion"><b>3.4.4 Function Coercion</b></a>.</p></li><li><p>When the required type is a record type and the supplied
                                 value is a map, then coercion may change the <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry order</a><sup><small>DM</small></sup>
                                 of the entries in the map.</p></li></ul></div></li><li><p>Otherwise, the item coercion rules (the rules in this section)
                              are applied to <var>J</var> recursively
                              with <var>R</var> set to each of the alternatives in the
                              choice or union item type, in order, until an alternative is found that
                              does not result in a type error; a type error is raised
                              only if all alternatives fail.</p><p>The error code used in the event of failure 
                              should be the error code arising from the
                              first unsuccessful matching attempt. (The diagnostic information
                              associated with the error may also describe how further attempts
                              failed.)</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>Suppose the required type is <code>(xs:integer | element(e))*</code>
                           and the supplied value is the sequence 
                           <code>(&lt;e&gt;22&lt;/e&gt;, 23, &lt;f&gt;24&lt;/f&gt;)</code>. 
                        Item coercion is applied independently to each of the three items in this
                        sequence. The first item matches one of the alternatives, namely <code>element(e)</code>,
                        so it is returned unchanged as an element node. The second item (the integer 23) also matches one of the
                        alternatives, and is returned unchanged as an integer. The third item does not match any of
                        the alternatives, so coercion is attempted to each one in turn. Coercion
                        to type <code>xs:integer</code> succeeds (by virtue of atomization and untyped
                           atomic conversion), so the final result is the sequence <code>(&lt;e&gt;22&lt;/e&gt;, 23, 24)</code>
                        </p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Suppose the required type is <code>enum("red", "green", "blue")</code>
                           and the supplied value is <code>"green"</code>. The enumeration type is defined
                           as a choice item type whose alternatives are singleton enumerations, so the rules
                           are applied first to the type <code>enum("red")</code> (which fails), and
                           then to the type <code>enum("green")</code> (which succeeds). The strings in an
                           enumeration type are required to be distinct so the order
                           of checking is in this case immaterial. The supplied value will be accepted, and
                           will be relabeled as an instance of <code>enum("green")</code>, which is treated
                           as a schema type equivalent to a type derived from <code>xs:string</code> by 
                           restriction.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Schema-defined union types behave in exactly the same way as
                        <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item types.</a>
                        </p></div></li><li><p>If <var>R</var> is an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>
                     and <var>J</var> is an <a title="atomic item" class="termref" href="#dt-atomic-item">atomic item</a>, then:</p><ol class="enumla"><li><p>If <var>J</var> is an instance of 
                           <var>R</var> then it is used unchanged.</p></li><li><p>If <var>J</var> is an instance of type <code>xs:untypedAtomic</code>
                        then:</p><ol class="enumlr"><li><p>If <var>R</var> is <a title="namespace-sensitive" class="termref" href="#dt-namespace-sensitive">namespace-sensitive</a> then 
                                    a <a title="type error" class="termref" href="#dt-type-error">type error</a>
                                    [<a href="#ERRXPTY0117" title="err:XPTY0117">err:XPTY0117</a>] is raised.</p></li><li><p>Otherwise,  <var>J</var> is cast to type <var>R</var>.</p></li></ol></li></ol></li><li><p>If there is an entry (<var>from</var>, <var>to</var>)
                     in the following table such that <var>J</var> is an instance of <var>from</var>,
                     and <var>to</var> is <var>R</var>, then <var>J</var> is cast to type <var>R</var>.</p><table style="border:1px solid" class="medium"><caption>Implicit Casting</caption><thead><tr><th>from</th><th>to</th></tr></thead><tbody><tr><td>
                                 <code>xs:decimal</code>
                              </td><td>
                                 <code>xs:double</code>
                              </td></tr><tr><td>
                                 <code>xs:double</code>
                              </td><td>
                                 <code>xs:decimal</code>
                              </td></tr><tr><td>
                                 <code>xs:decimal</code>
                              </td><td>
                                 <code>xs:float</code>
                              </td></tr><tr><td>
                                 <code>xs:float</code>
                              </td><td>
                                 <code>xs:decimal</code>
                              </td></tr><tr><td>
                                 <code>xs:float</code>
                              </td><td>
                                 <code>xs:double</code>
                              </td></tr><tr><td>
                                 <code>xs:double</code>
                              </td><td>
                                 <code>xs:float</code>
                              </td></tr><tr><td>
                                 <code>xs:string</code>
                              </td><td>
                                 <code>xs:anyURI</code>
                              </td></tr><tr><td>
                                 <code>xs:anyURI</code>
                              </td><td>
                                 <code>xs:string</code>
                              </td></tr><tr><td>
                                 <code>xs:hexBinary</code>
                              </td><td>
                                 <code>xs:base64Binary</code>
                              </td></tr><tr><td>
                                 <code>xs:base64Binary</code>
                              </td><td>
                                 <code>xs:hexBinary</code>
                              </td></tr></tbody></table><div class="note"><p class="prefix"><b>Note:</b></p><p>The item type in the <var>to</var> column must match <var>R</var>
                        exactly; however, <var>J</var> may belong to a subtype of the type in the <var>from</var>
                        column.</p><p>For example, an <code>xs:NCName</code> will be cast to type <code>xs:anyURI</code>,
                        but an <code>xs:anyURI</code> will not be cast to type <code>xs:NCName</code>.</p><p>Similarly, an <code>xs:integer</code> will be cast to type <code>xs:double</code>,
                        but an <code>xs:double</code> will not be cast to type <code>xs:integer</code>.</p></div></li><li><p>If <var>R</var> is derived from some primitive atomic type <var>P</var>, 
                     then <var>J</var> is <b>relabeled</b> as an instance of <var>R</var> if it satisfies
                     all the following conditions:</p><ul><li><p>
                              <var>J</var> is an instance of <var>P</var>.</p></li><li><p>
                              <var>J</var> is not an instance of <var>R</var>.</p></li><li><p>The <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-datum">datum</a><sup><small>DM</small></sup> of <var>J</var> is 
                           within the value space of <var>R</var>.</p></li></ul><p>Relabeling an atomic item changes the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> but not the 
                        <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-datum">datum</a><sup><small>DM</small></sup>. For example, the
                        <code>xs:integer</code> value 3 can be relabeled as an instance of <code>xs:unsignedByte</code>, because
                        the datum is within the value space of <code>xs:unsignedByte</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Relabeling is not the same as casting. For example, the <code>xs:decimal</code> value 10.1
                        can be cast to <code>xs:integer</code>, but it cannot be relabeled as <code>xs:integer</code>,
                        because its datum not within the value space of <code>xs:integer</code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The effect of this rule is that if, for example, a function parameter is declared
                        with an expected type of <code>xs:positiveInteger</code>, then a call that supplies the literal
                        value 3 will succeed, whereas a call that supplies -3 will fail.</p><p>This differs from previous versions of this specification, where both these calls would fail.</p><p>This change allows the arguments of existing functions to be defined with a
                           
                           more precise type. For example, the <code>$position</code> argument of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-get"><code>array:get</code></a>
                           
                           could be defined as <code>xs:positiveInteger</code> 
                           rather than <code>xs:integer</code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>If <var>T</var>
                           is a union type with members <code>xs:negativeInteger</code> and 
                           <code>xs:positiveInteger</code> and the supplied value is the
                           sequence <code>(20, -20)</code>, then the effect of these rules 
                           is that the first item <code>20</code> is relabeled as type
                           <code>xs:positiveInteger</code> and the second item <code>-20</code>is relabeled as type 
                           <code>xs:negativeInteger</code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Promotion (for example of <code>xs:float</code> to <code>xs:double</code>)
                        occurs only when <var>T</var> is a primitive type.
                        Relabeling occurs only when <var>T</var> is a derived type. Promotion and relabeling are therefore
                        never combined.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>A <a title="singleton enumeration type" class="termref" href="#dt-singleton-enumeration-type">singleton enumeration type</a> such as <code>enum("green")</code> is treated
                     as an atomic type derived by restriction from <code>xs:string</code>; so if the
                     <code>xs:string</code> value <code>"green"</code> is supplied in a context where
                     the required type is <code>enum("red", "green", "blue")</code>, the value will be
                        accepted and will be relabeled as an instance of <code>enum("green")</code>.</p></div></li><li><p>If <var>R</var> is an <a href="#doc-xpath40-ArrayType">ArrayType</a> other than <code>array(*)</code> and <var>J</var>
                        is an array, then <var>J</var> is converted to a new array by converting
                        each member to the required member type by applying the coercion rules
                        recursively.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>For example, if the required type is
                     <code>array(xs:double)</code> and the supplied value is
                        <code>[ 1, 2 ]</code>, the array is converted to
                        <code>[ 1e0, 2e0 ]</code>.</p></div></li><li><p>If <var>R</var> is a <a href="#doc-xpath40-MapType">MapType</a> other than <code>map(*)</code> and <var>J</var>
                        is a map, then <var>J</var> is converted to a new map as follows:</p><ol class="enumla"><li><p>Each key in the supplied map is converted to the required
                           map key type by applying the coercion rules. If the resulting map would
                           contain duplicate keys, a type error is raised
                           [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li><li><p>The corresponding value is converted to the required
                           map value type by applying the coercion rules recursively.</p></li><li><p>The order of entries in the map remains unchanged.</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>For example, if the required type is
                     <code>map(xs:string, xs:double)</code> and the supplied value is
                        <code>{ "x": 1, "y": 2 }</code>, the map is converted to
                        <code>{ "x": 1e0, "y": 2e0 }</code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Duplicate keys can occur if the value space of the target type
                        is more restrictive than the original type. For example, an error is raised
                        if the map <code>{ 1.2: 0, 1.2000001: 0 }</code>, which contains two keys
                        of type <code>xs:decimal</code>, is coerced to the type
                        <code>map(xs:float, xs:integer)</code>.</p></div></li><li><p>If <var>R</var> is a <a href="#doc-xpath40-RecordType">RecordType</a> and <var>J</var> is a map, then <var>J</var> is converted
                        to a new map as follows:</p><ol class="enumla"><li><p>The keys in the supplied map are unchanged.</p></li><li><p>In any map entry whose key is equal to the
                        name of one of the field declarations in <var>R</var> (under the rules
                           of the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-atomic-equal"><code>atomic-equal</code></a> function), the corresponding
                           value is converted to the required type defined by that field declaration, 
                           by applying the coercion rules recursively 
                           (but with XPath 1.0 compatibility mode treated as false).</p></li><li><p>The order of entries in the map is changed: entries whose keys correspond
                        to the names of field declarations in <var>R</var> appear first, in the order
                        of the corresponding field declarations, and (if the record type is extensible)
                        other entries then follow retaining their relative order in <var>J</var>.</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>For example, if the required type is
                     <code>record(longitude as xs:double, latitude as xs:double)</code> and the supplied value is 
                        <code>{ "latitude": 53.2, "longitude": 0 }</code>,
                        then the map is converted to <code>{ "longitude": 0.0e0, "latitude": 53.2e0 }</code>.</p></div></li><li><p>If <var>R</var> is a <a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a> and <var>J</var> is a function item, then <a title="function coercion" class="termref" href="#dt-function-coercion">function coercion</a> is applied to <var>J</var>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Function coercion applies even if <var>J</var> is already an instance
                        of <var>R</var>.</p><p>Maps and arrays are functions, so function coercion applies to them as well.</p></div></li><li><p> If, after the
		above conversions, the resulting item does not match
		the expected item type <var>R</var> according to the rules for <a title="SequenceType matching" class="termref" href="#dt-sequencetype-matching">SequenceType
		Matching</a>, a type error is
		raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Under the general rules for type errors 
                           (see <a href="#id-kinds-of-errors"><b>2.4.1 Kinds of Errors</b></a>), a processor
                        <span class="verb">may</span> report a type error during static
                        analysis if it will necessarily occur when the expression is evaluated.
                        For example, the function call <code>fn:abs("beer")</code>
                        will necessarily fail when evaluated, because the function requires
                        a numeric value as its argument; this <span class="verb">may</span> be detected and reported
                        as a static error.</p></div></li></ol></div><div class="div3">
<h4><a id="id-implausible-coercions"/>3.4.3 <a href="#id-implausible-coercions" style="text-decoration: none">Implausible Coercions</a></h4><p>An expression is deemed to be
                     <a title="implausible" class="termref" href="#dt-implausible">implausible</a>
                  [<a href="#ERRXPTY0006" title="err:XPTY0006">err:XPTY0006</a>] if the static type of the expression, after applying
                     all necessary coercions, is <b>substantively disjoint</b>
                     with the required type <var>T</var>.</p><p>
                  [<a id="dt-substantively-disjoint" title="substantively disjoint">Definition</a>: Two 
               <a title="sequence type" class="termref" href="#dt-sequence-type">sequence types</a> are
                     deemed to be <b>substantively disjoint</b> if (a) neither is a subtype
                     of the other (see <a href="#id-seqtype-subtype"><b>3.3.1 Subtypes of Sequence Types</b></a>) and 
                     (b) the only values that
                     are instances of both types are one or more of the following:
               </p><ul><li><p>The empty sequence, <code>()</code>.</p></li><li><p>The <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-empty-map">empty map</a><sup><small>DM</small></sup>, <code>{}</code>.</p></li><li><p>The <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-empty-array">empty array</a><sup><small>DM</small></sup>, <code>[]</code>.</p></li></ul><p>]</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Examples of pairs of sequence types that are substantively disjoint
                        include:</p><ul><li><p>
                           <code>xs:integer*</code> and <code>xs:string*</code>
                        </p></li><li><p>
                           <code>map(xs:integer, node())</code> and <code>map(xs:string, node())</code>
                        </p></li><li><p>
                           <code>array(xs:integer)</code> and <code>array(xs:string)</code>
                        </p></li></ul></div><p>For example, supplying a value whose static type is <code>xs:integer*</code>
                     when the required type is <code>xs:string*</code> is <a title="implausible" class="termref" href="#dt-implausible">implausible</a>,
                     because it can succeed only in the special case where the actual value supplied
                     is an empty sequence.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The case where the supplied type and the required type are completely
                  disjoint (for example <code>map(*)</code> and <code>array(*)</code>) is covered
                  by the general rules for type errors: that case can always be reported as a static
                  error.</p></div><p>Examples of implausible coercions include the following:</p><ul><li><p>
                        <code>round(timezone-from-time($now))</code>. The result of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-timezone-from-time"><code>fn:timezone-from-time</code></a>
                  is of type <code>xs:dayTimeDuration?</code>, which is substantively disjoint with the required type
                  of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-round"><code>fn:round</code></a>, namely <code>xs:numeric?</code>.</p></li><li><p>
                        <code>function($x as xs:integer) as array(xs:string) { array { 1 to $x } }</code>. The type
                  of the function body is <code>array(xs:integer)</code>, which is substantively disjoint with the
                  required type <code>array(xs:string)</code>: the function can succeed only in the exceptional case
                  where the function body delivers an <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-empty-array">empty array</a><sup><small>DM</small></sup>.</p></li></ul></div><div class="div3">
<h4><a id="id-function-coercion"/>3.4.4 <a href="#id-function-coercion" style="text-decoration: none">Function Coercion</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-numeric-literals">⬇</a> <a href="#id-coercion-rules">⬆</a></p><ol><li><p>
                     Function coercion now allows a function with arity <var>N</var> to be supplied where a function of arity 
                     greater than <var>N</var> is expected. For example this allows the function <code>true#0</code> 
                     to be supplied where a predicate function is required.
                  </p></li><li><p>
                     It has been clarified that function coercion applies even when the supplied function item
                     matches the required function type. This is to ensure that arguments supplied when calling
                     the function are checked against the signature of the required function type, which might
                     be stricter than the signature of the supplied function item.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1020">1020</a> PRs <a href="https://github.com/qt4cg/qtspecs/pull/1023">1023</a> <a href="https://github.com/qt4cg/qtspecs/pull/1128">1128</a> 9 April 2024]</i></p></li></ol></div><p>
        Function coercion is a transformation applied to <a title="function item" class="termref" href="#dt-function-item">function items</a> during application of the
        <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.
        [<a id="dt-function-coercion" title="function coercion">Definition</a>: 
                     <b>Function coercion</b> wraps a <a title="function item" class="termref" href="#dt-function-item">function item</a>
        in a new function whose signature is the same as the expected type.
        This effectively delays the checking
        of the argument and return types
        until the function is called.]
               </p><p>Given a function <var>F</var>, and an expected function type <var>T</var>,
                  <a title="function coercion" class="termref" href="#dt-function-coercion">function coercion</a>
	proceeds as follows:</p><ol class="enumar"><li><p>If <var>F</var> has higher arity than <var>T</var>,
                     a type error is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>]
                     </p></li><li><p>If <var>F</var> has lower arity than <var>T</var>,
                     then <var>F</var> is wrapped in a new function that declares and ignores the
                  additional argument; the following steps are then applied to this new function.</p><p>For example, if <var>T</var> is <code>function(node(), xs:boolean) as xs:string</code>,
                  and the supplied function is <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-name"><code>fn:name#1</code></a>, then the supplied function is effectively
                  replaced by <code>function($n as node(), $b as xs:boolean) as xs:string { fn:name($n) }</code>
                     </p><div class="note"><p class="prefix"><b>Note:</b></p><p>This mechanism makes it easier to design versatile and extensible higher-order functions. 
                     For example, in previous versions of this specification, the second argument of
                     the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-filter"><code>fn:filter</code></a> function expected an argument of type 
                     <code>function(item()) as xs:boolean</code>. This has now been extended to
                     <code>function(item(), xs:integer) as xs:boolean</code>, but existing code continues
                     to work, because callback functions that are not interested in the value of the second
                     argument simply ignore it.                    
                  </p></div></li><li><p>A type error is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>] 
                        if, for any parameter type, or for the result type, the 
                        relevant type in the signature of the supplied function and the relevant type
                        in the expected function type are <a title="substantively disjoint" class="termref" href="#dt-substantively-disjoint">substantively disjoint</a>.
                     </p><p>For example, the types <code>xs:integer</code> and <code>xs:string</code>
                     are substantively disjoint, so a function with signature <code>function(xs:integer) as xs:boolean</code>
                        cannot be supplied where the expected type is <code>function(xs:string) as xs:boolean</code>.</p></li><li><p>Function coercion then
        returns a new <a title="function item" class="termref" href="#dt-function-item">function item</a>
        with the following properties
        (as defined in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#function-items">Section 7.1 Function Items</a><sup><small>DM</small></sup>):

        </p><ul><li><p>
                                 <b>name</b>:
            The name of <var>F</var>
                                 <span>(if not absent)</span>.
          </p></li><li><p>
                                 <b>identity</b>: A new function
              identity distinct from the identity of any other function item.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>See also <a href="#id-function-identity"><b>4.5.7 Function Identity</b></a>.</p></div></li><li><p>
                                 <b>signature</b>:
            <code>Annotations</code> is set to the annotations of <var>F</var>. <code>TypedFunctionType</code> is set to the expected type.
          </p></li><li><p>
                                 <b>implementation</b>:
            In effect,
            a <code>FunctionBody</code> that calls <var>F</var>,
            passing it the parameters of this new function,
            in order.
          </p></li><li><p>
                                 <b>nonlocal variable bindings</b>:
            An empty mapping.
          </p></li></ul></li></ol><p>
        These rules have the following consequences:

        </p><ul><li><p>SequenceType matching of the function’s arguments and result are delayed until that function is called.
          </p></li><li><p>When the coerced function is called, the supplied arguments must match the parameter
              typed defined in <var>T</var>; it is not sufficient to match the parameter types defined
              in <var>F</var>.</p></li><li><p>
                           The <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a> rules applied to the function’s arguments and result are defined by the SequenceType
            it has most recently been coerced to. Additional coercion rules could apply when the wrapped function
            is called.
          </p></li><li><p>
            If an implementation has static type information about a function, that can be used to type check the
            function’s argument and return types during static analysis.
          </p></li><li><p>When function coercion is applied to a map or an array, the resulting function is not
           a map or array, and cannot be used as such. For example, the expression </p><div class="exampleInner"><pre>let $f as function(xs:integer) as xs:boolean := { 0: false(), 1: true() }
return $f?0</pre></div><p>raises a type error, because a lookup expression requires the left hand
           operand to be a map or array, and <code>$f</code> is neither.</p></li><li><p>When function types are used as alternatives in a <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item type</a>,
                 the supplied function is coerced to the first alternative for which coercion does
                 not raise a type error. In this situation it is important to write the alternatives
                 in order, with the most specific first. </p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>
                     The semantics of <a title="function coercion" class="termref" href="#dt-function-coercion">function coercion</a> are specified in terms of wrapping the functions.
        Static typing may be able to reduce the number of places where this is actually necessary.
        However, it cannot be assumed that because a supplied function is an instance of the required
        function type, no function coercion is necessary: the supplied function might not perform
        all required checks on the types of its arguments.
      </p></div><p>Since maps and arrays are also functions in XPath 4.0, 
                  <a title="function coercion" class="termref" href="#dt-function-coercion">function coercion</a> applies to them as well.

        For instance, consider the following expression:
      </p><div class="exampleInner"><pre>
let $m := {
  "Monday" : true(),
  "Wednesday" : false(),
  "Friday" : true()
}
let $days := ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
return filter($days, $m)</pre></div><p>
        The map <code>$m</code> is an instance of 
                  <code>function(xs:anyAtomicType?) as item()*</code>. 
                  When the <code>fn:filter()</code> function is called, the following 
                  occurs to the map:

</p><ol class="enumar"><li><p>The map <code>$m</code> is treated as a function equivalent to <code>map:get($m, ?)</code>.</p></li><li><p>The <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a> result in applying 
                           <a title="function coercion" class="termref" href="#dt-function-coercion">function coercion</a> 
                           to this function, wrapping it in a new function (<var>M′</var>) with the 
                           signature <code>function(item(), xs:integer) as xs:boolean</code>.</p></li><li><p>When <var>M′</var> is called by <code>fn:filter()</code>, <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion</a> 
                           and SequenceType matching rules are applied to the argument, 
                           resulting in an <code>item()</code> value 
                           (<code>$a</code>) or a type error.</p></li><li><p>
                           The function <code>map:get($m, ?)</code> is called with <code>$a</code>
                           as the argument; this returns either an <code>xs:boolean</code> or the empty sequence
                           (call the result <var>R</var>).</p></li><li><p>
                           The wrapper function <code>$p</code> applies the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>
                              to <var>R</var>. If <var>R</var> is an <code>xs:boolean</code> the matching succeeds. 
                              When it is an empty sequence (in particular, <code>$m</code> does not contain a 
                           key for <code>"Tuesday"</code>), a type error is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>], since the expected type is <code>xs:boolean</code> 
                           and the actual type is an empty sequence.</p></li></ol><p>Consider the following expression:
      </p><div class="exampleInner"><pre>

let $m := {
   "Monday" : true(),
   "Wednesday" : false(),
   "Friday" : true(),
}
let $days := ("Monday", "Wednesday", "Friday")
return filter($days, $m)</pre></div><p>In this case the result of the expression is the sequence <code>("Monday", "Friday")</code>.
                  But if the input sequence included the string <code>"Tuesday"</code>, the filter operation
                  would fail with a type error.
               </p><div class="note"><p class="prefix"><b>Note:</b></p><p>Function coercion applies even if the supplied function matches the required type.</p><p>For example, consider this case:</p><div class="exampleInner"><pre>
declare function local:filter(
  $s as item()*, 
  $p as function(xs:string) as xs:boolean
) as item()* {
  $s[$p(.)]
};

let $f := function($a) { $a mod 2 = 0 }
return local:filter(1 to 10, $f)</pre></div><p>Here the supplied function <code>$f</code> is an instance of the required type,
                  because its signature defaults the argument type to <code>item()*</code>, which
                  is a supertype of <code>xs:string</code>. The expression <code>$s[$p(.)]</code>
                  could in principle succeed. However, function coercion ensures that the supplied function 
                  is wrapped in a function that requires the argument to be of type <code>xs:string</code>,
                  so the call fails with a type error when the wrapping function is invoked supplying an
                  <code>xs:integer</code> as the argument.</p></div></div><div class="div3">
<h4><a id="id-coercion-examples"/>3.4.5 <a href="#id-coercion-examples" style="text-decoration: none">Examples of Coercions</a></h4><p>This section illustrates the effect of the coercion rules with examples.</p><div class="example">
<div class="exampleHeader"><a id="eg-coercion-to-string"/>Example: Coercion to <code>xs:string</code>
                  </div><p>Consider the case where the required type (of a variable, or a function argument)
                  is <code>xs:string</code>. For example, the second argument of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-matches"><code>fn:matches</code></a>,
                  which expects a regular expression. The table below illustrates the values that might be supplied, and
                  the coercions that are applied.</p><table class="medium"><thead><tr><th style="text-align:left">Supplied Value</th><th style="text-align:left">Coercion</th></tr></thead><tbody><tr><td style="vertical-align:top">
                              <code>"[0-9]"</code>
                           </td><td style="vertical-align:top">
                              <p>None; the supplied value is an instance of the required type.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>default-language()</code>
                           </td><td style="vertical-align:top">
                              <p>None; the supplied value is an instance of <code>xs:language</code>, which
                              is a subtype of the required type <code>xs:string</code>.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>&lt;a&gt;[0-9]&lt;/a&gt;</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied element node is atomized. Unless it has been schema-validated,
                           the typed value will be an instance of <code>xs:untypedAtomic</code>, which
                           is accepted when the required type is <code>xs:string</code>.</p>
                              <p>Supplying an element whose type annotation is (say) <code>xs:date</code> 
                                 will fail with a type error.</p>
                              <p class="xpath">The effect is subtly different if XPath 1.0
                                 compatibility mode is enabled. In this case coercion takes the string
                              value of the element node. This differs from the typed value only
                              in the case where the element has been schema-validated and has a type
                              annotation other than <code>xs:string</code>.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>xs:anyURI("urn:dummy")</code>
                           </td><td style="vertical-align:top">
                              <p>Supplying an instance of <code>xs:anyURI</code> where the expected type
                              is <code>xs:string</code> is permitted; this is one of the pairs of types
                           where implicit casting is allowed.</p>
                           </td></tr><tr class="xpath"><td style="vertical-align:top">
                              <code>17.2</code>
                           </td><td style="vertical-align:top">
                              <p>Supplying a number where a string is expected raises a type error.</p>
                              <p class="xpath">However, if XPath 1.0
                              compatibility mode is enabled, the number is converted to a string as if
                           by the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-string"><code>fn:string</code></a> function.</p>
                           </td></tr><tr class="xpath"><td style="vertical-align:top">
                              <code>//author/@id</code>
                           </td><td style="vertical-align:top">
                              <p>Supplying a sequence of nodes where a single string is expected will raise a type
                              error unless either there is only one node in the sequence. In this case
                              the typed value of the node will be used (this must be of type
                              <code>xs:string</code>, <code>xs:untypedAtomic</code>, or <code>xs:anyURI</code>).</p>
                              <p class="xpath">If XPath 1.0 compatibility mode is enabled, however, 
                                 all strings after the first are discarded, and the string value of 
                                 the first node is used; if the sequence is empty, a zero-length string is supplied.</p>
                           </td></tr><tr class="xpath"><td style="vertical-align:top">
                              <code>("red", "green", "blue")</code>
                           </td><td style="vertical-align:top">
                              <p>Supplying a sequence of strings where a single string is expected 
                              raises a type error.</p>
                              <p class="xpath">If XPath 1.0
                              compatibility mode is enabled, however, all strings after the first are discarded; the effect
                              is as if the supplied value were <code>"red"</code>.</p>
                           </td></tr><tr class="xpath"><td style="vertical-align:top">
                              <code>()</code>
                           </td><td style="vertical-align:top">
                              <p>Supplying an empty sequence where a single string is expected will fail.</p>
                              <p class="xpath">If XPath 1.0 compatibility mode is enabled, however, the value is coerced by
                                 applying the function <code>fn:string(())</code>,
                                 which delivers the zero-length string.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>["a|b"]</code>
                           </td><td style="vertical-align:top">
                              <p>Supplying an array holding a single string succeeds, because the rules cause the
                              array to be atomized, and the value after atomization is a single string.</p>
                              <p>Supplying an array holding multiple strings would fail.</p>
                              <p class="xpath">In XPath 1.0 compatibility mode, supplying an array will fail, 
                                 regardless of the array contents, because the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-string"><code>fn:string</code></a> function does not 
                                 accept arrays.</p>
                           </td></tr></tbody></table></div><p> </p><div class="example">
<div class="exampleHeader"><a id="eg-coercion-to-decimal"/>Example: Coercion to <code>xs:decimal?</code>
                  </div><p>Consider the case where the required type (of a variable, or a function argument)
                     is <code>xs:decimal?</code>. For example, the first argument of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-seconds"><code>fn:seconds</code></a>,
                     which expects a decimal number of seconds. The table below illustrates the values that might be supplied, and
                     the coercions that are applied.</p><table class="medium"><thead><tr><th style="text-align:left">Supplied Value</th><th style="text-align:left">Coercion</th></tr></thead><tbody><tr><td style="vertical-align:top">
                              <code>12.4</code>
                           </td><td style="vertical-align:top">
                              <p>None; the supplied value is an instance of the required type.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>()</code>
                           </td><td style="vertical-align:top">
                              <p>None; an empty sequence is an instance of the required type.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>42</code>
                           </td><td style="vertical-align:top">
                              <p>None; the supplied value is an instance of <code>xs:integer</code>,
                              which is a subtype of the required type.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>math:pi()</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is an instance of <code>xs:double</code>,
                              which can be converted to <code>xs:decimal</code> under the coercion rules.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>("a", "b")[.="c"]</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is an empty sequence, which is a valid
                              instance of the required type <code>xs:decimal?</code>. However,
                           the processor may (optionally) reject this as an implausible coercion,
                           on the grounds that it can only succeed in one special case, namely
                           where the filter expression selects no values. </p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>(1.5, 2.5, 3.5)</code>
                           </td><td style="vertical-align:top">
                              <p>A type error is raised<span class="xpath">, except in the case where XPath 1.0
                           compatibility is enabled, in which case all values after the first are discarded</span>.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>&lt;a&gt;3.14159&lt;/a&gt;</code>
                           </td><td style="vertical-align:top">
                              <p>The element node is atomized; unless it has been schema-validated, the
                              result will be <code>"3.14159"</code> as an instance of <code>xs:untypedAtomic</code>.
                             This is converted to an instance of <code>xs:decimal</code> following the rules
                           of the <code>cast as</code> operator.</p>
                           </td></tr><tr class="xpath"><td style="vertical-align:top">
                              <code>"12.2"</code>
                           </td><td style="vertical-align:top">
                              <p>Supplying a string where an <code>xs:decimal</code> is a type error<span class="xpath">,
                              even if XPath 1.0 compatibility mode is enabled. The rules for compatibility
                              mode would allow conversion if the required type were <code>xs:double</code>,
                              but not for <code>xs:decimal</code>
                                 </span>.
                        </p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>[1.5]</code>
                           </td><td style="vertical-align:top">
                              <p>The array is atomized, and the result is a valid instance of the required
                           type <code>xs:decimal?</code>
                              </p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>[]</code>
                           </td><td style="vertical-align:top">
                              <p>The array is atomized, and the result is an empty sequence, which is a valid instance of the required
                              type <code>xs:decimal?</code>
                              </p>
                           </td></tr></tbody></table></div><p> </p><div class="example">
<div class="exampleHeader"><a id="eg-coercion-to-positive-integer"/>Example: Coercion to <code>xs:positive-integer</code>
                  </div><p>Consider the case where the required type (of a variable, or a function argument)
                     is <code>xs:positive-integer</code>. The table below illustrates the values that might be supplied, and
                     the coercions that are applied.</p><table class="medium"><thead><tr><th style="text-align:left">Supplied Value</th><th style="text-align:left">Coercion</th></tr></thead><tbody><tr><td style="vertical-align:top">
                              <code>12</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is of type <code>xs:integer</code>. Because the supplied value and
                              the required type, <code>xs:positiveInteger</code>, both come under the primitive
                           type <code>xs:decimal</code>, and the value <code>12</code> is within the value space
                           of <code>xs:positiveInteger</code>, the value is relabeled as an <code>xs:positiveInteger</code>
                           and the call succeeds.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>12.1</code>
                           </td><td style="vertical-align:top">
                              <p>This fails with a type error, because the <code>xs:decimal</code> value <code>12.1</code>
                              is not a value in the value space of <code>xs:positiveInteger</code>. This is so even though
                           casting to <code>xs:positiveInteger</code> would succeed.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>math:pi()</code>
                           </td><td style="vertical-align:top">
                              <p>This fails with a type error. A value of type <code>xs:double</code> is accepted
                           where the required type is <code>xs:decimal</code> or <code>xs:float</code>,
                           but not where it is <code>xs:positiveInteger</code>.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>&lt;a&gt;1200&lt;/a&gt;</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied element node is atomized. If the element has not been schema-validated,
                           the result will be an <code>xs:untypedAtomic</code> item, which is successfully cast to the
                           required type <code>xs:positiveInteger</code>. If the element has been validated against a schema,
                           then coercion succeeds if the typed value would itself be acceptable, for example if it
                           is an <code>xs:positiveInteger</code>, or some other <code>xs:decimal</code> within the value space
                           of <code>xs:positiveInteger</code>.</p>
                           </td></tr></tbody></table></div><p> </p><div class="example">
<div class="exampleHeader"><a id="eg-coercion-to-union"/>Example: Coercion to a union type</div><p>Consider the first parameter of the function <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-char"><code>fn:char</code></a>, whose declared
                  type is <code>(xs:string | xs:positiveInteger)</code>. The rules are the same
                  as if it were a union typed declared in an imported schema.</p><table class="medium"><thead><tr><th style="text-align:left">Supplied Value</th><th style="text-align:left">Coercion</th></tr></thead><tbody><tr><td style="vertical-align:top">
                              <code>"amp"</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is of type <code>xs:string</code>, which is one of the allowed
                           types. The call therefore succeeds.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>"#"</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is of type <code>xs:string</code>, which is one of the allowed
                              types. As far as the coercion rules are concerned, the call therefore succeeds. Under the
                              semantic rules for the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-char"><code>fn:char</code></a> function, however, this value is not accepted;
                              a dynamic error (as distinct from a type error) is therefore raised.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>0x25</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is of type <code>xs:integer</code>. Although this is not one of the allowed
                              types, it is acceptable because coercion of the value to type <code>xs:positiveInteger</code>
                              succeeds. The value is relabeled as an instance of <code>xs:positiveInteger</code>.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>&lt;a&gt;0x25&lt;/a&gt;</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied element node is atomized. Assuming that the node has not been schema-validated,
                              the result is an instance of <code>xs:untypedAtomic</code>. The member types of the choice
                              are tested in order. Conversion to <code>xs:string</code> with the value "0x25" succeeds, so 
                              the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-char"><code>fn:char</code></a> function is called supplying this string; but the function rejects this
                              string as semantically invalid. The same would happen if the value were, say, &lt;a&gt;37&lt;/a&gt;.
                              Supplying such a value requires an explicit cast, for example <code>fn:char( xs:positiveInteger( ./a ))</code>.</p>
                           </td></tr></tbody></table></div><p> </p><div class="example">
<div class="exampleHeader"><a id="eg-coercion-to-choice"/>Example: Coercion to a choice type</div><p>Suppose the required type is <code>(record(x as xs:decimal, y as xs:decimal, *) | record(size as enum("S", "M", "L", "XL"), *))</code>.</p><table class="medium"><col style="width:40%" span="1"/><col style="width:60%" span="1"/><thead><tr><th style="text-align:left">Supplied Value</th><th style="text-align:left">Coercion</th></tr></thead><tbody><tr><td style="vertical-align:top">
                              <code>{ "x": 1, "y": 2, "z": 3 }</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is an instance of the first record type: no coercion is necessary.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>{ "size": "M" }</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is an instance of the second record type: no coercion is necessary.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>{ "x": 1, "y": 2, "size": "XL" }</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is an instance of both record types: no coercion is necessary.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>{ "x": 1e0, "y": 2e0, "size": "XL" }</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is not an instance of the first record type because the fields are of
                              type <code>xs:double</code> rather than <code>xs:decimal</code>. It is however an instance
                              of the second record type. It is therefore accepted <em>as is</em>; the fields
                           <code>x</code> and <code>y</code> are not converted from <code>xs:double</code> to
                           <code>xs:decimal</code>.</p>
                           </td></tr><tr><td style="vertical-align:top">
                              <code>{ "x": 1e0, "y": 2e0, "size": "XXL" }</code>
                           </td><td style="vertical-align:top">
                              <p>The supplied value is not an instance of the first record type because the fields are of
                              type <code>xs:double</code> rather than <code>xs:decimal</code>, and it is not an instance
                              of the second record type because the <code>size</code> value does not match the enumeration
                              type. Coercion is therefore attempted to the first record type, and succeeds. The <code>x</code>
                           and <code>y</code> fields are coerced to <code>xs:decimal</code>, and the <code>size</code> field
                           is accepted <em>as is</em>.</p>
                           </td></tr></tbody></table></div></div></div><div class="div2">
<h3><a id="id-predefined-types"/>3.5 <a href="#id-predefined-types" style="text-decoration: none">Schema Types</a></h3><p>
               [<a id="dt-schema-type" title="schema type">Definition</a>: A <b>schema type</b>
            is a complex type or simple type as defined in the <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a> specifications, including built-in types as well as user-defined types.]
            </p><p>Every schema type is either a <b>complex type</b> or a
            <b>simple type</b>; simple types are further subdivided into <b>list types</b>, <b>union
               types</b>, and <b>atomic types</b> (see <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a> for definitions and explanations of these terms.)</p><p>A schema type can appear as a type annotation on an
            element or attribute node. The type annotation on an element node can be
            a complex type or a simple type; the type annotation on an attribute node
            is always a simple type. Non-instantiable types such as <code>xs:NOTATION</code> or
            <code>xs:anyAtomicType</code> never appear as type annotations, but their derived
            types can be so used. Union types never appear as type annotations; when
            an element or attribute is validated against a union type, the resulting
            type annotation will be one of the types in the transitive membership of 
            the union type.</p><p>
               [<a id="dt-atomic-type" title="atomic type">Definition</a>: An <b>atomic type</b>
            is a simple <a title="schema type" class="termref" href="#dt-schema-type">schema type</a> whose 
            <a href="https://www.w3.org/TR/xmlschema11-1/#std-variety">{variety}<sup><small>XS11-1</small></sup></a>
            is <code>atomic</code>.]
            </p><p>An <b>atomic type</b> is either a built-in atomic
            type (defined either in the XSD specification or in this specification), or
            it is a user-defined atomic type included in an imported schema.</p><p class="xpath">The <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope schema types</a>
            in the <a title="static context" class="termref" href="#dt-static-context">static
               context</a> are initialized with a set of
            predefined schema types that is determined by the host
            language. This set may include some or all of the
            schema types in the
            namespace
            <code>http://www.w3.org/2001/XMLSchema</code>,
            represented in this document by the namespace prefix
            <code>xs</code>. The schema types in this namespace are defined in <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a>
            and augmented by additional types defined in <a href="#xpath-datamodel-40">[XQuery and XPath Data Model (XDM) 4.0]</a>. An implementation
            that has based its type system on <a href="#XMLSchema10">[XML Schema 1.0]</a> is not required to support the <code>xs:dateTimeStamp</code> or <code>xs:error</code> types.</p><p>The schema types defined in  <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#types-predefined">Section 4.1.4 Predefined Types</a><sup><small>DM</small></sup> are summarized below.</p><ol class="enumar"><li><p>
                     [<a id="dt-untyped" title="xs:untyped">Definition</a>: 
                        <code>xs:untyped</code> is  used as the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of an element node that has not been validated, or has been validated in <code>skip</code> mode.] 
                  No predefined schema types are derived from <code>xs:untyped</code>.</p></li><li><p>
                     [<a id="dt-untypedAtomic" title="xs:untypedAtomic">Definition</a>: 
                        <code>xs:untypedAtomic</code>
                     is an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a> that is used to denote untyped atomic data, 
                     such as text that has not been assigned a more specific type.] 
                  An attribute that has been validated in <code>skip</code> mode is represented in the <a title="data model" class="termref" href="#dt-datamodel">data model</a> by an attribute node with the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>
                     <code>xs:untypedAtomic</code>. No predefined schema types are derived from <code>xs:untypedAtomic</code>.</p></li><li><p>
                     [<a id="dt-dayTimeDuration" title="xs:dayTimeDuration">Definition</a>: 
                        <code>xs:dayTimeDuration</code> is derived by restriction from <code>xs:duration</code>. The  lexical representation of <code>xs:dayTimeDuration</code>
                     is restricted to contain only day, hour, minute, and second
                     components.]
                  </p></li><li><p>
                     [<a id="dt-yearMonthDuration" title="xs:yearMonthDuration">Definition</a>: 
                        <code>xs:yearMonthDuration</code> is derived by restriction from <code>xs:duration</code>. The lexical representation of <code>xs:yearMonthDuration</code> is
                     restricted to contain only year and month
                     components.]
                  </p></li><li><p>
                     [<a id="dt-anyAtomicType" title="xs:anyAtomicType">Definition</a>: 
                        <code>xs:anyAtomicType</code> is an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a> 
                     that includes all atomic items (and no values that
                     are not atomic). Its base type is
                     <code>xs:anySimpleType</code> from which all simple types, including atomic,
                     list, and union types, are derived. All primitive atomic types, such as
                     <code>xs:decimal</code> and <code>xs:string</code>, have <code>xs:anyAtomicType</code> as their base type.]
                  </p><div class="note"><p class="prefix"><b>Note:</b></p><p>
                        <code>xs:anyAtomicType</code>  will not appear as the type of an actual value in an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instance</a>.</p></div></li><li><p>
                     [<a id="dt-xs-error" title="xs:error">Definition</a>: 
                        <code>xs:error</code> is a simple type with no value space.  It is defined in <a href="https://www.w3.org/TR/xmlschema11-1/#xsd-error">Section 
                  
                     3.16.7.3 xs:error
                  
               </a><sup><small>XS11-1</small></sup> and  can be used in the <a href="#id-sequencetype-syntax"><b>3.1 Sequence Types</b></a> to raise errors.]
                  </p></li></ol><p>The relationships among the schema types in the <code>xs</code> namespace are illustrated in Figure 2. A more complete description of the XPath 4.0 type hierarchy can be found in 
            <a href="https://qt4cg.org/specifications/xpath-functions-40/#datatypes">Section 1.8 Type System</a><sup><small>FO</small></sup>.</p><img src="types.jpg" alt="Type Hierarchy Diagram"/><p>Figure 2: Hierarchy of Schema Types used in XPath 4.0.</p></div></div><div class="div1">
<h2><a id="id-expressions"/>4 <a href="#id-expressions" style="text-decoration: none">Expressions</a></h2><p>This section discusses each of the basic kinds of expression. Each kind of expression has a name such as <code>PathExpr</code>, which is introduced on the left side of the grammar production that defines the expression. Since XPath 4.0 is a composable language, each kind of expression is defined in terms of other expressions whose operators have a higher precedence. In this way, the precedence of operators is represented explicitly in the grammar.</p><p>The order in which expressions are discussed in this document does not reflect the order of operator precedence. In general, this document introduces the simplest kinds of expressions first, followed by more complex expressions.  For the complete grammar, see Appendix [<a href="#nt-bnf"><b>A XPath 4.0 Grammar</b></a>].</p><p>
            <span class="xpath">The highest-level symbol in the XPath grammar is XPath.</span>
         </p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-XPath"/><code><a href="#prod-xpath40-XPath">XPath</a></code></td><td>::=</td><td><code>
                  <a href="#doc-xpath40-XPath-Expr">Expr</a>
               </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-XPath-Expr"/><code><a href="#prod-xpath40-Expr">Expr</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-XPath-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-XPath-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                  <a href="#doc-xpath40-ForExpr">ForExpr</a>
                  <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                  <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                  <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                  <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
               </code></td></tr></tbody></table><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                  <a href="#doc-xpath40-ExprSingle-ForExpr">ForExpr</a>
                  <br/>|  <a href="#doc-xpath40-ExprSingle-LetExpr">LetExpr</a>
                  <br/>|  <a href="#doc-xpath40-ExprSingle-QuantifiedExpr">QuantifiedExpr</a>
                  <br/>|  <a href="#doc-xpath40-ExprSingle-IfExpr">IfExpr</a>
                  <br/>|  <a href="#doc-xpath40-ExprSingle-OrExpr">OrExpr</a>
               </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ExprSingle-ForExpr"/><code><a href="#prod-xpath40-ForExpr">ForExpr</a></code></td><td>::=</td><td><code>
                  <a href="#prod-xpath40-ForClause">ForClause</a>
                  <a href="#prod-xpath40-ForLetReturn">ForLetReturn</a>
               </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ExprSingle-LetExpr"/><code><a href="#prod-xpath40-LetExpr">LetExpr</a></code></td><td>::=</td><td><code>
                  <a href="#prod-xpath40-LetClause">LetClause</a>
                  <a href="#prod-xpath40-ForLetReturn">ForLetReturn</a>
               </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ExprSingle-QuantifiedExpr"/><code><a href="#prod-xpath40-QuantifiedExpr">QuantifiedExpr</a></code></td><td>::=</td><td><code>("some"  |  "every")  (<a href="#prod-xpath40-QuantifierBinding">QuantifierBinding</a> ++ ",")  "satisfies"  <a href="#doc-xpath40-ExprSingle">ExprSingle</a>
               </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ExprSingle-IfExpr"/><code><a href="#prod-xpath40-IfExpr">IfExpr</a></code></td><td>::=</td><td><code>"if"  "("  <a href="#prod-xpath40-Expr">Expr</a>  ")"  (<a href="#prod-xpath40-UnbracedActions">UnbracedActions</a>  |  <a href="#prod-xpath40-BracedAction">BracedAction</a>)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ExprSingle-OrExpr"/><code><a href="#prod-xpath40-OrExpr">OrExpr</a></code></td><td>::=</td><td><code>
                  <a href="#prod-xpath40-AndExpr">AndExpr</a>  ("or"  <a href="#prod-xpath40-AndExpr">AndExpr</a>)*</code></td></tr></tbody></table><p>The XPath 4.0 operator that has lowest precedence is the <a title="comma operator" class="termref" href="#dt-comma-operator">comma operator</a>, which is used to combine two operands to form a sequence. 
         As shown in the grammar, a general expression (<a href="#prod-xpath40-Expr">Expr</a>) can consist of multiple <a href="#doc-xpath40-ExprSingle">ExprSingle</a> operands, separated by commas.</p><p>The name <a href="#doc-xpath40-ExprSingle">ExprSingle</a> denotes an expression that does not contain a top-level <a title="comma operator" class="termref" href="#dt-comma-operator">comma operator</a> (despite its name, an <a href="#doc-xpath40-ExprSingle">ExprSingle</a> may evaluate to a sequence containing more than one item.)</p><p>The symbol <a href="#doc-xpath40-ExprSingle">ExprSingle</a> is used in various places in the grammar where an expression 
         is not allowed to contain a top-level comma. For example, each of the arguments 
         of a function call must be a <a href="#doc-xpath40-ExprSingle">ExprSingle</a>, because commas are 
         used to separate the arguments of a function call.</p><p>After the comma, the expressions that have next lowest precedence are

         <span class="xpath">
               <a href="#doc-xpath40-ForExpr">ForExpr</a>, <a href="#doc-xpath40-LetExpr">LetExpr</a>, </span>
            <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>,

         <a href="#doc-xpath40-IfExpr">IfExpr</a>,

and <a href="#doc-xpath40-OrExpr">OrExpr</a>. Each of these expressions is described in a separate section of this document.</p><div class="div2">
<h3><a id="comments"/>4.1 <a href="#comments" style="text-decoration: none">Comments</a></h3><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-Comment"/><code><a href="#prod-xpath40-Comment">Comment</a></code></td><td>::=</td><td><code>"(:"  (<a href="#doc-xpath40-Comment-CommentContents">CommentContents</a>  |  <a href="#doc-xpath40-Comment">Comment</a>)*  ":)"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#ws-explicit">ws: explicit</a>
                   */</td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-comments">gn: comments</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-Comment-CommentContents"/><code><a href="#prod-xpath40-CommentContents">CommentContents</a></code></td><td>::=</td><td><code>(<a href="#prod-xpath40-Char">Char</a>+ - (Char* ('(:' | ':)') Char*))</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#ws-explicit">ws: explicit</a>
                   */</td></tr></tbody></table><p>Comments may be used to provide information relevant to programmers who read 
            <span class="xpath">an expression</span>. Comments are lexical constructs only, and do not affect  
            <span class="xpath">expression</span> processing.</p><p>Comments are strings, delimited by the symbols <code>(:</code> and <code>:)</code>. Comments may be nested.</p><p>A comment may be used anywhere <a title="ignorable whitespace" class="termref" href="#IgnorableWhitespace">ignorable whitespace</a> is allowed (see <a href="#DefaultWhitespaceHandling"><b>A.3.5.1 Default Whitespace Handling</b></a>).</p><p>The following is an example of a comment:</p><div class="exampleInner"><pre>(: Houston, we have a problem :)</pre></div></div><div class="div2">
<h3><a id="id-primary-expressions"/>4.2 <a href="#id-primary-expressions" style="text-decoration: none">Primary Expressions</a></h3><p>
               [<a id="dt-primary-expression" title="primary expression">Definition</a>: 
                  <b>Primary expressions</b> are the basic primitives of the
	 language. They include literals, variable references, context value references,  and function calls. A primary expression may also be created by enclosing any expression in parentheses, which is sometimes helpful in controlling the precedence of operators.]
            Map and Array Constructors are described in <a href="#id-maps"><b>4.13.1 Maps</b></a> and <a href="#id-arrays"><b>4.13.2 Arrays</b></a>.

</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr"/><code><a href="#prod-xpath40-PrimaryExpr">PrimaryExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PrimaryExpr-Literal">Literal</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-VarRef">VarRef</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-ParenthesizedExpr">ParenthesizedExpr</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-ContextValueRef">ContextValueRef</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-FunctionCall">FunctionCall</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-FunctionItemExpr">FunctionItemExpr</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-MapConstructor">MapConstructor</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-ArrayConstructor">ArrayConstructor</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-StringTemplate">StringTemplate</a>
                     <br/>|  <a href="#doc-xpath40-PrimaryExpr-UnaryLookup">UnaryLookup</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-Literal"/><code><a href="#prod-xpath40-Literal">Literal</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-NumericLiteral">NumericLiteral</a>  |  <a href="#doc-xpath40-StringLiteral">StringLiteral</a>  |  <a href="#doc-xpath40-QNameLiteral">QNameLiteral</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-VarRef"/><code><a href="#prod-xpath40-VarRef">VarRef</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-EQName">EQName</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-ParenthesizedExpr"/><code><a href="#prod-xpath40-ParenthesizedExpr">ParenthesizedExpr</a></code></td><td>::=</td><td><code>"("  <a href="#prod-xpath40-Expr">Expr</a>?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-ContextValueRef"/><code><a href="#prod-xpath40-ContextValueRef">ContextValueRef</a></code></td><td>::=</td><td><code>"."</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-FunctionCall"/><code><a href="#prod-xpath40-FunctionCall">FunctionCall</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-EQName">EQName</a>
                     <a href="#prod-xpath40-ArgumentList">ArgumentList</a>
                  </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                   */</td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-parens">gn: parens</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-FunctionItemExpr"/><code><a href="#prod-xpath40-FunctionItemExpr">FunctionItemExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PrimaryExpr-NamedFunctionRef">NamedFunctionRef</a>  |  <a href="#doc-xpath40-PrimaryExpr-InlineFunctionExpr">InlineFunctionExpr</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-NamedFunctionRef"/><code><a href="#prod-xpath40-NamedFunctionRef">NamedFunctionRef</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-EQName">EQName</a>  "#"  <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>
                  </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-InlineFunctionExpr"/><code><a href="#prod-xpath40-InlineFunctionExpr">InlineFunctionExpr</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-MethodAnnotation">MethodAnnotation</a>*  ("function"  |  "fn")  <a href="#prod-xpath40-FunctionSignature">FunctionSignature</a>?  <a href="#prod-xpath40-FunctionBody">FunctionBody</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-MapConstructor"/><code><a href="#prod-xpath40-MapConstructor">MapConstructor</a></code></td><td>::=</td><td><code>"map"?  "{"  (<a href="#prod-xpath40-MapConstructorEntry">MapConstructorEntry</a> ** ",")  "}"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-ArrayConstructor"/><code><a href="#prod-xpath40-ArrayConstructor">ArrayConstructor</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-SquareArrayConstructor">SquareArrayConstructor</a>  |  <a href="#prod-xpath40-CurlyArrayConstructor">CurlyArrayConstructor</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-StringTemplate"/><code><a href="#prod-xpath40-StringTemplate">StringTemplate</a></code></td><td>::=</td><td><code>"`"  (<a href="#prod-xpath40-StringTemplateFixedPart">StringTemplateFixedPart</a>  |  <a href="#prod-xpath40-StringTemplateVariablePart">StringTemplateVariablePart</a>)*  "`"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#ws-explicit">ws: explicit</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PrimaryExpr-UnaryLookup"/><code><a href="#prod-xpath40-UnaryLookup">UnaryLookup</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-Lookup">Lookup</a>
                  </code></td></tr></tbody></table><div class="div3">
<h4><a id="id-literals"/>4.2.1 <a href="#id-literals" style="text-decoration: none">Literals</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-Literal"/><code><a href="#prod-xpath40-Literal">Literal</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-Literal-NumericLiteral">NumericLiteral</a>  |  <a href="#doc-xpath40-Literal-StringLiteral">StringLiteral</a>  |  <a href="#doc-xpath40-Literal-QNameLiteral">QNameLiteral</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-Literal-NumericLiteral"/><code><a href="#prod-xpath40-NumericLiteral">NumericLiteral</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>  |  <a href="#prod-xpath40-HexIntegerLiteral">HexIntegerLiteral</a>  |  <a href="#prod-xpath40-BinaryIntegerLiteral">BinaryIntegerLiteral</a>  |  <a href="#prod-xpath40-DecimalLiteral">DecimalLiteral</a>  |  <a href="#prod-xpath40-DoubleLiteral">DoubleLiteral</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-Literal-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-Literal-QNameLiteral"/><code><a href="#prod-xpath40-QNameLiteral">QNameLiteral</a></code></td><td>::=</td><td><code>"#"  <a href="#doc-xpath40-EQName">EQName</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody></table><p>
                  [<a id="dt-literal" title="literal">Definition</a>: A <b>literal</b> is a direct syntactic representation of an
		atomic item.] XPath 4.0 supports three kinds of literals: numeric literals,
		string literals, and QName literals.</p><div class="div4">
<h5><a id="id-numeric-literals"/>4.2.1.1 <a href="#id-numeric-literals" style="text-decoration: none">Numeric Literals</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-qname-literals">⬇</a> <a href="#id-function-coercion">⬆</a></p><ol><li><p>
                     Numeric literals can now be written in hexadecimal or binary notation; 
                     and underscores can be included for readability.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/429">429</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/433">433</a> 25 April 2023]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral"/><code><a href="#prod-xpath40-NumericLiteral">NumericLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NumericLiteral-IntegerLiteral">IntegerLiteral</a>  |  <a href="#doc-xpath40-NumericLiteral-HexIntegerLiteral">HexIntegerLiteral</a>  |  <a href="#doc-xpath40-NumericLiteral-BinaryIntegerLiteral">BinaryIntegerLiteral</a>  |  <a href="#doc-xpath40-NumericLiteral-DecimalLiteral">DecimalLiteral</a>  |  <a href="#doc-xpath40-NumericLiteral-DoubleLiteral">DoubleLiteral</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-IntegerLiteral"/><code><a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NumericLiteral-Digits">Digits</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-Digits"/><code><a href="#prod-xpath40-Digits">Digits</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NumericLiteral-DecDigit">DecDigit</a>  ((<a href="#doc-xpath40-NumericLiteral-DecDigit">DecDigit</a>  |  "_")*  <a href="#doc-xpath40-NumericLiteral-DecDigit">DecDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-DecDigit"/><code><a href="#prod-xpath40-DecDigit">DecDigit</a></code></td><td>::=</td><td><code>[0-9]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-HexIntegerLiteral"/><code><a href="#prod-xpath40-HexIntegerLiteral">HexIntegerLiteral</a></code></td><td>::=</td><td><code>"0x"  <a href="#doc-xpath40-NumericLiteral-HexDigits">HexDigits</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-HexDigits"/><code><a href="#prod-xpath40-HexDigits">HexDigits</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NumericLiteral-HexDigit">HexDigit</a>  ((<a href="#doc-xpath40-NumericLiteral-HexDigit">HexDigit</a>  |  "_")*  <a href="#doc-xpath40-NumericLiteral-HexDigit">HexDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-HexDigit"/><code><a href="#prod-xpath40-HexDigit">HexDigit</a></code></td><td>::=</td><td><code>[0-9a-fA-F]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-BinaryIntegerLiteral"/><code><a href="#prod-xpath40-BinaryIntegerLiteral">BinaryIntegerLiteral</a></code></td><td>::=</td><td><code>"0b"  <a href="#doc-xpath40-NumericLiteral-BinaryDigits">BinaryDigits</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-BinaryDigits"/><code><a href="#prod-xpath40-BinaryDigits">BinaryDigits</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NumericLiteral-BinaryDigit">BinaryDigit</a>  ((<a href="#doc-xpath40-NumericLiteral-BinaryDigit">BinaryDigit</a>  |  "_")*  <a href="#doc-xpath40-NumericLiteral-BinaryDigit">BinaryDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-BinaryDigit"/><code><a href="#prod-xpath40-BinaryDigit">BinaryDigit</a></code></td><td>::=</td><td><code>[01]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-DecimalLiteral"/><code><a href="#prod-xpath40-DecimalLiteral">DecimalLiteral</a></code></td><td>::=</td><td><code>("."  <a href="#doc-xpath40-NumericLiteral-Digits">Digits</a>)  |  (<a href="#doc-xpath40-NumericLiteral-Digits">Digits</a>  "."  <a href="#doc-xpath40-NumericLiteral-Digits">Digits</a>?)</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NumericLiteral-DoubleLiteral"/><code><a href="#prod-xpath40-DoubleLiteral">DoubleLiteral</a></code></td><td>::=</td><td><code>(("."  <a href="#doc-xpath40-NumericLiteral-Digits">Digits</a>)  |  (<a href="#doc-xpath40-NumericLiteral-Digits">Digits</a>  ("."  <a href="#doc-xpath40-NumericLiteral-Digits">Digits</a>?)?))  [eE]  [+-]?  <a href="#doc-xpath40-NumericLiteral-Digits">Digits</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody></table><p>The value of a numeric literal is determined as follows (taking the rules in order):</p><ol class="enumar"><li><p>Underscore characters are stripped out. Underscores may be included in a numeric
                  literal to aid readability, but have no effect on the value. For example, <code>1_000_000</code>
                  is equivalent to <code>1000000</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Underscores must not appear at the beginning or end of a sequence of digits, only
                  in intermediate positions. Multiple adjacent underscores are allowed.</p></div></li><li><p>A <code>HexIntegerLiteral</code> represents a non-negative integer
                      expressed in hexadecimal: for example <code>0xffff</code> represents the integer 65535, and
                     <code>0xFFFF_FFFF</code> represents the integer 4294967295.</p></li><li><p>A <code>BinaryIntegerLiteral</code> represents a non-negative integer
                     expressed in binary: for example <code>0b101</code> represents the integer 5, and
                     <code>0b1111_1111</code> represents the integer 255.</p></li><li><p>The value of a <b>numeric literal</b> containing no <code>.</code> and 
                     no <code>e</code> or <code>E</code> character is an atomic item of type <code>xs:integer</code>;
                     the value is obtained by casting from <code>xs:string</code> to <code>xs:integer</code> as specified in
                     <a href="https://qt4cg.org/specifications/xpath-functions-40/#casting-from-strings">Section 22.2 Casting from xs:string and xs:untypedAtomic</a><sup><small>FO</small></sup>.</p></li><li><p>The value of a numeric literal containing <code>.</code> but no <code>e</code> or <code>E</code> 
                     character is an atomic item of type <code>xs:decimal</code>;
                     the value is obtained by casting from <code>xs:string</code> to <code>xs:decimal</code> as specified in
                     <a href="https://qt4cg.org/specifications/xpath-functions-40/#casting-from-strings">Section 22.2 Casting from xs:string and xs:untypedAtomic</a><sup><small>FO</small></sup>.</p></li><li><p>The value of a numeric literal 
                     containing an <code>e</code> or <code>E</code> character is an atomic item of type 
                     <code>xs:double</code>;
                     the value is obtained by casting from <code>xs:string</code> to <code>xs:double</code> as specified in
                     <a href="https://qt4cg.org/specifications/xpath-functions-40/#casting-from-strings">Section 22.2 Casting from xs:string and xs:untypedAtomic</a><sup><small>FO</small></sup>.</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>The value of a numeric literal is always non-negative. An expression may
                  appear to include a negative number such as <code>-1</code>, but this is technically
                  an arithmetic expression comprising a unary minus operator followed by a numeric literal.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The effect of the above rules is that in the case of an integer or decimal literal, a dynamic error [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFOAR0002" title="err:FOAR0002">err:FOAR0002</a>]<sup><small>FO40</small></sup> will generally be raised if the literal is outside the range of values supported by the implementation (other options are available: see <a href="https://qt4cg.org/specifications/xpath-functions-40/#op.numeric">Section 4.2 Arithmetic operators on numeric values</a><sup><small>FO</small></sup> for details.)</p><p class="xpath">The XML Schema specification allows implementations to impose a limit (which
   must not be less than 18 digits) on the size of integer and decimal
   values. The full range of values of built-in subtypes of <code>xs:integer</code>,
   such as <code>xs:long</code> and <code>xs:unsignedLong</code>, can be supported only if the
   limit is 20 digits or higher. Negative numbers such as the minimum
   value of <code>xs:long</code> (<code>-9223372036854775808</code>) are technically unary
   expressions rather than literals, but implementations may prefer to
   ensure that they are expressible.</p></div><p>Here are some examples of numeric literals:</p><ul><li><p>
                           <code>12</code> denotes the <code>xs:integer</code> value twelve.</p></li><li><p>
                           <code>1_000_000</code> denotes the <code>xs:integer</code> value one million.</p></li><li><p>
                           <code>12.5</code> denotes the <code>xs:decimal</code> value twelve and one half.</p></li><li><p>
                           <code>3.14159_26535_89793e0</code>
                  is an <code>xs:double</code> value representing the mathematical constant
                  <var>π</var> to 15 decimal places. </p></li><li><p>
                           <code>125E2</code> denotes the <code>xs:double</code> value twelve thousand, five hundred.</p></li><li><p>
                           <code>0xffff</code> denotes the <code>xs:integer</code> value 65535.</p></li><li><p>
                           <code>0b1000_0001</code> denotes the <code>xs:integer</code> value 129.</p></li></ul></div><div class="div4">
<h5><a id="id-string-literal"/>4.2.1.2 <a href="#id-string-literal" style="text-decoration: none">String Literals</a></h5><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-StringLiteral-AposStringLiteral">AposStringLiteral</a>  |  <a href="#doc-xpath40-StringLiteral-QuotStringLiteral">QuotStringLiteral</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringLiteral-AposStringLiteral"/><code><a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a></code></td><td>::=</td><td><code>"'"  (<a href="#doc-xpath40-StringLiteral-EscapeApos">EscapeApos</a>  |  [^'])*  "'"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringLiteral-EscapeApos"/><code><a href="#prod-xpath40-EscapeApos">EscapeApos</a></code></td><td>::=</td><td><code>"''"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringLiteral-QuotStringLiteral"/><code><a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a></code></td><td>::=</td><td><code>'"'  (<a href="#doc-xpath40-StringLiteral-EscapeQuot">EscapeQuot</a>  |  [^"])*  '"'</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringLiteral-EscapeQuot"/><code><a href="#prod-xpath40-EscapeQuot">EscapeQuot</a></code></td><td>::=</td><td><code>'""'</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody></table><p>The value of a <b>string literal</b> is an atomic item whose type is
               <code>xs:string</code> and whose value is the string denoted by the characters between the
		delimiting apostrophes or quotation marks. If the literal is delimited by apostrophes, two adjacent 
		apostrophes within the literal are interpreted as a single apostrophe. Similarly, if the literal 
		is delimited by quotation marks, two adjacent quotation marks within the literal are interpreted 
		as one quotation mark.</p><p>Here are some examples of string literals:</p><ul><li><p>
                           <code>"He said, ""I don't like it."""</code> denotes a string containing two quotation marks and one apostrophe.</p></li><li><p>In XQuery, the string literal <code>"&amp;lt;"</code> denotes a string of length 1 containing the single character
                     <code>"&lt;"</code>. In XPath, the string literal <code>"&amp;lt;"</code> denotes a string of length 4 containing the four 
                     characters <code>"&amp;"</code>, <code>"l"</code>, <code>"t"</code>, <code>";"</code>. (However, when the XPath
                     expression is embedded in an XML document, the sequence <code>"&amp;lt;"</code> will typically have already been converted
                     to <code>"&lt;"</code> by the XML parser.)</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>When XPath or XQuery expressions are embedded in contexts where quotation
                     marks have special significance, such as inside XML attributes, or in string literals in a host language such
                     as Java or C#, then additional
                     escaping may be needed.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Fixed string values can also be written as string templates: 
                  see <a href="#id-string-templates"><b>4.9.2 String Templates</b></a>. A string template with no enclosed
               expressions, such as <code>`Jamaica`</code> evaluates to the same value as
               the string literals <code>"Jamaica"</code> or <code>'Jamaica'</code>. 
               A string template can contain both single and double quotation marks:
               <code>`He said: "I don't like it"`</code>. However, there there are
               some subtle differences:</p><ul><li><p>In string literals, the treatment of character and entity references
                  such as <code>&amp;amp;</code> varies between XQuery and XPath; in string templates,
                  such references are not expanded in either language.</p></li><li><p>String templates can only be used where an expression is expected. String
                  literals are also used in some non-expression contexts, for example in
                  defining an enumeration type: see <a href="#id-enumeration-types"><b>3.2.6 Enumeration Types</b></a>.</p></li><li><p>Curly brackets (<span class="unicode-codepoint">U+007B</span> (<span class="unicode-name">LEFT CURLY BRACKET</span>, <code>{</code>)  and <span class="unicode-codepoint">U+007D</span> (<span class="unicode-name">RIGHT CURLY BRACKET</span>, <code>}</code>) ) and backticks 
                  (<span class="unicode-codepoint">U+0060</span> (<span class="unicode-name">GRAVE ACCENT, BACKTICK</span>, <code>`</code>) ) have a reserved meaning in string templates.</p></li></ul></div></div><div class="div4">
<h5><a id="id-qname-literals"/>4.2.1.3 <a href="#id-qname-literals" style="text-decoration: none">QName Literals</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-filter-expression">⬇</a> <a href="#id-numeric-literals">⬆</a></p><ol><li><p>QName literals are new in 4.0.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1661">1661</a> ]</i></p></li></ol></div><p>A QName literal represents a value of type <code>xs:QName</code>.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QNameLiteral"/><code><a href="#prod-xpath40-QNameLiteral">QNameLiteral</a></code></td><td>::=</td><td><code>"#"  <a href="#doc-xpath40-QNameLiteral-EQName">EQName</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QNameLiteral-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                        </code></td></tr></tbody></table><p>For example, the expression <code>node-name($node) = #xml:space</code>
               returns true if the name of the node <code>$node</code> is the QName with local part <code>space</code>
                  and namespace URI <code>http://www.w3.org/XML/1998/namespace</code>.</p><p>If the <code>EQName</code> is an unprefixed <code>NCName</code>, then it is expanded using
                  the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>. If there
                  is no binding for the prefix in the <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a> then
                  a static error is raised [<a href="#ERRXPST0081" title="err:XPST0081">err:XPST0081</a>].
               </p><div class="note"><p class="prefix"><b>Note:</b></p><p>No whitespace or comment is permitted between the <code>#</code> character
                  and the <code>EQName</code>.</p><p>In XQuery, the character pair <code>(#</code> is recognized as the start of
                     a pragma. In order to ensure that an expression such as <code>error(#err:XPTY0004)</code>
                     is correctly parsed, the rules for pragmas have changed to require whitespace
                     after the opening <code>(#</code> and before the <code>EQName</code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>A <code>QNameLiteral</code> is an expression that evaluates to a single
                  item of type <code>xs:QName</code>; it is thus an alternative to calling the
                  <code>xs:QName</code> constructor function with a literal string argument.
                  This should not be confused with QNames that are used directly in XPath 4.0 to refer to constructs such as
               functions, variables, and elements.</p><p>A <code>QName</code> appearing on its own as an expression,
               for example <code>my:invoice</code>, is an abbreviation for the axis step <code>child::my:step</code>,
               which selects a child element of the context node having this particular element name. A different
               construct is therefore needed to represent an atomic value of type <code>xs:QName</code>.
               For example, the function <code>fn:error</code> expects an <code>xs:QName</code> value
               as its first argument, so (provided that the prefix <code>err</code> is defined in the static
               context) it is possible to use a call such as <code>error( #err:XPTY0004 )</code> to raise an
               error with this error code.</p></div></div><div class="div4">
<h5><a id="id-constants-other-types"/>4.2.1.4 <a href="#id-constants-other-types" style="text-decoration: none">Constants of Other Types</a></h5><p>
               
      The <code>xs:boolean</code> values <code>true</code> and <code>false</code> can be constructed by calls to the
      <a title="system function" class="termref" href="#dt-system-function">system functions</a>
                     <code>fn:true()</code> and <code>fn:false()</code>, respectively.
    </p><p>Values of other simple types can be constructed by calling the <a title="constructor function" class="termref" href="#dt-constructor-function">constructor function</a> for the given type. The constructor functions for XML Schema
		built-in types are defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#constructor-functions-for-xsd-types">Section 21.1 Constructor functions for XML Schema built-in atomic types</a><sup><small>FO</small></sup>. In general, the name of a constructor function for a given type is the same as the name of the type (including its namespace). For
		example:</p><ul><li><p>
                           <code>xs:integer("12")</code> returns the integer value twelve.</p></li><li><p>
                           <code>xs:date("2001-08-25")</code> returns an item whose type is <code>xs:date</code> and whose value represents the date 25th August 2001.</p></li><li><p>
                           <code>xs:dayTimeDuration("PT5H")</code> returns an item whose type is <code>xs:dayTimeDuration</code> and whose value represents a duration of five hours.</p></li></ul><p>Constructor functions can also be used to create special values that have no literal representation, as in the following examples:
</p><ul><li><p>
                              <code>xs:float("NaN")</code> returns the special floating-point value, "Not a Number."</p></li><li><p>
                              <code>xs:double("INF")</code> returns the special double-precision value, "positive infinity."</p></li></ul><p>Constructor functions are available for all simple types,
including union types. For example, if <code>my:dt</code> is a user-defined union
type whose member types are <code>xs:date</code>, <code>xs:time</code>, and <code>xs:dateTime</code>, then
the expression <code>my:dt("2011-01-10")</code> creates an atomic item of type
<code>xs:date</code>. The rules follow XML Schema validation rules for union types:
the effect is to choose the first member type that accepts the given
string in its lexical space.</p><p>It is also possible to construct values of various types by using a <code>cast</code> expression. For example:</p><ul><li><p>
                           <code>9 cast as
                        hatsize</code> returns the atomic item <code>9</code>
			 whose type is  <code>hatsize</code>.</p></li></ul></div></div><div class="div3">
<h4><a id="id-variables"/>4.2.2 <a href="#id-variables" style="text-decoration: none">Variable References</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-VarRef"/><code><a href="#prod-xpath40-VarRef">VarRef</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-VarRef-EQName">EQName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-VarRef-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody></table><p>
                  [<a id="dt-variable-reference" title="variable reference">Definition</a>: A <b>variable reference</b> is an EQName preceded by a $-sign.]
                The variable name is expanded using the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>.
                Two variable references are equivalent if their  <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a>  are equal (as defined by the <code>eq</code> operator). The scope of a variable binding is defined separately for each kind of
expression that can bind variables.</p><p>Every variable reference must match a name in the <a title="in-scope variables" class="termref" href="#dt-in-scope-variables">in-scope variables</a>. </p><p>Every variable binding has a static scope. The scope defines where
references to the variable can validly occur.

It is a <a title="static error" class="termref" href="#dt-static-error">static error</a>
                  [<a href="#ERRXPST0008" title="err:XPST0008">err:XPST0008</a>] to reference a variable that is not in scope. If a variable is bound in the <a title="static context" class="termref" href="#dt-static-context">static context</a> for an expression, that variable is in scope for the entire expression except where it is occluded by another binding that uses the same name within that scope.</p><p>
At evaluation time, the value of a variable reference is the value to which the relevant variable is bound.</p></div><div class="div3">
<h4><a id="id-context-value-references"/>4.2.3 <a href="#id-context-value-references" style="text-decoration: none">Context Value References</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ContextValueRef"/><code><a href="#prod-xpath40-ContextValueRef">ContextValueRef</a></code></td><td>::=</td><td><code>"."</code></td></tr></tbody></table><p>A <b>context value reference</b> evaluates to
              the <a title="context value" class="termref" href="#dt-context-value">context value</a>.</p><p>In many syntactic contexts, the context value will be a single item.
               For example this applies on the right-hand side of the <code>/</code> 
               or <code>!</code> operators, or within a <a href="#prod-xpath40-Predicate">Predicate</a>.</p><p>If the <a title="context value" class="termref" href="#dt-context-value">context value</a> is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>, a context value reference raises a <a title="type error" class="termref" href="#dt-type-error">type error</a>
                  [<a href="#ERRXPDY0002" title="err:XPDY0002">err:XPDY0002</a>].</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Being absent is not the same thing as being empty.</p></div></div><div class="div3">
<h4><a id="id-paren-expressions"/>4.2.4 <a href="#id-paren-expressions" style="text-decoration: none">Parenthesized Expressions</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ParenthesizedExpr"/><code><a href="#prod-xpath40-ParenthesizedExpr">ParenthesizedExpr</a></code></td><td>::=</td><td><code>"("  <a href="#doc-xpath40-ParenthesizedExpr-Expr">Expr</a>?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ParenthesizedExpr-Expr"/><code><a href="#prod-xpath40-Expr">Expr</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-ParenthesizedExpr-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ParenthesizedExpr-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr">ForExpr</a>
                        <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                        <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                        <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                        <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                     </code></td></tr></tbody></table><p>Parentheses may be used to override the precedence rules.
        For example, the expression <code>(2 + 4)
		    * 5</code> evaluates to thirty, since the parenthesized expression <code>(2 + 4)</code> is evaluated first and its result is multiplied by five. Without
			 parentheses, the expression <code>2 + 4 * 5</code> evaluates to twenty-two, because the multiplication operator has higher
			 precedence than the addition operator.</p><p>Empty parentheses are used to denote an empty sequence, as
		described in <a href="#construct_seq"><b>4.7.1 Sequence Concatenation</b></a>.</p></div><div class="div3">
<h4><a id="id-enclosed-expr"/>4.2.5 <a href="#id-enclosed-expr" style="text-decoration: none">Enclosed Expressions</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-EnclosedExpr"/><code><a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a></code></td><td>::=</td><td><code>"{"  <a href="#doc-xpath40-EnclosedExpr-Expr">Expr</a>?  "}"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-EnclosedExpr-Expr"/><code><a href="#prod-xpath40-Expr">Expr</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-EnclosedExpr-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-EnclosedExpr-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-ForExpr">ForExpr</a>
                           <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                           <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                           <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                           <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                        </code></td></tr></tbody></table><p>
                  [<a id="dt-enclosed-expression" title="enclosed expression">Definition</a>: An <b>enclosed expression</b> is an instance of the <a href="#doc-xpath40-EnclosedExpr">EnclosedExpr</a> production, which allows an optional expression within curly brackets.]
                  [<a id="dt-content-expression" title="content expression">Definition</a>: In an  <a title="enclosed expression" class="termref" href="#dt-enclosed-expression">enclosed expression</a>, the optional expression enclosed in curly brackets is called the <b>content expression</b>.] If the <a title="content expression" class="termref" href="#dt-content-expression">content expression</a> is not provided explicitly,  the content expression is <code>()</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Despite the name, an enclosed expression is not actually an expression
                  in its own right; rather it is a construct that is used in the grammar of many other expressions.</p></div></div></div><div class="div2">
<h3><a id="id-postfix-expression"/>4.3 <a href="#id-postfix-expression" style="text-decoration: none">Postfix Expressions</a></h3>
<a id="d3e16384"/><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PostfixExpr"/><code><a href="#prod-xpath40-PostfixExpr">PostfixExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PostfixExpr-PrimaryExpr">PrimaryExpr</a>  |  <a href="#doc-xpath40-PostfixExpr-FilterExpr">FilterExpr</a>  |  <a href="#doc-xpath40-PostfixExpr-DynamicFunctionCall">DynamicFunctionCall</a>  |  <a href="#doc-xpath40-PostfixExpr-LookupExpr">LookupExpr</a>  |  <a href="#doc-xpath40-PostfixExpr-FilterExprAM">FilterExprAM</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PostfixExpr-PrimaryExpr"/><code><a href="#prod-xpath40-PrimaryExpr">PrimaryExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-Literal">Literal</a>
                     <br/>|  <a href="#doc-xpath40-VarRef">VarRef</a>
                     <br/>|  <a href="#doc-xpath40-ParenthesizedExpr">ParenthesizedExpr</a>
                     <br/>|  <a href="#doc-xpath40-ContextValueRef">ContextValueRef</a>
                     <br/>|  <a href="#doc-xpath40-FunctionCall">FunctionCall</a>
                     <br/>|  <a href="#prod-xpath40-FunctionItemExpr">FunctionItemExpr</a>
                     <br/>|  <a href="#doc-xpath40-MapConstructor">MapConstructor</a>
                     <br/>|  <a href="#doc-xpath40-ArrayConstructor">ArrayConstructor</a>
                     <br/>|  <a href="#doc-xpath40-StringTemplate">StringTemplate</a>
                     <br/>|  <a href="#doc-xpath40-UnaryLookup">UnaryLookup</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PostfixExpr-FilterExpr"/><code><a href="#prod-xpath40-FilterExpr">FilterExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PostfixExpr">PostfixExpr</a>
                     <a href="#prod-xpath40-Predicate">Predicate</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PostfixExpr-DynamicFunctionCall"/><code><a href="#prod-xpath40-DynamicFunctionCall">DynamicFunctionCall</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PostfixExpr">PostfixExpr</a>
                     <a href="#prod-xpath40-PositionalArgumentList">PositionalArgumentList</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PostfixExpr-LookupExpr"/><code><a href="#prod-xpath40-LookupExpr">LookupExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PostfixExpr">PostfixExpr</a>
                     <a href="#prod-xpath40-Lookup">Lookup</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PostfixExpr-FilterExprAM"/><code><a href="#prod-xpath40-FilterExprAM">FilterExprAM</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PostfixExpr">PostfixExpr</a>  "?["  <a href="#prod-xpath40-Expr">Expr</a>  "]"</code></td></tr></tbody></table><p>A postfix expression takes one of the following forms:</p><ul><li><p>
                     [<a id="dt-filter-expression" title="filter expression">Definition</a>: 
               A <b>filter expression</b> is an expression in the form <code>E1[E2]</code>: 
               its effect is
               to return those items from the value of <code>E1</code> that
               satisfy the predicate in E2.]
                  </p><p>Filter expressions are described in <a href="#id-filter-expression"><b>4.4 Filter Expressions</b></a>.</p><p>An example of a filter expression is <code>(1 to 100)[. mod 2 = 0]</code>
            which returns all even numbers in the range 1 to 100.</p><p>The base expression <code>E1</code> can itself be a postfix expression,
            so multiple predicates are allowed, in the form <code>E1[E2][E3][E4]</code>.</p></li><li><p>An expression (other than a raw EQName) followed by an argument
               list in parentheses (that is, <code>E1(E2, E3, ...)</code>) is
               referred to as a <a title="dynamic function call" class="termref" href="#dt-dynamic-function-invocation">dynamic function call</a>. Its
               effect is to evaluate <code>E1</code> to obtain a function,
               and then call that function, with
               <code>E2</code>, <code>E3</code>, <code>...</code> as
               arguments. Dynamic function calls are described in <a href="#id-dynamic-function-invocation"><b>4.5.3 Dynamic Function Calls</b></a>.</p><p>An example of a dynamic function call is <code>$f("a", 2)</code> where
            the value of variable <code>$f</code> must be a function item.</p></li><li><p>A <code>lookup-expression</code> takes the form
            <code>E1?K</code>, where <code>E1</code> is an expression returning a sequence
            of maps or arrays, and <code>K</code> is a key specifier, which indicates which
            entries in a map, or members in an array, should be selected.</p><p>Lookup expressions are described in <a href="#id-postfix-lookup"><b>4.13.3.1 Postfix Lookup Expressions</b></a>.</p><p>An example of a lookup expression is <code>$emp?name</code>, where
            the value of variable <code>$emp</code> is a map, and the string <code>"name"</code>
            is the key of one of the entries in the map.</p></li></ul><p>Postfix expressions are evaluated from left-to-right. For example, the 
         expression <code>$E1[E2]?(E3)(E4)</code> is evaluated by first evaluating
            the filter expression <code>$E1[E2]</code> to produce a sequence of maps and arrays 
            (say <code>$S</code>), then evaluating the lookup expression <code>$S?(E3)</code>
            to produce a function item (say <code>$F</code>), then evaluating the dynamic
            function call <code>$F(E4)</code> to produce the final result.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The grammar for postfix expressions is defined here in a way designed
         to link clearly to the semantics of the different kinds of expression. For parsing
         purposes, the equivalent production rule:</p><div class="exampleInner"><pre>PostfixExpr := PrimaryExpr (Predicate | PositionalArgumentList | Lookup)*</pre></div><p>(as used in XPath 3.1) is probably more convenient.</p></div></div><div class="div2">
<h3><a id="id-filter-expression"/>4.4 <a href="#id-filter-expression" style="text-decoration: none">Filter Expressions</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-function-calls">⬇</a> <a href="#id-qname-literals">⬆</a></p><ol><li><p>
                  The value of a predicate in a filter expression can now be a sequence of integers.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/816">816</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/996">996</a> 6 February 2024]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExpr"/><code><a href="#prod-xpath40-FilterExpr">FilterExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-FilterExpr-PostfixExpr">PostfixExpr</a>
                     <a href="#doc-xpath40-FilterExpr-Predicate">Predicate</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExpr-PostfixExpr"/><code><a href="#prod-xpath40-PostfixExpr">PostfixExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PrimaryExpr">PrimaryExpr</a>  |  <a href="#doc-xpath40-FilterExpr">FilterExpr</a>  |  <a href="#doc-xpath40-DynamicFunctionCall">DynamicFunctionCall</a>  |  <a href="#doc-xpath40-LookupExpr">LookupExpr</a>  |  <a href="#doc-xpath40-FilterExprAM">FilterExprAM</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExpr-Predicate"/><code><a href="#prod-xpath40-Predicate">Predicate</a></code></td><td>::=</td><td><code>"["  <a href="#doc-xpath40-FilterExpr-Expr">Expr</a>  "]"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExpr-Expr"/><code><a href="#prod-xpath40-Expr">Expr</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-FilterExpr-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExpr-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ForExpr">ForExpr</a>
                     <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                     <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                     <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                     <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                  </code></td></tr></tbody></table><p>A filter expression consists of a base expression followed by
               a predicate, which is an expression written in square
               brackets. The result of the filter expression consists of the
               items returned by the base expression, filtered by applying the
               predicate to each item in turn. The ordering of the items
               returned by a filter expression is the same as their order in
               the result of the primary expression.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Where the expression before the square brackets is a
                  <a href="#prod-xpath40-ReverseStep">ReverseStep</a> or <a href="#prod-xpath40-ForwardStep">ForwardStep</a>, the expression is technically not a
                  filter expression but an <a href="#doc-xpath40-AxisStep">AxisStep</a>. There are minor differences
                  in the semantics: see <a href="#id-predicate"><b>4.6.5 Predicates within Steps</b></a>
               </p></div><p>Here are some examples of filter expressions:</p><ul><li><p>Given a sequence of products in a variable, return only those products whose price is greater than 100.</p><div class="exampleInner"><pre>$products[price gt 100]</pre></div></li><li><p>List all the integers from 1 to 100 that are divisible by 5. (See <a href="#construct_seq"><b>4.7.1 Sequence Concatenation</b></a> for an explanation of the <code>to</code> operator.)</p><div class="exampleInner"><pre>(1 to 100)[. mod 5 eq 0]</pre></div></li><li><p>The result of the following expression is the integer 25:</p><div class="exampleInner"><pre>(21 to 29)[5]</pre></div></li><li><p>The following example returns the fifth through ninth items in the sequence bound to variable <code>$orders</code>.</p><div class="exampleInner"><pre>$orders[5 to 9]</pre></div></li><li><p>The following example illustrates the use of a filter expression as a <a title="step" class="termref" href="#dt-step">step</a> in a <a title="path expression" class="termref" href="#dt-path-expression">path expression</a>. It returns the last chapter or appendix within the book bound to variable <code>$book</code>:</p><div class="exampleInner"><pre>$book/(chapter | appendix)[last()]</pre></div></li></ul><p>For each item in the input sequence, the predicate expression
               is evaluated using an <b>inner focus</b>, defined as
               follows: The context value is the item currently being tested
               against the predicate. The context size is the number of items
               in the input sequence. The context position is the position of
               the context value within the input sequence. </p><p>For each item in the input sequence, the result of the
               predicate expression is coerced to an <code>xs:boolean</code>
               value, called the <a title="predicate truth value" class="termref" href="#dt-predicate-truth-value">predicate truth value</a>, as
               described below. Those items for which the predicate truth value
               is <code>true</code> are retained, and those for which the
               predicate truth value is <code>false</code> are discarded.</p><p>
               [<a id="dt-predicate-truth-value" title="predicate truth value">Definition</a>: The
            <b>predicate truth value</b> of a value <code>$V</code>
            is the result of the expression <code>if ($V instance of xs:numeric+)
            then ($V = position()) else fn:boolean($V)</code>.]
            </p><p>Expanding this definition, the predicate truth value can be obtained 
               by applying the following rules, in order:</p><ol class="enumar"><li><p>If the value <var>V</var> of the predicate expression 
                     is a sequence whose first item is an instance of the type <code>xs:numeric</code>,
                     then:</p><ol class="enumla"><li><p>
                           <var>V</var> must be an instance of the type
                        <code>xs:numeric+</code> (that is, every item in <var>V</var>
                           must be numeric). A type error [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFORG0006" title="err:FORG0006">err:FORG0006</a>]<sup><small>FO40</small></sup> is
                        raised if this is not the case.</p></li><li><p>The predicate truth value is <code>true</code> if 
                           <var>V</var> is equal (by the
                           <code>=</code> operator) to the <b>context
                              position</b>, and is <code>false</code>
                           otherwise.</p></li></ol><p>In effect this means that an item in the input sequence is selected
                  if its position in the sequence is equal to one or more of the numeric
                  values in the predicate. For example, the predicate <code>[3 to 5]</code>
                  is true for the third, fourth, and fifth items in the input sequence.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>It is possible, though not generally useful, for the value of a numeric
                  predicate to depend on the focus, and thus to differ for different items
                  in the input sequence. For example, the predicate <code>[xs:integer(@seq)]</code>
                  selects those items in the input sequence whose <code>@seq</code> attribute
                  is numerically equal to their position in the input sequence.</p><p>It is also possible, and again not generally useful, for the value of the predicate
                  to be numeric for some items in the input sequence, and boolean for others.
                  For example, the predicate <code>[@special otherwise last()]</code>
                  is true for an item that either has an <code>@special</code> attribute,
                     or is the last item in the input sequence.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The truth value of a numeric predicate does not depend on the order
                  of the numbers in <var>V</var>. The predicates <code>[ 1, 2, 3 ]</code>
                  and <code>[ 3, 2, 1 ]</code> have exactly the same effect. The items in 
                  the result of a filter expression always retain the ordering of the input
                  sequence.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The truth value of a numeric predicate whose value is non-integral
                  or non-positive is always false.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Beware that using boolean operators (<code>and</code>, <code>or</code>,
                  <code>not()</code>) with numeric values may not have the intended effect.
                  For example the predicate <code>[1 or last()]</code> selects every item
                  in the sequence, because <code>or</code> operates on the <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a>
                  of its operands. The required effect can be achieved with the predicate
                  <code>[1, last()]</code>.</p></div></li><li><p>Otherwise, the predicate truth value is the <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a> of the
                     predicate expression.</p></li></ol></div><div class="div2">
<h3><a id="id-functions"/>4.5 <a href="#id-functions" style="text-decoration: none">Functions</a></h3><p>Functions in XPath 4.0 arise in two ways:</p><ul><li><p>A <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> contains information
               about a family of functions with the same name and a defined arity range. These functions
               are in most cases known statically (they appear in the <a title="statically known function definitions" class="termref" href="#dt-statically-known-function-definitions">statically known function definitions</a>), 
               but there may be further function definitions
               that are known only dynamically (appearing in the <a title="dynamically known function definitions" class="termref" href="#dt-dynamically-known-function-definitions">dynamically known function definitions</a>).</p></li><li><p>
                     <a title="function item" class="termref" href="#dt-function-item">Function items</a> are XDM items that can be called
               using a <a title="dynamic function call" class="termref" href="#dt-dynamic-function-call">dynamic function call</a>. They  are values that can be bound to variables, passed as
            arguments, returned as function results, and generally manipulated in the same way as other XDM values.</p></li></ul><p>The functions defined by a statically known <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> can be invoked using a
            <a title="static function call" class="termref" href="#dt-static-function-call">static function call</a>. <a title="function item" class="termref" href="#dt-function-item">Function items</a> corresponding
            to these definitions can also be obtained, as dynamic values, by evaluating a <a title="named function reference" class="termref" href="#dt-named-function-ref">named function reference</a>. 
            <a title="function item" class="termref" href="#dt-function-item">Function items</a> can also be obtained using the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-function-lookup"><code>fn:function-lookup</code></a>
            function: in this case the function name and arity do not need to be known statically, and the function definition
            need not be present in the <a title="static context" class="termref" href="#dt-static-context">static context</a>, so long as it is in the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a>.</p><p>Static and dynamic function calls are described in the following sections.</p><div class="div3">
<h4><a id="id-static-functions"/>4.5.1 <a href="#id-static-functions" style="text-decoration: none">Static Function Calls</a></h4><p>The <a title="static context" class="termref" href="#dt-static-context">static context</a> for an expression includes a set 
                  of <a title="statically known function definitions" class="termref" href="#dt-statically-known-function-definitions">statically known function definitions</a>. Every <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
               in the static context has a name (which is an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>) and 
                  an <a title="arity range" class="termref" href="#dt-arity-range">arity range</a>, which is a range of permitted arities for
               calls on that function. Two <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a> 
                  having the same name must not have overlapping arity ranges.
               This means that for a given static function call, it is possible to identify the target function definition
               in the static context unambiguously from knowledge of the function name and the number of supplied arguments.</p><p>A <a title="static function call" class="termref" href="#dt-static-function-call">static function call</a> is bound to a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> 
                  in the static context by matching the name
               and arity. If the function call has <var>P</var> positional arguments followed by
                  <var>K</var> keyword arguments, then the required arity is <var>P+K</var>, and the static context 
                  must include a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> whose name matches the <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> 
                  in the function call, and whose <a title="arity range" class="termref" href="#dt-arity-range">arity range</a> 
               includes this required arity. This is the function chosen to be called.
               The result of the function is obtained by evaluating the expression that forms its implementation, with
               a dynamic context that provides values for all the declared parameters, initialized
               as described in <a href="#id-eval-static-function-call"><b>4.5.1.2 Evaluating Static Function Calls</b></a> below.</p><p>Similarly, a function reference of the form <code>f#N</code> binds to a 
                  <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> in the
                  static context whose name matches <var>f</var> where <code>MinP ≤ N and MaxP ≥ N</code>.
               The result of evaluating a function reference is a <a title="function item" class="termref" href="#dt-function-item">function item</a> which can be called
               using a dynamic function call. Function items are never variadic and their arguments
               are always supplied positionally. For example, the function reference <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-concat"><code>fn:concat#3</code></a>
               returns a function item with arity 3, which is always called by supplying three positional
               arguments, and whose effect is the same as a static call on <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-concat"><code>fn:concat</code></a> with
               three positional arguments. </p><p>The detailed rules for evaluating static function calls and function references are defined
               in subsequent sections.</p><div class="div4">
<h5><a id="id-function-calls"/>4.5.1.1 <a href="#id-function-calls" style="text-decoration: none">Static Function Call Syntax</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-eval-dynamic-function-call">⬇</a> <a href="#id-filter-expression">⬆</a></p><ol><li><p>
                  Keyword arguments are allowed on static function calls, as well as positional arguments.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/155">155</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/159">159</a> 30 September 2020]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall"/><code><a href="#prod-xpath40-FunctionCall">FunctionCall</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-FunctionCall-EQName">EQName</a>
                           <a href="#doc-xpath40-FunctionCall-ArgumentList">ArgumentList</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                         */</td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#parse-note-parens">gn: parens</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall-ArgumentList"/><code><a href="#prod-xpath40-ArgumentList">ArgumentList</a></code></td><td>::=</td><td><code>"("  ((<a href="#doc-xpath40-FunctionCall-PositionalArguments">PositionalArguments</a>  (","  <a href="#doc-xpath40-FunctionCall-KeywordArguments">KeywordArguments</a>)?)  |  <a href="#doc-xpath40-FunctionCall-KeywordArguments">KeywordArguments</a>)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall-PositionalArguments"/><code><a href="#prod-xpath40-PositionalArguments">PositionalArguments</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-FunctionCall-Argument">Argument</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall-Argument"/><code><a href="#prod-xpath40-Argument">Argument</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-FunctionCall-ExprSingle">ExprSingle</a>  |  <a href="#doc-xpath40-FunctionCall-ArgumentPlaceholder">ArgumentPlaceholder</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-ForExpr">ForExpr</a>
                           <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                           <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                           <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                           <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall-ArgumentPlaceholder"/><code><a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholder</a></code></td><td>::=</td><td><code>"?"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall-KeywordArguments"/><code><a href="#prod-xpath40-KeywordArguments">KeywordArguments</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-FunctionCall-KeywordArgument">KeywordArgument</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FunctionCall-KeywordArgument"/><code><a href="#prod-xpath40-KeywordArgument">KeywordArgument</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-FunctionCall-EQName">EQName</a>  ":="  <a href="#doc-xpath40-FunctionCall-Argument">Argument</a>
                        </code></td></tr></tbody></table><p>
                     [<a id="dt-static-function-call" title="static function call">Definition</a>: A <b>static function call</b> 
               consists of an EQName followed by a parenthesized list of zero or more arguments.].</p><p>The EQName is expanded using the <a title="default function namespace rule" class="termref" href="#dt-default-function-namespace-rule">default function namespace rule</a>.</p><p>The argument list consists of zero or more positional arguments,
               followed by zero or more keyword arguments.</p><p>
                     [<a id="dt-arg-expr" title="argument expression">Definition</a>: An argument to a function call is either an
               <b>argument expression</b> or an <a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholder</a> 
               (<code>?</code>); in both cases it may
            either be supplied positionally, or identified by a name (called a keyword).]
                  </p><p>This section is concerned with static function calls in which none of the arguments are
               <a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholders</a>. 
               Calls using one or more <a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholders</a> are covered in the 
               section <a href="#id-partial-function-application"><b>4.5.4 Partial Function Application</b></a>.</p><p>The <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> used as the function name and the number of arguments used in the static function call 
               (the required arity) must match the name and arity range of a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> 
               in the <a title="static context" class="termref" href="#dt-static-context">static context</a>
                     <span> using the rules defined in the previous section</span>; if there is no match, a
               <a title="static error" class="termref" href="#dt-static-error">static error</a> is raised [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>].
               </p><p>Evaluation of static function calls is described in <a href="#id-eval-static-function-call"><b>4.5.1.2 Evaluating Static Function Calls</b></a>
                     .</p><p>Since the arguments of a function call are separated by commas, any <a title="argument expression" class="termref" href="#dt-arg-expr">argument expression</a> that contains a top-level <a title="comma operator" class="termref" href="#dt-comma-operator">comma operator</a> must be
               enclosed in parentheses. Here are some illustrative examples of
               static function calls:</p><ul><li><p>
                           <code>my:three-argument-function(1, 2, 3)</code> denotes a static function call with three 
                     <span>positional</span> arguments. <span>The 
                  corresponding function declaration must define at least three parameters, and may define
                  more, provided they are optional.</span>
                        </p></li><li><p>
                           <code>my:two-argument-function((1, 2), 3)</code> denotes a static function call with two arguments, the first of which is a
                     sequence of two values. <span>The 
                        corresponding function declaration must define at least two parameters, and may define
                        more, provided they are optional.</span>
                        </p></li><li><p>
                           <code>my:two-argument-function(1, ())</code> denotes a static function call with two arguments, 
                     the second of which is an empty sequence.</p></li><li><p>
                           <code>my:one-argument-function((1, 2,
                        3))</code> denotes a static function call with one argument that is a sequence of three
                     values. </p></li><li><p>
                           <code>my:one-argument-function(( ))</code> denotes a static function call with one argument that is an empty sequence.</p></li><li><p>
                           <code>my:zero-argument-function( )</code> denotes a static function call with zero arguments.</p></li><li><p>
                           <code>lang(node := $n, language := 'de')</code> is a static function 
                     call with two keyword arguments. The corresponding function declaration defines two parameters,
                     a required parameter <code>language</code> and an optional parameter <code>node</code>.
                     This call supplies values for both parameters. It is equivalent to the call 
                     <code>fn:lang('de', $n)</code>. Note that the keyword arguments are in a different
                     order from the parameter declarations.
                  </p></li><li><p>
                           <code>sort(//employee, key := fn($e) { xs:decimal($e/salary) })</code> is a static function 
                     call with one positional argument and one keyword argument. 
                     The corresponding function declaration defines three parameters,
                     a required parameter <code>$input</code>, an optional parameter <code>$collation</code>,
                     and an optional parameter <code>$key</code>
                     This call supplies values for the first and third parameters, leaving the second parameter (<code>$collation</code>)
                     to take its default value. The default value of the <code>$collation</code> parameter
                     is given as <code>fn:default-collation()</code>, so the value supplied to the function is the
                     default collation from the dynamic context of the caller. It is equivalent to the call 
                     <code>fn:sort(//employee, fn:default-collation(), fn($e) { xs:decimal($e/salary) })</code>.
                  </p></li></ul><p>An <code>EQName</code> in a <code>KeywordArgument</code> is expanded 
               using the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>.
            The keywords used in a function call (after expansion) must be distinct 
               [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>]; [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>].</p></div><div class="div4">
<h5><a id="id-eval-static-function-call"/>4.5.1.2 <a href="#id-eval-static-function-call" style="text-decoration: none">Evaluating Static Function Calls</a></h5><p>This section applies to static function calls where none of the
            arguments is an <code>ArgumentPlaceholder</code>. For function calls involving
            placeholders, see <a href="#id-partial-function-application"><b>4.5.4 Partial Function Application</b></a>.</p><p>
                      When a static function call <var>FC</var> is evaluated
                      with respect to a static context <var>SC</var> and
                      a dynamic context <var>DC</var>,
                      the result is obtained as follows:
                    </p><ol class="enumar"><li><p>
                          The <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
                           <var>FD</var> to be used is found in 
                        the <a title="statically known function definitions" class="termref" href="#dt-statically-known-function-definitions">statically known function definitions</a> of <var>SC</var>.
                        </p><p>The <b>required arity</b> is the total number of arguments in the
                        function call, including both positional and keyword arguments.</p><p>
                              There can be at most one <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
                           <var>FD</var> in the 
                              <a title="statically known function definitions" class="termref" href="#dt-statically-known-function-definitions">statically known function definitions</a> component of <var>SC</var> whose function name
                              matches the <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> in <var>FC</var> and whose <a title="arity range" class="termref" href="#dt-arity-range">arity range</a>
                              includes the arity of <var>FC</var>’s <code>ArgumentList</code>.
                           </p><p>If there is no such <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>, a static error [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>] is raised.</p></li><li><p>Each parameter in the <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
                           <var>FD</var> is matched to an argument
                     expression as follows:</p><ol class="enumla"><li><p>If there are <var>N</var> positional arguments in the function call <var>FC</var>,
                           then
                           the corresponding argument expressions are matched pairwise to the first <var>N</var>
                           parameters in the declaration. For this purpose the required parameters and optional parameters
                           in <var>FD</var> are concatenated into a single list, in order.</p></li><li><p>Any keyword arguments in <var>FC</var> are then matched to
                           parameters (whether required or optional) in <var>FD</var> by comparing the keyword
                           used in <var>FC</var> with the paramater name declared in <var>FD</var>.
                           Each keyword must match the name of a declared parameter [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>], 
                           and this must be one that has not already
                           been matched to a positional argument. [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>].</p></li><li><p>If any required parameter has not been matched to any argument in <var>FC</var>
                           by applying the above rules, a static error is reported [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>]
                              </p></li><li><p>If any optional parameter has not been matched to any argument in <var>FC</var>
                           by applying the above rules, then the parameter is matched to the 
                           default value expression for that parameter in <var>FD</var>.</p></li></ol></li><li><p>
                          Each argument expression established by the above rules is evaluated with respect to DC.
                          The order of argument evaluation is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation   dependent</a> and it is not
                        required that an argument be evaluated if the function body can be evaluated without evaluating
                        that argument.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>All argument expressions, including default value expressions, are evaluated in the dynamic
                     context of the function call. It is therefore possible to use a default value expression such as 
                     <code>.</code>, or <code>/</code>, or <code>fn:current-dateTime()</code>, whose value depends on the
                     dynamic context of the function call.</p></div><p>If the expression used for the default value of a parameter has no dependencies on the dynamic
                     context, then an implementation <span class="verb">may</span> choose to reuse the same value on repeated
                     function calls rather than re-evaluating it on each function call.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>This is relevant, for example, if the expression constructs new nodes.</p></div></li><li><p>The result of evaluating the argument expression is converted to the required type (the
                     declared type associated with the corresponding parameter in the function declaration, defaulting
                     to <code>item()*</code>) by applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.</p><p>
                           <span>This applies both to explicitly supplied arguments, and
                        to values obtained by evaluating default value expressions. In both cases a type
                        error will be raised if the value (after coercion) does not match the required type.</span>
                        </p></li><li><p>The result of the function call is obtained as follows:</p><ul><li><p>
                                 <var>FD</var>’s <span>body</span> is invoked
                                      in an implementation-dependent way.
                                      The processor makes the following information
                                      available to that invocation:
                                      
                                    </p><ul><li><p>The converted argument values;</p></li><li><p>If the function is <a title="context dependent" class="termref" href="#dt-context-dependent">context dependent</a>,
                                          the static context <var>SC</var> and dynamic context <var>DC</var> of the function call.
                                           </p></li></ul></li><li><p>The result is converted to the required type (the
                                             declared return type in the function declaration, defaulting
                                             to <code>item()*</code>) by applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.</p><p>The result of applying the coercion rules is either an instance of <var>FD</var>’s return type
                                      or a dynamic error.
                                      This result is then the result of evaluating <var>FC</var>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>A host language may define alternative rules for processing the result, especially
                                      in the case of external functions implemented using a non-XDM type system.</p></div></li><li><p>
                                      Errors raised by system functions are defined in
                                      <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.
                                    </p></li><li class="xpath"><p>
                                      Errors raised by host-language-dependent functions are
                                      <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>.
                                    </p></li></ul><div class="example">
<div class="exampleHeader"><a id="d3e17560"/>Example: A System Function</div><p>The following function call uses the function 
                                  <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-base-uri">Section 2.1.5 fn:base-uri</a><sup><small>FO</small></sup>.  Use of <code>SC</code> and <code>DC</code> and errors raised by this function are all defined in 
                                  <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.</p><div class="exampleInner"><pre>base-uri()</pre></div></div></li></ol></div></div><div class="div3">
<h4><a id="id-dynamic-functions"/>4.5.2 <a href="#id-dynamic-functions" style="text-decoration: none">Function Items</a></h4><p>A <a title="function item" class="termref" href="#dt-function-item">function item</a> is an XDM
            value that can be bound to a variable, or manipulated in various ways by XPath 4.0 expressions.
            The most significant such expression is a <a title="dynamic function call" class="termref" href="#dt-dynamic-function-call">dynamic function call</a>, which supplies
            values of arguments and evaluates the function to produce a result.</p><p>The syntax of dynamic function calls is defined in <a href="#id-dynamic-function-invocation"><b>4.5.3 Dynamic Function Calls</b></a>.</p><p>A number of constructs can be used to produce a <a title="function item" class="termref" href="#dt-function-item">function item</a>, notably:</p><ul><li><p>A <b>named function reference</b> (see <a href="#id-named-function-ref"><b>4.5.5 Named Function References</b></a>)
               constructs a function item by reference to <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a>
                  in the static context. For example, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-node-name"><code>fn:node-name#1</code></a>
               returns a function item whose effect is to call the static <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-node-name"><code>fn:node-name</code></a> function
               with one argument.</p></li><li><p>An <b>inline function</b> (see <a href="#id-inline-func"><b>4.5.6 Inline Function Expressions</b></a>
                        )
                  constructs a function item whose <span>body</span> is defined locally. For example, the
               construct <code>fn($x) { $x + 1 }</code> returns a function item whose effect is to increment
               the value of the supplied argument.</p></li><li><p>A <b>partial function application</b> (see 
                  <a href="#id-partial-function-application"><b>4.5.4 Partial Function Application</b></a>) derives one function item from another by supplying
               the values of some of its arguments. For example, <code>fn:ends-with(?, ".txt")</code> returns
               a function item with one argument that tests whether the supplied string ends with the substring
                  <code>".txt"</code>.</p></li><li><p>Maps and arrays are also function items. See <a href="#id-map-constructors"><b>4.13.1.1 Map Constructors</b></a>
               and <a href="#id-array-constructors"><b>4.13.2.1 Array Constructors</b></a>.</p></li><li><p>The <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-function-lookup"><code>fn:function-lookup</code></a> function can be called to discover functions
               that are present in the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a>.</p></li><li><p>The <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-load-xquery-module"><code>fn:load-xquery-module</code></a> function can be called to load functions
                  dynamically from an external XQuery library module.</p></li><li><p>Some system functions such as <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-random-number-generator"><code>fn:random-number-generator</code></a> 
                  and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-op"><code>fn:op</code></a> return a <a title="function item" class="termref" href="#dt-function-item">function item</a> as their result.</p></li></ul><p>These constructs are described in detail in the following sections, or in
               <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.</p></div><div class="div3">
<h4><a id="id-dynamic-function-invocation"/>4.5.3 <a href="#id-dynamic-function-invocation" style="text-decoration: none">Dynamic Function Calls</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-DynamicFunctionCall"/><code><a href="#prod-xpath40-DynamicFunctionCall">DynamicFunctionCall</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-DynamicFunctionCall-PostfixExpr">PostfixExpr</a>
                        <a href="#doc-xpath40-DynamicFunctionCall-PositionalArgumentList">PositionalArgumentList</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-DynamicFunctionCall-PostfixExpr"/><code><a href="#prod-xpath40-PostfixExpr">PostfixExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-PrimaryExpr">PrimaryExpr</a>  |  <a href="#doc-xpath40-FilterExpr">FilterExpr</a>  |  <a href="#doc-xpath40-DynamicFunctionCall">DynamicFunctionCall</a>  |  <a href="#doc-xpath40-LookupExpr">LookupExpr</a>  |  <a href="#doc-xpath40-FilterExprAM">FilterExprAM</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-DynamicFunctionCall-PositionalArgumentList"/><code><a href="#prod-xpath40-PositionalArgumentList">PositionalArgumentList</a></code></td><td>::=</td><td><code>"("  <a href="#doc-xpath40-DynamicFunctionCall-PositionalArguments">PositionalArguments</a>?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-DynamicFunctionCall-PositionalArguments"/><code><a href="#prod-xpath40-PositionalArguments">PositionalArguments</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-DynamicFunctionCall-Argument">Argument</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-DynamicFunctionCall-Argument"/><code><a href="#prod-xpath40-Argument">Argument</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-DynamicFunctionCall-ExprSingle">ExprSingle</a>  |  <a href="#doc-xpath40-DynamicFunctionCall-ArgumentPlaceholder">ArgumentPlaceholder</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-DynamicFunctionCall-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr">ForExpr</a>
                        <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                        <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                        <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                        <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-DynamicFunctionCall-ArgumentPlaceholder"/><code><a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholder</a></code></td><td>::=</td><td><code>"?"</code></td></tr></tbody></table><p>
                  [<a id="dt-dynamic-function-invocation" title="dynamic function call">Definition</a>: A <b>dynamic function call</b>
                     consists of a base expression that returns the function and a
                     parenthesized list of zero or more arguments (<a title="argument expression" class="termref" href="#dt-arg-expr">argument expressions</a> or
                     ArgumentPlaceholders).]
               </p><p>
                  A dynamic function call is evaluated as described in
                  <a href="#id-eval-dynamic-function-call"><b>4.5.3.1 Evaluating Dynamic Function Calls</b></a>.
               </p><p>The following are examples of some dynamic function calls:</p><ul><li><p>This example calls the function contained in <code>$f</code>, passing the arguments 2 and 3:
                        </p><div class="exampleInner"><pre>$f(2, 3)</pre></div></li><li><p>This example fetches the second item from sequence <code>$f</code>, treats it as a function and calls it, passing an <code>xs:string</code> argument:
                        </p><div class="exampleInner"><pre>$f[2]("Hi there")</pre></div></li><li><p>This example calls the function <code>$f</code> passing no arguments, and filters the result with a positional predicate:
                        </p><div class="exampleInner"><pre>$f()[2]</pre></div></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>Arguments in a dynamic function call are always supplied positionally.</p></div><div class="div4">
<h5><a id="id-eval-dynamic-function-call"/>4.5.3.1 <a href="#id-eval-dynamic-function-call" style="text-decoration: none">Evaluating Dynamic Function Calls</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-inline-func">⬇</a> <a href="#id-function-calls">⬆</a></p><ol><li><p>A dynamic function call can now be applied to a sequence of functions, and in particular
               to an empty sequence. This makes it easier to chain a sequence of calls.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1240">1240</a> ]</i></p></li></ol></div><p>This section applies to dynamic function calls whose arguments do not include
            an <code>ArgumentPlaceholder</code>. For function calls that include a placeholder,
            see <a href="#id-partial-function-application"><b>4.5.4 Partial Function Application</b></a>.</p><p>
                     [<a id="dt-dynamic-function-call" title="dynamic function call">Definition</a>: A <b>dynamic function call</b>
            is an expression that is evaluated by calling a <a title="function item" class="termref" href="#dt-function-item">function item</a>, which is
            typically obtained dynamically.]
                  </p><p>
                      When a dynamic function call <var>FC</var> is evaluated,
                      the result is obtained as follows:
                    </p><ol class="enumar"><li><p>
                          The base expression of the function call is evaluated.
                          If this is not of type <code>function(*)*</code> (a sequence
                        of zero or more function items) then a type error is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].
                     </p></li><li><p>The result of the dynamic function call is the <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a>
                        of the results of applying each function item individually, retaining order. That is, the
                        result of <code>
                              <var>F</var>(<var>X</var>, <var>Y</var>, ...)</code> is 
                        <code>for $FI in <var>F</var> return <var>$FI</var>(<var>X</var>, <var>Y</var>, ...)</code>.
                        The result of a dynamic function call applied to a single function item <var>FI</var> is defined
                        by the rules that follow.
                     </p></li><li><p>
                           [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].
                              If the arity of <var>FI</var> does not match the number of arguments in the <code>ArgumentList</code>,
                              a type error is raised
                              [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].
                            </p></li><li><p>
                           <a title="argument expression" class="termref" href="#dt-arg-expr">Argument expressions</a> are evaluated, 
                        producing <b>argument values</b>. The order of argument evaluation is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> and an argument need not be evaluated 
                        if the function body can be evaluated without evaluating that argument.</p></li><li><p>
                          Each argument value is converted
                          to the corresponding parameter type in <var>FI</var>’s signature
                          by applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>, resulting in a
                        <b>converted argument value</b>
                        </p></li><li><p>If <var>FI</var> is a map, it is evaluated as described in <a href="#id-map-lookup"><b>4.13.1.2 Maps as Functions</b></a>.</p></li><li><p>If <var>FI</var> is an array, it is evaluated as described in <a href="#id-array-lookup"><b>4.13.2.2 Arrays as Functions</b></a>.</p></li><li><p>
                                  If <var>FI</var>’s <span>body</span> is 
                                  
                                  an XPath 4.0 expression
                                  
                                    (for example, if <var>FI</var> is
                                    
                                    an <a title="anonymous function" class="termref" href="#dt-anonymous-function">anonymous function</a>,
                                    or a
                                    <a title="partially applied function" class="termref" href="#dt-partially-applied-function">partial application</a>
                                    of such a function):
                                  
                                </p><ol class="enumla"><li><p>
                                 <var>FI</var>’s <span>body</span> 
                                      is evaluated.
                                      
                                        The static context for this evaluation
                                        is the static context of the XPath 4.0 expression.
                                      
                                      The dynamic context for this evaluation is obtained
                                      by taking the dynamic context of the
                                      
                                          <span class="xpath">
                                    <code>InlineFunctionExpr</code>
                                 </span>
                                      that contains the <code>FunctionBody</code>, and
                                      making the following changes:
                                    </p><ul><li><p>
                                          The <a title="focus" class="termref" href="#dt-focus">focus</a>
                                          (context value, context position, and context size)
                                          is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>.
                                        </p></li><li><p>
                                          In the <a title="variable values" class="termref" href="#dt-variable-values">variable values</a> component of the dynamic context,
                                          each converted argument value is bound to the
                                          corresponding parameter name.
                                        </p><p>
                                       <span>When this is done,
                                          the converted argument values retain
                                          their <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic types</a>,
                                          even where these are <a title="subtype" class="termref" href="#dt-subtype">subtypes</a> 
                                          of the declared parameter types.</span>
                                          For example, a function with
                                          a parameter <code>$p</code> of type <code>xs:decimal</code>
                                          can be called with an argument of type <code>xs:integer</code>,
                                          which is derived from <code>xs:decimal</code>.
                                          During the processing of this function
                                          call, the value of <code>$p</code> inside the body of the function 
                                          retains its <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> of <code>xs:integer</code>.
                                        </p></li><li><p>
                                       <var>FI</var>’s nonlocal variable bindings
                                          are also added to the <a title="variable values" class="termref" href="#dt-variable-values">variable values</a>.
                                          (Note that the names of the nonlocal variables
                                          are by definition disjoint from the parameter names,
                                          so there can be no conflict.)
                                        </p></li></ul></li><li><p>
                                      The value returned by evaluating the function body
                                      is then converted to the declared return type of <var>FI</var>
                                      by applying the
                                      <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.
                                      The result is then the result of evaluating <var>FC</var>.
                                    </p><p>
                                      As with argument values,
                                      the value returned by a function
                                      retains its <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a>,
                                      which may be a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of the declared return type of <var>FI</var>.
                                      For example, a function that has
                                      a declared return type of <code>xs:decimal</code>
                                      may in fact return a value of dynamic type <code>xs:integer</code>.
                                    </p></li></ol></li><li><p>                                  
                                  If the implementation of <var>FI</var> is
                                  not an XPath 4.0 expression
                                  (for example, if <var>FI</var> is
                                  a <a title="system function" class="termref" href="#dt-system-function">system function</a>
                                    ), 
                                    the <span>body</span> of the function is
                                       evaluated, and the result is converted
                                       to the declared return type, in the same way as for a 
                                       static function call (see <a href="#id-function-calls"><b>4.5.1.1 Static Function Call Syntax</b></a>).</p><p>Errors may be raised in the same way.</p></li></ol><div class="example">
<div class="exampleHeader"><a id="d3e18057"/>Example: Derived Types and Nonlocal Variable Bindings</div><p>
                        <code>$incr</code> is a nonlocal variable that is available within the function because its variable binding has been added to the variable values of the function..  Even though the parameter and return type of this function are both <code>xs:decimal</code>,
                                  the more specific type <code>xs:integer</code> is preserved in both cases.</p><div class="exampleInner"><pre>
let $incr := 1
let $f := function($i as xs:decimal) as xs:decimal { $i + $incr }
return $f(5)</pre></div></div><p> </p><div class="example">
<div class="exampleHeader"><a id="d3e18072"/>Example: Using the Context Value in an Anonymous Function</div><p>The following example will raise a <a title="type error" class="termref" href="#dt-type-error">type error</a>
                        [<a href="#ERRXPDY0002" title="err:XPDY0002">err:XPDY0002</a>]:</p><div class="exampleInner"><pre>
let $vat := function() { @vat + @price }
return doc('wares.xml')/shop/article/$vat()</pre></div><p>Instead, the context value can be used as an argument to the anonymous function:</p><div class="exampleInner"><pre>
let $vat := function($art) { $art/@vat + $art/@price }
return doc('wares.xml')/shop/article/$vat(.)</pre></div><p>Alternatively, the value can be referenced as a nonlocal variable binding:</p><div class="exampleInner"><pre>
let $ctx := doc('wares.xml')/shop/article
let $vat := function() { for $a in $ctx return $a/@vat + $a/@price }
return $vat()</pre></div><p>Finally, a <a title="focus function" class="termref" href="#dt-focus-function">focus function</a> can be used.
                                       This binds the value of the argument to the context value within the function body:</p><div class="exampleInner"><pre>
let $vat := function { @vat + @price }
return $vat(doc('wares.xml')/shop/article)</pre></div></div><p> </p><div class="example">
<div class="exampleHeader"><a id="d3e18094"/>Example: Chaining method calls</div><p>Methods are described in <a href="#id-methods"><b>4.5.6.1 Methods</b></a>, and are commonly used in 
                  dynamic function calls such as <code>$rectangle?area()</code>. In this example <code>$rectangle</code>
               is typically a map, and <code>area</code> is the key of one of the entries in the map, the value
               of the entry being a <a title="method" class="termref" href="#dt-method">method</a>. The lookup expression
                  <code>$rectangle?area</code> returns a function item whose captured context includes the containing
               map, and the dynamic function call then evaluates the body of this method, which is
               able to access the containing map as the context item.</p><p>Such calls can be chained. For example if <code>$rectangle?resize(2)</code> returns a rectangle that
               is twice the size of the original, then <code>$rectangle?resize(2)?area()</code> returns the area of the
               enlarged rectangle.</p><p>This kind of chaining extends to the case where a method returns zero or more maps. For example, suppose
               that rectangles are nested, and that <code>$rectangle?contents()</code> delivers a sequence of zero or more 
               rectangles. Then the expression <code>$rectangle?area() - sum($rectangle?contents()?area())</code> returns
               the difference between the area of the containing rectangle and the total area of the contained
               rectangles. This works because the dynamic function call <code>$rectangle?contents()?area()</code>
               applies the function <code>area</code> to each of the function items in the sequence returned
               by the expression <code>$rectangle?contents()</code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Keyword arguments are not allowed in a dynamic function call.</p></div></div></div><div class="div3">
<h4><a id="id-partial-function-application"/>4.5.4 <a href="#id-partial-function-application" style="text-decoration: none">Partial Function Application</a></h4><p>
                  [<a id="dt-partial-function-application" title="partial function application">Definition</a>: 
                     A static or <a title="dynamic function call" class="termref" href="#dt-dynamic-function-invocation">dynamic</a>
                     function call is a <b>partial function application</b>
                     if one or more arguments is an <a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholder</a>.]
               </p><p>The rules for partial function application in static function calls and dynamic function
               calls have a great deal in common, but they are stated separately below for clarity.</p><p>Partial function application delivers
                  <a title="function item" class="termref" href="#dt-function-item">function items</a>, whose
                  arity is equal to the number of placeholders in the call.</p><p>A static partial function application always delivers one <a title="function item" class="termref" href="#dt-function-item">function item</a>.
               A dynamic partial function application delivers one <a title="function item" class="termref" href="#dt-function-item">function item</a> for each
               <a title="function item" class="termref" href="#dt-function-item">function item</a> in the input.</p><p>More specifically, each function item in the result of the partial function application is 
                  a <a title="partially applied function" class="termref" href="#dt-partially-applied-function">partially applied function</a>.
                  [<a id="dt-partially-applied-function" title="partially applied function">Definition</a>: A <b>partially applied function</b>
                  is a function created by  <a title="partial function application" class="termref" href="#dt-partial-function-application">partial function application</a>.]
               </p><p>For static function calls, the result is obtained as follows:</p><ol class="enumar"><li><p>The <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
                        <var>FD</var> to be partially applied
                        is determined in the same way as for a static function call without placeholders, 
                        as described in <a href="#id-function-calls"><b>4.5.1.1 Static Function Call Syntax</b></a>.
                        For this purpose an <code>ArgumentPlaceholder</code> contributes to the count of
                        arguments.</p></li><li><p>The parameters of <var>FD</var> are classified into three categories:</p><ul><li><p>Parameters that map to a placeholder, referred to as <b>placeholder parameters</b>.</p></li><li><p>Parameters for which an explicit value is given in the function call (either
                        positionally or by keyword), referred to as <b>explicitly supplied parameters</b>.</p></li><li><p>Parameters (which are necessarily optional parameters) for which no corresponding
                        argument is supplied, either as a placeholder or with an explicit value. These are referred to
                        as <b>defaulted parameters</b>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>A partial function application need not have any explicitly supplied parameters.
                        For example, the partial function application <code>fn:string(?)</code>
                        is allowed; it has exactly the same effect as the named function reference
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-string"><code>fn:string#1</code></a>. </p></div></li><li><p>Explicitly supplied parameters and defaulted parameters are evaluated and 
                        converted to the required type using the rules for a static function call.
                        This may result in an error being raised.</p><p>A type error is raised if any of the explicitly supplied or defaulted
                        parameters, after applying the
                        <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>, does not match the required type 
                        of the corresponding parameter.</p><p>In addition, a dynamic error <span class="verb">may</span> 
                        be raised if any of the explicitly supplied or defaulted parameters does not match other constraints on the
                        value of that parameter (for example, if the value supplied for a parameter expecting
                        a regular expression is not a valid regular expression); or if the processor is
                        able to establish that evaluation of the resulting function will fail
                        for any other reason (for example, if an error is raised while evaluating 
                        a subexpression in the function
                        body that depends only on explicitly supplied and defaulted parameters).</p><p>In all cases the error code is the same as for a static
                        function call supplying the same invalid value(s).</p><p>In the particular case where all the supplied arguments
                     are placeholders, the error behavior <span class="verb">should</span> be the same as
                     for an equivalent <a title="named function reference" class="termref" href="#dt-named-function-ref">named function reference</a>: for example, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-id"><code>fn:id#1</code></a>
                     fails if there is no context node, and <code>fn:id(?)</code>
                        <span class="verb">should</span>
                     fail likewise.</p></li><li><p>
                        The result is a <a title="partially applied function" class="termref" href="#dt-partially-applied-function">partially applied function</a> having
                        the following properties (which are defined in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#function-items">Section 7.1 Function Items</a><sup><small>DM</small></sup>):
                     </p><ul><li><p>
                              <b>name</b>: The name of <var>FD</var> if all parameters map
                              to placeholders, that is, if the partial function application is
                              equivalent to the corresponding <a title="named function reference" class="termref" href="#dt-named-function-ref">named function reference</a>.
                              Otherwise, the name is absent.
                           </p></li><li><p>
                              <b>identity</b>: A new function
                           identity distinct from the identity of any other function item.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>See also <a href="#id-function-identity"><b>4.5.7 Function Identity</b></a>.</p></div></li><li><p>
                              <b>arity</b>: The number of placeholders in the function call.</p></li><li><p>
                              <b>signature</b>: The parameters in the returned function
                              are the parameters of <var>FD</var>
                              that have been identified as placeholder parameters,
                              retaining the order in which the placeholders appear in the
                              function call. The result type of the returned function
                              is the same as the result type of <var>FD</var>.</p><p>An implementation which can determine a more specific signature (for example, 
                              through use of type analysis) is permitted to do so.
                           </p></li><li><p>
                              <b>annotations</b>: The annotations of 
                           <var>FD</var>.</p></li><li><p>
                              <b>body</b>: The body of <var>FD</var>.</p></li><li><p>
                              <b>captured context</b>: The
                              static and dynamic context of the function call, augmented,
                              for each explicitly supplied parameter and each defaulted parameter, with
                              a binding of the converted argument value
                              to the corresponding parameter name.
                           </p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>A partial function application can be used to change the order
                           of parameters, for example <code>fn:contains(substring := ?, value := ?)</code>
                           returns a function item that is equivalent to <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-contains"><code>fn:contains#2</code></a>,
                           but with the order of arguments reversed.</p></div><div class="example">
<div class="exampleHeader"><a id="d3e18324"/>Example: Partial Application of a System Function</div><p>The following partial function application creates a function 
                           item that computes the sum of squares of a sequence.</p><div class="exampleInner"><pre>let $sum-of-squares := fold-right(?, 0, function($a, $b) { $a*$a + $b })
return $sum-of-squares(1 to 3)</pre></div><p>
                           <code>$sum-of-squares</code> is an anonymous function. It has one parameter, named <code>$seq</code>, which is taken from the corresponding parameter in <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-fold-right"><code>fn:fold-right</code></a> (the other two parameters are fixed). The implementation is the implementation of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-fold-right"><code>fn:fold-right</code></a>, which is a context-independent system function. The nonlocal bindings contain the fixed bindings for the second and third parameters of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-fold-right"><code>fn:fold-right</code></a>.</p></div></li></ol><p>For dynamic function calls,  the result is obtained as follows:</p><ol class="enumar"><li><p>
                          The base expression of the function call is evaluated.
                          If this is not of type <code>function(*)*</code> (a sequence
                        of zero or more function items) then a type error is raised.
                     </p></li><li><p>The result of the dynamic function call is the <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a>
                        of the results of partial applying each function item, retaining order. That is, the
                        result of <code>
                           <var>F</var>(<var>X</var>, <var>Y</var>, ...)</code> is 
                        <code>for $FI in <var>F</var> return <var>$FI</var>(<var>X</var>, <var>Y</var>, ...)</code>.
                        The result of a dynamic function call applied to a single function item <var>FI</var> is defined
                        by the rules that follow.
                     </p></li><li><p>An <code>ArgumentPlaceholder</code> contributes to the count of
                        arguments.</p></li><li><p>The parameters of <var>FI</var> are classified into two categories:</p><ul><li><p>Parameters that map to a placeholder, referred to as <b>placeholder parameters</b>.</p></li><li><p>Parameters for which an explicit value is given in the function call, 
                           referred to as <b>supplied parameters</b>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>A partial function application need not have any explicitly supplied parameters.
                        For example, if <code>$f</code> is a function with arity 2, then
                           the partial function application <code>$f(?, ?)</code> returns
                           a function that has exactly the same effect as <code>$f</code>. </p></div></li><li><p>Arguments corresponding to supplied parameters are evaluated
                        and converted to the required
                     type of the parameter, using the rules for dynamic function calls.</p><p>A type error is raised if any of the supplied
                        parameters, after applying the
                        <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>, does not match the required type.</p><p>In addition, a dynamic error <span class="verb">may</span> 
                     be raised if any of the supplied parameters does not match other constraints on the
                     value of that parameter (for example, if the value supplied for a parameter expecting
                     a regular expression is not a valid regular expression); or if the processor is
                     able to establish that evaluation of the resulting function will fail
                     for any other reason (for example, if an error is raised while evaluating 
                     a subexpression in the function
                     body that depends only on explicitly supplied parameters).</p><p>In both cases the error code is the same as for a dynamic
                     function call supplying the same invalid value.</p></li><li><p>
                              The result of the partial function application is a  <a title="partially applied function" class="termref" href="#dt-partially-applied-function">partially applied function</a> with
                              the following properties (which are defined in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#function-items">Section 7.1 Function Items</a><sup><small>DM</small></sup>):
                            </p><ul><li><p>
                              <b>name</b>:
                                  Absent.
                                </p></li><li><p>
                              <b>arity</b>: The number of placeholders in the function call.</p></li><li><p>
                              <b>signature</b>:
                                  The signature of <var>FI</var>,
                                  removing the types of supplied parameters.
				  
				                      An implementation which can determine a more specific signature (for example, 
				                      through use of type analysis) is permitted to do so.
                                </p></li><li><p>
                              <b>annotations</b>: The annotations of <var>FI</var>.</p></li><li><p>
                              <b>body</b>: The body of <var>FI</var>.
                                </p></li><li><p>
                              <b>captured context</b>: the
                                    captured context of <var>FI</var>, augmented,
                                  for each supplied parameter, with
                                  a binding of the converted argument value
                                  to the corresponding parameter name.
                                </p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>In a dynamic partial function application, argument keywords
                                 are not available, so it is not possible to change the order of parameters.</p></div><div class="example">
<div class="exampleHeader"><a id="d3e18473"/>Example: Partial Application of an Anonymous Function</div><p>In the following example, <code>$f</code> is an anonymous function, and <code>$paf</code> is a partially applied function created from <code>$f</code>.</p><div class="exampleInner"><pre>
let $f := function($seq, $delim) { fold-left($seq, "", concat(?, $delim, ?)) }
let $paf := $f(?, ".")
return $paf(1 to 5)</pre></div><p>
                           <code>$paf</code> is also an anonymous function.  It has one parameter, named <code>$delim</code>, which is taken from the corresponding parameter in <code>$f</code>
                              (the other parameter is fixed).  The implementation of <code>$paf</code> is the implementation of <code>$f</code>, which is <code>fn:fold-left($seq, "", fn:concat(?, $delim, ?))</code>.  This implementation is associated with the <code>SC</code> and <code>DC</code> of the original expression in <code>$f</code>.  The nonlocal bindings associate the value <code>"."</code> with the parameter <code>$delim</code>.</p></div></li></ol><p>Partial function application never returns a map or an array.  If <code>$f</code> is a map or an array, then <code>$f(?)</code> is 
                  a partial function application that returns a function, but the function it returns is neither a map nor an array.</p><div class="example">
<div class="exampleHeader"><a id="d3e18533"/>Example: Partial Application of a Map</div><p>The following partial function application converts a map to an equivalent function that is not a map.</p><div class="exampleInner"><pre>let $a := { "A": 1, "B": 2 }(?)
return $a("A")</pre></div></div></div><div class="div3">
<h4><a id="id-named-function-ref"/>4.5.5 <a href="#id-named-function-ref" style="text-decoration: none">Named Function References</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NamedFunctionRef"/><code><a href="#prod-xpath40-NamedFunctionRef">NamedFunctionRef</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-NamedFunctionRef-EQName">EQName</a>  "#"  <a href="#doc-xpath40-NamedFunctionRef-IntegerLiteral">IntegerLiteral</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NamedFunctionRef-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NamedFunctionRef-IntegerLiteral"/><code><a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-NamedFunctionRef-Digits">Digits</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NamedFunctionRef-Digits"/><code><a href="#prod-xpath40-Digits">Digits</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-NamedFunctionRef-DecDigit">DecDigit</a>  ((<a href="#doc-xpath40-NamedFunctionRef-DecDigit">DecDigit</a>  |  "_")*  <a href="#doc-xpath40-NamedFunctionRef-DecDigit">DecDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NamedFunctionRef-DecDigit"/><code><a href="#prod-xpath40-DecDigit">DecDigit</a></code></td><td>::=</td><td><code>[0-9]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody></table><p>
                  [<a id="dt-named-function-ref" title="named function reference">Definition</a>: 
          A <b>named function reference</b> is an expression (written <code>name#arity</code>)
                  which evaluates to a <a title="function item" class="termref" href="#dt-function-item">function item</a>, <span>the details
                  of the function item being based on the properties of a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
                  in the <a title="static context" class="termref" href="#dt-static-context">static context</a>
                     </span>.]
               </p><p>The name and arity of the required function are known statically.</p><p>The EQName is expanded using the <a title="default function namespace rule" class="termref" href="#dt-default-function-namespace-rule">default function namespace rule</a>.</p><p>The <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> and arity must correspond to a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
               present in the <a title="static context" class="termref" href="#dt-static-context">static context</a>.
            <span>More specifically, for a named function reference <code>F#N</code>,
               there must be a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> in the <a title="statically known function definitions" class="termref" href="#dt-statically-known-function-definitions">statically known function definitions</a>
               whose name matches <var>F</var>, and whose <a title="arity range" class="termref" href="#dt-arity-range">arity range</a> includes <var>N</var>
                  </span>.
               Call this <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
                  <var>FD</var>.</p><p>If <var>FD</var> is
          <a title="context dependent" class="termref" href="#dt-context-dependent">context dependent</a> for the given arity, then the returned function item has
               a captured context comprising
          the static and dynamic context of the named function reference.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>In practice, it is necessary to retain only those
            parts of the static and dynamic context that can affect the outcome. These means it is 
            unnecessary to retain parts of the context that no <a title="system function" class="termref" href="#dt-system-function">system function</a>
            depends on (for example, local variables), or parts that are invariant within an
            execution scope (for example, the implicit timezone).</p></div><div class="example">
<div class="exampleHeader"><a id="d3e18679"/>Example: A Context-Dependent Named Function Reference</div><p>Consider:</p><div class="exampleInner"><pre>let $f := &lt;foo/&gt;/fn:name#0 return &lt;bar/&gt;/$f()</pre></div><p>The function <code>fn:name()</code>, with no arguments, returns the name of the context node. The function
               item delivered by evaluating the expression <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-name"><code>fn:name#0</code></a> returns the name of the element that was the
               context node at the point where the function reference was evaluated (that is, the <code>&lt;foo&gt;</code> element).
               This expression therefore returns <code>"foo"</code>, not <code>"bar"</code>.</p></div><p>An error is raised if the identified function depends on components of the static or dynamic
                  context that are not present, or that have unsuitable values. For example [<a href="#ERRXPDY0002" title="err:XPDY0002">err:XPDY0002</a>] is raised for the expression <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-name"><code>fn:name#0</code></a>
                  if the context item is absent, and [<a href="https://www.w3.org/TR/xpath-functions/#ERRFODC0001" title="err:FODC0001">err:FODC0001</a>]<sup><small>FO</small></sup> is raised for the call <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-id"><code>fn:id#1</code></a> if the context item is not a node
                  in a tree that is rooted at a document node. The error that is raised is the same as the error that would
                  be raised by the corresponding function if called with the same static and dynamic context.</p><p>If the <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> and arity in a named function reference do not match the 
               name and <a title="arity range" class="termref" href="#dt-arity-range">arity range</a> of a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> in the
          static context, a static error is raised [<a href="#ERRXPST0017" title="err:XPST0017">err:XPST0017</a>].</p><p>
            The value of a <code>NamedFunctionRef</code>
               is a <a title="function item" class="termref" href="#dt-function-item">function item</a>
                  <var>FI</var> 
               obtained from <var>FD</var>
               as follows:
               </p><ul><li><p>
                           <b>name</b>: The name of <var>FD</var>.</p></li><li><p>
                           <b>identity</b>:</p><ul><li><p>If <var>FD</var> is <a title="context dependent" class="termref" href="#dt-context-dependent">context dependent</a> for the given arity, then a new function
                        identity distinct from the identity of any other function item.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>In the general case, a function reference to a context-dependent function
                           will produce different results every time it is evaluated, because the resulting function
                           item has a <b>captured context</b> 
                              (see <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#function-items">Section 7.1 Function Items</a><sup><small>DM</small></sup>) that includes the dynamic context
                           of the particular evaluation. Optimizers, however, are allowed to detect cases where
                           the captured context happens to be the same, or where any variations are immaterial,
                           and where it is therefore safe to return the same function item each time. This might be
                           the case, for example, where the only context dependency of a function is on the default
                           collation, and the default collation for both evaluations is known to be the same.</p></div></li><li><p>Otherwise, a function identity that is the same as that produced by the evaluation
                           of any other named function reference with the same function name and arity.</p><p>This rule applies even across different 
                                <a href="https://qt4cg.org/specifications/xpath-functions-40/#execution-scope">execution scopes</a><sup><small>FO</small></sup>:
                             for example if a parameter to a call to <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-transform"><code>fn:transform</code></a> is set to the
                             result of the expression <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-abs"><code>fn:abs#1</code></a>, then the function item passed as the parameter
                                value will be identical to that obtained by evaluating the expression <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-abs"><code>fn:abs#1</code></a>
                             within the target XSLT stylesheet.</p><p>This rule also applies when the target function definition is 
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/#dt-nondeterministic">nondeterministic</a><sup><small>FO</small></sup>. 
                           For example all evaluations of the named function reference <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-keys"><code>map:keys#2</code></a>
                           return identical function items, even though two evaluations of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-keys"><code>map:keys</code></a>
                           with the same arguments may produce different results.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>See also <a href="#id-function-identity"><b>4.5.7 Function Identity</b></a>.</p></div></li><li><p>
                           <b>arity</b>: As specified in the named function reference.</p></li><li><p>
                           <b>signature</b>: Formed from the required types of the first
                        <var>A</var> parameters of <var>FD</var>, and the function result type of
                        <var>FD</var>.</p></li><li><p>
                           <b>annotations</b>: The annotations of 
                           <var>FD</var>.</p></li><li><p>
                           <b>body</b>: The body of <var>FD</var>.</p></li><li><p>
                           <b>captured context</b>: Comprises the
                        static and dynamic context of the named function reference, augmented with
                        bindings of the names of parameters of <var>FD</var> beyond the
                        <var>A</var>’th parameter, to their respective default values.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>In practice, it is necessary to retain
                        only the parts of the context that the function actually depends on
                        (if any).</p></div></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>Consider the system function <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a>,
            which has an arity range of 2 to 5. The named function reference <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date#3</code></a>
            returns a function item whose three parameters correspond to the first three parameters
            of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a>; the remaining two arguments will take their default values.
               To obtain an arity-3 function that binds to arguments 1, 2, and 5 of <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a>,
            use the partial function application <code>format-date(?, ?, place := ?)</code>.</p></div><p>The following are examples of named function references:
          </p><ul><li><p>
                        <code>fn:abs#1</code> references the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-abs"><code>fn:abs</code></a> function which takes a single argument.</p></li><li><p>
                        <code>fn:concat#5</code> references the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-concat"><code>fn:concat</code></a> function which takes 5 arguments.</p></li><li><p>
                        <code>local:myfunc#2</code> references a function named <code>local:myfunc</code> which takes 2 arguments.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>Function items, as values in the data model, have a fixed arity, and
            a dynamic function call always supplies the arguments positionally. In effect, the result of evaluating <code>my:func#3</code> is the
            same as the result of evaluating the inline function expression <code>fn($x, $y, $z) { my:func($x, $y, $z) }</code>,
            except that the returned function has a name (it retains the name <code>my:func</code>).</p></div></div><div class="div3">
<h4><a id="id-inline-func"/>4.5.6 <a href="#id-inline-func" style="text-decoration: none">Inline Function Expressions</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-methods">⬇</a> <a href="#id-eval-dynamic-function-call">⬆</a></p><ol><li><p>
                  In inline function expressions, the keyword <code>function</code> may be abbreviated
                  as <code>fn</code>.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1192">1192</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1197">1197</a> 21 May 2024]</i></p></li><li><p>New abbreviated syntax is introduced 
                     (<a title="focus function" class="termref" href="#dt-focus-function">focus function</a>) 
                     for simple inline functions taking a single argument. 
                     An example is <code>fn { ../@code }</code>
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/503">503</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/521">521</a> 30 May 2023]</i></p></li><li><p>An inline function may be annotated
               as a <code>%method</code>, giving it access to its containing map.<i>  [Issues <a href="https://github.com/qt4cg/qtspecs/issues/1800">1800</a> <a href="https://github.com/qt4cg/qtspecs/issues/1845">1845</a> PRs <a href="https://github.com/qt4cg/qtspecs/pull/1817">1817</a> <a href="https://github.com/qt4cg/qtspecs/pull/1853">1853</a> 4 March 2025]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr"/><code><a href="#prod-xpath40-InlineFunctionExpr">InlineFunctionExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-InlineFunctionExpr-MethodAnnotation">MethodAnnotation</a>*  ("function"  |  "fn")  <a href="#doc-xpath40-InlineFunctionExpr-FunctionSignature">FunctionSignature</a>?  <a href="#doc-xpath40-InlineFunctionExpr-FunctionBody">FunctionBody</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-MethodAnnotation"/><code><a href="#prod-xpath40-MethodAnnotation">MethodAnnotation</a></code></td><td>::=</td><td><code>"%method"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-FunctionSignature"/><code><a href="#prod-xpath40-FunctionSignature">FunctionSignature</a></code></td><td>::=</td><td><code>"("  <a href="#doc-xpath40-InlineFunctionExpr-ParamList">ParamList</a>  ")"  <a href="#doc-xpath40-InlineFunctionExpr-TypeDeclaration">TypeDeclaration</a>?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-ParamList"/><code><a href="#prod-xpath40-ParamList">ParamList</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-InlineFunctionExpr-VarNameAndType">VarNameAndType</a> ** ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-VarNameAndType"/><code><a href="#prod-xpath40-VarNameAndType">VarNameAndType</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-InlineFunctionExpr-EQName">EQName</a>
                        <a href="#doc-xpath40-InlineFunctionExpr-TypeDeclaration">TypeDeclaration</a>?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-TypeDeclaration"/><code><a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a></code></td><td>::=</td><td><code>"as"  <a href="#doc-xpath40-InlineFunctionExpr-SequenceType">SequenceType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                        <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-FunctionBody"/><code><a href="#prod-xpath40-FunctionBody">FunctionBody</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-InlineFunctionExpr-EnclosedExpr">EnclosedExpr</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InlineFunctionExpr-EnclosedExpr"/><code><a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a></code></td><td>::=</td><td><code>"{"  <a href="#prod-xpath40-Expr">Expr</a>?  "}"</code></td></tr></tbody></table><p>
                  [<a id="dt-inline-func" title="inline function expression">Definition</a>: An 
                  <b>inline function expression</b>
                     <span>, when evaluated,</span> creates
          an <a title="anonymous function" class="termref" href="#dt-anonymous-function">anonymous function</a>
          defined directly in the inline function expression.] 
               An inline function expression specifies the names and SequenceTypes of the parameters to the function,
          the SequenceType of the result, and the body of the function.</p><p>An <a title="inline function expression" class="termref" href="#dt-inline-func">inline function expression</a> whose 
               <a href="#prod-xpath40-FunctionSignature">FunctionSignature</a> is omitted
               is known as a <a title="focus function" class="termref" href="#dt-focus-function">focus function</a>. Focus functions are 
               described in <a href="#id-focus-functions"><b>4.5.6.2 Focus Functions</b></a>.</p><p>
                  [<a id="dt-anonymous-function" title="anonymous function">Definition</a>: 
             An <b>anonymous function</b> is a <a title="function item" class="termref" href="#dt-function-item">function item</a> with no name.  
             Anonymous functions may be created, for example, by evaluating an inline function 
             expression or by partial function application.]
               </p><p>The keywords <code>function</code> and <code>fn</code> 
               are synonymous.</p><p>The syntax allows the names and types of the function argument to be declared, along
           with the type of the result:</p><div class="exampleInner"><pre>function($x as xs:integer, $y as xs:integer) as xs:integer { $x + $y }</pre></div><p>The types can be omitted<span>, and the keyword can be abbreviated</span>:</p><div class="exampleInner"><pre>fn($x, $y) { $x + $y }</pre></div><p>A zero-arity function can be written as, for example, <code>fn() { current-date() }</code>.</p><p>
          If a function parameter is declared using a name but no type, its default type is <code>item()*</code>.
                  
               If the result type is omitted, its default result type is <code>item()*</code>.
          </p><p>
          The parameters of an inline function expression are considered to be variables whose scope is the function body. It is a static error
          [<a href="#ERRXQST0039" title="err:XQST0039">err:XQST0039</a>] for an inline function expression to have more than one parameter with the same name.
          </p><p class="xpath">The annotation keyword <code>%method</code> is described
            in <a href="#id-methods"><b>4.5.6.1 Methods</b></a>.</p><p>
          The static context for the function body is inherited from the location of the inline function expression.
          </p><p>
          The variables in scope for the function body include all variables representing the function parameters, as well as all variables that
          are in scope for the inline function expression.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>
            Function parameter names can mask variables that would otherwise be in scope for the function body.
          </p></div><p>The result of an inline function expression is a single function item
                  with the following properties (as defined in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#function-items">Section 7.1 Function Items</a><sup><small>DM</small></sup>):</p><ul><li><p>
                        <b>name</b>:
              Absent.
            </p></li><li><p>
                        <b>identity</b>: A new function
                              identity distinct from the identity of any other function item.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>See also <a href="#id-function-identity"><b>4.5.7 Function Identity</b></a>.</p></div></li><li><p>
                        <b>signature</b>:
              A <code>FunctionType</code>
              constructed from the
              
                           <code>SequenceType</code>s in the <code>InlineFunctionExpr</code>.
	      An implementation which can determine a more specific signature (for example, through use of type analysis of the function’s body) is permitted to do so.
            </p></li><li><p>
                        <b>annotations</b>: 
                              <span class="xpath">If the keyword <code>%method</code> is present, then a list containing
                                 a single annotation whose name is a QName with local name <code>"method"</code>
                                 and namespace <code>"http://www.w3.org/2012/xquery"</code> and whose
                                 value is an empty sequence; otherwise an empty set.</span>.</p></li><li><p>
                        <b>body</b>:
              The <code>FunctionBody</code> of the <code>InlineFunctionExpr</code>.
            </p></li><li><p>
                        <b>captured context</b>: the static context
                           is the static context of the inline function expression. The dynamic context has an absent
                           <a title="focus" class="termref" href="#dt-focus">focus</a>, and a set of variable bindings
                           comprising the <a title="variable values" class="termref" href="#dt-variable-values">variable values</a> component
                           of the dynamic context of the <code>InlineFunctionExpr</code>.
            </p></li></ul><p>The following are examples of some inline function expressions:</p><ul><li><p>This example creates a function that takes no arguments and returns a sequence of the first 6 primes:
                </p><div class="exampleInner"><pre>function() as xs:integer+ { 2, 3, 5, 7, 11, 13 }</pre></div></li><li><p>This example creates a function that takes two <code>xs:double</code> arguments and returns their product:
                </p><div class="exampleInner"><pre>fn($a as xs:double, $b as xs:double) as xs:double { $a * $b }</pre></div></li><li><p>This example creates and invokes a function that captures the value of a local variable in its scope:
                        </p><div class="exampleInner"><pre>let $incrementors := (
  for $x in 1 to 10
  return function($y) as xs:integer { $x + $y }
)
return $incrementors[2](4)</pre></div><p>The result of this expression is <code>6</code>
                     </p></li></ul><div class="div4">
<h5><a id="id-methods"/>4.5.6.1 <a href="#id-methods" style="text-decoration: none">Methods</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#axes">⬇</a> <a href="#id-inline-func">⬆</a></p><ol><li><p>An inline function may be annotated
               as a <code>%method</code>, giving it access to its containing map.<i>  [Issues <a href="https://github.com/qt4cg/qtspecs/issues/1800">1800</a> <a href="https://github.com/qt4cg/qtspecs/issues/1845">1845</a> PRs <a href="https://github.com/qt4cg/qtspecs/pull/1817">1817</a> <a href="https://github.com/qt4cg/qtspecs/pull/1853">1853</a> 4 March 2025]</i></p></li></ol></div><p>
                     [<a id="dt-method" title="method">Definition</a>: A <b>method</b> is a
               function item that has the annotation <code>%method</code>.]
                  </p><p>A method appearing as a value within a map can refer to the containing map as
                  the <a title="context value" class="termref" href="#dt-context-value">context value</a>. For example, given the variable:</p><div class="exampleInner"><pre>
let $rectangle := {
  'height': 3,
  'width': 4,
  'area': %method fn() { ?height × ?width }
}</pre></div><p>The dynamic function call <code>$rectangle?area()</code> returns <code>12</code>.</p><p>The detailed rule is as follows: When the lookup operator <code>?</code> or <code>??</code>
                  is applied to a map <var>M</var> (see <a href="#id-lookup"><b>4.13.3 Lookup Expressions</b></a>), 
                     and all the following conditions apply:</p><ul><li><p>The <code>Modifier</code> in the lookup expression is <code>items</code>,
                  explicitly or by default;</p></li><li><p>The <code>KeySpecifier</code> in the lookup expression is an
                  <code>NCName</code> or <code>StringLiteral</code>;</p></li><li><p>The lookup expression selects a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> value that
                  is a function item <var>F</var> declared as a <a title="method" class="termref" href="#dt-method">method</a>
                        </p></li></ul><p>then the function item <var>F′</var> that is returned by the lookup expression is
                  derived from <var>F</var> as follows:</p><ul><li><p>
                           <var>F′</var> has a <b>captured context</b> in which 
                        the <a title="focus" class="termref" href="#dt-focus">focus</a> is a <a title="singleton focus" class="termref" href="#dt-singleton-focus">singleton focus</a> 
                        based on the map <var>M</var>.
                     </p></li><li><p>
                           <var>F′</var>
                     has no <code>%method</code> annotation and is therefore not a <a title="method" class="termref" href="#dt-method">method</a>.
                     </p></li><li><p>
                           <var>F′</var>
                     has a separate function identity from <var>F</var>. It is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation   dependent</a>
                     whether repeated evaluations of lookup expressions with the same map and the same
                     key produce functions having the same function identity.
                     </p></li><li><p>In all other respects <var>F′</var> has the same properties as <var>F</var>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>Methods are typically invoked using a dynamic function call such as the call
                  <code>$rectangle?area()</code> in the example above. However, a method selected
                     using a lookup expression such as <code>$rectangle?area</code> can
                     be used in the same way as any other function item. For example,
                  it can be passed as an argument to a higher-order function, or it can be partially
                  applied.</p><p>Although methods mimic some of the capability of object-oriented
                  languages, the functionality is more limited:</p><ul><li><p>There is no encapsulation: the entries in a map are all publicly
                     exposed.</p></li><li><p>There is no class hierarchy, and no inheritance or overriding.</p></li><li><p>Methods within a map can be removed or replaced in the same way as
                     any other entries in the map.</p></li></ul><p>The context value in the body of a method is bound to the containing
                  map by any lookup expression (using the <code>?</code> or <code>??</code> operators)
                  that selects the method as a singleton item within a key/value pair. It is not
                  bound by other operations that deliver the value, for example a call on
                  <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-get"><code>map:get</code></a> or <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-for-each"><code>map:for-each</code></a>.
                  The result of the lookup expression is not itself a method.
                  The means, for example that the expression:</p><div class="exampleInner"><pre>

let $rectangle1 := { 'x':3, 'y':4, 'area': %method fn() { ?x × ?y } }
let $rectangle2 := { 'x':5, 'y':5, 'area': $rectangle1?area }
return $rectangle2?area()</pre></div><p>returns 12, because the function item bound to the key <code>"area"</code> in 
                  <code>$rectangle2</code> is an ordinary function item, not a method.</p><p>If the same method is to be used in both variables, this can be written:
                  </p><div class="exampleInner"><pre>
let $area := %method fn() { ?x × ?y }
let $rectangle1 := { 'x':3, 'y':4, 'area': $area }
let $rectangle2 := { 'x':5, 'y':5, 'area': $area }
return $rectangle2?area()</pre></div><p>which returns 25.</p><p>An attempt to evaluate a method without binding a context item
                     will typically result in a dynamic error indicating that the context
                     item is not bound. Given the declaration above, the expression
                     <code>$area()</code> raises a type error [<a href="#ERRXPDY0002" title="err:XPDY0002">err:XPDY0002</a>]
                     because a unary lookup expression (<code>?x</code> or <code>?y</code>)
                     requires the context value to be bound.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Methods can be useful when there is a need to write inline recursive
                  functions. For example:</p><div class="exampleInner"><pre>
let $lib := {
  'product': %method fn($in as xs:double*) {
    if (empty( $in ))
    then 1
    else head($in) × ?product(tail($in))
  }
}
return $lib?product((1.2, 1.3, 1.4))</pre></div><p>In an environment that supports XPath but not XQuery, this mechanism can be used 
                  to define all the functions that a particular XPath expression needs to invoke,
                  and these functions can be mutually recursive.</p></div></div><div class="div4">
<h5><a id="id-focus-functions"/>4.5.6.2 <a href="#id-focus-functions" style="text-decoration: none">Focus Functions</a></h5><p>
                     [<a id="dt-focus-function" title="focus function">Definition</a>: A <b>focus function</b>
               is an inline function expression in which the function signature is implicit: the function takes
               a single argument of type <code>item()*</code> (that is, any value), and binds this to the 
                  context value when evaluating
               the function body, which returns a result of type <code>item()*</code>.]
                  </p><p>Here are some examples of focus functions:</p><ul><li><p>
                           <code>fn { @age }</code> - a function that expects a node as its argument, and returns
                  the <code>@age</code> attribute of that node.</p></li><li><p>
                           <code>fn { . + 1 }</code> - a function that expects a number as its argument, and returns
                  that number plus one.</p></li><li><p>
                           <code>function { `${ . }` }</code> - a function that expects a string as its argument, and prepends
                     a <code>"$"</code> character.</p></li><li><p>
                           <code>function { head(.) + foot(.) }</code> - a function that expects a sequence of numbers
                     as its argument, and returns the sum of the first and last items in the sequence.</p></li></ul><p>Focus functions are often useful as arguments to simple higher-order functions such as <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-sort"><code>fn:sort</code></a>.
               For example, to sort employees by salary, write <code>sort(//employee, (), fn { +@salary })</code>.
               (The unary plus has the effect of converting the attribute’s value to a number, for numeric sorting).</p><p>Focus functions can also be useful on the right-hand side of the <a title="sequence arrow operator" class="termref" href="#dt-sequence-arrow-operator">sequence arrow operator</a>
                  and <a title="mapping arrow operator" class="termref" href="#dt-mapping-arrow-operator">mapping arrow operator</a>.
               For example, <code>$s =&gt; tokenize() =!&gt; fn { `"{.}"` }()</code> first tokenizes the string <code>$s</code>,
               then wraps each token in double quotation marks.</p><p>The result of calling the <code>function { EXPR }</code> (or <code>fn { EXPR }</code>), with
                  a single argument whose value is <var>$Z</var> arguments, is obtained by evaluating <code>EXPR</code>
                  with a <a title="fixed focus" class="termref" href="#dt-fixed-focus">fixed focus</a> in which the context value is <var>$Z</var>, the context position is 1 (one),
                  and the context size is 1 (one).</p><p>The expression <code>function { EXPR }</code> is thus formally equivalent to the expression
               <code>function($Z as item()*) as item()* { $Z -&gt; (EXPR) }</code>, where <code>$Z</code> is some variable name
               that is otherwise unused. Here <code>-&gt;</code> is the pipeline operator described
               in <a href="#id-pipeline-operator"><b>4.18 Pipeline operator</b></a>.</p><p>For example, the expression <code>every(1 to 10, fn { . gt 0 })</code> returns <code>true</code>.</p><p>A focus function cannot be annotated as a <a title="method" class="termref" href="#dt-method">method</a> 
                  ([<a href="#ERRXPST0107" title="err:XPST0107">err:XPST0107</a>]). (This is because the context value
               in a method is bound to the containing map, and is therefore not available to be bound to the function’s argument).</p></div></div><div class="div3">
<h4><a id="id-function-identity"/>4.5.7 <a href="#id-function-identity" style="text-decoration: none">Function Identity</a></h4><p>It is sometimes useful to be able to establish whether two variables refer to the same function
               or to different functions. For this purpose, every function item has an identity. Functions with the
               same identity are indistinguishable in every way; in particular, any function call with identical
               arguments will produce an identical result.</p><p>In general, evaluation of an expression that returns a function item other than one that was
               present in its operands delivers a function item whose identity is unique, and thus distinct
               from any other function item. There are two exceptions to this rule:</p><ul><li><p>Evaluating a function reference such as <code>count#1</code> returns the same function
                  every time. Specifically, if the function name identifies a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
                  that is not <a title="context dependent" class="termref" href="#dt-context-dependent">context dependent</a> (which is the most usual case), then all 
                     function references using this function name and arity return the same function.
                  For more details see <a href="#id-named-function-ref"><b>4.5.5 Named Function References</b></a>.</p></li><li><p>An optimizer is permitted to rewrite <a href="https://qt4cg.org/specifications/xpath-functions-40/#dt-deterministic">deterministic</a><sup><small>FO</small></sup>
                     expressions in such a way that repeated evaluation is avoided, and this may be
                     done without consideration of function identity. For example:</p><ul><li><p>If the expression
                           <code>contains(?, "e")</code> appears within the body of a <code>for</code>
                           clause, or if the same expression is written repeatedly in a query, then an
                           optimizer may decide to evaluate it once only, and thus return the same function
                           item each time.</p></li><li><p>Similarly, if the expression <code>fn($x) { $x + 1 }</code>
                           appears more than once, or is evaluated repeatedly, then it may return the same
                           function each time.</p></li><li><p>Optimizers are allowed to replace any expression with an
                           equivalent expression. For example, <code>count(?)</code> may be rewritten as
                           <code>count#1</code>. Similarly, <code>fn($x) { $x + 1 }</code> may be
                           rewritten as <code>fn($y) { $y + 1 }</code>. This may lead to different
                           expressions returning identical function items.</p></li><li><p>In principle, two function items are not identical if they
                           differ in their captured context. Optimizers, however, will often
                           be able to eliminate parts of the captured context that a function does
                           not actually use. For example, an inline function expression delivers
                           a function item whose captured context includes the values of all nonlocal
                           in-scope variables; but in practice the implementation is unlikely to retain the
                           values of such variables unless they are actually referenced.
                        </p></li></ul></li></ul></div></div><div class="div2">
<h3><a id="id-path-expressions"/>4.6 <a href="#id-path-expressions" style="text-decoration: none">Path Expressions</a></h3><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PathExpr"/><code><a href="#prod-xpath40-PathExpr">PathExpr</a></code></td><td>::=</td><td><code>("/"  <a href="#doc-xpath40-PathExpr-RelativePathExpr">RelativePathExpr</a>?)<br/>|  ("//"  <a href="#doc-xpath40-PathExpr-RelativePathExpr">RelativePathExpr</a>)<br/>|  <a href="#doc-xpath40-PathExpr-RelativePathExpr">RelativePathExpr</a>
                  </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-leading-lone-slash">xgc: leading-lone-slash</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PathExpr-RelativePathExpr"/><code><a href="#prod-xpath40-RelativePathExpr">RelativePathExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-StepExpr">StepExpr</a>  (("/"  |  "//")  <a href="#doc-xpath40-StepExpr">StepExpr</a>)*</code></td></tr></tbody></table><p>
               [<a id="dt-path-expression" title="path expression">Definition</a>: A path expression consists of a series of one or more
	              <a title="step" class="termref" href="#dt-step">steps</a>, separated by <code>/</code> or
	 <code>//</code>, and optionally beginning with
               <code>/</code> or <code>//</code>.  
               A <b>path expression</b> is typically used to locate nodes
               within trees. ]
            </p><p>Absolute path expressions (those starting with an initial <code>/</code>
            or <code>//</code>), start their selection from the root node of a tree;
            relative path expressions (those without a leading <code>/</code> or
            <code>//</code>) start from the <a title="context value" class="termref" href="#dt-context-value">context value</a>.</p><p>A path expression consisting of a single step is evaluated as
            described in <a href="#id-steps"><b>4.6.4 Steps</b></a>.</p><div class="div3">
<h4><a id="id-absolute-path-expressions"/>4.6.1 <a href="#id-absolute-path-expressions" style="text-decoration: none">Absolute Path Expressions</a></h4><p>A path expression consisting of <code>/</code> on its own
            is treated as an abbreviation for <code>/.</code>.</p><p>An expression of the form <code>/<var>PP</var>
                  </code> (that is, a path expression
            with a leading <code>/</code>) is treated as an abbreviation for
	 the expression <code>self::node()/(fn:root(.) treat as document-node())/<var>PP</var>
                  </code>. 
            The effect of this expansion is that for every item <var>J</var> 
            in the context value <var>V</var>:</p><ol class="enumar"><li><p>A <a title="type error" class="termref" href="#dt-type-error">type error</a> occurs if <var>J</var> is not a node
	       [<a href="#ERRXPTY0020" title="err:XPTY0020">err:XPTY0020</a>].</p></li><li><p>The root node <var>R</var> of the tree containing <var>J</var> is selected.</p></li><li><p>A <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> occurs if <var>R</var> is not a document node
	       [<a href="#ERRXPDY0050" title="err:XPDY0050">err:XPDY0050</a>].</p></li><li><p>The expression that follows the leading <code>/</code> is evaluated with 
	       <var>R</var> as the context value.</p></li></ol><p>The results of these multiple evaluations are then combined into a single sequence;
       if the result is a set of nodes, the nodes are delivered in document order with
       duplicates eliminated.</p><div class="note"><p class="prefix"><b>Note:</b></p><p id="Chg-slash-note">The <code>/</code> character
	 can be used either as a complete path expression or as the
	 beginning of a longer path expression such as
	 <code>/*</code>.  Also, <code>*</code>
	 is both the multiply operator and a wildcard in path
	 expressions.  This can cause parsing difficulties when
	 <code>/</code> appears on the left-hand side of
	 <code>*</code>.  This is resolved using the <a href="#parse-note-leading-lone-slash">leading-lone-slash
	 </a> constraint.  For example, <code>/*</code> and <code>/
	 *</code> are valid path expressions containing wildcards,
	 but <code>/*5</code> and <code>/ * 5</code> raise syntax
	 errors. Parentheses must be used when <code>/</code> is
	 used on the left-hand side of an operator that could be confused with a node test, as in <code>(/) * 5</code>. Similarly, <code>4 + / *
	 5</code> raises a syntax error, but <code>4 + (/) * 5</code> is a valid expression.
	 The expression <code>4 + /</code> is also
	 valid, because <code>/</code> does not occur on the left-hand
	 side of the operator.</p><p>Similarly, in the expression <code>/
	 union /*</code>, <code>union</code> is interpreted as an element name
	 rather than an operator. For it to be parsed as an operator,
	 the expression should be written <code>(/)
	 union /*</code>.</p></div><p>An expression of the form <code>//<var>PP</var>
                  </code> (that is, a path expression
            with a leading <code>//</code>) is treated as an abbreviation for
            the expression <code>self::node()/(fn:root(.) treat as document-node())/descendant-or-self:node()/<var>PP</var>
                  </code>. 
            The effect of this expansion is that for every item <var>J</var> 
            in the context value <var>V</var>:</p><ol class="enumar"><li><p>A <a title="type error" class="termref" href="#dt-type-error">type error</a> occurs if <var>J</var> is not a node
               [<a href="#ERRXPTY0020" title="err:XPTY0020">err:XPTY0020</a>].</p></li><li><p>The root node <var>R</var> of the tree containing <var>J</var> is selected.</p></li><li><p>A <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> occurs if <var>R</var> is not a document node
               [<a href="#ERRXPDY0050" title="err:XPDY0050">err:XPDY0050</a>].</p></li><li><p>The descendants of <var>R</var> are selected, along with <var>R</var> itself.</p></li><li><p>For every node <var>D</var> in this set of nodes, the expression that 
               follows the leading <code>//</code> is evaluated with <var>D</var> as the context value.</p></li></ol><p>The results of these multiple evaluations are then combined into a single sequence;
            if the result is a set of nodes, the nodes are delivered in document order with
            duplicates eliminated.</p><p>If the context value is not a node, a
	 <a title="type error" class="termref" href="#dt-type-error">type error</a> is
	 raised [<a href="#ERRXPTY0020" title="err:XPTY0020">err:XPTY0020</a>]. At evaluation time, if the
	 root node of the context node is not a document node, a
	 <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> is
	 raised [<a href="#ERRXPDY0050" title="err:XPDY0050">err:XPDY0050</a>].</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The descendants of a node do not include attribute
	 nodes<span class="xpath"> or namespace nodes</span>. However, the rules for expanding <code>//</code>
            ensure that <code>.//@*</code> selects all attributes of all descendants<span class="xpath">,
            and similarly <code>.//namespace::*</code> selects all namespaces of all descendants.</span>
                  </p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>
                     <code>//</code> on its own is not a valid expression.</p></div></div><div class="div3">
<h4><a id="id-relative-path-expressions"/>4.6.2 <a href="#id-relative-path-expressions" style="text-decoration: none">Relative Path Expressions</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RelativePathExpr"/><code><a href="#prod-xpath40-RelativePathExpr">RelativePathExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-RelativePathExpr-StepExpr">StepExpr</a>  (("/"  |  "//")  <a href="#doc-xpath40-RelativePathExpr-StepExpr">StepExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RelativePathExpr-StepExpr"/><code><a href="#prod-xpath40-StepExpr">StepExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-PostfixExpr">PostfixExpr</a>  |  <a href="#doc-xpath40-AxisStep">AxisStep</a>
                     </code></td></tr></tbody></table><p>
          A relative path expression is a path expression that selects
          nodes within a tree by following a series of steps starting
          at the nodes in the context value (which may be any kind of node,
          not necessarily the root of the tree).
          </p><p>
          Each non-initial occurrence of <code>//</code> in a path expression is
          expanded as described in <a href="#abbrev"><b>4.6.7 Abbreviated Syntax</b></a>, leaving a
          sequence of steps separated by <code>/</code>. This sequence of steps
          is then evaluated from left to right. So a path such as
          <code>
                     <var>E<sub>1</sub></var>/<var>E<sub>2</sub></var>/<var>E<sub>3</sub></var>/<var>E<sub>4</sub></var>
                  </code> is evaluated
          as <code>((<var>E<sub>1</sub></var>/<var>E<sub>2</sub></var>)/<var>E<sub>3</sub></var>)/<var>E<sub>4</sub></var>
                  </code>. 
               The semantics of a path
          expression are thus defined by the semantics of the
          binary <code>/</code> operator, which is defined in
          <a href="#id-path-operator"><b>4.6.3 Path operator (/)</b></a>.
          </p><div class="note"><p class="prefix"><b>Note:</b></p><p>
         Although the semantics describe the evaluation of a path with
         more than two steps as proceeding from left to right, the <code>/</code>
         operator is in most cases associative, so evaluation from
         right to left usually delivers the same result. The cases
         where <code>/</code> is not associative arise when the functions
         <code>fn:position()</code> and <code>fn:last()</code> are
         used: <code>A/B/position()</code> delivers a sequence of
         integers from 1 to the size of <code>(A/B)</code>, whereas
         <code>A/(B/position())</code> restarts the counting at each <code>B</code> element.
         </p></div><p>The following example illustrates the use of relative path expressions.
            In each case it is assumed that the context value is a single node,
            referred to as the context node.</p><div class="example"><ul><li><p>
                           <code>child::div1/child::para</code>
                        </p><p>Selects the
	     <code>para</code> element children of the <code>div1</code>
	     element children of the context node; that is, the
	     <code>para</code> element grandchildren of the context node
	     that have <code>div1</code> parents.</p></li></ul></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Since each step in a path provides context nodes for the following step, 
                  in effect, only the last step in a path is allowed to return a sequence of non-nodes.</p></div></div><div class="div3">
<h4><a id="id-path-operator"/>4.6.3 <a href="#id-path-operator" style="text-decoration: none">Path operator (<code>/</code>)</a></h4><p>The path operator <code>/</code> is primarily used for 
                  locating nodes within trees. Its left-hand operand must return 
                  a sequence of nodes. The result of the operator is either a sequence of nodes
                  (in document order, with no duplicates), or a sequence of non-nodes.</p><p>The operation <code>
                     <var>E<sub>1</sub></var>/<var>E<sub>2</sub></var>
                  </code> is evaluated as follows: Expression <var>E<sub>1</sub></var> 
                  is evaluated, and if the result is not a (possibly empty) sequence <var>S</var> of nodes, 
                  a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0019" title="err:XPTY0019">err:XPTY0019</a>]. Each node in <var>S</var> then serves in turn to provide an inner focus 
                  (the node as the context value, its position in <var>S</var> as the context 
                  position, the length of <code>S</code> as the context size) for an evaluation 
                  of <var>E<sub>2</sub></var>, as described in  <a href="#eval_context"><b>2.2.2 Dynamic Context</b></a>. The sequences resulting from all the evaluations of <var>E<sub>2</sub></var> 
                  are combined as follows:</p><ol class="enumar"><li><p>If every evaluation of <var>E<sub>2</sub></var> returns a (possibly empty) sequence of nodes, 
                        these sequences are combined, and duplicate nodes are eliminated based on node identity.
                        The resulting node sequence is returned in <a title="document order" class="termref" href="#dt-document-order">document order</a>.
                     </p></li><li><p>If every evaluation of <var>E<sub>2</sub></var> returns a (possibly empty) 
                        sequence of non-nodes, these sequences are concatenated, in order, and returned.
                        The returned sequence preserves the orderings within and among the subsequences 
                        generated by the evaluations of <var>E<sub>2</sub></var>.
                     </p></li><li><p>If the multiple evaluations of <var>E<sub>2</sub></var> return at least one node and at least one non-node, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0018" title="err:XPTY0018">err:XPTY0018</a>].</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>The semantics of the path operator can also be defined using the simple 
                     map operator (<code>!</code>) as follows (the function
                     <code>fn:distinct-ordered-nodes($R)</code> has the effect of 
                     eliminating duplicates and sorting nodes into document order):</p><div class="exampleInner"><pre>let $R := E1 ! E2
return if (every $r in $R satisfies $r instance of node())
       then (fn:distinct-ordered-nodes($R))
       else if (every $r in $R satisfies not($r instance of node()))
       then $R
       else error()</pre></div><p>For a table comparing the step operator to the map operator, see <a href="#id-map-operator"><b>4.19 Simple map operator (!)</b></a>.</p></div></div><div class="div3">
<h4><a id="id-steps"/>4.6.4 <a href="#id-steps" style="text-decoration: none">Steps</a></h4>
<a id="d3e20074"/><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StepExpr"/><code><a href="#prod-xpath40-StepExpr">StepExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-StepExpr-PostfixExpr">PostfixExpr</a>  |  <a href="#doc-xpath40-StepExpr-AxisStep">AxisStep</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StepExpr-PostfixExpr"/><code><a href="#prod-xpath40-PostfixExpr">PostfixExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-PrimaryExpr">PrimaryExpr</a>  |  <a href="#doc-xpath40-FilterExpr">FilterExpr</a>  |  <a href="#doc-xpath40-DynamicFunctionCall">DynamicFunctionCall</a>  |  <a href="#doc-xpath40-LookupExpr">LookupExpr</a>  |  <a href="#doc-xpath40-FilterExprAM">FilterExprAM</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StepExpr-AxisStep"/><code><a href="#prod-xpath40-AxisStep">AxisStep</a></code></td><td>::=</td><td><code>(<a href="#prod-xpath40-ReverseStep">ReverseStep</a>  |  <a href="#prod-xpath40-ForwardStep">ForwardStep</a>)  <a href="#prod-xpath40-Predicate">Predicate</a>*</code></td></tr></tbody></table><p>
                  [<a id="dt-step" title="step">Definition</a>: A <b>step</b> is a part of a <a title="path expression" class="termref" href="#dt-path-expression">path expression</a> that generates a sequence of items
		and then filters the sequence by zero or more
		<a title="" class="termref" href="#dt-predicate">predicates</a>. The value of the step
		consists of those items that satisfy the
		predicates, working from left to right. A step may be either an <a title="axis step" class="termref" href="#dt-axis-step">axis step</a> or a postfix expression.] Postfix expressions are described in <a href="#id-postfix-expression"><b>4.3 Postfix Expressions</b></a>.</p><p>
                  [<a id="dt-axis-step" title="axis step">Definition</a>: An <b>axis step</b> returns a sequence of nodes that are 
                  reachable from a starting node via a specified axis. Such a step has two parts: an
		<b>axis</b>, which defines the "direction of
		movement" for the step, and a <a title="node test" class="termref" href="#dt-node-test">node test</a>,
		which selects nodes based on their kind, name, and/or
		<a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> .]
               </p><p>If the context value is a 
               sequence of zero or more nodes, an axis
		step returns a sequence of zero or more
		nodes; otherwise, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is
		raised [<a href="#ERRXPTY0020" title="err:XPTY0020">err:XPTY0020</a>].</p><p>The step expression <code>S</code> is equivalent to <code>./S</code>.
               Thus, if the context value is a sequence containing multiple nodes,
               the semantics of a step expression are equivalent to a path expression
               in which the step is always applied to a single node. The following
               description therefore explains the semantics for the case where
               the context value is a single node, called the context node.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The equivalence of a step <code>S</code> to the
            path expression <code>./S</code> means that 
            the resulting node sequence is returned in <a title="document order" class="termref" href="#dt-document-order">document
	         order</a>.</p></div><p>An axis step may be either a <b>forward
		step</b> or a <b>reverse step</b>, followed
		by zero or more <a title="" class="termref" href="#dt-predicate">predicates</a>.</p><p>In the <b>abbreviated syntax</b> for a step, the axis can
		be omitted and other shorthand notations can be used as described in
		<a href="#abbrev"><b>4.6.7 Abbreviated Syntax</b></a>.</p><p>The unabbreviated syntax for an axis step consists of the axis name
		and node test separated by a double colon. The result of the step consists of the nodes
		reachable from the starting node via the specified axis that have the node kind, name,
		and/or <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> specified by the node test. For example, the
		step <code>child::para</code> selects the <code>para</code> element children of the context node: <code>child</code> is the name of the axis, and <code>para</code> is the name of the element nodes to be selected on this axis. The available axes are described in <a href="#axes"><b>4.6.4.1 Axes</b></a>. The
		available node tests are described in <a href="#node-tests"><b>4.6.4.2 Node Tests</b></a>. Examples of
		steps are provided in <a href="#unabbrev"><b>4.6.6 Unabbreviated Syntax</b></a> and <a href="#abbrev"><b>4.6.7 Abbreviated Syntax</b></a>.</p><div class="div4">
<h5><a id="axes"/>4.6.4.1 <a href="#axes" style="text-decoration: none">Axes</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#node-tests">⬇</a> <a href="#id-methods">⬆</a></p><ol><li><p>
                     Four new axes have been defined: <code>preceding-or-self</code>, <code>preceding-sibling-or-self</code>,
                     <code>following-or-self</code>, and <code>following-sibling-or-self</code>.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1519">1519</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1532">1532</a> 29 October 2024]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForwardAxis"/><code><a href="#prod-xpath40-ForwardAxis">ForwardAxis</a></code></td><td>::=</td><td><code>("attribute"<br/>|  "child"<br/>|  "descendant"<br/>|  "descendant-or-self"<br/>|  "following"<br/>|  "following-or-self"<br/>|  "following-sibling"<br/>|  "following-sibling-or-self"<br/>|  "namespace"<br/>|  "self")  "::"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ReverseAxis"/><code><a href="#prod-xpath40-ReverseAxis">ReverseAxis</a></code></td><td>::=</td><td><code>("ancestor"<br/>|  "ancestor-or-self"<br/>|  "parent"<br/>|  "preceding"<br/>|  "preceding-or-self"<br/>|  "preceding-sibling"<br/>|  "preceding-sibling-or-self")  "::"</code></td></tr></tbody></table><p class="xpath">XPath defines a set of <b>axes</b>
		  for traversing documents, but a <b>host language</b> may define a subset
		  of these axes. The following axes are defined:</p><ul><li><p>The <code>child</code> axis
				contains the children of the context
				node, which are the nodes returned by the
				<a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dm-children">Section 6.7.3 children Accessor</a><sup><small>DM</small></sup>.
                                </p><div class="note"><p class="prefix"><b>Note:</b></p><p>Only document nodes and element
                                                  nodes have
                                                  children. If the
                                                  context node is any
                                                  other kind of node,
                                                  or if the context
                                                  node is an empty
                                                  document or element
                                                  node, then the child
                                                  axis is an empty
                                                  sequence.  The
                                                  children of a
                                                  document node or
                                                  element node may be
                                                  element, processing
                                                  instruction,
                                                  comment, or text
                                                  nodes.  Attribute<span class="xpath">,
                                                  namespace,</span> and
                                                  document nodes can
                                                  never appear as
                                                  children.</p></div></li><li><p>The <code>descendant</code>
			 axis is defined as the transitive closure of
			 the child axis; it contains the descendants
			 of the context node (the children, the children of the children, and so on).</p><p>More formally, <code>$node/descendant::node()</code> delivers the result
                     of <code>fn:transitive-closure($node, fn { child::node() })</code>.</p></li><li><p>The <code>descendant-or-self</code> axis contains the context node and the descendants of the context
				node.</p><p>More formally, <code>$node/descendant-or-self::node()</code> delivers the result
                     of <code>$node/(. | descendant::node())</code>.</p></li><li><p>The <code>parent</code>
                                          axis contains the sequence
                                          returned by the
				          <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dm-parent">Section 6.7.11 parent Accessor</a><sup><small>DM</small></sup>, 
                                          which returns
                                          the parent of the context
                                          node, or an empty sequence
                                          if the context node has no
                                          parent.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>An attribute node may have an element node as its parent, even though the attribute node is not a child of the element node.</p></div></li><li><p>The
                                        <code>ancestor</code> axis is
                                        defined as the transitive
                                        closure of the parent axis; it
                                        contains the ancestors of the
                                        context node (the parent, the
                                        parent of the parent, and so
                                        on).</p><p>More formally, <code>$node/ancestor::node()</code> delivers the result
                     of <code>fn:transitive-closure($node, fn { parent::node() })</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The ancestor axis
                                        includes the root node of the
                                        tree in which the context node
                                        is found, unless the context
                                        node is the root node.</p></div></li><li><p>The <code>ancestor-or-self</code> axis contains the context node and the ancestors of the context node;
				thus, the ancestor-or-self axis will always include the root node.</p><p>More formally, <code>$node/ancestor-or-self::node()</code> delivers the result
                     of <code>$node/(. | ancestor::node())</code>.</p></li><li><p>The <code>following-sibling</code>
			 axis contains the context node’s following
			 siblings, that is, those children of the context
			 node’s parent that occur after the context
			 node in <a title="document order" class="termref" href="#dt-document-order">document order</a>. If the context node
			 is an attribute or namespace node, the
			 <code>following-sibling</code> axis is
			 empty.</p><p>More formally, <code>$node/following-sibling::node()</code> delivers the result
                     of <code>fn:siblings($node)[. &gt;&gt; $node])</code>.</p></li><li><p>The <code>following-sibling-or-self</code> axis contains the context node,
                     together with the contents of the <code>following-sibling</code> axis.</p><p>More formally, <code>$node/following-sibling-or-self::node()</code> delivers the result
                     of <code>fn:siblings($node)[not(. &lt;&lt; $node)]</code>
                        </p></li><li><p>The <code>preceding-sibling</code>
			 axis contains the context node’s preceding
			 siblings, that is, those children of the context
			 node’s parent that occur before the context
			 node in <a title="document order" class="termref" href="#dt-document-order">document order</a>. If the context node
			 is an attribute or namespace node, the
			 <code>preceding-sibling</code> axis is
			 empty.</p><p>More formally, <code>$node/preceding-sibling::node()</code> delivers the result
                     of <code>fn:siblings($node)[. &lt;&lt; $node]</code>.</p></li><li><p>The <code>preceding-sibling-or-self</code> axis contains the context node,
                     together with the contents of the <code>preceding-sibling</code> axis.</p><p>More formally, <code>$node/preceding-sibling-or-self::node()</code> delivers the result
                     of <code>fn:siblings($node)[not(. &gt;&gt; $node)</code>.</p></li><li><p>The <code>following</code> axis
				contains all nodes that are
				descendants of the root of the tree in
				which the context node is found, are
				not descendants of the context node,
				and occur after the context node in
				<a title="document order" class="termref" href="#dt-document-order">document order</a>.
                     </p><p>More formally, <code>$node/following::node()</code> delivers the result
                     of <code>$node/ancestor-or-self::node()/following-sibling::node()/descendant-or-self::node()</code>
                        </p></li><li><p>The <code>following-or-self</code> axis contains the context node,
                     together with the contents of the <code>following</code> axis.</p><p>More formally, <code>$node/following-or-self::node()</code> delivers the result
                     of <code>$node/(. | following::node())</code>.</p></li><li><p>The <code>preceding</code> axis
				contains all nodes that are
				descendants of the root of the tree in
				which the context node is found, are
				not ancestors of the context node, and
				occur before the context node in
				<a title="document order" class="termref" href="#dt-document-order">document order</a>.
                     </p><p>More formally, <code>$node/preceding::node()</code> delivers the result
                     of <code>$node/ancestor-or-self::node()/preceding-sibling::node()/descendant-or-self::node()</code>.</p></li><li><p>The <code>preceding-or-self</code> axis contains the context node,
                     together with the contents of the <code>preceding</code> axis.</p><p>More formally, <code>$node/preceding-or-self::node()</code> delivers the result
                     of <code>$node/(. | preceding::node())</code>.</p></li><li><p>The <code>attribute</code> axis
			 contains the attributes of the context node,
			 which are the nodes returned by the
			 <span>
                              <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dm-attributes">Section 6.7.1 attributes Accessor</a><sup><small>DM</small></sup>
                           </span>; the axis will be
			 empty unless the context node is an
			 element.</p></li><li><p>The <code>self</code> axis contains just the context node itself.</p><p>The <code>self</code> axis is primarily useful when testing whether the context
                     node satisfies particular conditions, for example <code>if ($x[self::chapter])</code>.</p><p>More formally, <code>$node/self::node()</code> delivers the result
                     of <code>$node</code>.</p></li><li class="xpath"><p>The <code>namespace</code> axis
				contains the namespace nodes of the
				context node, which are the nodes
				returned by the
                                <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dm-namespace-nodes">Section 6.7.7 namespace-nodes Accessor</a><sup><small>DM</small></sup>; this axis
				is empty unless the context node is an
				element node. The
				<code>namespace</code> axis is
				deprecated as of XPath 2.0. If <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0
		compatibility mode</a> is <code>true</code>, the <code>namespace</code> axis must be supported. If <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0
		compatibility mode</a> is <code>false</code>, then support for the
				<code>namespace</code> axis is
				<a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>. An implementation
				that does not support the
				<code>namespace</code> axis when <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0
		compatibility mode</a> is <code>false</code> must raise
				a <a title="static error" class="termref" href="#dt-static-error">static
				error</a>
                           [<a href="#ERRXPST0010" title="err:XPST0010">err:XPST0010</a>] if it is
				used. Applications needing information
				about the <a title="in-scope namespaces" class="termref" href="#dt-in-scope-namespaces">in-scope namespaces</a> of an element
				should use the function <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-in-scope-namespaces"><code>fn:in-scope-namespaces</code></a>.
                                </p></li></ul><p>Axes can be categorized as <b>forward axes</b> and
		  <b>reverse axes</b>. An axis that only ever contains the context node or
		  nodes that are after the context node in <a title="document order" class="termref" href="#dt-document-order">document order</a> is a forward axis. An
		  axis that only ever contains the context node or nodes that are before the
		  context node in <a title="document order" class="termref" href="#dt-document-order">document order</a> is a reverse axis.</p><p>The <code>parent</code>, <code>ancestor</code>, <code>ancestor-or-self</code>, 
                  <code>preceding</code>, <code>preceding-or-self</code>, 
                  <code>preceding-sibling</code>, and <code>preceding-sibling-or-self</code> axes 
                  are reverse axes; all other axes are forward axes.</p><p>The <code>ancestor</code>, <code>descendant</code>, 
                  <code>following</code>, <code>preceding</code> and <code>self</code> axes partition a document (ignoring attribute <span class="xpath">and namespace</span> nodes):
		  they do not overlap and together they contain all the nodes in the
		  document.</p><p>
                     [<a id="dt-principal-node-kind" title="principal node kind">Definition</a>: Every axis has a <b>principal node kind</b>. If an axis can
		  contain elements, then the principal node kind is element; otherwise, it is the
		  kind of nodes that the axis can contain.] Thus:</p><ul><li><p>For the attribute axis, the principal node kind is
				attribute.</p></li><li class="xpath"><p>For the namespace axis, the principal node kind is
				namespace.</p></li><li><p>For all other axes, the principal node kind is element.</p></li></ul></div><div class="div4">
<h5><a id="node-tests"/>4.6.4.2 <a href="#node-tests" style="text-decoration: none">Node Tests</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#implausible-axis-steps">⬇</a> <a href="#axes">⬆</a></p><ol><li><p>
                     If the default namespace for elements and types has the special value <code>##any</code>,
                     then an unprefixed name in a <code>NameTest</code> acts as a wildcard, matching
                     names in any namespace or none.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/296">296</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1181">1181</a> 30 April 2024]</i></p></li></ol></div><p>
                     [<a id="dt-node-test" title="node test">Definition</a>: A <b>node test</b> is a condition on the name, kind (element, attribute, text, document, comment,
		  or processing instruction), and/or  <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of a node. 
      A node test determines which nodes contained by an axis are selected by a <a title="step" class="termref" href="#dt-step">step</a>.]
                  </p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest"/><code><a href="#prod-xpath40-NodeTest">NodeTest</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NodeTest-UnionNodeTest">UnionNodeTest</a>  |  <a href="#doc-xpath40-NodeTest-SimpleNodeTest">SimpleNodeTest</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-UnionNodeTest"/><code><a href="#prod-xpath40-UnionNodeTest">UnionNodeTest</a></code></td><td>::=</td><td><code>"("  <a href="#doc-xpath40-NodeTest-SimpleNodeTest">SimpleNodeTest</a>  ("|"  <a href="#doc-xpath40-NodeTest-SimpleNodeTest">SimpleNodeTest</a>)*  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-SimpleNodeTest"/><code><a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NodeTest-KindTest">KindTest</a>  |  <a href="#doc-xpath40-NodeTest-NameTest">NameTest</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-KindTest"/><code><a href="#prod-xpath40-KindTest">KindTest</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NodeTest-DocumentTest">DocumentTest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-ElementTest">ElementTest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-AttributeTest">AttributeTest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-SchemaElementTest">SchemaElementTest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-SchemaAttributeTest">SchemaAttributeTest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-PITest">PITest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-CommentTest">CommentTest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-TextTest">TextTest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-NamespaceNodeTest">NamespaceNodeTest</a>
                           <br/>|  <a href="#doc-xpath40-NodeTest-AnyKindTest">AnyKindTest</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-DocumentTest"/><code><a href="#prod-xpath40-DocumentTest">DocumentTest</a></code></td><td>::=</td><td><code>"document-node"  "("  (<a href="#doc-xpath40-NodeTest-ElementTest">ElementTest</a>  |  <a href="#doc-xpath40-NodeTest-SchemaElementTest">SchemaElementTest</a>  |  <a href="#doc-xpath40-NodeTest-NameTestUnion">NameTestUnion</a>)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-ElementTest"/><code><a href="#prod-xpath40-ElementTest">ElementTest</a></code></td><td>::=</td><td><code>"element"  "("  (<a href="#doc-xpath40-NodeTest-NameTestUnion">NameTestUnion</a>  (","  <a href="#prod-xpath40-TypeName">TypeName</a>  "?"?)?)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-SchemaElementTest"/><code><a href="#prod-xpath40-SchemaElementTest">SchemaElementTest</a></code></td><td>::=</td><td><code>"schema-element"  "("  <a href="#prod-xpath40-ElementName">ElementName</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-NameTestUnion"/><code><a href="#prod-xpath40-NameTestUnion">NameTestUnion</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-NodeTest-NameTest">NameTest</a> ++ "|")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-AttributeTest"/><code><a href="#prod-xpath40-AttributeTest">AttributeTest</a></code></td><td>::=</td><td><code>"attribute"  "("  (<a href="#doc-xpath40-NodeTest-NameTestUnion">NameTestUnion</a>  (","  <a href="#prod-xpath40-TypeName">TypeName</a>)?)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-SchemaAttributeTest"/><code><a href="#prod-xpath40-SchemaAttributeTest">SchemaAttributeTest</a></code></td><td>::=</td><td><code>"schema-attribute"  "("  <a href="#prod-xpath40-AttributeName">AttributeName</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-PITest"/><code><a href="#prod-xpath40-PITest">PITest</a></code></td><td>::=</td><td><code>"processing-instruction"  "("  (<a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#doc-xpath40-NodeTest-StringLiteral">StringLiteral</a>)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-CommentTest"/><code><a href="#prod-xpath40-CommentTest">CommentTest</a></code></td><td>::=</td><td><code>"comment"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-TextTest"/><code><a href="#prod-xpath40-TextTest">TextTest</a></code></td><td>::=</td><td><code>"text"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-NamespaceNodeTest"/><code><a href="#prod-xpath40-NamespaceNodeTest">NamespaceNodeTest</a></code></td><td>::=</td><td><code>"namespace-node"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-AnyKindTest"/><code><a href="#prod-xpath40-AnyKindTest">AnyKindTest</a></code></td><td>::=</td><td><code>"node"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-NameTest"/><code><a href="#prod-xpath40-NameTest">NameTest</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-NodeTest-EQName">EQName</a>  |  <a href="#doc-xpath40-NodeTest-Wildcard">Wildcard</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-NodeTest-Wildcard"/><code><a href="#prod-xpath40-Wildcard">Wildcard</a></code></td><td>::=</td><td><code>"*"<br/>|  (<a href="#prod-xpath40-NCName">NCName</a>  ":*")<br/>|  ("*:"  <a href="#prod-xpath40-NCName">NCName</a>)<br/>|  (<a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>  "*")</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody></table><p>A <a href="#prod-xpath40-UnionNodeTest">UnionNodeTest</a> matches a node <var>N</var>
                  if at least one of the constituent <a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a>s matches <var>N</var>.</p><p>For example, <code>(div1|div2|div3)</code> matches a node named <code>div1</code>, <code>div2</code>, or <code>div3</code>
                  </p><p>
                     [<a id="dt-name-test" title="name test">Definition</a>: A node test that consists only of an EQName or a
		  Wildcard is called a <b>name test</b>.]
                  </p><p>A node test written as an EQName is expanded as follows:</p><ul><li><p>If the principal node kind of the axis step is <code>element</code>, 
                  using the <a title="element name matching rule" class="termref" href="#dt-element-name-matching-rule">element name matching rule</a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>If the <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a> has the
                     special value <code>"##any</code>, the result of the expanding an unprefixed name
                  <var>NN</var> is the wildcard <code>*:<var>NN</var>
                              </code>.</p></div></li><li><p>Otherwise, using the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>.</p></li></ul><p>If the expanded node test is an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>
                     <var>Q</var>, then it
               matches a node <var>N</var> if and only if the <b>kind</b> of
                  node <var>N</var> is the <a title="principal node kind" class="termref" href="#dt-principal-node-kind">principal node kind</a> for the step axis and the
		  <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> of the node is equal (as defined by the <code>eq</code> operator) to <var>Q</var>. For
		  example, <code>child::para</code>
		  selects the <code>para</code> element children of
		  the context node; if the context node has no
		  <code>para</code> children, it selects an empty set
		  of nodes. <code>attribute::abc:href</code> selects
		  the attribute of the context node with the QName
		  <code>abc:href</code>; if the context node has no
		  such attribute, it selects an empty set of
		  nodes.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>A name test is not satisfied by an element node whose name does not match the <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> of the name test, even if it is in a <a title="substitution group" class="termref" href="#dt-substitution-group">substitution group</a> whose head is the named element.</p></div><p>Wildcard node tests are interpreted as follows:</p><ul><li><p>The node test <code>*</code> is true for any node of the
		  <a title="principal node kind" class="termref" href="#dt-principal-node-kind">principal node
		  kind</a> of the step axis. For example, <code>child::*</code> will select all element
		  children of the context node, and <code>attribute::*</code> will select all
		  attributes of the context node.</p></li><li><p>A node test can have the form
		  <code>NCName:*</code>. In this case, the prefix is
		  expanded in the same way as with a <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a>, using the
		  <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known
		  namespaces</a> in the <a title="static context" class="termref" href="#dt-static-context">static context</a>. If
		  the prefix is not found in the statically known namespaces,
		  a <a title="static error" class="termref" href="#dt-static-error">static
		  error</a> is raised [<a href="#ERRXPST0081" title="err:XPST0081">err:XPST0081</a>].
		  The node test is true for any node of the <a title="principal node kind" class="termref" href="#dt-principal-node-kind">principal
		  node kind</a> of the step axis whose <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> has the namespace URI
		  to which the prefix is bound, regardless of the
		  local part of the name.</p></li><li><p>A node test can contain a <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>, for example
		  <code>Q{http://example.com/msg}*</code>. Such a node test is true for any node of the principal 
                  node kind of the step axis whose <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> has the namespace URI specified in 
                  the <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>, regardless of the local part of the name.</p></li><li><p>A node test can also
		  have the form <code>*:NCName</code>. In this case,
		  the node test is true for any node of the <a title="principal node kind" class="termref" href="#dt-principal-node-kind">principal
		  node kind</a> of the step axis whose local name matches the given NCName,
		  regardless of its namespace or lack of a namespace.</p></li></ul><p>
                     [<a id="dt-kind-test" title="kind test">Definition</a>: An alternative
		  form of a node test called a
		  <b>kind test</b> can select nodes based
		  on their kind, name, and <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>.] The syntax
		  and semantics of a kind test are described in
		  <a href="#id-sequencetype-syntax"><b>3.1 Sequence Types</b></a> and <a href="#id-sequencetype-matching"><b>3.1.2 Sequence Type Matching</b></a>. When a kind test is used
		  in a <a title="node test" class="termref" href="#dt-node-test">node test</a>, only those nodes on the designated
		  axis that match the kind test are selected. Shown
		  below are several examples of kind tests that might
		  be used in path
		  expressions:</p><ul><li><p>
                           <code>node()</code>
		  matches any
		  node.</p></li><li><p>
                           <code>text()</code> matches
		  any text
		  node.</p></li><li><p>
                           <code>comment()</code>
		  matches any comment
		  node.</p></li><li><p>
                           <code>namespace-node()</code> matches any
		  namespace node.</p></li><li><p>
                           <code>element()</code>
		  matches any element
		  node.</p></li><li><p>
                           <code>schema-element(person)</code>
		  matches any element node whose name is
		  <code>person</code> (or is in the <a title="substitution group" class="termref" href="#dt-substitution-group">substitution group</a>
		  headed by <code>person</code>), and whose type
		  annotation is the same as (or is derived from) the declared type of the <code>person</code>
		  element in the <a title="in-scope element declarations" class="termref" href="#dt-is-elems">in-scope element declarations</a>.</p></li><li><p>
                           <code>element(person)</code> matches any element node whose name is
		  <code>person</code>, regardless of its type annotation.</p></li><li><p>
                           <code>element(doctor|nurse)</code> matches any element node whose name is
                        <code>doctor</code> or <code>nurse</code>, regardless of its type annotation.</p></li><li><p>
                           <code>element(person, surgeon)</code> matches any non-nilled element node whose name
		  is <code>person</code>, and whose type
		  annotation is
		  <code>surgeon</code> or is derived from <code>surgeon</code>.</p></li><li><p>
                           <code>element(doctor|nurse, medical-staff)</code> matches any non-nilled element node whose name
                        is <code>doctor</code> or <code>nurse</code>, and whose type
                        annotation is
                        <code>medical-staff</code> or is derived from <code>medical-staff</code>.</p></li><li><p>
                           <code>element(*,
		  surgeon)</code> matches any non-nilled element node whose type
		  annotation is <code>surgeon</code> (or is derived from <code>surgeon</code>), regardless of
		  its
		  name.</p></li><li><p>
                           <code>attribute()</code> matches any
                  attribute node.</p></li><li><p>
                           <code>attribute(price)</code> matches
                  any attribute whose name is <code>price</code>,
                  regardless of its type annotation.</p></li><li><p>
                           <code>attribute(*,
                  xs:decimal)</code> matches any attribute whose type
                  annotation is <code>xs:decimal</code> (or is derived from <code>xs:decimal</code>), regardless of
                  its
                  name.</p></li><li><p>
                           <code>document-node()</code>
                  matches any document
                  node.</p></li><li><p>
                           <code>document-node(element(book))</code>
                  matches any document node whose children consist of
                  a single element node that satisfies the <a title="kind test" class="termref" href="#dt-kind-test">kind test</a>
                           <code>element(book)</code>, interleaved with zero or more
                  comments and processing
                  instructions, and no text nodes.</p></li><li><p>
                           <code>document-node(book)</code> is an abbreviation
                  for <code>document-node(element(book))</code>.</p></li></ul></div><div class="div4">
<h5><a id="implausible-axis-steps"/>4.6.4.3 <a href="#implausible-axis-steps" style="text-decoration: none">Implausible Axis Steps</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-arithmetic">⬇</a> <a href="#node-tests">⬆</a></p><ol><li><p>
                     The rules for reporting type errors during static analysis have been changed
                     so that a processor has more freedom to report errors in respect of constructs that
                     are evidently wrong, such as <code>@price/@value</code>, even though dynamic evaluation
                     is defined to return an empty sequence rather than an error.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/602">602</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/603">603</a> 25 July 2023]</i></p></li></ol></div><p>Certain axis steps, given an inferred type for the context value, are
               classified as <a title="implausible" class="termref" href="#dt-implausible">implausible</a>. During the static analysis
               phase, a processor <span class="verb">may</span> (subject to the rules in
               <a href="#id-implausible-expressions"><b>2.4.6 Implausible Expressions</b></a>) report a static error
               when such axis steps are encountered: [<a href="#ERRXPTY0144" title="err:XPTY0144">err:XPTY0144</a>].</p><p>More specifically, an axis step is classified as <a title="implausible" class="termref" href="#dt-implausible">implausible</a>
               if any of the following conditions applies:</p><ol class="enumar"><li><p>The inferred item type of the context value is a node kind for which the
                     specified axis is always empty: for example, the inferred item type
                     of the context value is <code>attribute()</code> 
                     and the axis is <code>child</code>.</p></li><li><p>The node test exclusively selects node kinds that cannot appear
                  on the specified axis: for example, the axis is <code>child</code>
                  and the node test is <code>document-node()</code>.</p></li><li><p>In a schema-aware environment, when using the <code>child</code>,
                     <code>descendant</code>, <code>descendant-or-self</code>, or <code>attribute</code>
                     axes, the inferred item type of the
                     context value has a content type that does not allow any node matching
                  the node test to be present on the relevant axis. For example, if the inferred
                  item type of the context value 
                  is <code>schema-element(list)</code> and the relevant element declaration 
                     (taking into account substitution group membership and wildcards) 
                     only allows <code>item</code> children,
                  the axis step <code>child::li</code> will never select anything and is therefore
                  classified as <a title="implausible" class="termref" href="#dt-implausible">implausible</a>.</p></li></ol><p>Examples of implausible axis steps include the following:</p><ul><li><p>
                           <code>@code/text()</code>: attributes cannot have text node children.</p></li><li><p>
                           <code>/@code</code>: document nodes cannot have attributes.</p></li><li><p>
                           <code>ancestor::text()</code>: the ancestor axis never returns text nodes.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>Processors may choose not to classify the expression <code>/..</code>
               as implausible, since XSLT 1.0 users were sometimes advised to use this construct
               as an explicit way of denoting the empty sequence.</p></div></div></div><div class="div3">
<h4><a id="id-predicate"/>4.6.5 <a href="#id-predicate" style="text-decoration: none">Predicates within Steps</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep"/><code><a href="#prod-xpath40-AxisStep">AxisStep</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-AxisStep-ReverseStep">ReverseStep</a>  |  <a href="#doc-xpath40-AxisStep-ForwardStep">ForwardStep</a>)  <a href="#doc-xpath40-AxisStep-Predicate">Predicate</a>*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-ReverseStep"/><code><a href="#prod-xpath40-ReverseStep">ReverseStep</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-AxisStep-ReverseAxis">ReverseAxis</a>
                        <a href="#doc-xpath40-AxisStep-NodeTest">NodeTest</a>)  |  <a href="#doc-xpath40-AxisStep-AbbrevReverseStep">AbbrevReverseStep</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-ReverseAxis"/><code><a href="#prod-xpath40-ReverseAxis">ReverseAxis</a></code></td><td>::=</td><td><code>("ancestor"<br/>|  "ancestor-or-self"<br/>|  "parent"<br/>|  "preceding"<br/>|  "preceding-or-self"<br/>|  "preceding-sibling"<br/>|  "preceding-sibling-or-self")  "::"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-NodeTest"/><code><a href="#prod-xpath40-NodeTest">NodeTest</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-UnionNodeTest">UnionNodeTest</a>  |  <a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-AbbrevReverseStep"/><code><a href="#prod-xpath40-AbbrevReverseStep">AbbrevReverseStep</a></code></td><td>::=</td><td><code>".."</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-ForwardStep"/><code><a href="#prod-xpath40-ForwardStep">ForwardStep</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-AxisStep-ForwardAxis">ForwardAxis</a>
                        <a href="#doc-xpath40-AxisStep-NodeTest">NodeTest</a>)  |  <a href="#doc-xpath40-AxisStep-AbbrevForwardStep">AbbrevForwardStep</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-ForwardAxis"/><code><a href="#prod-xpath40-ForwardAxis">ForwardAxis</a></code></td><td>::=</td><td><code>("attribute"<br/>|  "child"<br/>|  "descendant"<br/>|  "descendant-or-self"<br/>|  "following"<br/>|  "following-or-self"<br/>|  "following-sibling"<br/>|  "following-sibling-or-self"<br/>|  "namespace"<br/>|  "self")  "::"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-AbbrevForwardStep"/><code><a href="#prod-xpath40-AbbrevForwardStep">AbbrevForwardStep</a></code></td><td>::=</td><td><code>("@"  <a href="#doc-xpath40-AxisStep-NodeTest">NodeTest</a>)  |  <a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-Predicate"/><code><a href="#prod-xpath40-Predicate">Predicate</a></code></td><td>::=</td><td><code>"["  <a href="#doc-xpath40-AxisStep-Expr">Expr</a>  "]"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-Expr"/><code><a href="#prod-xpath40-Expr">Expr</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-AxisStep-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AxisStep-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr">ForExpr</a>
                        <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                        <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                        <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                        <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                     </code></td></tr></tbody></table><p id="dt-predicate">A predicate within a <a href="#doc-xpath40-AxisStep"/> has similar syntax and semantics
	 to a predicate within a <a title="filter expression" class="termref" href="#dt-filter-expression">filter expression</a>.  The
	 only difference is in the way the context position is set for
	 evaluation of the predicate.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The operator <code>[]</code> binds more tightly than <code>/</code>.
            This means that the expression <code>a/b[1]</code> is interpreted as <code>child::a/(child::b[1])</code>:
                  it selects the first <code>b</code>
            child of every <code>a</code> element, in contrast to <code>(a/b)[1]</code> which
            selects the first <code>b</code> element that is a child of some <code>a</code>
            element.</p><p>A common mistake is to write <code>//a[1]</code> where <code>(//a)[1]</code>
               is intended. The first expression, <code>//a[1]</code>, selects every descendant 
                  <code>a</code> element that is the first child of its parent (it expands
                  to <code>/descendant-or-self::node()/child::a[1]</code>), whereas 
               <code>(//a)[1]</code> selects the <code>a</code> element in the document.
               </p></div><p>For the purpose of evaluating the context position within
	 a predicate, the input sequence is considered to be sorted as
	 follows: into document order if the predicate is in a
	 forward-axis step, into reverse document order if the
	 predicate is in a reverse-axis step, or in its original order
	 if the predicate is not in a step.</p><p>More formally:</p><ul><li><p>For a step using a forwards axis, such as <code>child::<var>test</var>[<var>P</var>]</code>,
               the result is the same as for the equivalent <a title="filter expression" class="termref" href="#dt-filter-expression">filter expression</a>
                        <code>(child::<var>test</var>)[<var>P</var>]</code> (note the parentheses). The same applies if there
               are multiple predicates, for example <code>child::<var>test</var>[<var>P<sub>1</sub></var>][<var>P<sub>2</sub></var>][<var>P<sub>3</sub></var>]</code>
               is equivalent to <code>(child::<var>test</var>)[<var>P<sub>1</sub></var>][<var>P<sub>2</sub></var>][<var>P<sub>3</sub></var>]</code>.</p></li><li><p>For a step using a reverse axis, such as <code>ancestor::<var>test</var>[<var>P</var>]</code>,
               the result is the same as the expression  
               <code>reverse(ancestor::<var>test</var>)[<var>P</var>] =&gt; reverse()</code>. The same applies if there
               are multiple predicates, for example <code>ancestor::<var>test</var>[<var>P<sub>1</sub></var>][<var>P<sub>2</sub></var>][<var>P<sub>3</sub></var>]</code>
               is equivalent to <code>reverse(ancestor::<var>test</var>)[<var>P<sub>1</sub></var>][<var>P<sub>2</sub></var>][<var>P<sub>3</sub></var>] =&gt; reverse()</code>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The result of the expression <code>preceding-sibling::*</code> is in document order, but
               <code>preceding-sibling::*[1]</code> selects the last preceding sibling element,
               that is, the one that immediately precedes the context node.</p><p>Similarly, the expression <code>preceding-sibling::x[1, 2, 3]</code> selects the last three
               preceding siblings, returning them in document order. For example, given the input:</p><div class="exampleInner"><pre>&lt;doc&gt;&lt;a/&gt;&lt;b/&gt;&lt;c/&gt;&lt;d/&gt;&lt;e/&gt;&lt;f/&gt;&lt;/doc&gt;</pre></div><p>The result of <code>//e ! preceding-sibling::*[1, 2, 3]</code> is <code>&lt;b/&gt;, &lt;c/&gt;, &lt;d/&gt;</code>.
               The expression <code>//e ! preceding-sibling::*[3, 2, 1]</code> delivers exactly the same result.</p></div><p>Here are some examples of <a title="axis step" class="termref" href="#dt-axis-step">axis steps</a> that contain predicates:</p><ul><li><p>This example selects the second <code>chapter</code> element that is a child
			 of the context node:</p><div class="exampleInner"><pre>child::chapter[2]</pre></div></li><li><p>This example selects all the descendants of the
		  context node that are elements named
		  <code>"toy"</code> and whose <code>color</code>
		  attribute has the value <code>"red"</code>:</p><div class="exampleInner"><pre>descendant::toy[attribute::color = "red"]</pre></div></li><li><p>This example selects all the <code>employee</code> children of the context node
		that have both a <code>secretary</code> child element and an <code>assistant</code> child element:</p><div class="exampleInner"><pre>child::employee[secretary][assistant]</pre></div></li><li><p>This example selects the innermost <code>div</code> ancestor of the context node:</p><div class="exampleInner"><pre>ancestor::div[1]</pre></div></li><li><p>This example selects the outermost <code>div</code> ancestor of the context node:</p><div class="exampleInner"><pre>ancestor::div[last()]</pre></div></li><li><p>This example selects the names of all the ancestor elements of the context node that have an
                     <code>@id</code> attribute, outermost element first:</p><div class="exampleInner"><pre>ancestor::*[@id]</pre></div></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The expression <code>ancestor::div[1]</code> parses as an <a href="#doc-xpath40-AxisStep"/>
                  with a reverse axis, and the position <code>1</code> therefore
                  refers to the first ancestor <code>div</code> in reverse document order,
                  that is, the innermost <code>div</code>. By
contrast, <code>(ancestor::div)[1]</code> parses as a <a href="#doc-xpath40-FilterExpr">FilterExpr</a>,
                  and therefore returns the first qualifying <code>div</code>
element in the order of the <code>ancestor::div</code> expression, which is
                  in <a title="document order" class="termref" href="#dt-document-order">document order</a>.</p><p>The fact that a reverse-axis step assigns context positions in reverse
document order for the purpose of evaluating predicates does not alter the
fact that the final result of the step is always in document order.</p><p>The expression <code>ancestor::(div1|div2)[1]</code> 
               does not have the same meaning as <code>(ancestor::div1|ancestor::div2)[1]</code>. 
                  In the first expression,
               the predicate <code>[1]</code> is within a step that uses a reverse axis, so nodes are counted
               in reverse document order. In the second expression, the predicate applies to the result of
               a union expression, so nodes are counted in document order.</p></div><p>When the context value for evaluation of a step includes multiple nodes, the step is evaluated
            separately for each of those nodes, and the results are combined without reordering. This means, for example, that
            if the context value contains three <code>list</code> nodes, and each of those nodes has multiple
               <code>item</code> children, then the step <code>item[1]</code> will deliver a sequence of three <code>item</code>
               elements, namely the first <code>item</code> from each <code>list</code>, retaining the order of the
            respective <code>list</code> elements.</p></div><div class="div3">
<h4><a id="unabbrev"/>4.6.6 <a href="#unabbrev" style="text-decoration: none">Unabbreviated Syntax</a></h4><p>This section provides a number of examples of path expressions in which the
axis is explicitly specified in each <a title="step" class="termref" href="#dt-step">step</a>. The syntax used in these examples is
called the <b>unabbreviated syntax</b>. In many common cases, it is
possible to write path expressions more concisely using an <b>abbreviated
syntax</b>, as explained in <a href="#abbrev"><b>4.6.7 Abbreviated Syntax</b></a>.</p><p>These examples assume that the context value is a single node, referred to as the context node.</p><ul><li><p>
                        <code>child::para</code> selects
the <code>para</code> element children of the context node.</p></li><li><p>
                        <code>child::(para|bullet)</code> selects
                     the <code>para</code> and <code>bullet</code> element children of the context node.</p></li><li><p>
                        <code>child::*</code> selects all element children of the context node.</p></li><li><p>
                        <code>child::text()</code> selects all text node children of the context node.</p></li><li><p>
                        <code>child::(text()|comment())</code> selects all text node and comment node children of the context node.</p></li><li><p>
                        <code>child::node()</code> selects all the children of the context node. Note that no attribute nodes are returned, because attributes are not children.</p></li><li><p>
                        <code>attribute::name</code> selects the <code>name</code> attribute of the context node.</p></li><li><p>
                        <code>attribute::*</code> selects all the attributes of the context node.</p></li><li><p>
                        <code>parent::node()</code> selects the parent of the context node. If the context node is an attribute node, this expression returns the element node (if any) to which the attribute node is attached.</p></li><li><p>
                        <code>descendant::para</code> selects the <code>para</code> element descendants of the context node.</p></li><li><p>
                        <code>ancestor::div</code> selects all <code>div</code> ancestors of the context node.</p></li><li><p>
                        <code>ancestor-or-self::div</code> selects the <code>div</code> ancestors of the context node and, if the context node is a <code>div</code> element, the context node as well.</p></li><li><p>
                        <code>descendant-or-self::para</code> selects the <code>para</code> element descendants of the context node and, if the context node is a <code>para</code> element, the context node as well.</p></li><li><p>
                        <code>self::para</code> selects the context node if it is a <code>para</code> element, and otherwise returns an empty sequence.</p></li><li><p>
                        <code>self::(chapter|appendix)</code> selects the context node if it is a
                     <code>chapter</code> or <code>appendix</code> element, and otherwise returns an empty sequence.</p></li><li><p>
                        <code>child::chapter/descendant::para</code> selects the <code>para</code> element
descendants of the <code>chapter</code> element children of the context node.</p></li><li><p>
                        <code>child::*/child::para</code> selects all <code>para</code> grandchildren of the context node.</p></li><li><p>
                        <code>/</code> selects the root of the tree that contains the context node, but raises a dynamic error if this root is not a document node.</p></li><li><p>
                        <code>/descendant::para</code> selects all the <code>para</code> elements in the same document as the context node.</p></li><li><p>
                        <code>/descendant::list/child::member</code> selects all
the <code>member</code> elements that have a <code>list</code> parent and that are in the same document as the context node.</p></li><li><p>
                        <code>child::para[position() = 1]</code> selects the first <code>para</code> child of the context node.</p></li><li><p>
                        <code>child::para[position() = last()]</code> selects the last <code>para</code> child of the context node.</p></li><li><p>
                        <code>child::para[position() = last()-1]</code> selects the last but one <code>para</code> child of the context node.</p></li><li><p>
                        <code>child::para[position() &gt; 1]</code> selects all the <code>para</code> children of the context node other than the first <code>para</code> child of the context node.</p></li><li><p>
                        <code>following-sibling::chapter[position() = 1]</code> selects the next <code>chapter</code> sibling of the context node.</p></li><li><p>
                        <code>following-sibling::(chapter|appendix)[position() = 1]</code> selects the next sibling of the context node
                        that is either a <code>chapter</code> or an <code>appendix</code>.</p></li><li><p>
                        <code>preceding-sibling::chapter[position() = 1]</code> selects the previous <code>chapter</code> sibling of the context node.</p></li><li><p>
                        <code>/descendant::figure[position() = 42]</code> selects the forty-second <code>figure</code> element in the document containing the context node.</p></li><li><p>
                        <code>/child::book/child::chapter[position() = 5]/child::section[position() = 2]</code> selects the
second <code>section</code> of the fifth <code>chapter</code> of the <code>book</code> whose parent is the document node that contains the context node.</p></li><li><p>
                        <code>child::para[attribute::type eq "warning"]</code> selects
all <code>para</code> children of the context node that have a <code>type</code> attribute with value <code>warning</code>.
                  </p></li><li><p>
                        <code>child::para[attribute::type eq 'warning'][position() = 5]</code> selects the fifth <code>para</code> child of the context node that has a <code>type</code> attribute with value <code>warning</code>.
                  </p></li><li><p>
                        <code>child::para[position() = 5][attribute::type eq "warning"]</code> selects the fifth <code>para</code> child of the context node if that child has a <code>type</code> attribute with value <code>warning</code>.
                  </p></li><li><p>
                        <code>child::chapter[child::title = 'Introduction']</code> selects
the <code>chapter</code> children of the context node that have one or
more <code>title</code> children whose <a title="typed value" class="termref" href="#dt-typed-value">typed value</a> is equal to the
string <code>Introduction</code>.
                  </p></li><li><p>
                        <code>child::chapter[child::title]</code> selects the <code>chapter</code> children of the context node that have one or more <code>title</code> children.</p></li><li><p>
                        <code>child::*[self::chapter or self::appendix]</code>
selects the <code>chapter</code> and <code>appendix</code> children of the context node.</p></li><li><p>
                        <code>child::*[self::(chapter|appendix)][position() = last()]</code> selects the
last <code>chapter</code> or <code>appendix</code> child of the context node.</p></li></ul></div><div class="div3">
<h4><a id="abbrev"/>4.6.7 <a href="#abbrev" style="text-decoration: none">Abbreviated Syntax</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep"/><code><a href="#prod-xpath40-AbbrevForwardStep">AbbrevForwardStep</a></code></td><td>::=</td><td><code>("@"  <a href="#doc-xpath40-AbbrevForwardStep-NodeTest">NodeTest</a>)  |  <a href="#doc-xpath40-AbbrevForwardStep-SimpleNodeTest">SimpleNodeTest</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-NodeTest"/><code><a href="#prod-xpath40-NodeTest">NodeTest</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-UnionNodeTest">UnionNodeTest</a>  |  <a href="#doc-xpath40-AbbrevForwardStep-SimpleNodeTest">SimpleNodeTest</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-SimpleNodeTest"/><code><a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-AbbrevForwardStep-KindTest">KindTest</a>  |  <a href="#doc-xpath40-AbbrevForwardStep-NameTest">NameTest</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-KindTest"/><code><a href="#prod-xpath40-KindTest">KindTest</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-AbbrevForwardStep-DocumentTest">DocumentTest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-ElementTest">ElementTest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-AttributeTest">AttributeTest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-SchemaElementTest">SchemaElementTest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-SchemaAttributeTest">SchemaAttributeTest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-PITest">PITest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-CommentTest">CommentTest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-TextTest">TextTest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-NamespaceNodeTest">NamespaceNodeTest</a>
                        <br/>|  <a href="#doc-xpath40-AbbrevForwardStep-AnyKindTest">AnyKindTest</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-DocumentTest"/><code><a href="#prod-xpath40-DocumentTest">DocumentTest</a></code></td><td>::=</td><td><code>"document-node"  "("  (<a href="#doc-xpath40-AbbrevForwardStep-ElementTest">ElementTest</a>  |  <a href="#doc-xpath40-AbbrevForwardStep-SchemaElementTest">SchemaElementTest</a>  |  <a href="#doc-xpath40-AbbrevForwardStep-NameTestUnion">NameTestUnion</a>)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-ElementTest"/><code><a href="#prod-xpath40-ElementTest">ElementTest</a></code></td><td>::=</td><td><code>"element"  "("  (<a href="#doc-xpath40-AbbrevForwardStep-NameTestUnion">NameTestUnion</a>  (","  <a href="#prod-xpath40-TypeName">TypeName</a>  "?"?)?)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-SchemaElementTest"/><code><a href="#prod-xpath40-SchemaElementTest">SchemaElementTest</a></code></td><td>::=</td><td><code>"schema-element"  "("  <a href="#prod-xpath40-ElementName">ElementName</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-NameTestUnion"/><code><a href="#prod-xpath40-NameTestUnion">NameTestUnion</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-AbbrevForwardStep-NameTest">NameTest</a> ++ "|")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-NameTest"/><code><a href="#prod-xpath40-NameTest">NameTest</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-AbbrevForwardStep-EQName">EQName</a>  |  <a href="#doc-xpath40-AbbrevForwardStep-Wildcard">Wildcard</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-AttributeTest"/><code><a href="#prod-xpath40-AttributeTest">AttributeTest</a></code></td><td>::=</td><td><code>"attribute"  "("  (<a href="#doc-xpath40-AbbrevForwardStep-NameTestUnion">NameTestUnion</a>  (","  <a href="#prod-xpath40-TypeName">TypeName</a>)?)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-SchemaAttributeTest"/><code><a href="#prod-xpath40-SchemaAttributeTest">SchemaAttributeTest</a></code></td><td>::=</td><td><code>"schema-attribute"  "("  <a href="#prod-xpath40-AttributeName">AttributeName</a>  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-PITest"/><code><a href="#prod-xpath40-PITest">PITest</a></code></td><td>::=</td><td><code>"processing-instruction"  "("  (<a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#doc-xpath40-AbbrevForwardStep-StringLiteral">StringLiteral</a>)?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-CommentTest"/><code><a href="#prod-xpath40-CommentTest">CommentTest</a></code></td><td>::=</td><td><code>"comment"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-TextTest"/><code><a href="#prod-xpath40-TextTest">TextTest</a></code></td><td>::=</td><td><code>"text"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-NamespaceNodeTest"/><code><a href="#prod-xpath40-NamespaceNodeTest">NamespaceNodeTest</a></code></td><td>::=</td><td><code>"namespace-node"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-AnyKindTest"/><code><a href="#prod-xpath40-AnyKindTest">AnyKindTest</a></code></td><td>::=</td><td><code>"node"  "("  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevForwardStep-Wildcard"/><code><a href="#prod-xpath40-Wildcard">Wildcard</a></code></td><td>::=</td><td><code>"*"<br/>|  (<a href="#prod-xpath40-NCName">NCName</a>  ":*")<br/>|  ("*:"  <a href="#prod-xpath40-NCName">NCName</a>)<br/>|  (<a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>  "*")</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AbbrevReverseStep"/><code><a href="#prod-xpath40-AbbrevReverseStep">AbbrevReverseStep</a></code></td><td>::=</td><td><code>".."</code></td></tr></tbody></table><p>The abbreviated syntax permits the following abbreviations:</p><ol class="enumar"><li><p>The attribute axis <code>attribute::</code> can be
    abbreviated by <code>@</code>. For example, a path expression <code>para[@type = "warning"]</code> is short
    for <code>child::para[attribute::type = "warning"]</code> and
    so selects <code>para</code> children with a <code>type</code> attribute with value
    equal to <code>warning</code>.</p></li><li><p>If the axis name is omitted from an <a title="axis step" class="termref" href="#dt-axis-step">axis step</a>, the default axis is
    <code>child</code>, with two exceptions:

    (1) if the <a href="#doc-xpath40-NodeTest">NodeTest</a> in an axis step contains an <a href="#doc-xpath40-AttributeTest">AttributeTest</a> or <a href="#doc-xpath40-SchemaAttributeTest">SchemaAttributeTest</a> then the
    default axis is <code>attribute</code>;     
    (2) if the <a href="#doc-xpath40-NodeTest">NodeTest</a> in an axis step is a <a href="#prod-xpath40-NamespaceNodeTest">NamespaceNodeTest</a>
                        <span class="xpath">then the default axis is <code>namespace</code>, but in an implementation that does not support
    the namespace axis, an error is raised [<a href="#ERRXQST0134" title="err:XQST0134">err:XQST0134</a>].</span>
                        </p><div class="note"><p class="prefix"><b>Note:</b></p><p> The namespace
    axis is deprecated as of XPath 2.0, but is required in some languages
    that use XPath, including XSLT.</p></div><p>

    For example, the path expression <code>section/para</code> is an abbreviation for <code>child::section/child::para</code>, and the path
    expression <code>section/@id</code> is an
    abbreviation for <code>child::section/attribute::id</code>.  Similarly,
    <code>section/attribute(id)</code> is an
    abbreviation for <code>child::section/attribute::attribute(id)</code>. Note
    that the latter expression contains both an axis specification and
    a <a title="node test" class="termref" href="#dt-node-test">node test</a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>An abbreviated axis step that omits the axis name must use a
                  <a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a> rather than a <a href="#prod-xpath40-UnionNodeTest">UnionNodeTest</a>.
                  This means that a construct such as <code>(ul|ol)</code>
                  is treated as an abbreviation for <code>(child::ul|child::ol)</code> rather than <code>child::(ul|ol)</code>.
                  Since the two constructs have exactly the same semantics, this is not actually a restriction.</p></div></li><li><p> Each non-initial occurrence of <code>//</code> is effectively replaced by  <code>/descendant-or-self::node()/</code> during processing of a path expression. For example, <code>div1//para</code> is
short for <code>child::div1/descendant-or-self::node()/child::para</code> and so will select all <code>para</code> descendants of <code>div1</code> children.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The path expression <code>//para[1]</code> does <em>not</em> mean the same as the path
expression <code>/descendant::para[1]</code>. The latter selects the first descendant <code>para</code> element; the former
selects all descendant <code>para</code> elements that are the first <code>para</code> children of their respective parents.</p></div></li><li><p>A step consisting
of <code>..</code> is short
for <code>parent::node()</code>. For example, <code>../title</code> is short for <code>parent::node()/child::title</code> and so will select the <code>title</code> children of the parent of the context node.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The expression <code>.</code>, known as a <b>context value
   reference</b>, is a <a title="primary expression" class="termref" href="#dt-primary-expression">primary expression</a>,
   and is described in <a href="#id-context-value-references"><b>4.2.3 Context Value References</b></a>.</p></div></li></ol><p>Here are some examples of path expressions that use the abbreviated
syntax. These examples assume that the context value is a single node, referred to as the context node:</p><ul><li><p>
                        <code>para</code> selects the <code>para</code> element children of the context node.</p></li><li><p>
                        <code>*</code> selects all element children of the context node.</p></li><li><p>
                        <code>text()</code> selects all text node children of the context node.</p></li><li><p>
                        <code>@name</code> selects
the <code>name</code> attribute of the context node.</p></li><li><p>
                        <code>@(id|name)</code> selects
                     the <code>id</code> and <code>name</code> attributes of the context node.</p></li><li><p>
                        <code>@*</code> selects all the attributes of the context node.</p></li><li><p>
                        <code>para[1]</code> selects the first <code>para</code> child of the context node.</p></li><li><p>
                        <code>para[last()]</code> selects the last <code>para</code> child of the context node.</p></li><li><p>
                        <code>*/para</code> selects
all <code>para</code> grandchildren of the context node.</p></li><li><p>
                        <code>/book/chapter[5]/section[2]</code> selects the
second <code>section</code> of the fifth <code>chapter</code> of the <code>book</code> whose parent is the document node that contains the context node.</p></li><li><p>
                        <code>chapter//para</code> selects the <code>para</code> element descendants of the <code>chapter</code> element children of the context node.</p></li><li><p>
                        <code>//para</code> selects all
the <code>para</code> descendants of the root document node and thus selects all <code>para</code> elements in the same document as the context node.</p></li><li><p>
                        <code>//@version</code> selects all the <code>version</code> attribute nodes that are in the same document as the context node.</p></li><li><p>
                        <code>//list/member</code> selects all the <code>member</code> elements in the same document as the context node that have a <code>list</code> parent.</p></li><li><p>
                        <code>.//para</code> selects
the <code>para</code> element descendants of the context node.</p></li><li><p>
                        <code>..</code> selects the parent of the context node.</p></li><li><p>
                        <code>../@lang</code> selects
the <code>lang</code> attribute of the parent of the context node.</p></li><li><p>
                        <code>para[@type = "warning"]</code> selects all <code>para</code> children of the context node that have a <code>type</code> attribute with value <code>warning</code>.
                  </p></li><li><p>
                        <code>para[@type = "warning"][5]</code> selects the fifth <code>para</code> child of the context node that has a <code>type</code> attribute with value <code>warning</code>.
                  </p></li><li><p>
                        <code>para[5][@type = "warning"]</code> selects the fifth <code>para</code> child of the context node if that child has a <code>type</code> attribute with value <code>warning</code>.
                  </p></li><li><p>
                        <code>chapter[title = "Introduction"]</code> selects the <code>chapter</code> children of the context node that have one
or more <code>title</code> children whose <a title="typed value" class="termref" href="#dt-typed-value">typed value</a> is equal to the string <code>Introduction</code>.
                  </p></li><li><p>
                        <code>chapter[title]</code> selects the <code>chapter</code> children of the context node that have one or more <code>title</code> children.</p></li><li><p>
                        <code>employee[@secretary and @assistant]</code> selects all
the <code>employee</code> children of the context node that have both a <code>secretary</code> attribute and
an <code>assistant</code> attribute.</p></li><li><p>
                        <code>book/(chapter|appendix)/section</code> selects
every <code>section</code> element that has a parent that is either a <code>chapter</code> or an <code>appendix</code> element, that in turn is a child of a <code>book</code> element that is a child of the context node.</p></li><li><p>If <code>E</code> is any expression that returns a sequence of nodes, then the expression <code>E/.</code> returns the same nodes in <a title="document order" class="termref" href="#dt-document-order">document order</a>, with duplicates eliminated based on node identity.</p></li></ul></div></div><div class="div2">
<h3><a id="id-sequence-expressions"/>4.7 <a href="#id-sequence-expressions" style="text-decoration: none">Sequence Expressions</a></h3><p>XPath 4.0 supports operators to construct, filter,  and combine
<a title="sequence" class="termref" href="#dt-sequence">sequences</a> of <a title="item" class="termref" href="#dt-item">items</a>.
Sequences are never nested—for
example, combining the values <code>1</code>, <code>(2, 3)</code>, and <code>( )</code> into a single sequence results
in the sequence <code>(1, 2, 3)</code>.</p><div class="div3">
<h4><a id="construct_seq"/>4.7.1 <a href="#construct_seq" style="text-decoration: none">Sequence Concatenation</a></h4><p>
                  [<a id="dt-comma-operator" title="comma operator">Definition</a>: One way to construct a sequence is by using the <b>comma operator</b>, 
                  which evaluates each of its operands and concatenates the resulting sequences, in order, 
                  into a single result sequence.] See <a href="#prod-xpath40-Expr"/>. 
               Empty parentheses can be used to denote an empty sequence.</p><p>A sequence may contain duplicate
<a title="item" class="termref" href="#dt-item">items</a>, but a sequence is never an item in another sequence. When a
new sequence is created by concatenating two or more input sequences, the new
sequence contains all the items of the input sequences and its length is the
sum of the lengths of the input sequences.</p><p>
                  [<a id="dt-sequence-concatenation" title="sequence concatenation">Definition</a>: The 
               <b>sequence concatenation</b> of a number of sequences <var>S<sub>1</sub></var>, <var>S<sub>2</sub></var>, ... <var>S<sub>n</sub></var>
               is defined to be the sequence formed from the items of <var>S<sub>1</sub></var>, followed by the items
               from <var>S<sub>2</sub></var>, and so on, retaining order.] The comma operator returns the sequence
            concatenation of its two operands; repeated application (for example <code>$s1, $s2, $s3, $s4</code>)
            delivers the sequence concatenation of multiple sequences.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>In places where the grammar calls for <a href="#doc-xpath40-ExprSingle">ExprSingle</a>, such as the arguments of a function call, any expression that contains a top-level comma operator must be enclosed in parentheses.</p></div><p>Here are some examples of expressions that construct sequences:
</p><ul><li><p>The result of this expression is a sequence of five integers:</p><div class="exampleInner"><pre>(10, 1, 2, 3, 4)</pre></div></li><li><p>This expression combines four sequences of length one, two, zero, and two, respectively, into a single sequence of length five. The result of this expression is the sequence <code>10, 1, 2, 3, 4</code>.</p><div class="exampleInner"><pre>(10, (1, 2), (), (3, 4))</pre></div></li><li><p>The result of this expression is a sequence containing
all <code>salary</code> children of the context node followed by all <code>bonus</code> children.</p><div class="exampleInner"><pre>(salary, bonus)</pre></div></li><li><p>Assuming that <code>$price</code> is bound to
the value <code>10.50</code>, the result of this expression is the sequence <code>10.50, 10.50</code>.</p><div class="exampleInner"><pre>($price, $price)</pre></div></li></ul></div><div class="div3">
<h4><a id="id-range-expressions"/>4.7.2 <a href="#id-range-expressions" style="text-decoration: none">Range Expressions</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RangeExpr"/><code><a href="#prod-xpath40-RangeExpr">RangeExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-RangeExpr-AdditiveExpr">AdditiveExpr</a>  ("to"  <a href="#doc-xpath40-RangeExpr-AdditiveExpr">AdditiveExpr</a>)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-RangeExpr-AdditiveExpr"/><code><a href="#prod-xpath40-AdditiveExpr">AdditiveExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-MultiplicativeExpr">MultiplicativeExpr</a>  (("+"  |  "-")  <a href="#prod-xpath40-MultiplicativeExpr">MultiplicativeExpr</a>)*</code></td></tr></tbody></table><p>A <b>RangeExpression</b> can be used to construct a sequence of 
integers. Each of the operands is
converted as though it was an argument of a function with the expected
parameter type <code>xs:integer?</code>.
If either operand is an empty sequence, or if the integer derived from the first operand is greater than the integer derived from the second operand, the result of the range expression is an empty sequence. If the two operands convert to the same integer, the result of the range expression is that integer. Otherwise, the result is a sequence containing the two integer operands and
every integer between the two operands, in increasing order. </p><p>The following examples illustrate the semantics:</p><ul><li><p>
                        <code>1 to 4</code> returns the sequence <code>1, 2, 3, 4</code>
                     </p></li><li><p>
                        <code>10 to 10</code> returns the <a title="singleton" class="termref" href="#dt-singleton">singleton</a> sequence <code>10</code>
                     </p></li><li><p>
                        <code>10 to 1</code> returns the empty sequence</p></li><li><p>
                        <code>-13 to -10</code> returns the sequence <code>-13, -12, -11, -10</code>
                     </p></li></ul><p>More formally, a <b>RangeExpression</b> is evaluated as follows:</p><ol class="enumar"><li><p>Each of the operands of the <code>to</code> operator is converted as though it was an argument of a function
                  with the expected parameter type <code>xs:integer?</code>.</p></li><li><p>If either operand is an empty sequence, or if the integer derived from the first operand is greater
                  than the integer derived from the second operand, the result of the range expression is an empty sequence.</p></li><li><p>If the two operands convert to the same integer, the result of the range expression is that integer.</p></li><li><p>Otherwise, the result is a sequence containing the two integer operands and every integer between the 
                  two operands, in increasing order.</p></li></ol><p>The following examples illustrate the use of <code>RangeExpressions</code>
                  .</p><div class="example"><p>This example uses a range expression as one operand in constructing a sequence. 
                  It evaluates to the sequence <code>10, 1, 2, 3, 4</code>.</p><div class="exampleInner"><pre>(10, 1 to 4)</pre></div><p>This example selects the first four items from an input sequence:</p><div class="exampleInner"><pre>$input[1 to 4]</pre></div><p>This example returns the sequence <code>(0, 0.1, 0.2, 0.3, 0.5)</code>:</p><div class="exampleInner"><pre>$x = (1 to 5) ! . * 0.1</pre></div><p>This example constructs a sequence of length one containing the single integer 10.</p><div class="exampleInner"><pre>10 to 10</pre></div><p>The result of this example is a sequence of length zero.</p><div class="exampleInner"><pre>15 to 10</pre></div><p>This example uses the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-reverse"><code>fn:reverse</code></a> function to construct a sequence of six integers in decreasing order. 
                  It evaluates to the sequence 15, 14, 13, 12, 11, 10.</p><div class="exampleInner"><pre>reverse(10 to 15)</pre></div></div><div class="note"><p class="prefix"><b>Note:</b></p><p>To construct a sequence of integers based on steps other than 1, use the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-slice"><code>fn:slice</code></a>
               function, as defined in <a href="https://www.w3.org/TR/xpath-functions-31/#general-seq-funcs">Section 
            
               14.1 General functions and operators on sequences
         </a><sup><small>FO31</small></sup>.</p></div></div><div class="div3">
<h4><a id="combining_seq"/>4.7.3 <a href="#combining_seq" style="text-decoration: none">Combining Node Sequences</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnionExpr"/><code><a href="#prod-xpath40-UnionExpr">UnionExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-UnionExpr-IntersectExceptExpr">IntersectExceptExpr</a>  (("union"  |  "|")  <a href="#doc-xpath40-UnionExpr-IntersectExceptExpr">IntersectExceptExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnionExpr-IntersectExceptExpr"/><code><a href="#prod-xpath40-IntersectExceptExpr">IntersectExceptExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-UnionExpr-InstanceofExpr">InstanceofExpr</a>  (("intersect"  |  "except")  <a href="#doc-xpath40-UnionExpr-InstanceofExpr">InstanceofExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnionExpr-InstanceofExpr"/><code><a href="#prod-xpath40-InstanceofExpr">InstanceofExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-TreatExpr">TreatExpr</a>  ("instance"  "of"  <a href="#doc-xpath40-SequenceType">SequenceType</a>)?</code></td></tr></tbody></table><p>XPath 4.0 provides the following operators for combining sequences of
nodes:</p><ul><li><p>The <code>union</code> and <code>|</code> operators are equivalent. They take two node sequences as operands and
return a sequence containing all the nodes that occur in either of the
operands.</p></li><li><p>The <code>intersect</code> operator takes two node sequences as operands and returns a sequence
containing all the nodes that occur in both operands.</p></li><li><p>The <code>except</code> operator takes two node sequences as operands and returns a sequence
containing all the nodes that occur in the first operand but not in the second
operand.</p></li></ul><p>All these operators eliminate duplicate nodes from their result sequences based on node identity. 
               The resulting sequence is returned in <a title="document order" class="termref" href="#dt-document-order">document order</a>.
            </p><p>If an operand
of <code>union</code>, <code>intersect</code>, or <code>except</code> contains an item that is not a node, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p><p>
If an <code>IntersectExceptExpr</code> contains more than two <code>InstanceofExprs,
they are grouped from left to right.</code>
With a <code>UnionExpr</code>, it makes no difference how operands are grouped,
the results are the same.
</p><div class="example"><p>Here are some examples of expressions that combine sequences. 
                  Assume the existence of three element nodes that we will refer to by symbolic names A, B, and C. 
                  Assume that the variables <code>$seq1</code>, <code>$seq2</code>  and <code>$seq3</code> 
                  are bound to the following sequences of these nodes:</p><ul><li><p>
                           <code>$seq1</code> is bound to (A, B)</p></li><li><p>
                           <code>$seq2</code> is bound to (A, B)</p></li><li><p>
                           <code>$seq3</code> is bound to (B, C)</p></li></ul><p>Then: </p><ul><li><p>
                           <code>$seq1 union $seq2</code>  evaluates to the sequence (A, B). </p></li><li><p>
                           <code>$seq2 union $seq3</code>   evaluates to the sequence (A, B, C). </p></li><li><p>
                           <code>$seq1 intersect $seq2</code>  evaluates to the sequence (A, B). </p></li><li><p>
                           <code>$seq2 intersect $seq3</code>  evaluates to the sequence containing B only.</p></li><li><p>
                           <code>$seq1 except $seq2</code>   evaluates to the empty sequence.</p></li><li><p>
                           <code>$seq2 except $seq3</code>  evaluates to the sequence containing A only.</p></li></ul></div><p>In addition to the sequence operators described here, see <a href="https://qt4cg.org/specifications/xpath-functions-40/#sequence-functions">Section 14 Processing sequences</a><sup><small>FO</small></sup> for functions defined on sequences.
</p></div></div><div class="div2">
<h3><a id="id-arithmetic"/>4.8 <a href="#id-arithmetic" style="text-decoration: none">Arithmetic Expressions</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-string-templates">⬇</a> <a href="#implausible-axis-steps">⬆</a></p><ol><li><p>
               The symbols <code>×</code> and <code>÷</code> can be used for multiplication and division.
            </p></li></ol></div><p>XPath 4.0 provides binary arithmetic operators for addition, subtraction,
multiplication, division, and modulus:</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AdditiveExpr"/><code><a href="#prod-xpath40-AdditiveExpr">AdditiveExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-AdditiveExpr-MultiplicativeExpr">MultiplicativeExpr</a>  (("+"  |  "-")  <a href="#doc-xpath40-AdditiveExpr-MultiplicativeExpr">MultiplicativeExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AdditiveExpr-MultiplicativeExpr"/><code><a href="#prod-xpath40-MultiplicativeExpr">MultiplicativeExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-AdditiveExpr-UnionExpr">UnionExpr</a>  (("*"  |  "×"  |  "div"  |  "÷"  |  "idiv"  |  "mod")  <a href="#doc-xpath40-AdditiveExpr-UnionExpr">UnionExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-AdditiveExpr-UnionExpr"/><code><a href="#prod-xpath40-UnionExpr">UnionExpr</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-IntersectExceptExpr">IntersectExceptExpr</a>  (("union"  |  "|")  <a href="#prod-xpath40-IntersectExceptExpr">IntersectExceptExpr</a>)*</code></td></tr></tbody></table><p>In addition, unary operators are provided for addition and subtraction:</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryExpr"/><code><a href="#prod-xpath40-UnaryExpr">UnaryExpr</a></code></td><td>::=</td><td><code>("-"  |  "+")*  <a href="#doc-xpath40-UnaryExpr-ValueExpr">ValueExpr</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryExpr-ValueExpr"/><code><a href="#prod-xpath40-ValueExpr">ValueExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-UnaryExpr-SimpleMapExpr">SimpleMapExpr</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryExpr-SimpleMapExpr"/><code><a href="#prod-xpath40-SimpleMapExpr">SimpleMapExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PathExpr">PathExpr</a>  ("!"  <a href="#doc-xpath40-PathExpr">PathExpr</a>)*</code></td></tr></tbody></table><p>A subtraction operator must be preceded by whitespace if
it could otherwise be interpreted as part of the previous token. For
example, <code>a-b</code> will be interpreted as a
name, but <code>a - b</code> and <code>a -b</code> will be interpreted as arithmetic expressions. (See <a href="#whitespace-rules"><b>A.3.5 Whitespace Rules</b></a> for further details on whitespace handling.)</p><p>The arithmetic operator symbols <code>*</code> and <span class="unicode-codepoint">U+00D7</span> (<span class="unicode-name">MULTIPLICATION SIGN</span>, <code>×</code>)  are interchangeable,
         and denote multiplication.</p><p>The arithmetic operator symbols <code>div</code> and <span class="unicode-codepoint">U+00F7</span> (<span class="unicode-name">DIVISION SIGN</span>, <code>÷</code>)  are interchangeable,
            and denote division.</p><p>
If an <code>AdditiveExpr</code> contains more than two <code>MultiplicativeExprs</code>,
they are grouped from left to right. So, for instance,
</p><div class="exampleInner"><pre>A - B + C - D</pre></div><p>
is equivalent to
</p><div class="exampleInner"><pre>((A - B) + C) - D</pre></div><p>
Similarly, the operands of a <code>MultiplicativeExpr</code> are grouped from left to right.
</p><p>The first step in evaluating an arithmetic expression is to evaluate its operand (for
            a unary operator) or operands (for a binary operator). 
            The order in which the operands are evaluated is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>.</p><p class="xpath">If <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>true</code>, each operand is evaluated by applying the following steps, in order:</p><ol class="enumar"><li><p>
                     <a title="atomization" class="termref" href="#dt-atomization">Atomization</a> is applied to the operand. The result of this
    operation is called the <b>atomized operand</b>.</p></li><li><p>If the atomized operand is an empty sequence, the result of
    the arithmetic expression is the <code>xs:double</code> value <code>NaN</code>, and the implementation
    need not evaluate the other operand or apply the operator. However,
    an implementation may choose to evaluate the other operand in order
    to determine whether it raises an error.</p></li><li><p>If the atomized operand is a sequence of length greater than one, any items after the first item in the sequence are discarded.</p></li><li><p>If the atomized operand is now an instance of type <code>xs:boolean</code>, <code>xs:string</code>,
<code>xs:decimal</code> (including <code>xs:integer</code>), <code>xs:float</code>, or <code>xs:untypedAtomic</code>, then it
is converted to the type <code>xs:double</code> by applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-number"><code>fn:number</code></a> function. (Note that <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-number"><code>fn:number</code></a> returns the value <code>NaN</code> if its operand cannot be converted to a number.)</p></li></ol><p>
               <span class="xpath">If <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>false</code>, each</span>
             operand is evaluated by applying the following steps, in order:</p><ol class="enumar"><li><p>
                     <a title="atomization" class="termref" href="#dt-atomization">Atomization</a> is applied to the operand. The result of this
    operation is called the <b>atomized operand</b>.</p></li><li><p>If the atomized operand is an empty sequence, the result of
    the arithmetic expression is an empty sequence, and the implementation
    need not evaluate the other operand or apply the operator. However,
    an implementation may choose to evaluate the other operand in order
    to determine whether it raises an error.</p></li><li><p> If the atomized operand is a sequence of
length greater than one, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li><li><p>If the atomized operand is of type <code>xs:untypedAtomic</code>, it is cast to  <code>xs:double</code>. If
the cast fails, a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic
error</a> is raised. [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFORG0001" title="err:FORG0001">err:FORG0001</a>]<sup><small>FO40</small></sup>
                  </p></li></ol><p>If, after this process, both operands of a binary arithmetic operator
            are instances of <code>xs:numeric</code>
         but have different primitive types, they are coerced to a common type by applying
         the following rules:</p><ol class="enumar"><li><p>If either of the items is of type <code>xs:double</code>, then 
            both the values are cast to type <code>xs:double</code>.</p></li><li><p>Otherwise, if either of the items is of type <code>xs:float</code>, then 
              both the values are cast to type <code>xs:float</code>.</p></li><li><p>Otherwise, no casting takes place: the values remain as <code>xs:decimal</code>.</p></li></ol><p>After this preparation, the arithmetic expression is evaluated by applying the appropriate
         function listed in the table below. The definitions of these functions are found in <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.</p><table style="border:1px solid" class="small"><caption>Unary Arithmetic Operators</caption><thead><tr><th>Expression</th><th>Type of A</th><th>Function</th><th>Result type</th></tr></thead><tbody><tr><td>+ A</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-unary-plus"><code>op:numeric-unary-plus</code></a>
                        <code>(A)</code>
                     </td><td>xs:numeric</td></tr><tr><td>- A</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-unary-minus"><code>op:numeric-unary-minus</code></a>
                        <code>(A)</code>
                     </td><td>xs:numeric</td></tr></tbody></table><table style="border:1px solid" class="small"><caption>Binary Arithmetic Operators</caption><tbody><tr><th>Expression</th><th>Type of A</th><th>Type of B</th><th>Function</th><th>Result type</th></tr><tr><td>A + B</td><td>xs:numeric</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-add"><code>op:numeric-add</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:numeric</td></tr><tr><td>A + B</td><td>xs:date</td><td>xs:yearMonthDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-yearMonthDuration-to-date"><code>op:add-yearMonthDuration-to-date</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:date</td></tr><tr><td>A + B</td><td>xs:yearMonthDuration</td><td>xs:date</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-yearMonthDuration-to-date"><code>op:add-yearMonthDuration-to-date</code></a>
                        <code>(B, A)</code>
                     </td><td>xs:date</td></tr><tr><td>A + B</td><td>xs:date</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-dayTimeDuration-to-date"><code>op:add-dayTimeDuration-to-date</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:date</td></tr><tr><td>A + B</td><td>xs:dayTimeDuration</td><td>xs:date</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-dayTimeDuration-to-date"><code>op:add-dayTimeDuration-to-date</code></a>
                        <code>(B, A)</code>
                     </td><td>xs:date</td></tr><tr><td>A + B</td><td>xs:time</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-dayTimeDuration-to-time"><code>op:add-dayTimeDuration-to-time</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:time</td></tr><tr><td>A + B</td><td>xs:dayTimeDuration</td><td>xs:time</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-dayTimeDuration-to-time"><code>op:add-dayTimeDuration-to-time</code></a>
                        <code>(B, A)</code>
                     </td><td>xs:time</td></tr><tr><td>A + B</td><td>xs:dateTime</td><td>xs:yearMonthDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-yearMonthDuration-to-dateTime"><code>op:add-yearMonthDuration-to-dateTime</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dateTime</td></tr><tr><td>A + B</td><td>xs:yearMonthDuration</td><td>xs:dateTime</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-yearMonthDuration-to-dateTime"><code>op:add-yearMonthDuration-to-dateTime</code></a>
                        <code>(B, A)</code>
                     </td><td>xs:dateTime</td></tr><tr><td>A + B</td><td>xs:dateTime</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-dayTimeDuration-to-dateTime"><code>op:add-dayTimeDuration-to-dateTime</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dateTime</td></tr><tr><td>A + B</td><td>xs:dayTimeDuration</td><td>xs:dateTime</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-dayTimeDuration-to-dateTime"><code>op:add-dayTimeDuration-to-dateTime</code></a>
                        <code>(B, A)</code>
                     </td><td>xs:dateTime</td></tr><tr><td>A + B</td><td>xs:yearMonthDuration</td><td>xs:yearMonthDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-yearMonthDurations"><code>op:add-yearMonthDurations</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:yearMonthDuration</td></tr><tr><td>A + B</td><td>xs:dayTimeDuration</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-add-dayTimeDurations"><code>op:add-dayTimeDurations</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dayTimeDuration</td></tr><tr><td>A - B</td><td>xs:numeric</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-subtract"><code>op:numeric-subtract</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:numeric</td></tr><tr><td>A - B</td><td>xs:date</td><td>xs:date</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-dates"><code>op:subtract-dates</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dayTimeDuration</td></tr><tr><td>A - B</td><td>xs:date</td><td>xs:yearMonthDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-yearMonthDuration-from-date"><code>op:subtract-yearMonthDuration-from-date</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:date</td></tr><tr><td>A - B</td><td>xs:date</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-dayTimeDuration-from-date"><code>op:subtract-dayTimeDuration-from-date</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:date</td></tr><tr><td>A - B</td><td>xs:time</td><td>xs:time</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-times"><code>op:subtract-times</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dayTimeDuration</td></tr><tr><td>A - B</td><td>xs:time</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-dayTimeDuration-from-time"><code>op:subtract-dayTimeDuration-from-time</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:time</td></tr><tr><td>A - B</td><td>xs:dateTime</td><td>xs:dateTime</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-dateTimes"><code>op:subtract-dateTimes</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dayTimeDuration</td></tr><tr><td>A - B</td><td>xs:dateTime</td><td>xs:yearMonthDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-yearMonthDuration-from-dateTime"><code>op:subtract-yearMonthDuration-from-dateTime</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dateTime</td></tr><tr><td>A - B</td><td>xs:dateTime</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-dayTimeDuration-from-dateTime"><code>op:subtract-dayTimeDuration-from-dateTime</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dateTime</td></tr><tr><td>A - B</td><td>xs:yearMonthDuration</td><td>xs:yearMonthDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-yearMonthDurations"><code>op:subtract-yearMonthDurations</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:yearMonthDuration</td></tr><tr><td>A - B</td><td>xs:dayTimeDuration</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-subtract-dayTimeDurations"><code>op:subtract-dayTimeDurations</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dayTimeDuration</td></tr><tr><td>A * B</td><td>xs:numeric</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-multiply"><code>op:numeric-multiply</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:numeric</td></tr><tr><td>A * B</td><td>xs:yearMonthDuration</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-multiply-yearMonthDuration"><code>op:multiply-yearMonthDuration</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:yearMonthDuration</td></tr><tr><td>A * B</td><td>xs:numeric</td><td>xs:yearMonthDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-multiply-yearMonthDuration"><code>op:multiply-yearMonthDuration</code></a>
                        <code>(B, A)</code>
                     </td><td>xs:yearMonthDuration</td></tr><tr><td>A * B</td><td>xs:dayTimeDuration</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-multiply-dayTimeDuration"><code>op:multiply-dayTimeDuration</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dayTimeDuration</td></tr><tr><td>A * B</td><td>xs:numeric</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-multiply-dayTimeDuration"><code>op:multiply-dayTimeDuration</code></a>
                        <code>(B, A)</code>
                     </td><td>xs:dayTimeDuration</td></tr><tr><td>A idiv B</td><td>xs:numeric</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-integer-divide"><code>op:numeric-integer-divide</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:integer</td></tr><tr><td>A div B</td><td>xs:numeric</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-divide"><code>op:numeric-divide</code></a>
                        <code>(A, B)</code>
                     </td><td>numeric; but xs:decimal if both operands are xs:integer</td></tr><tr><td>A div B</td><td>xs:yearMonthDuration</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-divide-yearMonthDuration"><code>op:divide-yearMonthDuration</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:yearMonthDuration</td></tr><tr><td>A div B</td><td>xs:dayTimeDuration</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-divide-dayTimeDuration"><code>op:divide-dayTimeDuration</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:dayTimeDuration</td></tr><tr><td>A div B</td><td>xs:yearMonthDuration</td><td>xs:yearMonthDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-divide-yearMonthDuration-by-yearMonthDuration"><code>op:divide-yearMonthDuration-by-yearMonthDuration</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:decimal</td></tr><tr><td>A div B</td><td>xs:dayTimeDuration</td><td>xs:dayTimeDuration</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-divide-dayTimeDuration-by-dayTimeDuration"><code>op:divide-dayTimeDuration-by-dayTimeDuration</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:decimal</td></tr><tr><td>A mod B</td><td>xs:numeric</td><td>xs:numeric</td><td>
                        <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-mod"><code>op:numeric-mod</code></a>
                        <code>(A, B)</code>
                     </td><td>xs:numeric</td></tr></tbody></table><div class="note"><p class="prefix"><b>Note:</b></p><p>The operator symbol <code>×</code> is a synonym of <code>*</code>, while <code>÷</code> is
a synonym of <code>div</code>.</p></div><p>If there is no entry in the table for the combination of operator and operands,
    then a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p><p>Errors may also occur during coercion of the operands, or during evaluation of the
 identified function (for example, an error
might result from dividing by zero).</p><div class="note"><p class="prefix"><b>Note:</b></p><p>XPath 4.0 provides three division operators:</p><ul><li><p>The <code>div</code> and <code>÷</code> operators are synonyms, and implement
               numeric division as well as division of duration values; the semantics are defined in
               <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-numeric-divide">Section 4.2.4 op:numeric-divide</a><sup><small>FO</small></sup>
                     </p></li><li><p>The <code>idiv</code> operator implements integer division; the semantics are defined
               in <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-numeric-integer-divide">Section 4.2.5 op:numeric-integer-divide</a><sup><small>FO</small></sup>
                     </p></li></ul></div><p>Here are some examples of arithmetic expressions:</p><ul><li><p>The first expression below returns the <code>xs:decimal</code> value <code>-1.5</code>, and the second expression returns the <code>xs:integer</code> value <code>-1</code>:</p><div class="exampleInner"><pre>-3 div 2
-3 idiv 2</pre></div></li><li><p>Subtraction of two date values results in a value of type <code>xs:dayTimeDuration</code>:</p><div class="exampleInner"><pre>$emp/hiredate - $emp/birthdate</pre></div></li><li><p>This example illustrates the difference between a subtraction operator and a
hyphen:</p><div class="exampleInner"><pre>$unit-price - $unit-discount</pre></div></li><li><p>Unary operators have higher precedence than binary operators (other than <code>!</code>, <code>/</code>, and <code>[]</code>), subject of
course to the use of parentheses. Therefore, the following two examples have different meanings:</p><div class="exampleInner"><pre>-$bellcost + $whistlecost
-($bellcost + $whistlecost)</pre></div></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p id="note-consecutive-unary-ops">Multiple consecutive unary arithmetic operators are permitted (though not useful).</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Negation is not the same as subtraction from zero: if <code>$x</code> is positive zero,
            then <code>-$x</code> returns negative zero, wheras <code>0 - $x</code> returns positive zero.</p></div></div><div class="div2">
<h3><a id="id-string-expr"/>4.9 <a href="#id-string-expr" style="text-decoration: none">String Expressions</a></h3><p>This section describes several ways of constructing strings.</p><div class="div3">
<h4><a id="id-string-concat-expr"/>4.9.1 <a href="#id-string-concat-expr" style="text-decoration: none">String Concatenation Expressions</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringConcatExpr"/><code><a href="#prod-xpath40-StringConcatExpr">StringConcatExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-StringConcatExpr-RangeExpr">RangeExpr</a>  ("||"  <a href="#doc-xpath40-StringConcatExpr-RangeExpr">RangeExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringConcatExpr-RangeExpr"/><code><a href="#prod-xpath40-RangeExpr">RangeExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-AdditiveExpr">AdditiveExpr</a>  ("to"  <a href="#doc-xpath40-AdditiveExpr">AdditiveExpr</a>)?</code></td></tr></tbody></table><p>String concatenation expressions allow the string representations of values to be
               concatenated. In XPath 4.0, <code>$a || $b</code> is equivalent to
               <code>fn:concat($a, $b)</code>.
               The following expression evaluates to the string <code>concatenate</code>:</p><div class="exampleInner"><pre>() || "con" || ("cat", "enate")</pre></div></div><div class="div3">
<h4><a id="id-string-templates"/>4.9.2 <a href="#id-string-templates" style="text-decoration: none">String Templates</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-value-comparisons">⬇</a> <a href="#id-arithmetic">⬆</a></p><ol><li><p>
                  String templates provide a new way of constructing strings: for example <code>`{$greeting}, {$planet}!`</code>
                  is equivalent to <code>$greeting || ', ' || $planet || '!'</code>
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/58">58</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/324">324</a> 29 January 2023]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringTemplate"/><code><a href="#prod-xpath40-StringTemplate">StringTemplate</a></code></td><td>::=</td><td><code>"`"  (<a href="#doc-xpath40-StringTemplate-StringTemplateFixedPart">StringTemplateFixedPart</a>  |  <a href="#doc-xpath40-StringTemplate-StringTemplateVariablePart">StringTemplateVariablePart</a>)*  "`"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringTemplate-StringTemplateFixedPart"/><code><a href="#prod-xpath40-StringTemplateFixedPart">StringTemplateFixedPart</a></code></td><td>::=</td><td><code>((<a href="#doc-xpath40-StringTemplate-Char">Char</a> - ('{' | '}' | '`'))  |  "{{"  |  "}}"  |  "``")*</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringTemplate-Char"/><code><a href="#prod-xpath40-Char">Char</a></code></td><td>::=</td><td><code>
                        <a href="https://www.w3.org/TR/REC-xml/#NT-Char">[http://www.w3.org/TR/REC-xml#NT-Char]</a><sup><small>XML</small></sup>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-xml-version">xgc: xml-version</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringTemplate-StringTemplateVariablePart"/><code><a href="#prod-xpath40-StringTemplateVariablePart">StringTemplateVariablePart</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-StringTemplate-EnclosedExpr">EnclosedExpr</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-StringTemplate-EnclosedExpr"/><code><a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a></code></td><td>::=</td><td><code>"{"  <a href="#prod-xpath40-Expr">Expr</a>?  "}"</code></td></tr></tbody></table><p>String templates provide an alternative way of constructing strings. For example,
            the expression <code>`Pi is { round(math:pi(), 4) }`</code> returns the string <code>"Pi is 3.1416"</code>.</p><p>A string template starts and ends with <span class="unicode-codepoint">U+0060</span> (<span class="unicode-name">GRAVE ACCENT, BACKTICK</span>, <code>`</code>) , popularly known as a back-tick. Between
               the back-ticks is a string consisting of an sequence of fixed parts and
               variable parts:</p><ul><li><p>A variable part consists of an optional XPath expression enclosed in curly brackets (<code>{}</code>):
                  more specifically, a string conforming 
                  to the XPath production <code>Expr?</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>An expression within a variable part may contain an unescaped <span class="unicode-codepoint">U+007B</span> (<span class="unicode-name">LEFT CURLY BRACKET</span>, <code>{</code>)  
                        or <span class="unicode-codepoint">U+007D</span> (<span class="unicode-name">RIGHT CURLY BRACKET</span>, <code>}</code>)  within
                        a <a href="#doc-xpath40-StringLiteral">StringLiteral</a> or within
                        a comment.</p><p>The fact that the expression is optional means that the
                        string contained between the curly brackets may be zero-length, may comprise whitespace
                        only, or may contain XPath comments. The effective value in this case is a zero-length
                        string, which is equivalent to omitting the variable part entirely, together with its 
                        curly-bracket delimiters. </p></div></li><li><p>A fixed part may contain any characters, except that:</p><ul><li><p>The character <span class="unicode-codepoint">U+007B</span> (<span class="unicode-name">LEFT CURLY BRACKET</span>, <code>{</code>) 
                              <span class="verb">must</span>
                        be written as <code>{{</code>.</p></li><li><p>The character <span class="unicode-codepoint">U+007D</span> (<span class="unicode-name">RIGHT CURLY BRACKET</span>, <code>}</code>) 
                              <span class="verb">must</span> be
                        written as <code>}}</code>.</p></li><li><p>The character <span class="unicode-codepoint">U+0060</span> (<span class="unicode-name">GRAVE ACCENT, BACKTICK</span>, <code>`</code>) 
                              <span class="verb">must</span> be
                        written as <code>``</code>.</p></li></ul><p>Following the principles of the “longest token” rule, any occurrence
                  of <code>{{</code> within the fixed part is interpreted as an escaped left
                  curly bracket. This means that the enclosed expression must not start with
                  <span class="unicode-codepoint">U+007B</span> (<span class="unicode-name">LEFT CURLY BRACKET</span>, <code>{</code>) : if this is required, the two left curly brackets can
                  be separated by whitespace. For example the string template
                     <code>`{{"key":"{ {1:"yes", 0:"no"}?$condition}"}}`</code>
                     evaluates to the string <code>{"key":"yes"}</code> or <code>{"key":"no"}</code>
                     depending on the value of <code>$condition</code>.</p><p>By contrast, if the enclosed expression ends with <span class="unicode-codepoint">U+007D</span> (<span class="unicode-name">RIGHT CURLY BRACKET</span>, <code>}</code>) ,
                  this can be immediately followed by the closing <span class="unicode-codepoint">U+007D</span> (<span class="unicode-name">RIGHT CURLY BRACKET</span>, <code>}</code>) 
                  delimiter without intervening whitespace.</p></li></ul><p>
               The result of evaluating a
                  string template is the string obtained by concatenating the expansions of the fixed
               and variable parts:</p><ul><li><p>The expansion of a fixed part is obtained by replacing any double curly
                     brackets (<code>{{</code> or <code>}}</code>) by the corresponding single curly
                     bracket, and replacing doubled back-ticks (<code>``</code>) by a single back-tick.</p></li><li><p>The expansion of a variable part containing an expression is as follows:</p><ol class="enumar"><li><p>
                              <a title="atomization" class="termref" href="#dt-atomization">Atomization</a> is applied to the value of the enclosed expression, 
                           converting it to a sequence of atomic items.</p></li><li><p>If the result of atomization is an empty sequence, the result 
                           is the zero-length string. Otherwise, each atomic item in the 
                           atomized sequence is cast into a string.</p></li><li><p>The individual strings resulting from the previous step are 
                           merged into a single string by concatenating them with a 
                           single space character between each pair.</p></li></ol></li><li><p>The expansion of an empty variable part (one that contains no expression) is a zero-length string.</p></li></ul><p>For example:</p><div class="exampleInner"><pre>let $greeting := "Hello", $planet := "Mars"
return `{ $greeting }, { $planet }!`</pre></div><p>returns <code>"Hello, Mars!"</code>.</p><p>The expression:</p><div class="exampleInner"><pre>let $long-months := (1, 3, 5, 7, 8, 10, 12)
return `The months with 31 days are: { $long-months }.`</pre></div><p>returns <code>"The months with 31 days are: 1 3 5 7 8 10 12."</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The rules for processing an enclosed expression are identical to the rules for attributes in
               XQuery direct element constructors. These rules differ slightly from the rules in XSLT
               attribute value templates, where adjacent text nodes are concatenated with no separator,
               prior to atomization.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>A string template containing no variable parts is effectively just another
                  way of writing a string literal: <code>"Goethe"</code>, <code>'Goethe'</code>, and <code>`Goethe`</code>
               are interchangeable. This means that back-ticks can sometimes be a useful way of delimiting a string
               that contains both single and double quotes: <code>`He said: "I didn't."`</code>.</p><p>It is sometimes useful to use string templates in conjunction with the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-char"><code>fn:char</code></a> function
                  to build strings containing special characters, for example <code>`Chapter{ fn:char("nbsp") }{ $chapNr }`</code>.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>String literals containing an ampersand behave differently between XPath and XQuery: in XPath 
               (unless first expanded by an XML parser) the string literal <code>"Bacon &amp; Eggs"</code> 
               represents a string containing an ampersand, while in XQuery
               it is an error, because an ampersand is taken as introducing a character reference. This difference
               does not arise for string templates, since neither XPath nor XQuery recognizes entity or character references
               in a string template.
               This means that back-tick delimited strings (such as <code>`Bacon &amp; Eggs`</code>) 
                  may be useful in contexts where an XPath expression
               is required to have the same effect whether it is evaluated using an XPath or an XQuery processor.</p></div><p>In XQuery, the token <code>``[</code> is recognized as the start of a 
               <code>StringConstructor</code>,
            under the “longest token” rule (see <a href="#lexical-structure"><b>A.3 Lexical structure</b></a>). This means that the construct
               <code>``[1]</code> is not recognized as a <a href="#doc-xpath40-StringTemplate">StringTemplate</a> followed by a predicate. 
               <span class="xpath">Although
               the token <code>``[</code> is not used in XPath, it is reserved for compatibility reasons, and <span class="verb">must</span>
               be rejected as syntactically invalid.</span> In the unlikely event that an empty <a href="#doc-xpath40-StringTemplate">StringTemplate</a>
            followed by a predicate is wanted, whitespace or parentheses can be used to avoid the tokenization problem.</p></div></div><div class="div2">
<h3><a id="id-comparisons"/>4.10 <a href="#id-comparisons" style="text-decoration: none">Comparison Expressions</a></h3><p>Comparison expressions allow two values to be compared. XPath 4.0 provides
three kinds of comparison expressions, called value comparisons, general
comparisons, and node comparisons.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ComparisonExpr"/><code><a href="#prod-xpath40-ComparisonExpr">ComparisonExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ComparisonExpr-OtherwiseExpr">OtherwiseExpr</a>  ((<a href="#doc-xpath40-ComparisonExpr-ValueComp">ValueComp</a>  |  <a href="#doc-xpath40-ComparisonExpr-GeneralComp">GeneralComp</a>  |  <a href="#doc-xpath40-ComparisonExpr-NodeComp">NodeComp</a>)  <a href="#doc-xpath40-ComparisonExpr-OtherwiseExpr">OtherwiseExpr</a>)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ComparisonExpr-OtherwiseExpr"/><code><a href="#prod-xpath40-OtherwiseExpr">OtherwiseExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-StringConcatExpr">StringConcatExpr</a>  ("otherwise"  <a href="#doc-xpath40-StringConcatExpr">StringConcatExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ComparisonExpr-ValueComp"/><code><a href="#prod-xpath40-ValueComp">ValueComp</a></code></td><td>::=</td><td><code>"eq"  |  "ne"  |  "lt"  |  "le"  |  "gt"  |  "ge"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ComparisonExpr-GeneralComp"/><code><a href="#prod-xpath40-GeneralComp">GeneralComp</a></code></td><td>::=</td><td><code>"="  |  "!="  |  "&lt;"  |  "&lt;="  |  "&gt;"  |  "&gt;="</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ComparisonExpr-NodeComp"/><code><a href="#prod-xpath40-NodeComp">NodeComp</a></code></td><td>::=</td><td><code>"is"  |  "&lt;&lt;"  |  "&gt;&gt;"</code></td></tr></tbody></table><div class="note"><p class="prefix"><b>Note:</b></p><p class="xpath">When an XPath expression is written
within an XML document, the XML escaping rules for special characters
must be followed; thus <code>&lt;</code> must be written as
<code>&amp;lt;</code>.</p></div><p>For a summary of the differences between different ways of comparing atomic items
         in XPath 4.0, see <a href="#id-atomic-comparisons"><b>G Atomic Comparisons: An Overview</b></a>.</p><div class="div3">
<h4><a id="id-value-comparisons"/>4.10.1 <a href="#id-value-comparisons" style="text-decoration: none">Value Comparisons</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-general-comparisons">⬇</a> <a href="#id-string-templates">⬆</a></p><ol><li><p>
                  The rules for value comparisons when comparing values of different types (for example, decimal and double)
                  have changed to be transitive. A decimal value is no longer converted to double, instead the double is converted
                  to a decimal without loss of precision. This may affect compatibility in edge cases involving comparison of
                  values that are numerically very close.
               </p></li></ol></div><p>The value comparison operators are <code>eq</code>, <code>ne</code>, <code>lt</code>, <code>le</code>, <code>gt</code>, and <code>ge</code>. Value comparisons are used for comparing single values.</p><p>The first step in evaluating a value comparison is to evaluate its operands. The order in which the operands are evaluated is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>. Each operand is evaluated by applying the following steps, in order:</p><ol class="enumar"><li><p>
                        <a title="atomization" class="termref" href="#dt-atomization">Atomization</a> is applied to each  operand. The result of this
    operation is called the <b>atomized operand</b>.</p></li><li><p>If an atomized operand is an empty sequence, the result of
    the value comparison is an empty sequence, and the implementation
    need not evaluate the other operand or apply the operator. However,
    an implementation may choose to evaluate the other operand in order
    to determine whether it raises an error.</p></li><li><p> If an atomized operand is a sequence of
length greater than one, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li><li><p>If an  atomized operand is of type
  <code>xs:untypedAtomic</code>, it is cast to
  <code>xs:string</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The purpose of this rule is to
  make value comparisons transitive. Users should be aware that the
  general comparison operators have a different rule for casting of
  <code>xs:untypedAtomic</code> operands. Users should also be aware
  that transitivity of value comparisons may be compromised by loss of
  precision during type conversion (for example, two
  <code>xs:integer</code> values that differ slightly may both be
  considered equal to the same <code>xs:float</code> value because
  <code>xs:float</code> has less precision than
  <code>xs:integer</code>).</p></div></li><li><p>If the two operands are instances of different primitive types (meaning the 19 primitive types defined in <a href="https://www.w3.org/TR/xmlschema-2/#built-in-primitive-datatypes">Section 
3.2 Primitive datatypes</a><sup><small>XS2</small></sup>), then:
    </p><ol class="enumla"><li><p>If each operand is an instance of one of the types <code>xs:string</code> or <code>xs:anyURI</code>, then both operands are cast to type <code>xs:string</code>.</p></li><li><p>If each operand is an instance of one of the types <code>xs:decimal</code> or <code>xs:float</code>, then both operands are cast to type <code>xs:float</code>.</p></li><li><p>If each operand is an instance of one of the types <code>xs:decimal</code>, <code>xs:float</code>, or <code>xs:double</code>, then both operands are cast to type <code>xs:double</code>.</p></li><li><p>Otherwise, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The primitive type of an <code>xs:integer</code> value for this purpose is <code>xs:decimal</code>.</p></div></li></ol></li><li><p>Expressions using operators other than <code>eq</code> and <code>lt</code>
                  are rewritten as follows:</p><blockquote><p>
                           <code>A ne B</code> becomes <code>not(A eq B)</code>
                        <br/>
                           <code>A le B</code> becomes <code>A lt B or A eq B</code>
                        <br/>
                           <code>A gt B</code> becomes <code>B lt A</code>
                        <br/>
                           <code>A ge B</code> becomes <code>B lt A or B eq A</code>
                        </p></blockquote></li><li><p>Finally, if the types of the operands are a valid
  combination for the given operator, the operator is applied to the
  operands by applying the appropriate function from the table below.</p></li></ol><table style="border:1px solid" class="small"><caption>Value Comparison Operators</caption><tbody><tr><th>Expression</th><th>Type(A)</th><th>Type(B)</th><th>Function</th><th>Result type</th></tr><tr><td>A eq B</td><td>xs:numeric</td><td>xs:numeric</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-equal"><code>op:numeric-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:boolean</td><td>xs:boolean</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean-equal"><code>op:boolean-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:string</td><td>xs:string</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-equal"><code>op:numeric-equal</code></a>
                           <code>(</code>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-compare"><code>fn:compare</code></a>
                           <code>(A, B), 0)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:date</td><td>xs:date</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-date-equal"><code>op:date-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:time</td><td>xs:time</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-time-equal"><code>op:time-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:dateTime</td><td>xs:dateTime</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-dateTime-equal"><code>op:dateTime-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:duration</td><td>xs:duration</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-duration-equal"><code>op:duration-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:gYear</td><td>xs:gYear</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-gYear-equal"><code>op:gYear-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:gYearMonth</td><td>xs:gYearMonth</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-gYearMonth-equal"><code>op:gYearMonth-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:gMonth</td><td>xs:gMonth</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-gMonth-equal"><code>op:gMonth-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:gMonthDay</td><td>xs:gMonthDay</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-gMonthDay-equal"><code>op:gMonthDay-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:gDay</td><td>xs:gDay</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-gDay-equal"><code>op:gDay-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>(xs:hexBinary | xs:base64Binary)</td><td>(xs:hexBinary | xs:base64Binary)</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-binary-equal"><code>op:binary-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:QName</td><td>xs:QName</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-QName-equal"><code>op:QName-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A eq B</td><td>xs:NOTATION</td><td>xs:NOTATION</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-NOTATION-equal"><code>op:NOTATION-equal</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>xs:numeric</td><td>xs:numeric</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-less-than"><code>op:numeric-less-than</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>xs:boolean</td><td>xs:boolean</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean-less-than"><code>op:boolean-less-than</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>xs:string</td><td>xs:string</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-numeric-less-than"><code>op:numeric-less-than</code></a>
                           <code>(</code>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-compare"><code>fn:compare</code></a>
                           <code>(A, B), 0)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>xs:date</td><td>xs:date</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-date-less-than"><code>op:date-less-than</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>xs:time</td><td>xs:time</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-time-less-than"><code>op:time-less-than</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>xs:dateTime</td><td>xs:dateTime</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-dateTime-less-than"><code>op:dateTime-less-than</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>xs:yearMonthDuration</td><td>xs:yearMonthDuration</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-yearMonthDuration-less-than"><code>op:yearMonthDuration-less-than</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>xs:dayTimeDuration</td><td>xs:dayTimeDuration</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-dayTimeDuration-less-than"><code>op:dayTimeDuration-less-than</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr><tr><td>A lt B</td><td>(xs:hexBinary | xs:base64Binary)</td><td>(xs:hexBinary | xs:base64Binary)</td><td>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-binary-less-than"><code>op:binary-less-than</code></a>
                           <code>(A, B)</code>
                        </td><td>xs:boolean</td></tr></tbody></table><p>The definitions of the operator functions are found in <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.</p><p>If the table contains no entry corresponding to the types of the operands, 
               after evaluation, then a <a title="type error" class="termref" href="#dt-type-error">type error</a>
is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p><p>Here are some examples of value comparisons:</p><ul><li><p>The following comparison atomizes the node(s) that are returned by the expression <code>$book/author</code>. The comparison is true only if the result of atomization is the value "Kennedy" as an instance of <code>xs:string</code> or <code>xs:untypedAtomic</code>. If the result of atomization is an empty sequence, the result of the comparison is an empty sequence. If the result of atomization is a sequence containing more than one value, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p><div class="exampleInner"><pre>$book1/author eq "Kennedy"</pre></div></li><li><p>The following comparison is <code>true</code> because atomization converts an array to its member sequence:</p><div class="exampleInner"><pre>[ "Kennedy" ] eq "Kennedy"</pre></div></li><li><p>The following <a title="path expression" class="termref" href="#dt-path-expression">path expression</a> contains a predicate that selects products whose weight is greater than 100. For any product that does not have a <code>weight</code> subelement, the value of the predicate is the empty sequence, and the product is not selected. This example assumes that <code>weight</code> is a validated element with a numeric type.</p><div class="exampleInner"><pre>//product[weight gt 100]</pre></div></li><li><p>The following comparison is true if <code>my:hatsize</code> and <code>my:shoesize</code> are both user-defined types that are derived by restriction from a primitive <a title="numeric" class="termref" href="#dt-numeric">numeric</a> type:</p><div class="exampleInner"><pre>my:hatsize(5) eq my:shoesize(5)</pre></div></li><li><p>The following comparison is true. The <code>eq</code> operator compares two QNames by performing codepoint-comparisons of their namespace URIs and their local names, ignoring their namespace prefixes.</p><div class="exampleInner"><pre>QName("http://example.com/ns1", "this:color") eq
QName("http://example.com/ns1", "that:color")</pre></div></li></ul></div><div class="div3">
<h4><a id="id-general-comparisons"/>4.10.2 <a href="#id-general-comparisons" style="text-decoration: none">General Comparisons</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-for-expressions">⬇</a> <a href="#id-value-comparisons">⬆</a></p><ol><li><p>
                  Operators such as <code>&lt;</code> and <code>&gt;</code> can use the full-width forms
                  <code>＜</code> and <code>＞</code> to avoid the need for XML escaping.
               </p></li></ol></div><p>The general comparison operators are <code>=</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code>. General comparisons are existentially quantified comparisons that may be applied to operand sequences of any length. The result of a general comparison that does not raise an error is
always <code>true</code> or <code>false</code>.</p><p class="xpath">If <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>true</code>, a general comparison is evaluated by applying the following rules, in order:</p><ol class="enumar"><li><p>If either operand is a single atomic item that is an instance of
<code>xs:boolean</code>, then the other operand is converted to <code>xs:boolean</code> by taking its
<a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a>.</p></li><li><p>
                        <a title="atomization" class="termref" href="#dt-atomization">Atomization</a> is applied to each operand. After atomization, each operand is a sequence of atomic items.</p></li><li><p>If the comparison operator is <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, or <code>&gt;=</code>, then each item in both of the
operand sequences is converted to the type  <code>xs:double</code> by applying the
<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-number"><code>fn:number</code></a> function. (Note that <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-number"><code>fn:number</code></a> returns the value <code>NaN</code> if its operand cannot be converted to a number.)</p></li><li><p>The result of the comparison is <code>true</code> if and only if there is a pair of
atomic items, one in the first operand sequence and the other in the second operand sequence, that have the required
<b>magnitude relationship</b>. Otherwise the result of the  comparison is
<code>false</code> or an error. The <b>magnitude relationship</b> between two atomic items is determined by
applying the following rules. If a <code>cast</code> operation called for by these rules is not successful, a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>  is raised. [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFORG0001" title="err:FORG0001">err:FORG0001</a>]<sup><small>FO40</small></sup>
                     </p><ol class="enumla"><li><p>If at least one of the two atomic items is an instance of a <a title="numeric" class="termref" href="#dt-numeric">numeric</a> type, then both atomic items are converted to the type <code>xs:double</code> by
applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-number"><code>fn:number</code></a> function.</p></li><li><p>If at least one of the two atomic items is an instance of <code>xs:string</code>,
or if both atomic items are instances of <code>xs:untypedAtomic</code>, then both
atomic items are cast to the type <code>xs:string</code>.</p></li><li><p>If one of the atomic items is an instance of <code>xs:untypedAtomic</code> and the other is not an instance of <code>xs:string</code>, <code>xs:untypedAtomic</code>, or any <a title="numeric" class="termref" href="#dt-numeric">numeric</a> type, then the <code>xs:untypedAtomic</code> item is
cast to the <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> of the other value.</p></li><li><p>After performing the conversions described above, the atomic items are
compared using one of the value comparison operators <code>eq</code>, <code>ne</code>, <code>lt</code>, <code>le</code>, <code>gt</code>, or
<code>ge</code>, depending on whether the general comparison operator was <code>=</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>,
<code>&gt;</code>, or <code>&gt;=</code>. The values have the required <b>magnitude relationship</b> if and only if the result
of this value comparison is <code>true</code>.</p></li></ol></li></ol><p>
                  <span class="xpath">If <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>false</code>, a</span>
                general comparison is evaluated by applying the following rules, in order:</p><ol class="enumar"><li><p>
                        <a title="atomization" class="termref" href="#dt-atomization">Atomization</a> is applied to each operand. After atomization, each operand is a sequence of atomic items.</p></li><li><p>The result of the comparison is <code>true</code> if and only if there is a pair of
atomic items, one in the first operand sequence and the other in the second operand sequence, that have the required
<b>magnitude relationship</b>. Otherwise the result of the  comparison is
<code>false</code> or an error. The <b>magnitude relationship</b> between two atomic items is determined by
applying the following rules. If a <code>cast</code> operation called for by these rules is not successful, a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>  is raised. [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFORG0001" title="err:FORG0001">err:FORG0001</a>]<sup><small>FO40</small></sup>
                     </p><ol class="enumla"><li><p>If both atomic items are instances of <code>xs:untypedAtomic</code>,
                then the values are cast to the type <code>xs:string</code>.
             </p></li><li><p>If exactly one of the atomic items is an instance of
                <code>xs:untypedAtomic</code>, it is cast to a type depending on
                the other value’s dynamic type T according to the following rules,
                in which V denotes the value to be cast:
             </p><ol class="enumlr"><li><p>If T is a numeric type or is derived from a numeric type,
                      then V is cast to <code>xs:double</code>.</p></li><li><p>If T is <code>xs:dayTimeDuration</code> or is derived from
                      <code>xs:dayTimeDuration</code>,
                      then V is cast to <code>xs:dayTimeDuration</code>.</p></li><li><p>If T is <code>xs:yearMonthDuration</code> or is derived from
                      <code>xs:yearMonthDuration</code>,
                      then V is cast to <code>xs:yearMonthDuration</code>.</p></li><li><p>In all other cases, V is cast to the primitive base type of T.</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>
                The special treatment of the duration types is required to avoid
                errors that may arise when comparing the primitive type
                <code>xs:duration</code> with any duration type.
             </p></div></li><li><p>After performing the conversions described above, the atomic items are
compared using one of the value comparison operators <code>eq</code>, <code>ne</code>, <code>lt</code>, <code>le</code>, <code>gt</code>, or
<code>ge</code>, depending on whether the general comparison operator was <code>=</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>,
<code>&gt;</code>, or <code>&gt;=</code>. The values have the required <b>magnitude relationship</b> if and only if the result
of this value comparison is <code>true</code>.</p></li></ol></li></ol><p>When evaluating a general comparison in which either operand is a sequence of items, an implementation may return <code>true</code> as soon as it finds an item in the first operand and an item in the second operand that have the required <b>magnitude relationship</b>. Similarly, a general comparison may raise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> as soon as it encounters an error in evaluating either operand, or in comparing a pair of items from the two operands. As a result of these rules, the result of a general comparison is not deterministic in the presence of errors.</p><p>Here are some examples of  general comparisons:</p><ul><li><p>The following comparison is true if the <a title="typed value" class="termref" href="#dt-typed-value">typed value</a> of any
<code>author</code> subelement of <code>$book1</code> is "Kennedy" as an instance of <code>xs:string</code> or <code>xs:untypedAtomic</code>:</p><div class="exampleInner"><pre>$book1/author = "Kennedy"</pre></div></li><li><p>The following comparison is <code>true</code> because atomization converts an array to its member sequence:</p><div class="exampleInner"><pre>[ "Obama", "Nixon", "Kennedy" ] = "Kennedy"</pre></div></li><li><p>The following example contains three general comparisons. The value of the first two comparisons is <code>true</code>, and the value of the third comparison is <code>false</code>. This example illustrates the fact that general comparisons are not transitive.</p><div class="exampleInner"><pre>(1, 2) = (2, 3)
(2, 3) = (3, 4)
(1, 2) = (3, 4)</pre></div></li><li><p>The following example contains two general comparisons, both of which are <code>true</code>. This example illustrates the fact that the <code>=</code> and <code>!=</code> operators are not inverses of each other.</p><div class="exampleInner"><pre>(1, 2) = (2, 3)
(1, 2) != (2, 3)</pre></div></li><li><p>Suppose that <code>$a</code>, <code>$b</code>, and <code>$c</code> are bound to element nodes with type annotation <code>xs:untypedAtomic</code>, with <a title="string value" class="termref" href="#dt-string-value">string values</a>
                        <code>"1"</code>, <code>"2"</code>, and <code>"2.0"</code> respectively. Then <code>($a, $b) = ($c, 3.0)</code> returns <code>false</code>, because <code>$b</code> and <code>$c</code> are compared as strings. However, <code>($a, $b) = ($c, 2.0)</code> returns <code>true</code>, because <code>$b</code> and <code>2.0</code> are compared as numbers.</p></li></ul></div><div class="div3">
<h4><a id="id-node-comparisons"/>4.10.3 <a href="#id-node-comparisons" style="text-decoration: none">Node Comparisons</a></h4><p>Node comparisons are used to compare two nodes, by their identity or by their <a title="document order" class="termref" href="#dt-document-order">document order</a>. The result of a node comparison is defined by the following rules:</p><ol class="enumar"><li><p>The operands of a node comparison are evaluated in <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> order.</p></li><li><p>If either operand is an empty sequence, the result of the
    comparison is an empty sequence, and the implementation need not
    evaluate the other operand or apply the operator. However, an
    implementation may choose to evaluate the other operand in order to
    determine whether it raises an error.</p></li><li><p> Each operand must be either a single node or an empty sequence; otherwise
a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li><li><p>A comparison with the <code>is</code> operator is <code>true</code> if the two operand nodes are the same node; otherwise it
is <code>false</code>. See <a href="#xpath-datamodel-40">[XQuery and XPath Data Model (XDM) 4.0]</a> for  the definition of node identity.</p></li><li><p>A comparison with the <code>&lt;&lt;</code> operator returns <code>true</code> if the left operand node precedes the right operand node in
<a title="document order" class="termref" href="#dt-document-order">document order</a>; otherwise it returns <code>false</code>.</p></li><li><p>A comparison with the <code>&gt;&gt;</code> operator returns <code>true</code> if the left operand node follows the right operand node in
<a title="document order" class="termref" href="#dt-document-order">document order</a>; otherwise it returns <code>false</code>.</p></li></ol><p>Here are some examples of node comparisons:</p><ul><li><p>The following comparison is true only if the left and right sides each
evaluate to exactly the same single node:</p><div class="exampleInner"><pre>/books/book[isbn = "1558604820"] is /books/book[call = "QA76.9 C3845"]</pre></div></li><li><p>The following comparison is true only if the node identified by the left
side occurs before the node identified by the right side in document order:</p><div class="exampleInner"><pre>/transactions/purchase[parcel = "28-451"] &lt;&lt; /transactions/sale[parcel = "33-870"]</pre></div></li></ul></div></div><div class="div2">
<h3><a id="id-logical-expressions"/>4.11 <a href="#id-logical-expressions" style="text-decoration: none">Logical Expressions</a></h3><p>A <b>logical expression</b> is either an <b>and-expression</b> or
an <b>or-expression</b>. If a logical expression does not raise an error, its value is always one
of the boolean values <code>true</code> or <code>false</code>.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-OrExpr"/><code><a href="#prod-xpath40-OrExpr">OrExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-OrExpr-AndExpr">AndExpr</a>  ("or"  <a href="#doc-xpath40-OrExpr-AndExpr">AndExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-OrExpr-AndExpr"/><code><a href="#prod-xpath40-AndExpr">AndExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-OrExpr-ComparisonExpr">ComparisonExpr</a>  ("and"  <a href="#doc-xpath40-OrExpr-ComparisonExpr">ComparisonExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-OrExpr-ComparisonExpr"/><code><a href="#prod-xpath40-ComparisonExpr">ComparisonExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-OtherwiseExpr">OtherwiseExpr</a>  ((<a href="#prod-xpath40-ValueComp">ValueComp</a>  |  <a href="#prod-xpath40-GeneralComp">GeneralComp</a>  |  <a href="#prod-xpath40-NodeComp">NodeComp</a>)  <a href="#doc-xpath40-OtherwiseExpr">OtherwiseExpr</a>)?</code></td></tr></tbody></table><p>The first step in evaluating a logical expression is to find the  <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a> of each of its operands (see <a href="#id-ebv"><b>2.5.4 Effective Boolean Value</b></a>).</p><p>The value of an and-expression is determined by the effective
boolean values (EBVs) of its operands, as shown in the following table:</p><table style="width:80%" class="medium"><tbody><tr><th>AND:</th><th>EBV<sub>2</sub> =
<code>true</code>
                     </th><th>EBV<sub>2</sub> = <code>false</code>
                     </th><th>error in EBV<sub>2</sub>
                     </th></tr><tr><th>EBV<sub>1</sub> =
<code>true</code>
                     </th><td>
                        <code>true</code>
                     </td><td>
                        <code>false</code>
                     </td><td>error</td></tr><tr><th>EBV<sub>1</sub>
= <code>false</code>
                     </th><td>
                        <code>false</code>
                     </td><td>
                        <code>false</code>
                     </td><td>
                        <span class="xpath">if <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>true</code>, then <code>false</code>; otherwise either <code>false</code> or error.</span>
                     </td></tr><tr><th>error in EBV<sub>1</sub>
                     </th><td>error</td><td>
                        <span class="xpath">if <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>true</code>, then error; otherwise either <code>false</code> or error.</span>
                     </td><td>error</td></tr></tbody></table><p>The value of an
or-expression is determined by the effective boolean values (EBVs) of
its operands, as shown in
the following table:</p><table style="width:80%" class="medium"><tbody><tr><th>OR:</th><th>EBV<sub>2</sub> =
<code>true</code>
                     </th><th>EBV<sub>2</sub> = <code>false</code>
                     </th><th>error in
EBV<sub>2</sub>
                     </th></tr><tr><th>EBV<sub>1</sub> =
<code>true</code>
                     </th><td>
                        <code>true</code>
                     </td><td>
                        <code>true</code>
                     </td><td>
                        <span class="xpath">if <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>true</code>, then <code>true</code>; otherwise either <code>true</code> or error.</span>
                     </td></tr><tr><th>EBV<sub>1</sub> =
<code>false</code>
                     </th><td>
                        <code>true</code>
                     </td><td>
                        <code>false</code>
                     </td><td>error</td></tr><tr><th>error
in EBV<sub>1</sub>
                     </th><td>
                        <span class="xpath">if <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>true</code>, then error; otherwise either <code>true</code> or error.</span>
                     </td><td>error</td><td>error</td></tr></tbody></table><p class="xpath">If <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>true</code>, the order in which the operands of a logical expression are evaluated is effectively prescribed. Specifically, it is defined that when there is no
need to evaluate the second operand in order to determine the result, then
no error can occur as a result of evaluating the second operand.</p><p>
               <span class="xpath">If <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a> is <code>false</code>, the
order in which the operands of a logical expression are evaluated is
<a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>. In this case,</span>
             an or-expression can return <code>true</code> if the first
expression evaluated is true, and it can raise an error if evaluation
of the first expression raises an error. Similarly, an and-expression
can return <code>false</code> if the first expression evaluated is
false, and it can raise an error if evaluation of the first expression
raises an error. As a result of these rules, a logical expression is
not deterministic in the presence of errors, as illustrated in the examples
below.</p><p>Here are some examples of logical expressions:</p><ul><li><p>The following expressions return
<code>true</code>:</p><div class="exampleInner"><pre>1 eq 1 and 2 eq 2</pre></div><div class="exampleInner"><pre>1 eq 1 or 2 eq 3</pre></div></li><li><p>The following
expression may return either <code>false</code> or raise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>
                     <span class="xpath"> (in <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a>, the result must be <code>false</code>)</span>:</p><div class="exampleInner"><pre>1 eq 2 and 3 idiv 0 = 1</pre></div></li><li><p>The
following expression may return either <code>true</code> or raise a
<a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>
                     <span class="xpath"> (in <a title="XPath 1.0 compatibility     mode" class="termref" href="#dt-xpath-compat-mode">XPath 1.0 compatibility mode</a>, the result must be <code>true</code>)</span>:</p><div class="exampleInner"><pre>1 eq 1 or 3 idiv 0 = 1</pre></div></li><li><p>The
following expression must raise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>:</p><div class="exampleInner"><pre>1 eq 1 and 3 idiv 0 = 1</pre></div></li></ul><p>In addition to and- and or-expressions, XPath 4.0 provides a
function named <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-not"><code>fn:not</code></a> that takes a general sequence as
parameter and returns a boolean value.  The <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-not"><code>fn:not</code></a> function
is defined in <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>. The
<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-not"><code>fn:not</code></a> function reduces its parameter to an <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a>. It then returns
<code>true</code> if the effective boolean value of its parameter is
<code>false</code>, and <code>false</code> if the effective boolean
value of its parameter is <code>true</code>. If an error is
encountered in finding the effective boolean value of its operand,
<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-not"><code>fn:not</code></a> raises the same error.</p></div><div class="div2">
<h3><a id="id-flwor-expressions"/>4.12 <a href="#id-flwor-expressions" style="text-decoration: none">
               <span class="xpath">For and Let Expressions</span>
            </a></h3><p class="xpath">XPath provides two closely-related expressions, called For and Let
         expressions, that can be used to bind variables to values. These are described in the
         following sections.</p><div class="div3 xpath">
<h4><a id="id-for-expressions"/>4.12.1 <a href="#id-for-expressions" style="text-decoration: none">For Expressions</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-let-expressions">⬇</a> <a href="#id-general-comparisons">⬆</a></p><ol><li><p>A <code>for member</code> clause is added to FLWOR expressions to allow iteration over
               an array.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/49">49</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/344">344</a> 10 February 2023]</i></p></li><li><p>
               Multiple <code>for</code> and <code>let</code> clauses can be combined
               in an expression without an intervening <code>return</code> keyword.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/22">22</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/28">28</a> 18 December 2020]</i></p></li><li><p>
               A <code>for key/value</code> clause is added to FLWOR expressions to allow iteration over
               maps.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/31">31</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1249">1249</a> 1 June 2024]</i></p></li><li><p>
               A positional variable can be defined in a <code>for</code> expression.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/231">231</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1131">1131</a> 1 April 2024]</i></p></li><li><p>
               The type of a variable used in a <code>for</code> expression can be declared.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/796">796</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1131">1131</a> 1 April 2024]</i></p></li></ol></div><p>XPath provides an iteration facility called a <b>for expression</b>. 
         It can be used to iterate over the items of a sequence, the
            members of an array, or the entries in a map.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr"/><code><a href="#prod-xpath40-ForExpr">ForExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr-ForClause">ForClause</a>
                        <a href="#doc-xpath40-ForExpr-ForLetReturn">ForLetReturn</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ForClause"/><code><a href="#prod-xpath40-ForClause">ForClause</a></code></td><td>::=</td><td><code>"for"  (<a href="#doc-xpath40-ForExpr-ForBinding">ForBinding</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ForBinding"/><code><a href="#prod-xpath40-ForBinding">ForBinding</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr-ForItemBinding">ForItemBinding</a>  |  <a href="#doc-xpath40-ForExpr-ForMemberBinding">ForMemberBinding</a>  |  <a href="#doc-xpath40-ForExpr-ForEntryBinding">ForEntryBinding</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ForItemBinding"/><code><a href="#prod-xpath40-ForItemBinding">ForItemBinding</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr-VarNameAndType">VarNameAndType</a>
                        <a href="#doc-xpath40-ForExpr-PositionalVar">PositionalVar</a>?  "in"  <a href="#doc-xpath40-ForExpr-ExprSingle">ExprSingle</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-VarNameAndType"/><code><a href="#prod-xpath40-VarNameAndType">VarNameAndType</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-ForExpr-EQName">EQName</a>
                        <a href="#doc-xpath40-ForExpr-TypeDeclaration">TypeDeclaration</a>?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-TypeDeclaration"/><code><a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a></code></td><td>::=</td><td><code>"as"  <a href="#doc-xpath40-SequenceType">SequenceType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-PositionalVar"/><code><a href="#prod-xpath40-PositionalVar">PositionalVar</a></code></td><td>::=</td><td><code>"at"  <a href="#doc-xpath40-ForExpr-VarName">VarName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-VarName"/><code><a href="#prod-xpath40-VarName">VarName</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-ForExpr-EQName">EQName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr">ForExpr</a>
                        <br/>|  <a href="#doc-xpath40-ForExpr-LetExpr">LetExpr</a>
                        <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                        <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                        <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ForMemberBinding"/><code><a href="#prod-xpath40-ForMemberBinding">ForMemberBinding</a></code></td><td>::=</td><td><code>"member"  <a href="#doc-xpath40-ForExpr-VarNameAndType">VarNameAndType</a>
                        <a href="#doc-xpath40-ForExpr-PositionalVar">PositionalVar</a>?  "in"  <a href="#doc-xpath40-ForExpr-ExprSingle">ExprSingle</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ForEntryBinding"/><code><a href="#prod-xpath40-ForEntryBinding">ForEntryBinding</a></code></td><td>::=</td><td><code>((<a href="#doc-xpath40-ForExpr-ForEntryKeyBinding">ForEntryKeyBinding</a>
                        <a href="#doc-xpath40-ForExpr-ForEntryValueBinding">ForEntryValueBinding</a>?)  |  <a href="#doc-xpath40-ForExpr-ForEntryValueBinding">ForEntryValueBinding</a>)  <a href="#doc-xpath40-ForExpr-PositionalVar">PositionalVar</a>?  "in"  <a href="#doc-xpath40-ForExpr-ExprSingle">ExprSingle</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ForEntryKeyBinding"/><code><a href="#prod-xpath40-ForEntryKeyBinding">ForEntryKeyBinding</a></code></td><td>::=</td><td><code>"key"  <a href="#doc-xpath40-ForExpr-VarNameAndType">VarNameAndType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ForEntryValueBinding"/><code><a href="#prod-xpath40-ForEntryValueBinding">ForEntryValueBinding</a></code></td><td>::=</td><td><code>"value"  <a href="#doc-xpath40-ForExpr-VarNameAndType">VarNameAndType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-ForLetReturn"/><code><a href="#prod-xpath40-ForLetReturn">ForLetReturn</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr">ForExpr</a>  |  <a href="#doc-xpath40-ForExpr-LetExpr">LetExpr</a>  |  ("return"  <a href="#doc-xpath40-ForExpr-ExprSingle">ExprSingle</a>)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ForExpr-LetExpr"/><code><a href="#prod-xpath40-LetExpr">LetExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-LetClause">LetClause</a>
                        <a href="#doc-xpath40-ForExpr-ForLetReturn">ForLetReturn</a>
                     </code></td></tr></tbody></table><p>A <code>for</code> expression is evaluated as follows:</p><ol class="enumar"><li><p>If the <a href="#prod-xpath40-ForClause">ForClause</a> includes multiple 
                  <a href="#prod-xpath40-ForBinding">ForBinding</a>s with a comma separator, 
                  the <code>for</code>expression is first expanded to a set of nested <code>for</code> 
                  expressions, each of which contains a single <a href="#prod-xpath40-ForBinding">ForBinding</a>.
               More specifically, every separating comma is replaced by <code>for</code>.</p><p>For example, the expression
               <code>for $x in X, $y in Y return $x + $y</code> is expanded to
               <code>for $x in X for $y in Y return $x + $y</code>.</p></li><li><p>Having performed this expansion, 
                  variables bound in the <a href="#prod-xpath40-ForClause">ForClause</a>
                  are called the <b>range variables</b>,
                  the variable named in the <a href="#prod-xpath40-PositionalVar">PositionalVar</a> (if present)
                  is called the <b>position variable</b>,
                  the expression that follows the <code>in</code> keyword is called the <b>binding expression</b>, 
                  and the expression in the <a href="#prod-xpath40-ForLetReturn">ForLetReturn</a> part 
                  (that is, the following <a href="#doc-xpath40-LetExpr">LetExpr</a> or
                  <a href="#doc-xpath40-ForExpr">ForExpr</a>, or the <a href="#doc-xpath40-ExprSingle">ExprSingle</a> 
                  that follows the <code>return</code> keyword) is called the <b>return expression</b>. 
               </p><p>
                        [<a id="dt-binding-collection-xp" title="binding collection">Definition</a>: The 
                  result of evaluating the <b>binding expression</b> in a 
                  <code>for</code> expression is called the 
                  <b>binding collection</b>
                        ].</p></li><li><p>If a <b>position variable</b> is declared, its type is implicitly
                  <code>xs:integer</code>. Its name (as an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>) must be
               different from the name of a <b>range variable</b> declared in the same
                <a href="#prod-xpath40-ForBinding">ForBinding</a>.  
                  [<a href="#ERRXQST0089" title="err:XQST0089">err:XQST0089</a>].
               </p></li><li><p>When a <a href="#prod-xpath40-ForItemBinding">ForItemBinding</a> is used (that is, when none
                  of the keywords <code>member</code>, <code>key</code>, or <code>value</code>
                  is used), the expression iterates over the items in a sequence:
                  </p><ol class="enumla"><li><p>If a <a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a>
                        is present then each item in the <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a>
                        is converted to the specified type by applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.</p></li><li><p>The <b>return expression</b> is evaluated once 
                        for each item in the <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a>, with a dynamic context in which
                        the <b>range variable</b> is bound to that item, and the <b>position variable</b> 
                        (if present) is bound to the one-based position of that item in the 
                        <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a>, as an instance of type 
                        <code>xs:integer</code>.</p></li><li><p>The result of the <code>for</code> expression is the <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a>
                        of the results of the successive evaluations of the <b>return expression</b>.</p></li></ol></li><li><p>When the <code>member</code> keyword is present:
                  </p><ol class="enumla"><li><p>The value of the <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a> must be a single array.
                        Otherwise, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised: [<a href="#ERRXPTY0141" title="err:XPTY0141">err:XPTY0141</a>].
                     </p></li><li><p>If a <a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a>
                        is present then each member of the <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a> array
                        is converted to the specified type by applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.
                        (Recall that this can be any sequence, not necessarily a single item). </p></li><li><p>The result of the single-variable <code>for member</code> expression is obtained
                        by evaluating the <b>return expression</b> once 
                        for each member of that array, with the range variable bound to that member </p></li><li><p>The <b>return expression</b> is evaluated once 
                        for each member of the <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a> array, 
                        with a dynamic context in which
                        the <b>range variable</b> is bound to that member, and the <b>position variable</b> 
                        (if present) is bound to the one-based position of that member in the <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a>.</p></li><li><p>The result of the <code>for</code> expression is the <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a>
                        of the results of the successive evaluations of the <b>return expression</b>.</p><p>Note that the result is a sequence, not an array.</p></li></ol></li><li><p>When the <code>key</code> and/or <code>value</code> keywords are present:
                  </p><ol class="enumla"><li><p>The value of the <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a> must be a single map.
                        Otherwise, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised: 
                        [<a href="#ERRXPTY0141" title="err:XPTY0141">err:XPTY0141</a>]. The map is treated as a sequence of key/value
                        pairs, in <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation   dependent</a> order.
                     </p></li><li><p>If the <code>key</code> keyword is present, then the corresponding
                     variable is bound to the key part of the key/value pair.</p></li><li><p>If the <code>value</code> keyword is present, then the corresponding
                     variable is bound to the value part of the key/value pair.</p></li><li><p>If both the <code>key</code> and <code>value</code> keywords are present, 
                        then the corresponding variables must have distinct names.
                        [<a href="#ERRXQST0089" title="err:XQST0089">err:XQST0089</a>].</p></li><li><p>If a <a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a>
                        is present for the key, then each key is converted to the specified type by 
                        applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.</p></li><li><p>If a <a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a>
                        is present for the value, then each value is converted to the specified type by 
                        applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.</p></li><li><p>The result of the single-variable <code>for key/value</code> expression is obtained
                        by evaluating the <b>return expression</b> once 
                        for each entry in the map, with the range variables bound to that entry as described.</p></li><li><p>The <b>return expression</b> is evaluated once 
                        for each entry of the <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a> map, 
                        with a dynamic context in which
                        the <code>key</code>
                                 <b>range variable</b> (if present)
                        is bound to the key part of that entry, 
                        the <code>value</code>
                                 <b>range variable</b> (if present)
                        is bound to the value part of that entry, and the <b>position variable</b> 
                        (if present) is bound to the one-based position of that entry in the 
                        <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation   dependent</a> ordering of the 
                        <a title="binding collection" class="termref" href="#dt-binding-collection-xp">binding collection</a>.</p></li><li><p>The result of the <code>for</code> expression is the <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a>
                        of the results of the successive evaluations of the <b>return expression</b>.</p><p>Note that the result is a sequence, not a map.</p></li></ol></li></ol><p class="xpath">The following example illustrates the use of a <code>for</code> expression in restructuring an input document. The example is based on the following
input:</p><div class="exampleInner"><pre>&lt;bib&gt;
  &lt;book&gt;
    &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
    &lt;author&gt;Stevens&lt;/author&gt;
    &lt;publisher&gt;Addison-Wesley&lt;/publisher&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;title&gt;Advanced Programming in the Unix Environment&lt;/title&gt;
    &lt;author&gt;Stevens&lt;/author&gt;
    &lt;publisher&gt;Addison-Wesley&lt;/publisher&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;title&gt;Data on the Web&lt;/title&gt;
    &lt;author&gt;Abiteboul&lt;/author&gt;
    &lt;author&gt;Buneman&lt;/author&gt;
    &lt;author&gt;Suciu&lt;/author&gt;
  &lt;/book&gt;
&lt;/bib&gt;</pre></div><p>The following example transforms the input document into a list in
which each author’s name appears only once, followed by a list of
titles of books written by that author. This example assumes that the
context value is the <code>bib</code> element in the input
document.</p><div class="exampleInner"><pre><span class="parse-test">for $a in distinct-values(book/author)
return ((book/author[. = $a])[1], book[author = $a]/title)</span></pre></div><p>The result of the above expression consists of the following
sequence of elements. The titles of books written by a given author
are listed after the name of the author.

The ordering of <code>author</code> elements in the result is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> due to the semantics of the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-distinct-values"><code>fn:distinct-values</code></a> function.</p><div class="exampleInner"><pre>&lt;author&gt;Stevens&lt;/author&gt;
&lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
&lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
&lt;author&gt;Abiteboul&lt;/author&gt;
&lt;title&gt;Data on the Web&lt;/title&gt;
&lt;author&gt;Buneman&lt;/author&gt;
&lt;title&gt;Data on the Web&lt;/title&gt;
&lt;author&gt;Suciu&lt;/author&gt;
&lt;title&gt;Data on the Web&lt;/title&gt;</pre></div><p>The following example illustrates a <code>for</code> expression containing more than one variable:</p><div class="exampleInner"><pre><span class="parse-test">for $i in (10, 20),
    $j in (1, 2)
return ($i + $j)</span></pre></div><p>The result of the above expression, expressed as a sequence of numbers, is as follows: <code>11, 12, 21, 22</code>
               </p><p>The scope of a variable bound in a <code>for</code> expression is the <b>return expression</b>. 
            The scope does not include the expression to which the variable is bound. 
            The following example illustrates how a variable binding may reference another variable 
            bound earlier in the same  <code>for</code> expression:</p><div class="exampleInner"><pre><span class="parse-test">for $x in $z, $y in f($x)
return g($x, $y)</span></pre></div><p>The following example illustrates processing of an array.</p><div class="exampleInner"><pre>for member $map in parse-json('[{ "x": 1, "y": 2 }, { "x": 10, "y": 20 }]') 
return $map ! (?x + ?y)</pre></div><p>The result is the sequence <code>(3, 30)</code>.</p><p>The following example illustrates processing of a map.</p><div class="exampleInner"><pre>for key $key value $value in { "x": 1, "y": 2, "z: 3 }
return `{$key}={$value}`</pre></div><p>The result is the sequence <code>("x=1", "y=2", "z=3")</code> (but
         not necessarily in that order).</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The focus for evaluation of the <code>return</code> clause of a <code>for</code> expression
is the same as the focus for evaluation of the <code>for</code> expression itself. The
following example, which attempts to find the total value of a set of
order-items, is therefore incorrect:

</p><div class="exampleInner"><pre>sum(for $i in order-item return @price * @qty)</pre></div><p>

Instead, the expression must be written to use the variable bound in the <code>for</code> clause:</p><div class="exampleInner"><pre>sum(for $i in order-item return $i!(@price * @qty))</pre></div></div><div class="note"><p class="prefix"><b>Note:</b></p><p>XPath 4.0 allows the format:</p><div class="exampleInner"><pre>for $order in //orders
for $line in $order/order-line
return $line/value</pre></div><p>primarily because it is familiar to XQuery users, some of whom may regard it as more
            readable than the XPath 3.1 alternative which uses a comma in place of the second <code>for</code>.</p></div></div><div class="div3 xpath">
<h4><a id="id-let-expressions"/>4.12.2 <a href="#id-let-expressions" style="text-decoration: none">Let Expressions</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-maps">⬇</a> <a href="#id-for-expressions">⬆</a></p><ol><li><p>
               Multiple <code>for</code> and <code>let</code> clauses can be combined
               in an expression without an intervening <code>return</code> keyword.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/22">22</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/28">28</a> 18 December 2020]</i></p></li><li><p>
               The type of a variable used in a <code>let</code> expression can be declared.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/796">796</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1131">1131</a> 1 April 2024]</i></p></li></ol></div><p>XPath allows a variable to be declared and bound to a value using a <b>let expression</b>.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr"/><code><a href="#prod-xpath40-LetExpr">LetExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-LetExpr-LetClause">LetClause</a>
                        <a href="#doc-xpath40-LetExpr-ForLetReturn">ForLetReturn</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-LetClause"/><code><a href="#prod-xpath40-LetClause">LetClause</a></code></td><td>::=</td><td><code>"let"  (<a href="#doc-xpath40-LetExpr-LetBinding">LetBinding</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-LetBinding"/><code><a href="#prod-xpath40-LetBinding">LetBinding</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-LetExpr-VarNameAndType">VarNameAndType</a>  ":="  <a href="#doc-xpath40-LetExpr-ExprSingle">ExprSingle</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-VarNameAndType"/><code><a href="#prod-xpath40-VarNameAndType">VarNameAndType</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-LetExpr-EQName">EQName</a>
                        <a href="#doc-xpath40-LetExpr-TypeDeclaration">TypeDeclaration</a>?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-TypeDeclaration"/><code><a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a></code></td><td>::=</td><td><code>"as"  <a href="#doc-xpath40-LetExpr-SequenceType">SequenceType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                        <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-LetExpr-ForExpr">ForExpr</a>
                        <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                        <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                        <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                        <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-ForLetReturn"/><code><a href="#prod-xpath40-ForLetReturn">ForLetReturn</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-LetExpr-ForExpr">ForExpr</a>  |  <a href="#doc-xpath40-LetExpr">LetExpr</a>  |  ("return"  <a href="#doc-xpath40-LetExpr-ExprSingle">ExprSingle</a>)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LetExpr-ForExpr"/><code><a href="#prod-xpath40-ForExpr">ForExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ForClause">ForClause</a>
                        <a href="#doc-xpath40-LetExpr-ForLetReturn">ForLetReturn</a>
                     </code></td></tr></tbody></table><p>A let expression is evaluated as follows:</p><ul><li><p>If the let expression uses multiple variables, it is first expanded to a
set of nested let expressions, each of which uses only one variable. 
Specifically, any separating comma is replaced by <code>let</code>.</p><p>For example, the expression 
                  <code>let $x := 4, $y := 3 return $x + $y</code> is expanded to
                   <code>let $x := 4 let $y := 3 return $x + $y</code>.</p></li><li><p>In a single-variable let expression, the variable is called the range
variable. The expression that follows the <code>:=</code> symbol is evaluated, and
                  if a <a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a> is present, its value is converted
                     to the specified type by applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>.
                     The resulting value is called
the binding sequence. The expression in the <a href="#prod-xpath40-ForLetReturn">ForLetReturn</a> part 
                  (that is, the following <a href="#doc-xpath40-LetExpr">LetExpr</a> or
                  <a href="#doc-xpath40-ForExpr">ForExpr</a>, or the <a href="#doc-xpath40-ExprSingle">ExprSingle</a> 
                  that follows the <code>return</code> keyword) is
called the <b>return expression</b>. The result of the let expression is obtained
by evaluating the <b>return expression</b> with a dynamic context in which the range variable is bound to the
binding sequence. </p></li></ul><p>The scope of a variable bound in a let expression is the
            <b>return expression</b>.
The scope does not include the expression to which the variable is bound.
The following example illustrates how a variable binding may reference
another variable bound earlier in the same let expression:</p><div class="exampleInner"><pre>
let $x := doc('a.xml')/*, $y := $x//*
return $y[@value gt $x/@min]</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>It is not required that the variables should have distinct names. It is permitted, 
               for example, to write:</p><div class="exampleInner"><pre>
let $x := "[A fine romance]"
let $x := substring-after($x, "[")
let $x := substring-before($x, "]")
return upper-case($x)</pre></div><p>which returns the result <code>"A FINE ROMANCE"</code>. Note that this expression declares
            three separate variables which happen to have the same name; it should not be read as declaring
            a single variable and binding it successively to different values.</p></div></div></div><div class="div2">
<h3><a id="id-maps-and-arrays"/>4.13 <a href="#id-maps-and-arrays" style="text-decoration: none">Maps and Arrays</a></h3><p>Most modern programming languages have support for collections of
key/value pairs, which may be called maps, dictionaries, associative
arrays, hash tables, keyed lists, or objects (these are not the same
thing as objects in object-oriented systems). In XPath 4.0, we call
these maps. Most modern programming languages also support ordered
lists of values, which may be called arrays, vectors, or sequences.
In XPath 4.0, we have both
sequences and arrays. Unlike sequences, an array is an
item, and can appear as an item in a sequence.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The XPath 4.0 specification focuses on syntax provided for maps
  and arrays, especially constructors and lookup.</p><p>Some of the functionality typically needed for maps and
  arrays is provided by functions defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#maps">Section 18 Processing maps</a><sup><small>FO</small></sup>
               and <a href="https://qt4cg.org/specifications/xpath-functions-40/#arrays">Section 19 Processing arrays</a><sup><small>FO</small></sup>, including functions used to
  read JSON to create maps and arrays, serialize maps and arrays to
  JSON, combine maps to create a new map, remove map entries to create
  a new map, iterate over the keys of a map, convert an array to
  create a sequence, combine arrays to form a new array, and iterate
  over arrays in various ways.</p></div><div class="div3">
<h4><a id="id-maps"/>4.13.1 <a href="#id-maps" style="text-decoration: none">Maps</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-map-constructors">⬇</a> <a href="#id-let-expressions">⬆</a></p><ol><li><p>Ordered maps are introduced.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1651">1651</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1703">1703</a> 14 January 2025]</i></p></li></ol></div><p>
                  [<a id="dt-map" title="map">Definition</a>: A <b>map</b> is a function
  that associates a set of keys with values, resulting in a collection
  of key / value pairs.]
                  [<a id="dt-entry" title="entry">Definition</a>: Each key / value pair in a map
  is called an <b>entry</b>.]
                  [<a id="dt-associated-value" title="associated value">Definition</a>: The value
  associated with a given key is called the <b>associated
  value</b> of the key.]
               </p><p>Maps and their properties are defined in the data model:
               see <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#map-items">Section 7.2 Map Items</a><sup><small>DM</small></sup>. For an overview
            of the functions available for processing maps,
            see <a href="https://qt4cg.org/specifications/xpath-functions-40/#maps">Section 18 Processing maps</a><sup><small>FO</small></sup>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Maps in XPath 4.0 are ordered.
                  The effect of this property is explained 
                  in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#map-items">Section 7.2 Map Items</a><sup><small>DM</small></sup>. 
                  In an ordered map, the order of entries is predictable
                  and depends on the order in which they were added to the map.</p></div><div class="div4">
<h5><a id="id-map-constructors"/>4.13.1.1 <a href="#id-map-constructors" style="text-decoration: none">Map Constructors</a></h5><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-lookup">⬇</a> <a href="#id-maps">⬆</a></p><ol><li><p>
                     In map constructors, the keyword <code>map</code> is now optional, so 
                     <code>map { 0: false(), 1: true() }</code> can now be written <code>{ 0: false(), 1: true() }</code>,
                     provided it is used in a context where this creates no ambiguity.
                  <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1070">1070</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1071">1071</a> 26 March 2024]</i></p></li><li><p>The order of key-value
                     pairs in the map constructor is now retained in the constructed map.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1651">1651</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1703">1703</a> 14 January 2025]</i></p></li></ol></div><p>A map can be created using a <a href="#doc-xpath40-MapConstructor">MapConstructor</a>.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapConstructor"/><code><a href="#prod-xpath40-MapConstructor">MapConstructor</a></code></td><td>::=</td><td><code>"map"?  "{"  (<a href="#doc-xpath40-MapConstructor-MapConstructorEntry">MapConstructorEntry</a> ** ",")  "}"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapConstructor-MapConstructorEntry"/><code><a href="#prod-xpath40-MapConstructorEntry">MapConstructorEntry</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-MapConstructor-MapKeyExpr">MapKeyExpr</a>  ":"  <a href="#doc-xpath40-MapConstructor-MapValueExpr">MapValueExpr</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapConstructor-MapKeyExpr"/><code><a href="#prod-xpath40-MapKeyExpr">MapKeyExpr</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-MapConstructor-ExprSingle">ExprSingle</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapConstructor-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-ForExpr">ForExpr</a>
                           <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                           <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                           <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                           <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-MapConstructor-MapValueExpr"/><code><a href="#prod-xpath40-MapValueExpr">MapValueExpr</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-MapConstructor-ExprSingle">ExprSingle</a>
                        </code></td></tr></tbody></table><div class="note"><p class="prefix"><b>Note:</b></p><p>The keyword <code>map</code> was required in earlier versions
                  of the language; in XPath 4.0 it becomes optional. There may be cases
                  where using the keyword improves readability.</p><p>In order to allow the <code>map</code> keyword to be omitted,
                  an incompatible change has been made to XQuery computed element
                  and attribute constructors: if the name of the constructed element
                  or attribute is a language keyword, it must now be written in quotes,
                  for example <code>element "div" {}</code>.</p><p>Although the grammar allows a <a href="#doc-xpath40-MapConstructor">MapConstructor</a>
                  to appear within an <a href="#doc-xpath40-EnclosedExpr">EnclosedExpr</a> (that is, between
                  curly brackets), this may be confusing to readers, and using the <code>map</code>
                  keyword in such cases may improve clarity. In any event, if the <a href="#doc-xpath40-EnclosedExpr">EnclosedExpr</a>
                  appears in a context such as a <a href="#doc-xpath40-StringTemplate">StringTemplate</a>,
                  the two adjacent left opening braces must at least be separated by whitespace.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>
    In some circumstances, it is necessary to include whitespace before or after the colon
    of a <a href="#prod-xpath40-MapConstructorEntry">MapConstructorEntry</a> to ensure that it is parsed as intended.
    </p><p>
    For instance, consider the expression <code>{a:b}</code>.
    Although it matches the EBNF for MapConstructor
    (with <code>a</code> matching MapKeyExpr and <code>b</code> matching MapValueExpr),
    the "longest possible match" rule requires that <code>a:b</code> be parsed as a QName,
    which results in a syntax error.
    Changing the expression to <code>{a :b}</code> or <code>{a: b}</code>
    will prevent this, resulting in the intended parse.
    </p><p>Similarly, consider these three expressions:</p><div class="exampleInner"><pre>
{a:b:c}
{a:*:c}
{*:b:c}</pre></div><p>
    In each case, the expression matches the EBNF in two different ways,
    but the “longest possible match” rule forces the parse in which
    the MapKeyExpr is <code>a:b</code>, <code>a:*</code>, or <code>*:b</code> (respectively)
    and the MapValueExpr is <code>c</code>.
    To achieve the alternative parse
    (in which the MapKeyExpr is merely <code>a</code> or <code>*</code>),
    insert whitespace before and/or after the first colon.
    </p><p>See <a href="#lexical-structure"><b>A.3 Lexical structure</b></a>.</p></div><p>The value of the expression (whether or not the <code>map</code> keyword is present)
                  is a map whose entries
    correspond to the key-value pairs obtained by
    evaluating the successive <a href="#prod-xpath40-MapKeyExpr">MapKeyExpr</a> and
    <a href="#prod-xpath40-MapValueExpr">MapValueExpr</a> expressions.</p><p>Each <a href="#prod-xpath40-MapKeyExpr">MapKeyExpr</a> expression is evaluated
    and atomized; a 

    
    <a title="type error" class="termref" href="#dt-type-error">type error</a>
                     [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>] occurs if the result is
    not a single atomic item.

      The associated value is the
    result of evaluating the corresponding
    <a href="#prod-xpath40-MapValueExpr">MapValueExpr</a>.

    If the <a href="#prod-xpath40-MapValueExpr">MapValueExpr</a> evaluates to a node, the
    associated value is the node itself, not a new node with the same
    values.

    
                  [<a id="dt-same-key" title="same key">Definition</a>: Two atomic items <code>K1</code> and
    <code>K2</code> have the <b>same key value</b> if
    
    
      <code>fn:atomic-equal(K1, K2)</code> returns <code>true</code>, as specified in <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-atomic-equal">Section 14.2.1 fn:atomic-equal</a><sup><small>FO</small></sup>
                     ]

    If two or more entries have the <a title="same key" class="termref" href="#dt-same-key">same key value</a> then a dynamic
    error is raised [<a href="#ERRXQDY0137" title="err:XQDY0137">err:XQDY0137</a>].
                  
                  The error <span class="verb">may</span> be raised statically if two or more entries can be determined statically
                  to have the <a title="same key" class="termref" href="#dt-same-key">same key value</a>.

    
    </p><p>The <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry order</a><sup><small>DM</small></sup>
                  of the entries in the constructed map retains the order of the 
                  <a href="#prod-xpath40-MapConstructorEntry">MapConstructorEntry</a> entries
                  in the input.</p><div class="example">
<div class="exampleHeader"><a id="d3e27707"/>Example: Constructing a fixed map</div><p>The following expression constructs a map with seven entries:</p><div class="exampleInner"><a id="map-weekdays"/><pre>
{
  "Su" : "Sunday",
  "Mo" : "Monday",
  "Tu" : "Tuesday",
  "We" : "Wednesday",
  "Th" : "Thursday",
  "Fr" : "Friday",
  "Sa" : "Saturday"
}</pre></div></div><p> </p><div class="example">
<div class="exampleHeader"><a id="d3e27712"/>Example: Constructing nested maps</div><p>Maps can nest, and can contain any XDM value. Here is an example of a nested map with values that can be string values, numeric values, or arrays:</p><div class="exampleInner"><a id="map-book"/><pre>

{
  "book": {
    "title": "Data on the Web",
    "year": 2000,
    "author": [
      {
        "last": "Abiteboul",
        "first": "Serge"
      },
      {
        "last": "Buneman",
        "first": "Peter"
      },
      {
        "last": "Suciu",
        "first": "Dan"
      }
    ],
    "publisher": "Morgan Kaufmann Publishers",
    "price": 39.95
  }
}</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The syntax deliberately mimics JSON, but there are a few differences.
               JSON constructs that are not accepted in XPath 4.0 map
               constructors include the keywords <code>true</code>, <code>false</code>,
               and <code>null</code>, and backslash-escaped characters such as <code>"\n"</code>
               in string literals. In an XPath 4.0 map constructor, of course, any literal 
               value can be replaced with an expression.</p></div></div><p>Both the keys and the values in a map constructor can be supplied as expressions
               rather than as constants; the number of entries in the map, however, is fixed.
               There are several functions that can be used to construct maps with a variable
               number of entries:</p><ul><li><p>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-build"><code>map:build</code></a> takes any sequence as input, and for each
                  item in the sequence, it computes a key and a value, by calling user-supplied functions.</p></li><li><p>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-merge"><code>map:merge</code></a> takes a sequence of maps (often but not necessarily
                  <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-single-entry-map">single-entry map</a><sup><small>DM</small></sup>) and merges them into a single map.</p></li><li><p>
                           <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-of-pairs"><code>map:of-pairs</code></a> takes a sequence of 
                     <a href="https://qt4cg.org/specifications/xpath-functions-40/#dt-key-value-pair-map">key-value pair maps</a><sup><small>FO</small></sup>
                     and merges them into a single map.</p></li></ul><p>Any of these functions can be used to build an index of employee
                  elements using the value of the <code>@id</code> attribute as a key:</p><ul><li><p>
                           <code>map:build(//employee, fn { @id })</code>
                        </p></li><li><p>
                           <code>map:merge(//employee ! { @id, . })</code>
                        </p></li><li><p>
                           <code>map:of-pairs(//employee ! { 'key': @id, 'value': . })</code>
                        </p></li></ul><p>All three functions also provide control over:</p><ul><li><p>The way in which duplicate keys are handled, and </p></li><li><p>The ordering of entries in the resulting map.</p></li></ul></div><div class="div4">
<h5><a id="id-map-lookup"/>4.13.1.2 <a href="#id-map-lookup" style="text-decoration: none">Maps as Functions</a></h5><p>Maps are <a title="function item" class="termref" href="#dt-function-item">function items</a>, and 
                  a <a title="dynamic function call" class="termref" href="#dt-dynamic-function-call">dynamic function call</a> can be used to look up
    the value associated with a key in a map.

    
    If <code>$map</code> is a map and <code>$key</code> is a key,
    then <code>$map($key)</code> is equivalent to <code>map:get($map, $key)</code>.
    The semantics of such a function call are formally defined in
    <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-map-get">Section 18.4.9 map:get</a><sup><small>FO</small></sup>.
    
    </p><p>Examples:</p><ul><li><p>
                           <code>$weekdays("Su")</code> returns the <a title="associated value" class="termref" href="#dt-associated-value">associated value</a> of the key <code>Su</code>.</p></li><li><p>
                           <code>$books("Green Eggs and Ham")</code> returns  <a title="associated value" class="termref" href="#dt-associated-value">associated value</a>  of the key <code>Green Eggs and Ham</code>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>XPath 4.0 also provides an alternate syntax for map and
      array lookup that is more terse, supports wildcards, and allows lookup to
      iterate over a sequence of maps or arrays. See <a href="#id-lookup"><b>4.13.3 Lookup Expressions</b></a> for details.</p></div><p>Map lookups can be chained.</p><p>Examples: (These examples assume that <code>$b</code> is bound to the books map from the previous section)</p><ul><li><p>The expression <code>$b("book")("title")</code> returns the string <code>Data on the Web</code>.</p></li><li><p>The expression <code>$b("book")("author")</code> returns the array of authors.</p></li><li><p>The expression <code>$b("book")("author")(1)("last")</code> returns the string <code>Abiteboul</code>.</p><p>(This example combines <a href="#id-array-lookup"><b>4.13.2.2 Arrays as Functions</b></a> with map lookups.)</p></li></ul></div></div><div class="div3">
<h4><a id="id-arrays"/>4.13.2 <a href="#id-arrays" style="text-decoration: none">Arrays</a></h4><div class="div4">
<h5><a id="id-array-constructors"/>4.13.2.1 <a href="#id-array-constructors" style="text-decoration: none">Array Constructors</a></h5><p>
                     [<a id="dt-array" title="array">Definition</a>: An <b>array</b> is
      a <a title="function item" class="termref" href="#dt-function-item">function item</a> that associates a set of positions, represented as
      positive integer keys, with values.] The first position
      in an array is associated with the integer 1.
      [<a id="dt-member" title="member">Definition</a>: The values of an array are called
      its <b>members</b>.]

      In the type hierarchy, array has a distinct type, which is
      derived from function.
      Atomization converts arrays to sequences (see <a title="atomization" class="termref" href="#dt-atomization">Atomization</a>).  
      </p><p>An array is created using an <a href="#doc-xpath40-ArrayConstructor">ArrayConstructor</a>.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayConstructor"/><code><a href="#prod-xpath40-ArrayConstructor">ArrayConstructor</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-ArrayConstructor-SquareArrayConstructor">SquareArrayConstructor</a>  |  <a href="#doc-xpath40-ArrayConstructor-CurlyArrayConstructor">CurlyArrayConstructor</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayConstructor-SquareArrayConstructor"/><code><a href="#prod-xpath40-SquareArrayConstructor">SquareArrayConstructor</a></code></td><td>::=</td><td><code>"["  (<a href="#doc-xpath40-ArrayConstructor-ExprSingle">ExprSingle</a> ** ",")  "]"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayConstructor-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-ForExpr">ForExpr</a>
                           <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                           <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                           <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                           <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayConstructor-CurlyArrayConstructor"/><code><a href="#prod-xpath40-CurlyArrayConstructor">CurlyArrayConstructor</a></code></td><td>::=</td><td><code>"array"  <a href="#doc-xpath40-ArrayConstructor-EnclosedExpr">EnclosedExpr</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrayConstructor-EnclosedExpr"/><code><a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a></code></td><td>::=</td><td><code>"{"  <a href="#prod-xpath40-Expr">Expr</a>?  "}"</code></td></tr></tbody></table><p>
      If a member of an array is a
      node, its node identity is preserved.

      

        In both forms of an <a href="#doc-xpath40-ArrayConstructor">ArrayConstructor</a>, if a member
        expression evaluates to a node, the associated value is the
        node itself, not a new node with the same values.  If the
        member expression evaluates to a map or array, the associated
        value is a new map or array with the same values.

        </p><p>A <a href="#prod-xpath40-SquareArrayConstructor">SquareArrayConstructor</a> 
      consists of a comma-delimited set of argument expressions. It returns an array in which each member contains the value of the corresponding argument expression.</p><p>Examples:</p><ul><li><p>
                           <code>[ 1, 2, 5, 7 ]</code> creates an array with four members: <code>1</code>, <code>2</code>, <code>5</code>, and <code>7</code>.</p></li><li><p>
                           <code>[ (), (27, 17, 0) ]</code> creates an array with two members: <code>()</code> and the sequence <code>(27, 17, 0)</code>.</p></li><li><p>
                           <code>[ $x, local:items(), &lt;tautology&gt;It is what it is.&lt;/tautology&gt; ]</code> creates an array with three members: the value of $x, the result of evaluating the function call, and a tautology element.</p></li></ul><p>A <a href="#prod-xpath40-CurlyArrayConstructor">CurlyArrayConstructor</a>
      can use any  expression to create its members.  It
      evaluates its operand expression to obtain a sequence of items
      and creates an array with these items as members.  Unlike a
      SquareArrayConstructor, a comma in a CurlyArrayConstructor is
      the  <a title="comma operator" class="termref" href="#dt-comma-operator">comma operator</a>, not a delimiter.
      </p><p>Examples:</p><ul><li><p>
                           <code>array { $x }</code> creates an array with one member for each item in the sequence to which $x is bound.</p></li><li><p>
                           <code>array { local:items() }</code> creates an array with one member for each item  in the sequence to which <code>local:items()</code> evaluates.</p></li><li><p>
                           <code>array { 1, 2, 5, 7 }</code> creates an array with four members: <code>1</code>, <code>2</code>, <code>5</code>, and <code>7</code>.</p></li><li><p>
                           <code>array { (), (27, 17, 0) }</code> creates an array with three members: <code>27</code>, <code>17</code>, and <code>0</code>.</p></li><li><p>
                           <code>array { $x, local:items(), &lt;tautology&gt;It is what it is.&lt;/tautology&gt; }</code> creates an array with the following members: the items to which <code>$x</code> is bound, followed by the items to which <code>local:items()</code> evaluates, followed by a tautology element.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>XPath 4.0 does not provide explicit support for sparse arrays. Use integer-valued maps to represent sparse arrays, 
                     for example: <code>{ 27 : -1, 153 : 17 }</code>.</p></div></div><div class="div4">
<h5><a id="id-array-lookup"/>4.13.2.2 <a href="#id-array-lookup" style="text-decoration: none">Arrays as Functions</a></h5><p>Arrays are <a title="function item" class="termref" href="#dt-function-item">function items</a>, 
                  and a <a title="dynamic function call" class="termref" href="#dt-dynamic-function-call">dynamic function call</a> can be used to look up
    the value associated with position in an array.

    

    
    If <code>$array</code> is an array and <code>$index</code> is an integer corresponding to a position in the array,
    then <code>$array($key)</code> is equivalent to <code>array:get($array, $key)</code>.
    The semantics of such a function call are formally defined in
    <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-array-get">Section 19.2.11 array:get</a><sup><small>FO</small></sup>.
    
    </p><p>Examples:</p><ul><li><p>
                           <code>[ 1, 2, 5, 7 ](4)</code> evaluates to <code>7</code>.</p></li><li><p>
                           <code>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ](2)</code> evaluates to <code>[ 4, 5, 6 ]</code>.</p></li><li><p>
                           <code>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ](2)(2)</code> evaluates to <code>5</code>.</p></li><li><p>
                           <code>[ 'a', 123, &lt;name&gt;Robert Johnson&lt;/name&gt; ](3)</code> evaluates to <code>&lt;name&gt;Robert Johnson&lt;/name&gt;</code>.</p></li><li><p>
                           <code>array { (), (27, 17, 0) }(1)</code> evaluates to  <code>27</code>.</p></li><li><p>
                           <code>array { (), (27, 17, 0) }(2)</code> evaluates to  <code>17</code>.</p></li><li><p>
                           <code>array { "licorice", "ginger" }(20)</code> raises a dynamic error [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFOAY0001" title="err:FOAY0001">err:FOAY0001</a>]<sup><small>FO40</small></sup>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>XPath 4.0 also provides an alternate syntax for map and
      array lookup that is more terse, supports wildcards, and allows
      lookup to iterate over a sequence of maps or arrays. See
      <a href="#id-lookup"><b>4.13.3 Lookup Expressions</b></a> for details.</p></div></div></div><div class="div3">
<h4><a id="id-lookup"/>4.13.3 <a href="#id-lookup" style="text-decoration: none">Lookup Expressions</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-filter-maps-and-arrays">⬇</a> <a href="#id-map-constructors">⬆</a></p><ol><li><p>
                  The lookup operator <code>?</code> can now be followed by a string literal, for cases where
                     map keys are strings other than NCNames. It can also be followed by a variable
                     reference.
               </p></li><li><p>
                  A deep lookup operator <code>??</code> is provided for searching
                  trees of maps and arrays.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/297">297</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/837">837</a> 23 November 2023]</i></p></li><li><p>
                  Lookup expressions can now take a modifier (such as <code>keys</code>,
                  <code>values</code>, or <code>pairs</code>) enabling them to return
                  structured results rather than a flattened sequence. 
               <i>  [Issues <a href="https://github.com/qt4cg/qtspecs/issues/960">960</a> <a href="https://github.com/qt4cg/qtspecs/issues/1094">1094</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1125">1125</a> 23 April 2024]</i></p></li><li><p>An inline function may be annotated
               as a <code>%method</code>, giving it access to its containing map.<i>  [Issues <a href="https://github.com/qt4cg/qtspecs/issues/1800">1800</a> <a href="https://github.com/qt4cg/qtspecs/issues/1845">1845</a> PRs <a href="https://github.com/qt4cg/qtspecs/pull/1817">1817</a> <a href="https://github.com/qt4cg/qtspecs/pull/1853">1853</a> 4 March 2025]</i></p></li><li><p>
                  The key specifier can reference an item type or sequence type, to select
                  values of that type only. This is especially useful when processing
                  trees of maps and arrays, as encountered when processing JSON input.
               <i>  [Issues <a href="https://github.com/qt4cg/qtspecs/issues/1456">1456</a> <a href="https://github.com/qt4cg/qtspecs/issues/1866">1866</a> PRs <a href="https://github.com/qt4cg/qtspecs/pull/1864">1864</a> <a href="https://github.com/qt4cg/qtspecs/pull/1877">1877</a>]</i></p></li></ol></div><p>XPath 4.0 provides two lookup operators <code>?</code> and <code>??</code>
               for maps and arrays. These provide a terse syntax
               for accessing the entries in a map or the members of an array.</p><p>The operator "?", known as the shallow lookup operator,
               returns values found immediately in the operand map or array. The operator "??",
            known as the deep lookup operator, also searches nested maps and arrays. The effect of the
            deep lookup operator "??" is explained in <a href="#id-deep-lookup"><b>4.13.3.3 Deep Lookup</b></a>.</p><div class="div4">
<h5><a id="id-postfix-lookup"/>4.13.3.1 <a href="#id-postfix-lookup" style="text-decoration: none">Postfix Lookup Expressions</a></h5><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr"/><code><a href="#prod-xpath40-LookupExpr">LookupExpr</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-LookupExpr-PostfixExpr">PostfixExpr</a>
                           <a href="#doc-xpath40-LookupExpr-Lookup">Lookup</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-PostfixExpr"/><code><a href="#prod-xpath40-PostfixExpr">PostfixExpr</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-PrimaryExpr">PrimaryExpr</a>  |  <a href="#doc-xpath40-FilterExpr">FilterExpr</a>  |  <a href="#doc-xpath40-DynamicFunctionCall">DynamicFunctionCall</a>  |  <a href="#doc-xpath40-LookupExpr">LookupExpr</a>  |  <a href="#doc-xpath40-FilterExprAM">FilterExprAM</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-Lookup"/><code><a href="#prod-xpath40-Lookup">Lookup</a></code></td><td>::=</td><td><code>("?"  |  "??")  (<a href="#doc-xpath40-LookupExpr-Modifier">Modifier</a>  "::")?  <a href="#doc-xpath40-LookupExpr-KeySpecifier">KeySpecifier</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-Modifier"/><code><a href="#prod-xpath40-Modifier">Modifier</a></code></td><td>::=</td><td><code>"pairs"  |  "keys"  |  "values"  |  "items"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-KeySpecifier"/><code><a href="#prod-xpath40-KeySpecifier">KeySpecifier</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#doc-xpath40-LookupExpr-IntegerLiteral">IntegerLiteral</a>  |  <a href="#doc-xpath40-LookupExpr-StringLiteral">StringLiteral</a>  |  <a href="#doc-xpath40-LookupExpr-VarRef">VarRef</a>  |  <a href="#doc-xpath40-LookupExpr-ParenthesizedExpr">ParenthesizedExpr</a>  |  <a href="#doc-xpath40-LookupExpr-LookupWildcard">LookupWildcard</a>  |  <a href="#doc-xpath40-LookupExpr-TypeSpecifier">TypeSpecifier</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-IntegerLiteral"/><code><a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-LookupExpr-Digits">Digits</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-Digits"/><code><a href="#prod-xpath40-Digits">Digits</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-LookupExpr-DecDigit">DecDigit</a>  ((<a href="#doc-xpath40-LookupExpr-DecDigit">DecDigit</a>  |  "_")*  <a href="#doc-xpath40-LookupExpr-DecDigit">DecDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-DecDigit"/><code><a href="#prod-xpath40-DecDigit">DecDigit</a></code></td><td>::=</td><td><code>[0-9]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-VarRef"/><code><a href="#prod-xpath40-VarRef">VarRef</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-EQName">EQName</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-ParenthesizedExpr"/><code><a href="#prod-xpath40-ParenthesizedExpr">ParenthesizedExpr</a></code></td><td>::=</td><td><code>"("  <a href="#prod-xpath40-Expr">Expr</a>?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-LookupWildcard"/><code><a href="#prod-xpath40-LookupWildcard">LookupWildcard</a></code></td><td>::=</td><td><code>"*"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-TypeSpecifier"/><code><a href="#prod-xpath40-TypeSpecifier">TypeSpecifier</a></code></td><td>::=</td><td><code>"~["  <a href="#doc-xpath40-LookupExpr-SequenceType">SequenceType</a>  "]"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-LookupExpr-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                           <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody></table><p>A <code>Lookup</code> has two parts: the <code>KeySpecifier</code>
               determines which entries (in a map) or members (in an array) are
               selected, and the <code>Modifier</code> determines how they are
               delivered in the result. The default modifier is <code>items</code>,
               which delivers the result as a flattened sequence of items.</p><p>To take a simple example, given <code>$A</code> as an array
                  <code>[ ("a", "b"), ("c", "d"), ("e", "f"), 42 ]</code>, some example Lookup expressions
                  are:</p><table style="width:100%"><caption>Example Lookup Expressions on an Array</caption><thead><tr><th>Expression</th><th>Result</th></tr></thead><tbody><tr><td>
                              <code>$A?*</code> (or <code>$A?items::*)</code>
                           </td><td>
                              <div class="exampleInner"><pre>("a", "b", "c", "d", "e", "f", 42)</pre></div>
                           </td></tr><tr><td>
                              <code>$A?pairs::*</code>
                           </td><td>
                              <div class="exampleInner"><pre>({ "key": 1, "value": ("a", "b") }, 
 { "key": 2, "value": ("c", "d") }, 
 { "key": 3, "value": ("e", "f") },
 { "key": 4, "value": 42 })</pre></div>
                           </td></tr><tr><td>
                              <code>$A?values::*</code>
                           </td><td>
                              <div class="exampleInner"><pre>([ "a", "b" ], [ "c", "d" ], [ "e", "f" ], [42])</pre></div>
                           </td></tr><tr><td>
                              <code>$A?keys::*</code>
                           </td><td>
                              <div class="exampleInner"><pre>(1, 2, 3, 4)</pre></div>
                           </td></tr><tr><td>
                              <code>$A?2</code> (or <code>$A?items::2)</code>
                           </td><td>
                              <div class="exampleInner"><pre>("c", "d")</pre></div>
                           </td></tr><tr><td>
                              <code>$A?pairs::2</code>
                           </td><td>
                              <div class="exampleInner"><pre>({ "key": 2, "value":("c", "d") })</pre></div>
                           </td></tr><tr><td>
                              <code>$A?values::2</code>
                           </td><td>
                              <div class="exampleInner"><pre>([ "c", "d" ])</pre></div>
                           </td></tr><tr><td>
                              <code>$A?keys::2</code>
                           </td><td>
                              <div class="exampleInner"><pre>(2)</pre></div>
                           </td></tr><tr><td>
                              <code>$A?(3, 1)</code> (or <code>$A?items::(3, 1))</code>
                           </td><td>
                              <div class="exampleInner"><pre>("e", "f", "a", "b")</pre></div>
                           </td></tr><tr><td>
                              <code>$A?pairs::(3, 1)</code>
                           </td><td>
                              <div class="exampleInner"><pre>({ "key": 3, "value": ("e", "f") }, 
 { "key": 1, "value": ("a", "b") })</pre></div>
                           </td></tr><tr><td>
                              <code>$A?values::(3, 1)</code>
                           </td><td>
                              <div class="exampleInner"><pre>([ "e", "f" ][ "a", "b" ])</pre></div>
                           </td></tr><tr><td>
                              <code>$A?keys::(3, 1)</code>
                           </td><td>
                              <div class="exampleInner"><pre>(3, 1)</pre></div>
                           </td></tr><tr><td>
                              <code>$A?~[xs:integer]</code>
                           </td><td>
                              <div class="exampleInner"><pre>42</pre></div>
                           </td></tr><tr><td>
                              <code>$A?keys::~[xs:integer]</code>
                           </td><td>
                              <div class="exampleInner"><pre>4</pre></div>
                           </td></tr><tr><td>
                              <code>$A?keys::~[xs:string+]</code>
                           </td><td>
                              <div class="exampleInner"><pre>(1, 2, 3)</pre></div>
                           </td></tr></tbody></table><p>Similarly, given <code>$M</code> as a map
                  <code>{ "X": ("a", "b"), "Y": ("c", "d"), "Z": ("e", "f"), "N": 42 }</code>, 
                  some example lookup expressions are as follows.</p><table style="width:100%"><caption>Example Lookup Expressions on a Map</caption><thead><tr><th>Expression</th><th>Result</th></tr></thead><tbody><tr><td>
                              <code>$M?*</code> (or <code>$M?items::*)</code>
                           </td><td>
                              <div class="exampleInner"><pre>("a", "b", "c", "d", "e", "f", 42)</pre></div>
                           </td></tr><tr><td>
                              <code>$M?pairs::*</code>
                           </td><td>
                              <div class="exampleInner"><pre>({ "key": "X", "value": ("a", "b") }, 
 { "key": "Y", "value": ("c", "d") }, 
 { "key": "Z", "value": ("e", "f") },
 { "key": "N", "value": 42 })</pre></div>
                           </td></tr><tr><td>
                              <code>$M?values::*</code>
                           </td><td>
                              <div class="exampleInner"><pre>([ "a", "b" ], [ "c", "d" ], [ "e", "f" ], [42])</pre></div>
                           </td></tr><tr><td>
                              <code>$M?keys::*</code>
                           </td><td>
                              <div class="exampleInner"><pre>("X", "Y", "Z", "N")</pre></div>
                           </td></tr><tr><td>
                              <code>$M?Y</code> (or <code>$M?items::Y)</code>
                           </td><td>
                              <div class="exampleInner"><pre>("c", "d")</pre></div>
                           </td></tr><tr><td>
                              <code>$M?pairs::Y</code>
                           </td><td>
                              <div class="exampleInner"><pre>({ "key": "Y", "value":("c", "d") })</pre></div>
                           </td></tr><tr><td>
                              <code>$M?values::Y</code>
                           </td><td>
                              <div class="exampleInner"><pre>([ "c", "d" ])</pre></div>
                           </td></tr><tr><td>
                              <code>$M?keys::Y</code>
                           </td><td>
                              <div class="exampleInner"><pre>("Y")</pre></div>
                           </td></tr><tr><td>
                              <code>$M?("Z", "X")</code> (or <code>$A?items::("Z", "X"))</code>
                           </td><td>
                              <div class="exampleInner"><pre>("e", "f", "a", "b")</pre></div>
                           </td></tr><tr><td>
                              <code>$M?pairs::("Z", "X")</code>
                           </td><td>
                              <div class="exampleInner"><pre>({ "key": "Z", "value": ("e", "f") }, 
 { "key": "X", "value": ("a", "b") })</pre></div>
                           </td></tr><tr><td>
                              <code>$M?values::("Z", "X")</code>
                           </td><td>
                              <div class="exampleInner"><pre>([ "e", "f" ][ "a", "b" ])</pre></div>
                           </td></tr><tr><td>
                              <code>$M?keys::("Z", "X")</code>
                           </td><td>
                              <div class="exampleInner"><pre>("Z", "X")</pre></div>
                           </td></tr><tr><td>
                              <code>$M?~[xs:integer]</code>
                           </td><td>
                              <div class="exampleInner"><pre>42</pre></div>
                           </td></tr><tr><td>
                              <code>$M?keys::~[xs:integer]</code>
                           </td><td>
                              <div class="exampleInner"><pre>"N"</pre></div>
                           </td></tr><tr><td>
                              <code>$M?keys::~[xs:string+]</code>
                           </td><td>
                              <div class="exampleInner"><pre>("X", "Y", "Z")</pre></div>
                           </td></tr></tbody></table><p>The semantics of a postfix lookup expression <code>
                        <var>E</var>?pairs::<var>KS</var>
                     </code> are defined as follows.
               The results with other modifiers can be derived from this result, as explained below.</p><ol class="enumar"><li><p>
                           <var>E</var> is evaluated to produce a value <code>$V</code>.</p></li><li><p>If <code>$V</code> is not a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> 
                     (that is if <code>count($V) ne 1</code>),
                  then the result (by recursive application of these rules) is the value of
                  <code>for $v in $V return $v?pairs::<var>KS</var>
                           </code>.</p></li><li><p>If <code>$V</code> is a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> array item (that is, 
                     if <code>$V instance of array(*)</code>) then:</p><ol class="enumla"><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> is a <code>ParenthesizedExpr</code>,
                        then it is evaluated to produce a value <code>$K</code>
                        and the result is:</p><div class="exampleInner"><pre>data($K) ! { "key": ., "value": array:get($V, .) }</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The focus for evaluating the key specifier expression is the 
                              same as the focus for the <code>Lookup</code> expression itself.</p></div></li><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> is an <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a> with value <code>$i</code>, 
                        the result is the same as <code>$V?pairs::($i)</code>.</p></li><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> is an <code>NCName</code>
                                 <span>or <code>StringLiteral</code>
                                 </span>, 
                           the expression raises a type error [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <code>KS</code> is a wildcard
                           (<code>*</code>), 
                           the result is the same as <code>$V?pairs::(1 to array:size($V))</code>:</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Note that array items are returned in order.</p></div></li><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> 
                           is a <a href="#prod-xpath40-TypeSpecifier">TypeSpecifier</a>
                                 <code>~[<var>T</var>]</code>,
                           the result is the same as <code>$V?pairs::*[?value instance of <var>T</var>]</code>.
                        </p></li></ol></li><li><p>If <var>$V</var> is a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> 
                     map item (that is, if <code>$V instance of map(*)</code>)
                     then:</p><ol class="enumla"><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> 
                           is a <code>ParenthesizedExpr</code>,
                           then it is evaluated to produce a value <code>$K</code>
                           and the result is:</p><div class="exampleInner"><pre>data($K) ! { "key": ., "value": map:get($V, .) }</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The focus for evaluating the key specifier expression is the 
                                 same as the focus for the <code>Lookup</code> expression itself.</p></div></li><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> is an <code>NCName</code>
                              or a <code>StringLiteral</code>, with value <code>$S</code>, 
                              the result is the same as <code>$V?pairs::($S)</code>
                              </p></li><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> is an <code>IntegerLiteral</code>
                              with value <code>$N</code>, 
                              the result is the same as <code>$V?pairs::($N)</code>.</p></li><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> is a wildcard (<code>*</code>), 
                              the result is the same as <code>$V?pairs::(map:keys($V))</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The order of entries in the result sequence 
                                 reflects the <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry order</a><sup><small>DM</small></sup>
                                 of the map.</p></div></li><li><p>If the <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                                 <var>KS</var> 
                              is a <a href="#prod-xpath40-TypeSpecifier">TypeSpecifier</a>
                                 <code>~<var>T</var>
                                 </code>,
                              the result is the same as <code>$V?pairs::*[?value instance of <var>T</var>]</code>.
                              Note that <var>T</var> is in general a sequence type: if there is an occurrence
                              indicator, then it must be written within parentheses, but if it is a plain
                              item type with no occurrence indicator, then the parentheses may be omitted.
                           </p></li></ol></li><li><p>Otherwise (that is, if <code>$V</code> is neither a map nor an array)
                     a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li></ol><p>For modifiers other than <code>pairs</code>, the resulting key-value pair
               is post-processed as follows:</p><ul><li><p>If the modifier is <code>items</code> (explicitly or by default), 
                     and the key specifier is an <code>NCName</code> or <code>StringLiteral</code>, then the result of
                  <code>$V?items::<var>KS</var>
                           </code> is the result of the expression:</p><div class="exampleInner"><pre>
for $KVP in $V?pairs::<var>KS</var>
let $value := map:get($KVP, 'value')
return if ($value instance of %method function(*))
       then bind-focus($value, $V)
       else $value</pre></div><p>where <code>bind-focus($F, $V)</code> is a function that takes a function
                     item <code>$F</code> and returns a modified function item whose captured context
                     has the focus set to <code>$V</code>: for more detail see <a href="#id-methods"><b>4.5.6.1 Methods</b></a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The effect of this is that if any of the selected values is a 
                     <a title="singleton" class="termref" href="#dt-singleton">singleton</a>
                              <a title="method" class="termref" href="#dt-method">method</a>, the selected
                     function item is modified by binding the context value to
                     the containing map <var>$V</var>. In other cases the result is
                     the <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a> of the value parts.</p></div></li><li><p>If the modifier is <code>items</code> (explicitly or by default),
                     and the key specifier is not an <code>NCName</code> nor a <code>StringLiteral</code>,
                     then the result of <code>$V?items::<var>KS</var>
                           </code> is the result of the expression
                     <code>$V?pairs::KS ! map:get(., "value")</code>. This returns the sequence concatenation
                     of the selected values.</p></li><li><p>If the modifier is <code>values</code>, the result of
                  <code>$V?values::KS</code> is the same as the result of 
                  <code>$V?pairs::KS ! array { map:get(., "value") }</code>.
                  This returns each value as an array.</p></li><li><p>If the modifier is <code>keys</code>, the result of
                  <code>$V?keys::KS</code> is the same as the result of 
                  <code>$V?pairs::KS ! map:get(., "key")</code>.
                  This returns the keys (integer indexes in the case of an array)
                     without the values.</p></li></ul><p>Examples:</p><ul><li><p>
                           <code>{ "first" : "Jenna", "last" : "Scott" }?first</code> evaluates to <code>"Jenna"</code>
                        </p></li><li><p>
                           <code>{ "first name" : "Jenna", "last name" : "Scott" }?"first name"</code> evaluates to <code>"Jenna"</code>
                        </p></li><li><p>
                           <code>[ 4, 5, 6 ]?2</code> evaluates to <code>5</code>.</p></li><li><p>
                           <code>({ "first": "Tom" }, { "first": "Dick" }, { "first": "Harry" })?first</code> evaluates to the sequence <code>("Tom", "Dick", "Harry")</code>.</p></li><li><p>
                           <code>([ 1, 2, 3 ], [ 4, 5, 6 ])?2</code> evaluates to the sequence <code>(2, 5)</code>.</p></li><li><p>
                           <code>([ 1, [ "a", "b" ], [ 4, 5, [ "c", "d"] ])?value::*[. instance of array(xs:string)]</code> evaluates to 
                        the sequence <code>([ "a", "b" ], [ "c", "d" ])</code>.</p></li><li><p>
                           <code>[ "a", "b" ]?3</code> raises a dynamic error [<a href="https://qt4cg.org/specifications/xpath-functions-40/#ERRFOAY0001" title="err:FOAY0001">err:FOAY0001</a>]<sup><small>FO40</small></sup>
                        </p></li></ul></div><div class="div4">
<h5><a id="id-unary-lookup"/>4.13.3.2 <a href="#id-unary-lookup" style="text-decoration: none">Unary Lookup</a></h5><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup"/><code><a href="#prod-xpath40-UnaryLookup">UnaryLookup</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-UnaryLookup-Lookup">Lookup</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-Lookup"/><code><a href="#prod-xpath40-Lookup">Lookup</a></code></td><td>::=</td><td><code>("?"  |  "??")  (<a href="#doc-xpath40-UnaryLookup-Modifier">Modifier</a>  "::")?  <a href="#doc-xpath40-UnaryLookup-KeySpecifier">KeySpecifier</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-Modifier"/><code><a href="#prod-xpath40-Modifier">Modifier</a></code></td><td>::=</td><td><code>"pairs"  |  "keys"  |  "values"  |  "items"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-KeySpecifier"/><code><a href="#prod-xpath40-KeySpecifier">KeySpecifier</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#doc-xpath40-UnaryLookup-IntegerLiteral">IntegerLiteral</a>  |  <a href="#doc-xpath40-UnaryLookup-StringLiteral">StringLiteral</a>  |  <a href="#doc-xpath40-UnaryLookup-VarRef">VarRef</a>  |  <a href="#doc-xpath40-UnaryLookup-ParenthesizedExpr">ParenthesizedExpr</a>  |  <a href="#doc-xpath40-UnaryLookup-LookupWildcard">LookupWildcard</a>  |  <a href="#doc-xpath40-UnaryLookup-TypeSpecifier">TypeSpecifier</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-IntegerLiteral"/><code><a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-UnaryLookup-Digits">Digits</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-Digits"/><code><a href="#prod-xpath40-Digits">Digits</a></code></td><td>::=</td><td><code>
                           <a href="#doc-xpath40-UnaryLookup-DecDigit">DecDigit</a>  ((<a href="#doc-xpath40-UnaryLookup-DecDigit">DecDigit</a>  |  "_")*  <a href="#doc-xpath40-UnaryLookup-DecDigit">DecDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-DecDigit"/><code><a href="#prod-xpath40-DecDigit">DecDigit</a></code></td><td>::=</td><td><code>[0-9]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-StringLiteral"/><code><a href="#prod-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                           <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                        </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                           <a href="#ws-explicit">ws: explicit</a>
                         */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-VarRef"/><code><a href="#prod-xpath40-VarRef">VarRef</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-EQName">EQName</a>
                        </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-ParenthesizedExpr"/><code><a href="#prod-xpath40-ParenthesizedExpr">ParenthesizedExpr</a></code></td><td>::=</td><td><code>"("  <a href="#prod-xpath40-Expr">Expr</a>?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-LookupWildcard"/><code><a href="#prod-xpath40-LookupWildcard">LookupWildcard</a></code></td><td>::=</td><td><code>"*"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-TypeSpecifier"/><code><a href="#prod-xpath40-TypeSpecifier">TypeSpecifier</a></code></td><td>::=</td><td><code>"~["  <a href="#doc-xpath40-UnaryLookup-SequenceType">SequenceType</a>  "]"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-UnaryLookup-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                           <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody></table><p>Unary lookup is most commonly used in predicates (for example, <code>$map[?name = 'Mike']</code>)
                  or with the simple map operator (for example, <code>avg($maps ! (?price - ?discount))</code>).</p><p>The unary lookup expression <code>?modifier::KS</code> is defined to be equivalent to the postfix lookup
                  expression <code>.?modifier::KS</code> which has the context value (<code>.</code>) as the implicit first operand.
                  See <a href="#id-postfix-lookup"><b>4.13.3.1 Postfix Lookup Expressions</b></a> for the postfix lookup operator.</p><p>Examples:</p><ul><li><p>
                           <code>?name</code> is equivalent to <code>.("name")</code>, an appropriate lookup for a map.</p></li><li><p>
                           <code>?2</code> is equivalent to <code>.(2)</code>, an appropriate lookup for an array or an integer-valued map.</p></li><li><p>If the context item is the result of parsing the JSON input:</p><div class="exampleInner"><pre>{
  "name": "John Smith",
  "address": { "street": "18 Acacia Avenue", "postcode": "MK12 2EX" },
  "previous-address": { "street": "12 Seaview Road", "postcode": "EX8 9AA" }
}</pre></div><p>then <code>?*[. instance of record(street, postcode)]?postcode</code>
                        returns <code>("MK12 2EX", "EX8 9AA")</code> (reflecting the 
                        <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry order</a><sup><small>DM</small></sup> of the map).</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Writing <code>?*?postcode</code> would raise a type error, because the result of the initial
                        step <code>?*</code> includes an item (the string <code>"John Smith"</code>) that is neither
                        a map nor an array.</p></div></li><li><p>
                           <code>?"first name"</code> is equivalent to <code>.("first name")</code>
                        </p></li><li><p>
                           <code>?($a)</code>
                           <span>and <code>?$a</code> are</span> 
                        equivalent to <code>for $k in $a return .($k)</code>, allowing keys for an array or map to be passed using a variable.</p></li><li><p>
                           <code>?(2 to 4)</code> is equivalent to <code>for $k in (2, 3, 4) return .($k)</code>, a convenient way to return a range of values from an array.</p></li><li><p>
                           <code>?(3.5)</code> raises a type error if the context value is an array because the parameter must be an integer.</p></li><li><p>
                           <code>([ 1, 2, 3 ], [ 1, 2, 5 ], [ 1, 2 ])[?3 = 5]</code> raises an error because <code>?3</code> on one of the
                        items in the sequence fails.</p></li><li><p>If <code>$m</code> is bound to the weekdays map described in <a href="#id-maps"><b>4.13.1 Maps</b></a>, then <code>$m?*</code> returns the values <code>("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")</code>, in <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> order.</p></li><li><p>
                           <code>[ 1, 2, 5, 7 ]?*</code> evaluates to <code>(1, 2, 5, 7)</code>.</p></li><li><p>
                           <code>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]?*</code> evaluates to <code>([ 1, 2, 3 ], [ 4, 5, 6 ])</code>
                        </p></li><li><p>
                           <code>[ [ 1, 2, 3 ], 4, 5 ]?*[. instance of array(xs:integer)]</code> evaluates to <code>([ 1, 2, 3 ])</code>
                        </p></li><li><p>
                           <code>[ [ 1, 2, 3 ], [ 4, 5, 6 ], 7 ]?*[. instance of array(*)]?2</code> evaluates to <code>(2, 5)</code>
                        </p></li><li><p>
                           <code>[ [ 1, 2, 3 ], 4, 5 ]?*[. instance of xs:integer]</code> 
                        evaluates to <code>(4, 5)</code>.
                     </p></li></ul></div><div class="div4">
<h5><a id="id-deep-lookup"/>4.13.3.3 <a href="#id-deep-lookup" style="text-decoration: none">Deep Lookup</a></h5><p>The deep lookup operator <code>??</code> has both unary and postfix forms. The unary form <code>??modifier::KS</code>
               (where <var>KS</var> is any <code>KeySpecifier</code>) has the same effect as the binary form <code>.??modifier::KS</code>.
               </p><p>The semantics are defined as follows.</p><p>First we define the <b>recursive content</b> of an item as follows:</p><div class="exampleInner"><pre>declare function immediate-content($item as item()) as record(key, value)* {
  if ($item instance of map(*)) {
    map:pairs($item)
  } else if ($item instance of array(*)) {
    for member $m at $p in $item
    return { "key": $p, "value": $m }
  }
};    

declare function recursive-content($item as item()) as record(key, value)* {
  immediate-content($item) ! (., ?items::value ! recursive-content(.))
};</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Explanation: the immediate content of a map is obtained by splitting it
               into a sequence of key-value pairs, each representing one entry. The immediate
               content of an array is obtained by constructing a sequence of key-value pairs,
               one for each array member, where the key is the array index and the
               value is the corresponding member. Each key-value pair is of type
               <code>record(key as xs:anyAtomicType, value as item()*)</code>.
                  The recursive content of an item contains
               the key-value pairs in its immediate content, each followed by the recursive
               content obtained by expanding any maps or arrays in the immediate content.
               </p></div><p>It is then useful to represent the recursive content as a sequence of
               <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-single-entry-map">single-entry-maps</a><sup><small>DM</small></sup>: 
                  so each pair <code>{ "key": $K, "value": $V }</code>
               is converted to the form <code>{ $K: $V }</code>. This can be achieved
               using the expression <code>recursive-content($V) ! { ?key: ?value }</code>.</p><p>In addition we define the function <code>array-or-map</code> as follows:</p><div class="exampleInner"><pre>declare function array-or-map($item as item()) {
  typeswitch ($item) {
    case array(*) | map(*) return $item
    default return error( #err:XPTY0004 )
  }
}</pre></div><p>The result of the expression <code>E??pairs::KS</code>, where <code>E</code> is any expression
               and <code>KS</code> is any <code>KeySpecifier</code>, is then:</p><div class="exampleInner"><pre>(<var>E</var> ! array-or-map(.) -&gt; recursive-content(.) ! { ?key: ?value })
?pairs::<var>KS</var></pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>This is best explained by considering examples.</p><p>Consider the expression <code>let $V := [ { "first": "John", "last": "Smith" }, { "first": "Mary", "last": "Evans" } ]</code>.</p><p>The recursive content of this array is the sequence of six maps:</p><ol class="enumar"><li><p>
                              <code>{ "key": 1, "value": { "first": "John", "last": "Smith" } }</code>
                           </p></li><li><p>
                              <code>{ "key": 2, "value": { "first": "Mary", "last": "Evans" } }</code>
                           </p></li><li><p>
                              <code>{ "key": "first", "value": "John" }</code>
                           </p></li><li><p>
                              <code>{ "key": "last", "value": "Smith" }</code>
                           </p></li><li><p>
                              <code>{ "key": "first", "value": "Mary" }</code>
                           </p></li><li><p>
                              <code>{ "key": "last", "value": "Evans" }</code>
                           </p></li></ol><p>The expression <code>$V??pairs::*</code> returns this sequence.</p><p>With some other <code>KeySpecifier</code>
                        <code>KS</code>, <code>$V??pairs::KS</code> returns
                     selected items from this sequence that match <code>KS</code>.
                     Formally this is achieved by converting the key-value pairs to 
                     <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-single-entry-map">single-entry maps</a><sup><small>DM</small></sup>,
                     applying the <code>KeySpecifier</code> to the sequence of single-entry maps,
                     and then converting the result back into a sequence of key-value pairs.</p><p>For example, given the expression <code>$V??pairs::first</code>, the selection from
                     the converted sequence will include the two <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-single-entry-map">single
                     entry maps</a><sup><small>DM</small></sup>
                        <code>{ "first" : "John" }</code> and <code>{ "first" : "Mary" }</code>,
                  which will be delivered in key-value pair form as 
                     <code>{ "key": "first", "value": "John" }, { "key": "first", "value": "Mary" }</code>.</p></div><p>The effect of using modifiers other than <code>pairs</code> is the same as with
                  shallow lookup expressions:</p><ul><li><p>If the modifier is <code>items</code> (explicitly or by default), the result of
                     <code>$V??items::KS</code> is the same as the result of 
                     <code>$V??pairs::KS ! map:get(., "value")</code>; that is,
                  it is the <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a> of the value parts.</p></li><li><p>If the modifier is <code>values</code>, the result of
                     <code>$V??values::KS</code> is the same as the result of 
                     <code>$V??pairs::KS ! array { map:get(., "value") }</code>.</p></li><li><p>If the modifier is <code>keys</code>, the result of
                     <code>$V??keys::KS</code> is the same as the result of 
                     <code>$V??pairs::KS ! map:get(., "key")</code>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>This means that with the example given earlier:</p><ul><li><p>The expression <code>$V ?? first</code> 
                        returns the sequence <code>"John", "Mary"</code>.</p></li><li><p>The expression <code>$V ?? last</code> 
                        returns the sequence <code>"Smith", "Evans"</code>.</p></li><li><p>The expression <code>$V ?? 1</code> 
                        returns the sequence <code>{ "first": "John", "last": "Smith" }</code>.</p></li><li><p>The expression <code>$V ?? ~[record(first, last)] ! `{ ?first } { ?last }`</code> 
                        returns the sequence <code>"John Smith", "Mary Evans"</code>. This expression
                     selects all values of type <code>record(first, last)</code> at any level in the tree.</p></li></ul></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The effect of evaluating all shallow lookups on maps rather than arrays is that no error arises
                  if an array subscript is out of bounds. In the above example, <code>$value??3</code> would
                  return an empty sequence, it would not raise an error.</p><p>This also affects the way an <code>xs:untypedAtomic</code> key value is handled. 
                     Given the shallow lookup
                     expression <code>$A?$x</code>, if <code>$A</code> is an array and <code>$x</code>
                     (after atomization) is <code>xs:untypedAtomic</code> then the value of <code>$x</code>
                     is converted to an integer (by virtue of the coercion rules applying to a call
                     on <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-get"><code>array:get</code></a>). With a deep lookup expression <code>$A??$x</code>, by
                     contrast, the semantics are defined in terms of a map lookup, in which 
                     <code>xs:untypedAtomic</code> items are always treated as strings.
                  </p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The definition of the <code>recursive-content</code> function is such that items
                  in the top-level value that are not maps or arrays are ignored, whereas items that
                  are not themselves maps or arrays, but which appear in the content of a map or array
                  at the top level, are included. This means that <code>E??X</code> mirrors the
                  behavior of <code>E//X</code>, in that it includes all items that are one-or-more levels
                  deep in the tree.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The result of the deep lookup operator retains order when processing sequences and
                  arrays, but not when processing maps.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>An expression involving multiple deep lookup operators may return duplicates.
                     For example, the result of the expression 
                     <code>[ [ [ "a" ], [ "b" ] ], [ [ "c" ], [ "d" ] ] ] ?? 1 ?? 1</code>
                     is <code>([ "a" ], "a", "b", "a", "c")</code>. This is because the first <code>??</code> operator
                     selects members in position 1 at all three levels, that is it selects the arrays
                     <code>[ [ "a" ], [ "b" ] ]</code>, <code>[ "a" ]</code>, and <code>[ "c" ]</code> as well
                     as each of the four string values. The second <code>??</code> operator 
                  selects members in position 1 within each of these values, which results in the string
                  <code>"a"</code> being selected twice.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>A type error is raised if the value of the left-hand expression includes an item
                  that is neither a map nor an array.</p></div><div class="example">
<div class="exampleHeader"><a id="ex-deep-lookup"/>Example: Examples of Deep Lookup Expressions</div><p>Consider the tree <code>$tree</code> of maps and arrays that results from applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-parse-json"><code>fn:parse-json</code></a>
                  function to the following input:</p><div class="exampleInner"><pre>
{
  "desc"    : "Distances between several cities, in kilometers.",
  "updated" : "2014-02-04T18:50:45",
  "uptodate": true,
  "author"  : null,
  "cities"  : {
    "Brussels": [
      { "to": "London",    "distance": 322 },
      { "to": "Paris",     "distance": 265 },
      { "to": "Amsterdam", "distance": 173 }
    ],
    "London": [
      { "to": "Brussels",  "distance": 322 },
      { "to": "Paris",     "distance": 344 },
      { "to": "Amsterdam", "distance": 358 }
    ],
    "Paris": [
      { "to": "Brussels",  "distance": 265 },
      { "to": "London",    "distance": 344 },
      { "to": "Amsterdam", "distance": 431 }
     ],
    "Amsterdam": [
      { "to": "Brussels",  "distance": 173 },
      { "to": "London",    "distance": 358 },
      { "to": "Paris",     "distance": 431 }
    ]
  }
}</pre></div><p>Given two variables <code>$from</code> and <code>$to</code> containing the
                  names of two cities that are present in this table, the distance between the
                  two cities can be obtained with the expression:</p><div class="exampleInner"><pre>$tree ?? $from ?? ~[record(to, distance)][?to = $to] ? distance</pre></div><p>The names of all pairs of cities whose distance is represented in the data
                  can be obtained with the expression:</p><div class="exampleInner"><pre>$tree ?? $cities
=&gt; map:for-each(fn($key, $val) { $val ?? to ! ($key || "-" || .) })</pre></div></div><p> </p><div class="example">
<div class="exampleHeader"><a id="d3e29959"/>Example: Comparison with JSONPath</div><p>This example provides XPath equivalents to some examples given in the
                  JSONPath specification. [TODO: add a reference].</p><p>The examples query the result of parsing the following JSON value, representing
                     a store whose stock consists of four books and a bicycle:</p><div class="exampleInner"><pre>{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 399
    }
  }
}</pre></div><p>The following table illustrates some queries on this data, expressed
                  both in JSONPath and in XPath 4.0.</p><table style="width:100%" class="small"><caption>JSONPath vs XPath 4.0 Comparison</caption><thead><tr><th>Query</th><th>JSONPath</th><th>XPath 4.0</th></tr></thead><tbody><tr><td>The authors of all books in the store</td><td>
                                 <code>$.store.book[*].author</code>
                              </td><td>
                                 <code>$m?store?book??author</code>
                              </td></tr><tr><td>All authors</td><td>
                                 <code>$..author</code>
                              </td><td>
                                 <code>$m??author</code>
                              </td></tr><tr><td>All things in store (four books and a red bicycle)</td><td>
                                 <code>$.store.*  </code>
                              </td><td>
                                 <code>$m?store?*</code>
                              </td></tr><tr><td>The prices of everything in the store</td><td>
                                 <code>$.store..price</code>
                              </td><td>
                                 <code>$m?store??price</code>
                              </td></tr><tr><td>The third book</td><td>
                                 <code>$..book[2]  </code>
                              </td><td>
                                 <code>$m??book?3</code>
                              </td></tr><tr><td>The third book's author</td><td>
                                 <code>$..book[2].author</code>
                              </td><td>
                                 <code>$m??book?3?author</code>
                              </td></tr><tr><td>The third book's publisher (empty result)</td><td>
                                 <code>$..book[2].publisher</code>
                              </td><td>
                                 <code>$m??book?3?publisher</code>
                              </td></tr><tr><td>The last book (in order)</td><td>
                                 <code>$..book[-1]</code>
                              </td><td>
                                 <code>$m??book =&gt; array:foot()</code>
                              </td></tr><tr><td>The first two books</td><td>
                                 <code>$..book[0,1]</code>
                              </td><td>
                                 <code>$m??book?(1, 2)</code>
                              </td></tr><tr><td>All books with an ISBN</td><td>
                                 <code>$..book[?@.isbn]</code>
                              </td><td>
                                 <code>$m??book[?isbn]</code>
                              </td></tr><tr><td>All books cheaper than 10</td><td>
                                 <code>$..book[?@.price&lt;10]</code>
                              </td><td>
                                 <code>$m??book[?price lt 10]</code>
                              </td></tr><tr><td>All member values and array elements contained in the input value</td><td>
                                 <code>$..*</code>
                              </td><td>
                                 <code>$m??*</code>
                              </td></tr></tbody></table></div></div><div class="div4">
<h5><a id="id-implausible-lookup-expressions"/>4.13.3.4 <a href="#id-implausible-lookup-expressions" style="text-decoration: none">Implausible Lookup Expressions</a></h5><p>Under certain conditions a lookup expression that will never select anything
                  is classified as <a title="implausible" class="termref" href="#dt-implausible">implausible</a>. During the static analysis
                  phase, a processor <span class="verb">may</span> (subject to the rules in
                  <a href="#id-implausible-expressions"><b>2.4.6 Implausible Expressions</b></a>) report a static error
                  when such lookup expressions are encountered: [<a href="#ERRXPTY0145" title="err:XPTY0145">err:XPTY0145</a>].</p><p>More specifically, a shallow unary or postfix lookup is classified as 
                  <a title="implausible" class="termref" href="#dt-implausible">implausible</a> if any of the following conditions applies:</p><ol class="enumar"><li><p>The inferred type of the left-hand operand (or the context value, in the case
                  of a unary expression) is a record type (see <a href="#id-record-test"><b>3.2.8.3 Record Types</b></a>),
                  and the <code>KeySpecifier</code> is an <code>IntegerLiteral</code>.
                  </p></li><li><p>The inferred type of the left-hand operand (or the context value, in the case
                     of a unary expression) is a record type (see <a href="#id-record-test"><b>3.2.8.3 Record Types</b></a>),
                     and the <code>KeySpecifier</code> is an <code>NCName</code> or <code>StringLiteral</code>
                     that cannot validly appear as a field name in the record.
                  </p></li><li><p>The inferred type of the left-hand operand (or the context value, in the case
                     of a unary expression) is a map type,
                     and the inferred type of the <code>KeySpecifier</code>, after coercion, is a type that
                     is disjoint with the key type of the map.
                  </p></li><li><p>The inferred type of the left-hand operand (or the context value, in the case
                     of a unary expression) is an array type,
                     and the <code>KeySpecifier</code> is the <code>IntegerLiteral</code>
                           <code>0</code> (zero).
                  </p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>Other errors, such as using an <code>NCName</code>
                        <code>KeySpecifier</code>
               for an array lookup, are handled under the general provisions for type errors.</p></div><p>Examples of implausible lookup expressions include the following:</p><ul><li><p>
                           <code>parse-uri($uri)?3</code>: the declared result type of <code>parse-uri</code> is a record
                  test, so the selector <code>3</code> will never select anything.</p></li><li><p>
                           <code>in-scope-namespaces($node)(current-date())</code>: the result type of
                  <code>in-scope-namespaces</code> is a map with <code>xs:string</code> keys, so the selector
                  <code>current-date()</code> will never select anything.</p></li><li><p>
                           <code>array:subarray($a, 2, 5)?0</code>: the integer zero cannot select any member
                  of an array, because numbering starts at 1.</p></li></ul></div></div><div class="div3">
<h4><a id="id-filter-maps-and-arrays"/>4.13.4 <a href="#id-filter-maps-and-arrays" style="text-decoration: none">Filter Expressions for Maps and Arrays</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-conditionals">⬇</a> <a href="#id-lookup">⬆</a></p><ol><li><p>
                  Filter expressions for maps and arrays are introduced.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1159">1159</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1163">1163</a> 20 April 2024]</i></p></li><li><p>
                  Predicates in filter expressions for maps and arrays can now be numeric.
               <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1207">1207</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1217">1217</a> 15 May 2024]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExprAM"/><code><a href="#prod-xpath40-FilterExprAM">FilterExprAM</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-FilterExprAM-PostfixExpr">PostfixExpr</a>  "?["  <a href="#doc-xpath40-FilterExprAM-Expr">Expr</a>  "]"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExprAM-PostfixExpr"/><code><a href="#prod-xpath40-PostfixExpr">PostfixExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-PrimaryExpr">PrimaryExpr</a>  |  <a href="#doc-xpath40-FilterExpr">FilterExpr</a>  |  <a href="#doc-xpath40-DynamicFunctionCall">DynamicFunctionCall</a>  |  <a href="#doc-xpath40-LookupExpr">LookupExpr</a>  |  <a href="#doc-xpath40-FilterExprAM">FilterExprAM</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExprAM-Expr"/><code><a href="#prod-xpath40-Expr">Expr</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-FilterExprAM-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-FilterExprAM-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ForExpr">ForExpr</a>
                        <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                        <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                        <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                        <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                     </code></td></tr></tbody></table><p>Maps and arrays can be filtered using the construct <code>
                     <var>INPUT</var>?[<var>FILTER</var>]</code>.
            For example, <code>$array?[count(.)=1]</code> filters an array to retain only those members that 
            are single items.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The character-pair <code>?[</code> forms a single token; no intervening whitespace
            or comment is allowed.</p></div><p>The required type of the left-hand operand <code>
                     <var>INPUT</var>
                  </code> is
               <code>(map(*)|array(*))?</code>: that is, it must be either an empty sequence, a single
            map, or a single array [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>]. If it is an empty sequence, 
               the result of the expression is an empty sequence.</p><p>If the value of <code>
                     <var>INPUT</var>
                  </code> is an array, then the 
               <code>
                     <var>FILTER</var>
                  </code> expression is evaluated
            for each member of the array, with that member as the context value, with its position in the
            array as the context position, and with the size of the array as the context size. The result
            of the expression is an array containing those members of the input array for which
               the <a title="predicate truth value" class="termref" href="#dt-predicate-truth-value">predicate truth value</a> of the 
               <code>
                     <var>FILTER</var>
                  </code> expression is true. The order
            of retained members is preserved.</p><p>For example, the following expression:</p><div class="exampleInner"><pre>let $array := [ (), 1, (2, 3), (4, 5, 6) ]
return $array?[count(.) ge 2]</pre></div><p>returns:</p><div class="exampleInner"><pre>[ (2, 3), (4, 5, 6) ]</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Numeric predicates are handled in the same way as with filter expressions for 
               sequences. However, the result is always an array, even if only one member
            is selected. For example, given the <code>$array</code> shown above, the result
            of <code>$array?[3]</code> is the <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-single-member-array">single-member array</a><sup><small>DM</small></sup>
                     <code>[ (2, 3) ]</code>.
            Contrast this with <code>$array?3</code> which delivers the sequence <code>2, 3</code>.</p></div><p>If the value of <code>
                     <var>INPUT</var>
                  </code> is a map, then the 
               <code>
                     <var>FILTER</var>
                  </code> expression is evaluated
               for each entry in the map, with the context value set to an item of type
               <code>record(key as xs:anyAtomicType, value as item()*)</code>, in which the <code>key</code>
               and <code>value</code> fields represent the key and value of the map entry. 
               The context position is the position of the entry in the map 
               (in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry order</a><sup><small>DM</small></sup>),
               and the context size is the number of entries in the map. The result
               of the expression is a map containing those entries of the input map for which
               the <a title="predicate truth value" class="termref" href="#dt-predicate-truth-value">predicate truth value</a> of the <code>
                     <var>FILTER</var>
                  </code> expression is true.
               The relative order of entries in the result retains the relative order of entries in the input.
            </p><p>For example, the following expression:</p><div class="exampleInner"><pre>let map := { 1: "alpha", 2: "beta", 3: "gamma" }
return $map?[?key ge 2]</pre></div><p>returns:</p><div class="exampleInner"><pre>{ 2: "beta", 3: "gamma" }</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>A filter expression such as <code>$map?[last()-1, last()]</code>
                  might be used to return the last two entries of a map in
                  <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-entry-order">entry order</a><sup><small>DM</small></sup>.</p></div></div><div class="div3">
<h4><a id="id-pinned-maps-and-arrays"/>4.13.5 <a href="#id-pinned-maps-and-arrays" style="text-decoration: none">Pinned Maps and Arrays</a></h4><p>Unlike navigation within node trees derived from XML, navigation within a tree of maps and
               arrays derived from JSON is normally “downwards only”: there is no equivalent of the parent
            or ancestor axis. This means, for example, that having selected leav nodes in the tree
            with an expression such as <code>??name</code>, there is no way of navigating from 
               the items in the result to any related items. 
               Pinned maps and arrays provide a solution to this problem; if a map or array
            is pinned (by calling the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-pin"><code>fn:pin</code></a> function), then values found by navigating
            within the map or array are <b>labeled</b>, which provides supplementary information
            about their location within the containing tree structure.</p><p>For further information about pinned and labeled values 
               see <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#id-LabeledItems">Section 3.3 Labeled Items</a><sup><small>DM</small></sup>. </p><p>More specifically, if a map <code>$M</code> or an array <code>$A</code> is pinned,
            then any value returned by <code>map:get($M, $key)</code> or <code>array:get($A, $index)</code>
            will be a sequence of labeled items. The label can be obtained by calling the function
            <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-label"><code>fn:label</code></a>, and the result will be a map having the following properties:</p><ul><li><p>
                        <code>pinned</code>: set to <code>true</code>. This means that any
               further selections from this value (if it is itself a map or array) will
               also deliver labeled items.</p></li><li><p>
                        <code>parent</code>: the containing map (<code>$M</code>) or array
               (<code>$A</code>).</p></li><li><p>
                        <code>key</code>: the key (<code>$key</code>) or index (<code>$index</code>)
               that was used to select the item.</p></li><li><p>
                        <code>position</code>: in the general case the value returned by
               <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-get"><code>map:get</code></a> or <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-get"><code>array:get</code></a> is a sequence, and each item in the
                  sequence is labeled with its 1-based position in that sequence.</p></li><li><p>
                        <code>ancestors</code>: a zero-arity function that delivers the item’s parent (its
               containing map or array), that item’s parent, and so on, recursively, up to
               the map or array that was the root of the selection. The sequence is in upwards
               navigation order (the immediate parent comes first).</p></li><li><p>
                        <code>path</code>: a zero-arity function that delivers the sequence of
               keys (in the case of maps) or integer indexes (in the case of arrays) by which the
               item was reached. The sequence is in downwards navigation order (the immediate
               key or index of the item comes last).</p></li></ul><p>The formal model for the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-pin"><code>fn:pin</code></a> is that it returns a deep copy of the 
               supplied map or array in which all items in the recursive content have been labeled.
            This is a useful model because it avoids the need to specify the effect of each individual
            function and operator on the structure. For example, the rule has the consequence that the result of
            <code>pin([ 11, 12, 13, 14 ]) =&gt; array:remove(3) =&gt; array:for-each(fn { label(.)?key })</code> is
            <code>[ 1, 2, 4 ]</code>. In a practical implementation, however, it is likely that labels
            will be attached to items lazily, at the time they are retrieved. Such an implementation will need
            to recognize pinned maps and arrays and treat them specially when operations such as
            <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-get"><code>array:get</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-remove"><code>array:remove</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-for-each"><code>array:for-each</code></a>,
            <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-array-subarray"><code>array:subarray</code></a>, and their map counterparts, are evaluated.</p><p>Because maps and arrays selected from a pinned map or array are themselves pinned,
            deep lookup operations (whether conducted using the deep lookup operator <code>??</code>,
            or the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-map-find"><code>map:find</code></a> function, or by user-written recursive code) will deliver
            a labeled value whose <code>parent</code> or <code>ancestor</code> properties can 
               be used to navigate back up through the tree.</p><p>For example, given the example map shown in <a href="#id-map-constructors"><b>4.13.1.1 Map Constructors</b></a>,
            the expression <code>$map??last[. = "Suciu"]</code> selects the map entry with key 
            <code>"last"</code> and value <code>"Suciu"</code>, but by itself gives no information
            about where this entry was found. By first pinning the map, this extra information
            can be made available through the label on the result. For example you can select
            all co-authors of "Suciu" by writing:</p><div class="exampleInner"><pre>pin($map)??last[. = "Suciu"] =&gt; label()?ancestors()?author??last</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>When an entry in a map, or a member of an array, has the empty sequence
            as its value, the value contains no items and is therefore unchanged in the pinned
            version of the containing structure. In addition, the lookup operators <code>?</code>
            and <code>??</code> flatten their result to a single sequence, so any empty values
            are effectively discarded from the result. For this reason, pinned arrays and maps
            work best when all values in arrays and maps are <a title="singleton" class="termref" href="#dt-singleton">singleton</a> 
               items. An option is therefore provided
            on the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-parse-json"><code>fn:parse-json</code></a> and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-json-doc"><code>fn:json-doc</code></a> functions to change
            the representation of JSON <code>null</code> values (whose default is an empty
            sequence, <code>()</code>) to a user-supplied value.</p></div><table class="ednote" caption="Editorial note"><tr><td style="text-align: left; vertical-align:top; width: 50%;"><b>Editorial note</b></td><td style="text-align: right; vertical-align:top; width: 50%;"> </td></tr><tr style="text-align: left; vertical-align: top;"><td colspan="2">That note is anticipating a proposal in a separate PR.</td></tr></table></div></div><div class="div2">
<h3><a id="id-conditionals"/>4.14 <a href="#id-conditionals" style="text-decoration: none">Conditional Expressions</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-otherwise">⬇</a> <a href="#id-filter-maps-and-arrays">⬆</a></p><ol><li><p>
               Alternative syntax for conditional expressions is available: <code>if (condition) { X }</code>.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/234">234</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/284">284</a> 23 January 2023]</i></p></li></ol></div><p>XPath 4.0 allows conditional expressions to be written in several different ways.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-IfExpr"/><code><a href="#prod-xpath40-IfExpr">IfExpr</a></code></td><td>::=</td><td><code>"if"  "("  <a href="#doc-xpath40-IfExpr-Expr">Expr</a>  ")"  (<a href="#doc-xpath40-IfExpr-UnbracedActions">UnbracedActions</a>  |  <a href="#doc-xpath40-IfExpr-BracedAction">BracedAction</a>)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-IfExpr-Expr"/><code><a href="#prod-xpath40-Expr">Expr</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-IfExpr-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-IfExpr-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ForExpr">ForExpr</a>
                     <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                     <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                     <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                     <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-IfExpr-UnbracedActions"/><code><a href="#prod-xpath40-UnbracedActions">UnbracedActions</a></code></td><td>::=</td><td><code>"then"  <a href="#doc-xpath40-IfExpr-ExprSingle">ExprSingle</a>  "else"  <a href="#doc-xpath40-IfExpr-ExprSingle">ExprSingle</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-IfExpr-BracedAction"/><code><a href="#prod-xpath40-BracedAction">BracedAction</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-IfExpr-EnclosedExpr">EnclosedExpr</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-IfExpr-EnclosedExpr"/><code><a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a></code></td><td>::=</td><td><code>"{"  <a href="#doc-xpath40-IfExpr-Expr">Expr</a>?  "}"</code></td></tr></tbody></table><p>The braced expression <code>if (<var>C</var>) then {<var>T</var>}</code> is equivalent to the
         unbraced expression <code>if (<var>C</var>) then <var>T</var> else ()</code>.</p><p>The value <var>V</var> of a conditional expression in the form <code>if (<var>C</var>) then <var>T</var>
         else <var>E</var>
               </code> is obtained as follows:</p><ol class="enumar"><li><p>Let <var>B</var> be the <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a> of the test expression
                       <var>C</var>, as defined in <a href="#id-ebv"><b>2.5.4 Effective Boolean Value</b></a>.</p></li><li><p>If <var>B</var> is true, <var>V</var> is the
                    result of evaluating <var>T</var>.</p></li><li><p>Otherwise, <var>V</var> is the
                    result of evaluating <var>E</var>.</p></li></ol><p>Conditional expressions have a special rule for propagating <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic errors</a>: <span>expressions whose value is not needed for
                  computing the result are
            <a title="guarded" class="termref" href="#dt-guarded">guarded</a>, as described in <a href="#id-guarded-expressions"><b>2.4.5 Guarded Expressions</b></a>, to prevent
                  spurious dynamic errors.</span>
            </p><p>Here are some examples of conditional expressions:</p><ul><li><p>In this example, the test expression is a comparison expression:</p><div class="exampleInner"><pre>if ($widget1/unit-cost &lt; $widget2/unit-cost)
then $widget1
else $widget2</pre></div></li><li><p>In this example, the test expression tests for the existence of an attribute
named <code>discounted</code>, independently of its value:</p><div class="exampleInner"><pre>if ($part/@discounted)
then $part/wholesale
else $part/retail</pre></div></li><li><p>The following example returns the attribute node <code>@discount</code> provided the value of <code>@price</code>
                  is greater than 100; otherwise it returns the empty sequence:</p><div class="exampleInner"><pre>if (@price gt 100) { @discount }</pre></div></li><li><p>The following example tests a number of conditions:</p><div class="exampleInner"><pre>if (@code = 1) then
  "food"
else if (@code = 2) then
  "fashion"
else if (@code = 3) then
  "household"
else 
  "general"</pre></div></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>The “dangling else ambiguity” found in many other languages cannot arise:</p><ul><li><p>In the unbraced format, both the <code>then</code> and <code>else</code> clauses
                  are mandatory.</p></li><li><p>In the braced format, the expression terminates unambiguously with the closing
                  brace.</p></li></ul></div></div><div class="div2">
<h3><a id="id-otherwise"/>4.15 <a href="#id-otherwise" style="text-decoration: none">Otherwise Expressions</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-quantified-expressions">⬇</a> <a href="#id-conditionals">⬆</a></p><ol><li><p>
               An <code>otherwise</code> operator is introduced: <code>A otherwise B</code> returns the
               value of <code>A</code>, unless it is an empty sequence, in which case it returns the value of <code>B</code>.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1024">1024</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1031">1031</a> 27 February 2024]</i></p></li></ol></div><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-OtherwiseExpr"/><code><a href="#prod-xpath40-OtherwiseExpr">OtherwiseExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-OtherwiseExpr-StringConcatExpr">StringConcatExpr</a>  ("otherwise"  <a href="#doc-xpath40-OtherwiseExpr-StringConcatExpr">StringConcatExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-OtherwiseExpr-StringConcatExpr"/><code><a href="#prod-xpath40-StringConcatExpr">StringConcatExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-RangeExpr">RangeExpr</a>  ("||"  <a href="#doc-xpath40-RangeExpr">RangeExpr</a>)*</code></td></tr></tbody></table><p>The <code>otherwise</code> expression returns the value of its first operand, unless this is an empty
         sequence, in which case it returns the value of its second operand.</p><p>For example, <code>@price - (@discount otherwise 0)</code> returns the value of <code>@price - @discount</code>,
         if the attribute <code>@discount</code> exists, or the value of <code>@price</code> if the <code>@discount</code>
            attribute is absent.</p><p>To prevent spurious errors, the right hand operand is <a title="guarded" class="termref" href="#dt-guarded">guarded</a>: it cannot throw any
            dynamic error unless the left-hand operand returns an empty sequence.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The operator is associative (even under error conditions): <code>A otherwise (B otherwise C)</code> returns
         the same result as <code>(A otherwise B) otherwise C</code>.</p><p>The <code>otherwise</code> operator binds more tightly than comparison operators such as
            <code>=</code>, but less tightly than string concatenation (<code>||</code>) or arithemetic
            operators. The expression <code>$a = @x otherwise @y + 1</code> parses as 
               <code>$a = (@x otherwise (@y + 1))</code>.</p></div></div><div class="div2">
<h3><a id="id-quantified-expressions"/>4.16 <a href="#id-quantified-expressions" style="text-decoration: none">Quantified Expressions</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-pipeline-operator">⬇</a> <a href="#id-otherwise">⬆</a></p><ol><li><p>
               If a type declaration is present, the supplied values in the input sequence are now
               coerced to the required type. Type declarations are now permitted in XPath as well as XQuery.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1316">1316</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1384">1384</a> 13 August 2024]</i></p></li></ol></div><p>Quantified expressions support existential and universal quantification. The
value of a quantified expression is always <code>true</code> or <code>false</code>.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QuantifiedExpr"/><code><a href="#prod-xpath40-QuantifiedExpr">QuantifiedExpr</a></code></td><td>::=</td><td><code>("some"  |  "every")  (<a href="#doc-xpath40-QuantifiedExpr-QuantifierBinding">QuantifierBinding</a> ++ ",")  "satisfies"  <a href="#doc-xpath40-QuantifiedExpr-ExprSingle">ExprSingle</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QuantifiedExpr-QuantifierBinding"/><code><a href="#prod-xpath40-QuantifierBinding">QuantifierBinding</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-QuantifiedExpr-VarNameAndType">VarNameAndType</a>  "in"  <a href="#doc-xpath40-QuantifiedExpr-ExprSingle">ExprSingle</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QuantifiedExpr-VarNameAndType"/><code><a href="#prod-xpath40-VarNameAndType">VarNameAndType</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-QuantifiedExpr-EQName">EQName</a>
                     <a href="#doc-xpath40-QuantifiedExpr-TypeDeclaration">TypeDeclaration</a>?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QuantifiedExpr-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QuantifiedExpr-TypeDeclaration"/><code><a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a></code></td><td>::=</td><td><code>"as"  <a href="#doc-xpath40-QuantifiedExpr-SequenceType">SequenceType</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QuantifiedExpr-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                     <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-QuantifiedExpr-ExprSingle"/><code><a href="#prod-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ForExpr">ForExpr</a>
                     <br/>|  <a href="#doc-xpath40-LetExpr">LetExpr</a>
                     <br/>|  <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                     <br/>|  <a href="#doc-xpath40-IfExpr">IfExpr</a>
                     <br/>|  <a href="#doc-xpath40-OrExpr">OrExpr</a>
                  </code></td></tr></tbody></table><p>A <b>quantified expression</b> begins with
a <b>quantifier</b>, which is the keyword <code>some</code> or <code>every</code>, 
followed by one or more in-clauses that are used to bind variables,
followed by the keyword <code>satisfies</code> and a test expression. Each in-clause associates a variable with an
expression that returns a sequence of items, called the binding sequence for that variable. 
The value of the quantified expression is defined by the following rules:</p><ol class="enumar"><li><p>If the <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a> contains
               more than one <a href="#prod-xpath40-QuantifierBinding">QuantifierBinding</a>, then it is equivalent
            to the expression obtained by replacing each comma with <code>satisfies some</code> or <code>satisfies every</code>
            respectively. For example, the expression <code>some $x in X, $y in Y satisfies $x = $y</code>
               is equivalent to <code>some $x in X satisfies some $y in Y satisfies $x = $y</code>,
               while the expression <code>every $x in X, $y in Y satisfies $x lt $y</code> is equivalent to
               <code>every $x in X satisfies every $y in Y satisfies $x lt $y</code>
                  </p></li><li><p>If the quantifier is <code>some</code>, the <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a> returns <code>true</code> 
                  if at least one evaluation of the test expression has the <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a>
                     <code>true</code>; otherwise it returns <code>false</code>. In consequence, if the binding sequence is empty, 
                  the result of the <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a> is <code>false</code>.</p></li><li><p>If the quantifier is <code>every</code>, the <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a> returns <code>true</code> 
                  if every evaluation of the test expression has the <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a>
                     <code>true</code>; otherwise it returns <code>false</code>. In consequence, if the binding sequence is empty, 
                  the result of the <a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a> is <code>true</code>.</p></li></ol><p>The scope of a variable bound in a quantified expression comprises all
subexpressions of the quantified expression that appear after the variable binding. The scope does not include the expression to which the variable is bound.
</p><p>Each variable binding may be accompanied by a <b>type declaration</b>, 
            which consists of the keyword <code>as</code> followed by the static type of 
            the variable, declared using the syntax in  <a href="#id-sequencetype-syntax"><b>3.1 Sequence Types</b></a>. 
            The type declaration defines a required type for the
            value. At run-time, the supplied value for the variable is converted to the required type
            by applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>. If conversion is not possible,                     
            a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p><p>The order in which test expressions are evaluated 
            for the various items in the binding sequence is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>. If the quantifier
               is <code>some</code>, an implementation may
               return <code>true</code> as soon as it finds one item for which the test expression has
               an <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a> of <code>true</code>, and it may raise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> as soon as it finds one item for
            which the test expression raises an error. Similarly, if the quantifier is <code>every</code>, an 
            implementation may return <code>false</code> as soon as it finds one item for which the test expression has
            an <a title="effective boolean value" class="termref" href="#dt-ebv">effective boolean value</a> of <code>false</code>, and it may raise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> as soon as it finds one item for
            which the test expression raises an error. As a result of these rules, the
            value of a quantified expression is not deterministic in the presence of
            errors, as illustrated in the examples below.</p><p>Here are some examples of quantified expressions:</p><ul><li><p>This expression is <code>true</code> if every <code>part</code> element has a <code>discounted</code> attribute (regardless of the values of these attributes):</p><div class="exampleInner"><pre>every $part in /parts/part satisfies $part/@discounted</pre></div></li><li><p>This expression is <code>true</code> if at least
one <code>employee</code> element satisfies the given comparison expression:</p><div class="exampleInner"><pre>some $emp in /emps/employee satisfies $emp/bonus &gt; 0.25 * $emp/salary</pre></div></li><li><p>This expression is <code>true</code> if every
                  <code>employee</code> element has at least one <code>salary</code> child with the attribute <code>current="true"</code>:</p><div class="exampleInner"><pre>every $emp in /emps/employee satisfies (
  some $sal in $emp/salary satisfies $sal/@current = 'true'
)</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>Like many quantified expressions, this can be simplified. This example can be written
                  <code>every $emp in /emps/employee satisfies $emp/salary[@current = 'true']</code>, or even
                  more concisely as <code>empty(/emps/employee[not(salary/@current = 'true')]</code>.</p><p>Another alternative in XPath 4.0 is to use the higher-order functions <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-some"><code>fn:some</code></a> and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-every"><code>fn:every</code></a>.
               This example can be written <code>every(/emps/employee, fn { salary/@current = 'true' })</code>
                     </p></div></li><li><p>In the following examples, each quantified expression evaluates its test
expression over nine pairs of items, formed from the Cartesian
product of the sequences <code>(1, 2, 3)</code> and <code>(2, 3, 4)</code>. 
                  The expression beginning with <code>some</code> evaluates to <code>true</code>, 
                  and the expression beginning with <code>every</code> evaluates to <code>false</code>.</p><div class="exampleInner"><pre>some $x in (1, 2, 3), $y in (2, 3, 4) satisfies $x + $y = 4</pre></div><div class="exampleInner"><pre>every $x in (1, 2, 3), $y in (2, 3, 4) satisfies $x + $y = 4</pre></div></li><li><p>This quantified expression may either return  <code>true</code> or raise a <a title="type error" class="termref" href="#dt-type-error">type error</a>, since its test expression returns <code>true</code> for one item
and raises a <a title="type error" class="termref" href="#dt-type-error">type error</a> for another:</p><div class="exampleInner"><pre>some $x in (1, 2, "cat") satisfies $x * 2 = 4</pre></div></li><li><p>This quantified expression may either return <code>false</code> or raise a <a title="type error" class="termref" href="#dt-type-error">type error</a>, since its test expression returns <code>false</code> for one item and raises a <a title="type error" class="termref" href="#dt-type-error">type error</a> for another:</p><div class="exampleInner"><pre>every $x in (1, 2, "cat") satisfies $x * 2 = 4</pre></div></li><li><p>This quantified expression returns <code>true</code>, because the binding sequence
                  is empty, despite the fact that the condition can never be satisfied:</p><div class="exampleInner"><pre>every $x in () satisfies ($x lt 0 and $x gt 0)</pre></div></li><li><p>This quantified expression is <a title="implausible" class="termref" href="#dt-implausible">implausible</a> because
                  it will always fail with a type error except in the case where <code>$input</code>
                  is an empty sequence. If <code>$input</code> contains one or more <code>xs:date</code>
                  values, a processor <span class="verb">must</span> raise a type error on the grounds that an <code>xs:date</code>
                  cannot be compared to an <code>xs:integer</code>. If <code>$input</code> is empty, the
                  processor <span class="verb">may</span> (or may not) report this error:</p><div class="exampleInner"><pre>every $x as xs:date in $input satisfies ($x lt 0)</pre></div></li><li><p>This quantified expression  contains a <a href="#prod-xpath40-TypeDeclaration">type declaration</a> that is not satisfied by every item in the  test expression. 
                  The expression may either return <code>true</code> or raise a <a title="type error" class="termref" href="#dt-type-error">type error</a>.</p><div class="exampleInner"><pre>some $x as xs:integer in (1, 2, "cat") satisfies $x * 2 = 4</pre></div></li></ul></div><div class="div2">
<h3><a id="id-expressions-on-datatypes"/>4.17 <a href="#id-expressions-on-datatypes" style="text-decoration: none">Expressions on SequenceTypes</a></h3><p>
The <code>instance
of</code>, <code>cast</code>, <code>castable</code>,
and <code>treat</code> expressions are used to test whether a value
conforms to a given type or to convert it to an instance of a given
type.
</p><div class="div3">
<h4><a id="id-instance-of"/>4.17.1 <a href="#id-instance-of" style="text-decoration: none">Instance Of</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InstanceofExpr"/><code><a href="#prod-xpath40-InstanceofExpr">InstanceofExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-InstanceofExpr-TreatExpr">TreatExpr</a>  ("instance"  "of"  <a href="#doc-xpath40-InstanceofExpr-SequenceType">SequenceType</a>)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InstanceofExpr-TreatExpr"/><code><a href="#prod-xpath40-TreatExpr">TreatExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-CastableExpr">CastableExpr</a>  ("treat"  "as"  <a href="#doc-xpath40-InstanceofExpr-SequenceType">SequenceType</a>)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-InstanceofExpr-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                        <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody></table><p>The boolean
operator <code>instance of</code>
returns <code>true</code> if the value of its first operand matches
the <a href="#doc-xpath40-SequenceType">SequenceType</a> in its second
operand, according to the rules for <a title="SequenceType matching" class="termref" href="#dt-sequencetype-matching">SequenceType
matching</a>; otherwise it returns <code>false</code>. For example:</p><ul><li><p>
                        <code>5 instance of xs:integer</code>
                     </p><p>This example returns <code>true</code> because the given value is an instance of the given type.</p></li><li><p>
                        <code>5 instance of xs:decimal</code>
                     </p><p>This example returns <code>true</code> because the given value is an integer literal, and <code>xs:integer</code> is derived by restriction from <code>xs:decimal</code>.</p></li><li><p>
                        <code>(5, 6) instance of xs:integer+</code>
                     </p><p>This example returns <code>true</code> because the given sequence contains two integers, and is a valid instance of the specified type.</p></li><li><p>
                        <code>. instance of element()</code>
                     </p><p>This example returns <code>true</code> if the context value is a
                     single element node or <code>false</code> if the context value is defined 
                     but is not a single element node. If the context value is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>, a <a title="type error" class="termref" href="#dt-type-error">type error</a>
                        is raised [<a href="#ERRXPDY0002" title="err:XPDY0002">err:XPDY0002</a>].</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>An <code>instance of</code> test does not allow any kind of casting or coercion.
            The results may therefore be counterintuitive. For example, the expression
            <code>3 instance of xs:positiveInteger</code> returns <code>false</code>, because
            the expression <code>3</code> evaluates to an instance of <code>xs:integer</code>,
            not <code>xs:positiveInteger</code>. For similar reasons, <code>"red" instance of
            enum("red", "green", "blue")</code> returns false.</p><p>On such occasions, a <code>castable as</code> test may be more appropriate:
            see <a href="#id-castable"><b>4.17.3 Castable</b></a>
                  </p></div></div><div class="div3">
<h4><a id="id-cast"/>4.17.2 <a href="#id-cast" style="text-decoration: none">Cast</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastExpr"/><code><a href="#prod-xpath40-CastExpr">CastExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-CastExpr-PipelineExpr">PipelineExpr</a>  ("cast"  "as"  <a href="#doc-xpath40-CastExpr-CastTarget">CastTarget</a>  "?"?)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastExpr-PipelineExpr"/><code><a href="#prod-xpath40-PipelineExpr">PipelineExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-ArrowExpr">ArrowExpr</a>  ("-&gt;"  <a href="#doc-xpath40-ArrowExpr">ArrowExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastExpr-CastTarget"/><code><a href="#prod-xpath40-CastTarget">CastTarget</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-CastExpr-TypeName">TypeName</a>  |  <a href="#doc-xpath40-CastExpr-ChoiceItemType">ChoiceItemType</a>  |  <a href="#doc-xpath40-CastExpr-EnumerationType">EnumerationType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastExpr-TypeName"/><code><a href="#prod-xpath40-TypeName">TypeName</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-CastExpr-EQName">EQName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastExpr-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastExpr-ChoiceItemType"/><code><a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a></code></td><td>::=</td><td><code>"("  (<a href="#doc-xpath40-CastExpr-ItemType">ItemType</a> ++ "|")  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastExpr-ItemType"/><code><a href="#prod-xpath40-ItemType">ItemType</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AnyItemTest">AnyItemTest</a>  |  <a href="#doc-xpath40-CastExpr-TypeName">TypeName</a>  |  <a href="#prod-xpath40-KindTest">KindTest</a>  |  <a href="#doc-xpath40-FunctionType">FunctionType</a>  |  <a href="#doc-xpath40-MapType">MapType</a>  |  <a href="#doc-xpath40-ArrayType">ArrayType</a>  |  <a href="#doc-xpath40-RecordType">RecordType</a>  |  <a href="#doc-xpath40-CastExpr-EnumerationType">EnumerationType</a>  |  <a href="#doc-xpath40-CastExpr-ChoiceItemType">ChoiceItemType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastExpr-EnumerationType"/><code><a href="#prod-xpath40-EnumerationType">EnumerationType</a></code></td><td>::=</td><td><code>"enum"  "("  (<a href="#doc-xpath40-StringLiteral">StringLiteral</a> ++ ",")  ")"</code></td></tr></tbody></table><p>Sometimes
it is necessary to convert a value to a specific datatype. For this
purpose, XPath 4.0 provides a <code>cast</code> expression that
creates a new value of a specific type based on an existing value. A
<code>cast</code> expression takes two operands: an <b>input
expression</b> and a <b>target type</b>. The type of the
atomized value of the input expression is called the <b>input type</b>. 
The target type must be a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>. In practice
               this means it may be any of:</p><ul><li><p>The name of an <a title="named item type" class="termref" href="#dt-named-item-type">named item type</a>
               defined in the <a title="static context" class="termref" href="#dt-static-context">static context</a>, which in turn must refer to an item
               type in one of the following categories.</p></li><li><p>The name of a type defined in the  <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope schema types</a>, 
                  which must be a simple type (of variety atomic, list or union) [<a href="#ERRXQST0052" title="err:XQST0052">err:XQST0052</a>] .
                  In addition, the target type cannot be <code>xs:NOTATION</code>, <code>xs:anySimpleType</code>,
                  or <code>xs:anyAtomicType</code>
                     </p></li><li><p>A <code>ChoiceItemType</code> representing a 
                  <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a> (such as <code>(xs:date | xs:dateTime)</code>).</p></li><li><p>An <code>EnumerationType</code> such as <code>enum("red", "green", "blue")</code>.</p></li></ul><p>Otherwise, a static error is raised [<a href="#ERRXPST0080" title="err:XPST0080">err:XPST0080</a>].</p><p>The optional occurrence indicator <code>?</code> denotes that an empty
sequence is permitted.</p><p>Casting a node to <code>xs:QName</code> can cause surprises because it uses the <a title="static context" class="termref" href="#dt-static-context">static context</a>
               of the cast expression to provide the <a title="namespace binding" class="termref" href="#dt-namespace-binding">namespace bindings</a> for this operation. 
               Instead of casting to <code>xs:QName</code>, it is generally preferable to use the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-QName"><code>fn:QName</code></a> 
               function, which allows the namespace context to be taken from the document containing the QName.</p><p>The semantics of the <code>cast</code> expression
are as follows:</p><ol class="enumar"><li><p>The input expression is evaluated.</p></li><li><p>The result of the first step is <a title="atomization" class="termref" href="#dt-atomization">atomized</a>.</p></li><li><p> If the result of atomization is a
sequence of more than one atomic item, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li><li><p>If the result
of atomization is an empty sequence:</p><ol class="enumla"><li><p>If
<code>?</code> is specified after the target type, the result of the
<code>cast</code> expression is an empty sequence.</p></li><li><p>
If <code>?</code> is not specified after the target type, a <a title="type error" class="termref" href="#dt-type-error">type error</a> is raised [<a href="#ERRXPTY0004" title="err:XPTY0004">err:XPTY0004</a>].</p></li></ol></li><li><p>If the result of atomization is a single
atomic item, the result of the cast expression is determined by
casting to the target type as described in <a href="https://qt4cg.org/specifications/xpath-functions-40/#casting">Section 22 Casting</a><sup><small>FO</small></sup>. When casting, an
implementation may need to determine whether one type is derived by
restriction from another. An implementation can determine this either
by examining the <a title="in-scope schema definitions" class="termref" href="#dt-issd">in-scope schema
definitions</a> or by using an alternative, <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>
mechanism such as a data dictionary.

The result of a cast expression is one of the following: 

</p><ol class="enumla"><li><p> 
    A value of the target type (or, in the case of list types,
    a sequence of values that are instances of the item type of the
    list type).
  </p></li><li><p> 
    A type error, if casting from the source type to the
    target type is not supported (for example attempting to convert an
    integer to a date).
  </p></li><li><p> 
    A dynamic error, if the particular input value cannot be
    converted to the target type (for example, attempting to convert
    the string <code>"three"</code> to an integer).
  </p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>Casting to an enumeration type relies on the fact that an enumeration type
                  is a generalized atomic type. So <code>cast $x as enum("red")</code> is equivalent
                  to casting to an anonymous atomic type derived from <code>xs:string</code>
                  whose enumeration facet restricts the value space to the single string <code>"red"</code>,
                     while <code>cast $x as enum("red", "green")</code> is equivalent to casting
                  to <code>(enum("red") | enum("green"))</code>.</p></div></li></ol></div><div class="div3">
<h4><a id="id-castable"/>4.17.3 <a href="#id-castable" style="text-decoration: none">Castable</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastableExpr"/><code><a href="#prod-xpath40-CastableExpr">CastableExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-CastableExpr-CastExpr">CastExpr</a>  ("castable"  "as"  <a href="#doc-xpath40-CastableExpr-CastTarget">CastTarget</a>  "?"?)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastableExpr-CastExpr"/><code><a href="#prod-xpath40-CastExpr">CastExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-PipelineExpr">PipelineExpr</a>  ("cast"  "as"  <a href="#doc-xpath40-CastableExpr-CastTarget">CastTarget</a>  "?"?)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastableExpr-CastTarget"/><code><a href="#prod-xpath40-CastTarget">CastTarget</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-CastableExpr-TypeName">TypeName</a>  |  <a href="#doc-xpath40-CastableExpr-ChoiceItemType">ChoiceItemType</a>  |  <a href="#doc-xpath40-CastableExpr-EnumerationType">EnumerationType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastableExpr-TypeName"/><code><a href="#prod-xpath40-TypeName">TypeName</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-CastableExpr-EQName">EQName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastableExpr-EQName"/><code><a href="#prod-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastableExpr-ChoiceItemType"/><code><a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a></code></td><td>::=</td><td><code>"("  (<a href="#doc-xpath40-CastableExpr-ItemType">ItemType</a> ++ "|")  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastableExpr-ItemType"/><code><a href="#prod-xpath40-ItemType">ItemType</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AnyItemTest">AnyItemTest</a>  |  <a href="#doc-xpath40-CastableExpr-TypeName">TypeName</a>  |  <a href="#prod-xpath40-KindTest">KindTest</a>  |  <a href="#doc-xpath40-FunctionType">FunctionType</a>  |  <a href="#doc-xpath40-MapType">MapType</a>  |  <a href="#doc-xpath40-ArrayType">ArrayType</a>  |  <a href="#doc-xpath40-RecordType">RecordType</a>  |  <a href="#doc-xpath40-CastableExpr-EnumerationType">EnumerationType</a>  |  <a href="#doc-xpath40-CastableExpr-ChoiceItemType">ChoiceItemType</a>
                     </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-CastableExpr-EnumerationType"/><code><a href="#prod-xpath40-EnumerationType">EnumerationType</a></code></td><td>::=</td><td><code>"enum"  "("  (<a href="#doc-xpath40-StringLiteral">StringLiteral</a> ++ ",")  ")"</code></td></tr></tbody></table><p>XPath 4.0
provides an expression that tests whether a given value
is castable into a given target type. 

The <span>target type</span> is subject to the same
               rules as the target type of a <code>cast</code> expression.</p><p>The expression <code>E castable as T</code> returns <code>true</code> 
if the result of evaluating <code>E</code>  
can be successfully cast into the target type <code>T</code> by using a <code>cast</code> expression; 
otherwise it returns <code>false</code>. 
If evaluation of <code>E</code> fails with a dynamic error or if the value of <code>E</code> cannot be atomized, 
the <code>castable</code> expression as a whole fails.</p><p>The <code>castable</code> expression can be used as a <a title="" class="termref" href="#dt-predicate">predicate</a>  to
avoid errors at evaluation time. 
It can also be used to select an appropriate type for processing of a given value, as illustrated in
the following example:</p><div class="exampleInner"><pre>
if ($x castable as hatsize)
then $x cast as hatsize
else if ($x castable as IQ)
then $x cast as IQ
else $x cast as xs:string</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The expression <code>$x castable as enum("red", "green", "blue")</code>
               is for most practical purposes equivalent to <code>$x = ("red", "green", "blue")</code>;
            the main difference is that it uses the Unicode codepoint collation for comparing strings,
            not the default collation from the static context.</p></div></div><div class="div3">
<h4><a id="id-constructor-functions"/>4.17.4 <a href="#id-constructor-functions" style="text-decoration: none">Constructor Functions</a></h4><p>For every simple type in the <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope schema types</a>  (except <code>xs:NOTATION</code> and 
               <code>xs:anyAtomicType</code>, and <code>xs:anySimpleType</code>, which 
               are not instantiable), a <b>constructor function</b> is implicitly defined. 
               In each case, the name of the constructor function is the same as the name of 
               its target type (including namespace). The signature of the constructor 
               function for  a given type depends on the type that is being constructed, 
               and can be found in <a href="https://qt4cg.org/specifications/xpath-functions-40/#constructor-functions">Section 21 Constructor functions</a><sup><small>FO</small></sup>.</p><p>There is also a constructor function for every <a title="named item type" class="termref" href="#dt-named-item-type">named item type</a> 
               in the <a title="static context" class="termref" href="#dt-static-context">static context</a>
            that expands either to a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>
                  <span>or to
            a <a href="#doc-xpath40-RecordType">RecordType</a>
                  </span>.</p><p>All such constructor functions are classified as
               <a title="system function" class="termref" href="#dt-system-function">system functions</a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The constructor function is present in the static
            context if and only if the corresponding type is present in the static context.</p><p>For XSLT, this means that a constructor function corresponding to an imported
            schema type is private to the stylesheet package, and a constructor function
            corresponding to an <code>xsl:item-type</code> declaration has the same visibility
               as the <code>xsl:item-type</code> declaration.</p><p>For XQuery, this means that a constructor function corresponding to an imported
                  schema type is private to the query module, and a constructor function
                  corresponding to a named item type declaration is <code>%public</code>
                  or <code>%private</code> according to the annotations on the item type declaration.</p></div><p>
                  [<a id="dt-constructor-function" title="constructor function">Definition</a>: The <b>constructor function</b> for a given simple type is used to convert instances of other  simple types into the given type. 
                  The semantics of the constructor function call <code>T($arg)</code> are defined to be equivalent to the expression <code>$arg cast as T?</code>.]
               </p><p>The following examples illustrate the use of constructor functions:</p><ul><li><p>This
example is equivalent to <code>"2000-01-01" cast as
xs:date?</code>.</p><div class="exampleInner"><pre>xs:date("2000-01-01")</pre></div></li><li><p>This
example is equivalent to

<code>($floatvalue * 0.2E-5) cast as xs:decimal?</code>.</p><div class="exampleInner"><pre>xs:decimal($floatvalue * 0.2E-5)</pre></div></li><li><p>This example returns an
<code>xs:dayTimeDuration</code> value equal to 21 days. It is
equivalent to <code>"P21D" cast as xs:dayTimeDuration?</code>.</p><div class="exampleInner"><pre>xs:dayTimeDuration("P21D")</pre></div></li><li><p>If
                     <code>usa:zipcode</code> is a user-defined <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>
in the <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope schema types</a>, then the
following expression is equivalent to the
expression <code>("12345" cast as
usa:zipcode?)</code>.</p><div class="exampleInner"><pre>usa:zipcode("12345")</pre></div></li><li><p>If <code>my:chrono</code> is a named item type that expands to
                     <code>(xs:date | xs:time | xs:dateTime)</code>, then the result
                     of <code>my:chrono("12:00:00Z")</code> is the <code>xs:time</code>
                     value <code>12:00:00Z</code>.</p></li><li><p>If <code>my:location</code> is a named item type that expands
                  to <code>record(latitude as xs:double, longitude as xs:double)</code>,
                  then the result of <code>my:location(50.52, -3.02)</code> is
                  the map <code>{ 'latitude': 50.52e0, 'longitude': -3.02e0 }</code>.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>
                  An instance of an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a> whose name is in no namespace can be
  constructed by using a <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a> 
  in either a cast expression or a constructor function call.  Examples:
  </p><div class="exampleInner"><pre>17 cast as Q{}apple</pre></div><div class="exampleInner"><pre>Q{}apple(17)</pre></div><p>In either context, using an unqualified NCName might not work:
                  in a cast expression, an unqualified name is it is interpreted
                  according to the <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>,
                  while an unqualified name in a constructor function call is resolved using the
                  <a title="default function namespace" class="termref" href="#dt-default-function-namespace">default function namespace</a> which will often be inappropriate.
               </p></div></div><div class="div3">
<h4><a id="id-treat"/>4.17.5 <a href="#id-treat" style="text-decoration: none">Treat</a></h4><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-TreatExpr"/><code><a href="#prod-xpath40-TreatExpr">TreatExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-TreatExpr-CastableExpr">CastableExpr</a>  ("treat"  "as"  <a href="#doc-xpath40-TreatExpr-SequenceType">SequenceType</a>)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-TreatExpr-CastableExpr"/><code><a href="#prod-xpath40-CastableExpr">CastableExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-CastExpr">CastExpr</a>  ("castable"  "as"  <a href="#prod-xpath40-CastTarget">CastTarget</a>  "?"?)?</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-TreatExpr-SequenceType"/><code><a href="#prod-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#doc-xpath40-ItemType">ItemType</a>
                        <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr></tbody></table><p>XPath 4.0 provides an
expression called <code>treat</code> that can be used to modify the
<a title="static type" class="termref" href="#dt-static-type">static type</a> of its
operand.</p><p>Like <code>cast</code>, the <code>treat</code>
expression takes two operands: an expression and a <a href="#doc-xpath40-SequenceType">SequenceType</a>. Unlike
<code>cast</code>, however, <code>treat</code> does not change the
<a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> or value of its operand. Instead, the purpose of
<code>treat</code> is to ensure that an expression has an expected
dynamic type at evaluation time.</p><p>The semantics of <em>
                     <code>expr1</code>
                  </em>
                  <code> treat as </code>
                  <em>
                     <code>type1</code>
                  </em> are as
follows:</p><ul><li><p>During static analysis:</p><p> The
<a title="static type" class="termref" href="#dt-static-type">static type</a> of the
<code>treat</code> expression is <em>
                           <code>type1</code>
                        </em>. This enables the
expression to be used as an argument of a function that requires a
parameter of <em>
                           <code>type1</code>
                        </em>.</p></li><li><p>During expression
evaluation:</p><p>If <em>
                           <code>expr1</code>
                        </em> matches <em>
                           <code>type1</code>
                        </em>,
using the rules for <a title="SequenceType matching" class="termref" href="#dt-sequencetype-matching">SequenceType
matching</a>,
the <code>treat</code> expression returns the value of
<em>
                           <code>expr1</code>
                        </em>; otherwise, it raises a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a>
                        [<a href="#ERRXPDY0050" title="err:XPDY0050">err:XPDY0050</a>].
If the value of <em>
                           <code>expr1</code>
                        </em> is returned, the identity of any nodes in the value is
preserved. The <code>treat</code> expression ensures that the value of
its expression operand conforms to the expected type at
run-time.</p></li><li><p>Example:</p><div class="exampleInner"><pre>$myaddress treat as element(*, USAddress)</pre></div><p>The
<a title="static type" class="termref" href="#dt-static-type">static type</a> of
<code>$myaddress</code> may be <code>element(*, Address)</code>, a
less specific type than <code>element(*, USAddress)</code>. However,
at run-time, the value of <code>$myaddress</code> must match the type
<code>element(*, USAddress)</code> using rules for <a title="SequenceType matching" class="termref" href="#dt-sequencetype-matching">SequenceType
matching</a>;
otherwise a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> is
raised [<a href="#ERRXPDY0050" title="err:XPDY0050">err:XPDY0050</a>].</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>Earlier releases of XPath and XQuery defined a mode of operation,
               sometimes called strict static typing, in which it was required that the static
               type of every expression should conform to the required type of the context
               in which it appeared. In this situation it was often necessary to define
               a more precise static type for an expression by the use of <code>treat as</code>.
               In the absence of this feature, the <code>treat as</code> expression is
               rarely necessary, though it can be useful for documentation, and might in
               some cases (depending on the processor) have performance benefits.
          </p></div></div></div><div class="div2">
<h3><a id="id-pipeline-operator"/>4.18 <a href="#id-pipeline-operator" style="text-decoration: none">Pipeline operator</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-arrow-operator">⬇</a> <a href="#id-quantified-expressions">⬆</a></p><ol><li><p>
               With the pipeline operator <code>-&gt;</code>, the result of an expression
               can be bound to the context value before evaluating another expression.
            <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1685">1685</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1686">1686</a> 9 January 2025]</i></p></li></ol></div>
<a id="d3e32382"/><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PipelineExpr"/><code><a href="#prod-xpath40-PipelineExpr">PipelineExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-PipelineExpr-ArrowExpr">ArrowExpr</a>  ("-&gt;"  <a href="#doc-xpath40-PipelineExpr-ArrowExpr">ArrowExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-PipelineExpr-ArrowExpr"/><code><a href="#prod-xpath40-ArrowExpr">ArrowExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-UnaryExpr">UnaryExpr</a>  (<a href="#prod-xpath40-SequenceArrowTarget">SequenceArrowTarget</a>  |  <a href="#prod-xpath40-MappingArrowTarget">MappingArrowTarget</a>)*</code></td></tr></tbody></table><p>
               [<a id="dt-pipeline-operator" title="pipeline operator">Definition</a>: 
            The <b>pipeline operator</b>
                  <code>-&gt;</code> evaluates an expression and
            binds the result to the context value before evaluating another expression.]
            </p><p>Each operation <code>
                  <var>E<sub>1</sub></var> -&gt; <var>E<sub>2</sub></var>
               </code> is evaluated as follows: 
            Expression <var>E<sub>1</sub></var> is evaluated to a sequence <code>S</code>. 
            <var>S</var> then serves in turn to provide an inner <a title="fixed focus" class="termref" href="#dt-fixed-focus">fixed focus</a>
            (with the context value set to <var>S</var>) for an evaluation of <var>E<sub>2</sub></var> in the
            <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a>.
            Unlike the <a href="#id-map-operator"><b>4.19 Simple map operator (!)</b></a>, the result of <var>E<sub>1</sub></var> is bound
            just once and as a whole to the context value.
         </p><p>The following examples illustrate the use of pipeline operators:</p><div class="example"><ul><li><p>Tokenizes a string, counts the tokens, creates a concatenated string and returns
                     <code>count=3</code>:</p><div class="exampleInner"><pre>'a b c' -&gt; tokenize(.) -&gt; count(.) -&gt; concat('count=', .)</pre></div><p>An equivalent expression is:
                     </p><div class="exampleInner"><pre>
let $string := 'a b c'
let $tokens := tokenize($string)
let $count := count($tokens)
return concat('count=', $count)</pre></div></li><li><p>Calculates the sum of powers of <code>2</code> and returns
                     <code>2046</code>.</p><div class="exampleInner"><pre>(1 to 10) ! math:pow(2, .) -&gt; sum(.)</pre></div><p>An equivalent expression is:
                     </p><div class="exampleInner"><pre>
let $powers := (
  for $exp in 1 to 10
  return math:pow(2, $exp)
)
return sum($powers)</pre></div></li><li><p>Doubles the values of a sequence, compares the values pairwise with another
                     sequence, checks if some comparisons were successful, and returns
                     <code>true</code>.</p><div class="exampleInner"><pre>
(1 to 4)
-&gt; for-each(., op('+'))
-&gt; for-each-pair(4 to 7, ., op('&gt;'))
-&gt; some(.)</pre></div><p>An equivalent expression is:
                     </p><div class="exampleInner"><pre>
let $data := 1 to 4
let $data := for-each($data, op('+'))
let $data := for-each-pair(4 to 7, $data, op('&gt;'))
return some($data)</pre></div></li><li><p>Reduces a long sequence to at most 9 elements, with dots appended,
                  and returns a single string.</p><div class="exampleInner"><pre>
$dictionary/word
-&gt; (if (count(.) &lt; 10) then . else (.[1 to 9], '…'))
-&gt; string-join(., '; ')</pre></div><p>An equivalent expression is:
                     </p><div class="exampleInner"><pre>
let $words := $dictionary/word
let $chopped := (if (count($words) &lt; 10) then $words else ($words[1 to 9], '…'))
return string-join($chopped, '; ')</pre></div></li></ul></div></div><div class="div2">
<h3><a id="id-map-operator"/>4.19 <a href="#id-map-operator" style="text-decoration: none">Simple map operator (<code>!</code>)</a></h3><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SimpleMapExpr"/><code><a href="#prod-xpath40-SimpleMapExpr">SimpleMapExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-SimpleMapExpr-PathExpr">PathExpr</a>  ("!"  <a href="#doc-xpath40-SimpleMapExpr-PathExpr">PathExpr</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-SimpleMapExpr-PathExpr"/><code><a href="#prod-xpath40-PathExpr">PathExpr</a></code></td><td>::=</td><td><code>("/"  <a href="#doc-xpath40-RelativePathExpr">RelativePathExpr</a>?)<br/>|  ("//"  <a href="#doc-xpath40-RelativePathExpr">RelativePathExpr</a>)<br/>|  <a href="#doc-xpath40-RelativePathExpr">RelativePathExpr</a>
                  </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-leading-lone-slash">xgc: leading-lone-slash</a>
                   */</td></tr></tbody></table><p>
    A mapping expression <code>
                  <var>S</var>!<var>E</var>
               </code> evaluates the
    expression <var>E</var> once for every item in the sequence
    obtained by evaluating <var>S</var>. The simple mapping operator
    <code>!</code> can be applied to any sequence, regardless of the
    types of its items, and it can deliver a mixed sequence of nodes,
    atomic items, and functions. Unlike the similar <code>/</code>
    operator, it does not sort nodes into document order or eliminate
    duplicates.
  </p><p>Each operation <code>
                  <var>E<sub>1</sub></var>!<var>E<sub>2</sub></var>
               </code> is evaluated as follows: 
            Expression <var>E<sub>1</sub></var> is evaluated to a sequence <code>S</code>. 
            Each item in <var>S</var> then serves in turn to provide an inner focus 
            (the item as the context value, its position in <var>S</var> as the 
            context position, the length of <var>S</var> as the context size) 
            for an evaluation of <var>E<sub>2</sub></var> in the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a>. The sequences resulting from all the 
            evaluations of <var>E<sub>2</sub></var> are combined as follows: Every evaluation 
            of <var>E<sub>2</sub></var> returns a (possibly empty) sequence of items. 
            The final result is the <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a> of these sequences.
            The returned sequence preserves the orderings within and among the subsequences 
            generated by the evaluations of <var>E<sub>2</sub></var>.
         </p><p>Simple map operators have functionality similar to <a href="#id-path-operator"><b>4.6.3 Path operator (/)</b></a>.
  The following table summarizes the differences between these two operators</p><table style="width:100%" class="medium"><thead><tr><th>Operator</th><th>Path operator (<code>E1 / E2</code>)</th><th>Simple map operator (<code>E1 ! E2</code>)</th></tr></thead><tbody><tr><th>E1</th><td>Any sequence of nodes</td><td>Any sequence of items</td></tr><tr><th>E2</th><td>Either a sequence of nodes or a sequence of non-node items</td><td>A sequence of items</td></tr><tr><th>Additional processing</th><td>Duplicate elimination and document ordering</td><td>Simple <a title="sequence concatenation" class="termref" href="#dt-sequence-concatenation">sequence concatenation</a>
                     </td></tr></tbody></table><p>The following examples illustrate the use of simple map operators combined with path expressions.</p><div class="example"><ul><li><p>
                        <code>child::div1 / child::para / string() ! concat("id-", .)</code>
                     </p><p>Selects the <code>para</code> element children of the <code>div1</code> element children of the context node; that is, the <code>para</code> element grandchildren of the context node that have <code>div1</code> parents. It then outputs the strings obtained by prepending <code>"id-"</code> to each of the string values of these grandchildren.</p></li><li><p>
                        <code>$emp ! (@first, @middle, @last)</code>
                     </p><p>Returns the values of the attributes <code>first</code>, <code>middle</code>, and <code>last</code> for each element in <code>$emp</code>, in the order given. (The <code>/</code> operator, if used here, would return the attributes in an unpredictable order.)</p></li><li><p>
                        <code>$docs ! ( //employee)</code>
                     </p><p>Returns all the <code>employee</code> elements within all the documents identified by the variable <code>$docs</code>, in document order within each document, but retaining the order of documents.</p></li><li><p>
                        <code>avg( //employee / salary ! translate(., '$', '') ! number(.))</code>
                     </p><p>Returns the average salary of the employees, having converted the salary to a number by removing any <code>$</code> sign and then converting to a number. (The second occurrence of <code>!</code> could not be written as <code>/</code> because the left-hand operand of <code>/</code> cannot be an atomic item.)</p></li><li><p>
                        <code>string-join((1 to $n) ! "*")</code>
                     </p><p>Returns a string containing <code>$n</code> asterisks.</p></li><li><p>
                        <code>$values ! (.*.) =&gt; sum()</code>
                     </p><p>Returns the sum of the squares of a sequence of numbers.</p></li><li><p>
                        <code>string-join(ancestor::* ! name(), '/')</code>
                     </p><p>Returns the names of ancestor elements, joined by <code>/</code> characters, i.e., the path to the parent of the context.</p></li></ul></div></div><div class="div2">
<h3><a id="id-arrow-operator"/>4.20 <a href="#id-arrow-operator" style="text-decoration: none">Arrow Expressions</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-mapping-arrow-expression">⬇</a> <a href="#id-pipeline-operator">⬆</a></p><ol><li><p>The syntax on the right-hand side of an arrow operator
               has been relaxed; a dynamic function call no longer needs to start with a variable reference
            or a parenthesized expression, it can also be (for example) an inline function expression
            or a map or array constructor.<i>  [Issues <a href="https://github.com/qt4cg/qtspecs/issues/1716">1716</a> <a href="https://github.com/qt4cg/qtspecs/issues/1829">1829</a> PRs <a href="https://github.com/qt4cg/qtspecs/pull/1763">1763</a> <a href="https://github.com/qt4cg/qtspecs/pull/1830">1830</a> 25 February 2025]</i></p></li></ol></div><p>Arrow expressions apply a function to a value, using the value of the
         left-hand expression as the first argument to the function.</p><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr"/><code><a href="#prod-xpath40-ArrowExpr">ArrowExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ArrowExpr-UnaryExpr">UnaryExpr</a>  (<a href="#doc-xpath40-ArrowExpr-SequenceArrowTarget">SequenceArrowTarget</a>  |  <a href="#doc-xpath40-ArrowExpr-MappingArrowTarget">MappingArrowTarget</a>)*</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-UnaryExpr"/><code><a href="#prod-xpath40-UnaryExpr">UnaryExpr</a></code></td><td>::=</td><td><code>("-"  |  "+")*  <a href="#prod-xpath40-ValueExpr">ValueExpr</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-SequenceArrowTarget"/><code><a href="#prod-xpath40-SequenceArrowTarget">SequenceArrowTarget</a></code></td><td>::=</td><td><code>"=&gt;"  <a href="#doc-xpath40-ArrowExpr-ArrowTarget">ArrowTarget</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-ArrowTarget"/><code><a href="#prod-xpath40-ArrowTarget">ArrowTarget</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ArrowExpr-FunctionCall">FunctionCall</a>  |  <a href="#doc-xpath40-ArrowExpr-RestrictedDynamicCall">RestrictedDynamicCall</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-FunctionCall"/><code><a href="#prod-xpath40-FunctionCall">FunctionCall</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-EQName">EQName</a>
                     <a href="#prod-xpath40-ArgumentList">ArgumentList</a>
                  </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                   */</td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-parens">gn: parens</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-RestrictedDynamicCall"/><code><a href="#prod-xpath40-RestrictedDynamicCall">RestrictedDynamicCall</a></code></td><td>::=</td><td><code>(<a href="#doc-xpath40-ArrowExpr-VarRef">VarRef</a>  |  <a href="#doc-xpath40-ArrowExpr-ParenthesizedExpr">ParenthesizedExpr</a>  |  <a href="#doc-xpath40-ArrowExpr-FunctionItemExpr">FunctionItemExpr</a>  |  <a href="#doc-xpath40-ArrowExpr-MapConstructor">MapConstructor</a>  |  <a href="#doc-xpath40-ArrowExpr-ArrayConstructor">ArrayConstructor</a>)  <a href="#doc-xpath40-ArrowExpr-PositionalArgumentList">PositionalArgumentList</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-VarRef"/><code><a href="#prod-xpath40-VarRef">VarRef</a></code></td><td>::=</td><td><code>"$"  <a href="#doc-xpath40-EQName">EQName</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-ParenthesizedExpr"/><code><a href="#prod-xpath40-ParenthesizedExpr">ParenthesizedExpr</a></code></td><td>::=</td><td><code>"("  <a href="#prod-xpath40-Expr">Expr</a>?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-FunctionItemExpr"/><code><a href="#prod-xpath40-FunctionItemExpr">FunctionItemExpr</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-ArrowExpr-NamedFunctionRef">NamedFunctionRef</a>  |  <a href="#doc-xpath40-ArrowExpr-InlineFunctionExpr">InlineFunctionExpr</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-NamedFunctionRef"/><code><a href="#prod-xpath40-NamedFunctionRef">NamedFunctionRef</a></code></td><td>::=</td><td><code>
                     <a href="#doc-xpath40-EQName">EQName</a>  "#"  <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>
                  </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                     <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                   */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-InlineFunctionExpr"/><code><a href="#prod-xpath40-InlineFunctionExpr">InlineFunctionExpr</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-MethodAnnotation">MethodAnnotation</a>*  ("function"  |  "fn")  <a href="#prod-xpath40-FunctionSignature">FunctionSignature</a>?  <a href="#prod-xpath40-FunctionBody">FunctionBody</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-MapConstructor"/><code><a href="#prod-xpath40-MapConstructor">MapConstructor</a></code></td><td>::=</td><td><code>"map"?  "{"  (<a href="#prod-xpath40-MapConstructorEntry">MapConstructorEntry</a> ** ",")  "}"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-ArrayConstructor"/><code><a href="#prod-xpath40-ArrayConstructor">ArrayConstructor</a></code></td><td>::=</td><td><code>
                     <a href="#prod-xpath40-SquareArrayConstructor">SquareArrayConstructor</a>  |  <a href="#prod-xpath40-CurlyArrayConstructor">CurlyArrayConstructor</a>
                  </code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-PositionalArgumentList"/><code><a href="#prod-xpath40-PositionalArgumentList">PositionalArgumentList</a></code></td><td>::=</td><td><code>"("  <a href="#doc-xpath40-ArrowExpr-PositionalArguments">PositionalArguments</a>?  ")"</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-PositionalArguments"/><code><a href="#prod-xpath40-PositionalArguments">PositionalArguments</a></code></td><td>::=</td><td><code>(<a href="#prod-xpath40-Argument">Argument</a> ++ ",")</code></td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="doc-xpath40-ArrowExpr-MappingArrowTarget"/><code><a href="#prod-xpath40-MappingArrowTarget">MappingArrowTarget</a></code></td><td>::=</td><td><code>"=!&gt;"  <a href="#doc-xpath40-ArrowExpr-ArrowTarget">ArrowTarget</a>
                  </code></td></tr></tbody></table><p>The arrow syntax is particularly helpful when applying multiple
            functions to a value in turn. For example, the following
            expression invites syntax errors due to misplaced parentheses:
         </p><div class="exampleInner"><pre>tokenize((normalize-unicode(upper-case($string))),"\s+")</pre></div><p>In the following reformulation, it is easier to see that the parentheses are balanced:</p><div class="exampleInner"><pre>$string =&gt; upper-case() =&gt; normalize-unicode() =&gt; tokenize("\s+")</pre></div><p>When the operator is written as <code>=!&gt;</code>, the function
            is applied to each item in the sequence in turn. 
            Assuming that <code>$string</code> is a single string, the above example could
            equally be written:</p><div class="exampleInner"><pre>$string =!&gt; upper-case() =!&gt; normalize-unicode() =!&gt; tokenize("\s+")</pre></div><p>The difference between the two operators is seen when the left-hand
            operand evaluates to a sequence:</p><div class="exampleInner"><pre>(1, 2, 3) =&gt; avg()</pre></div><p>returns a value of only one item, <code>2</code>, the average of all three items. </p><p>This example could also be written as using the <a title="pipeline operator" class="termref" href="#dt-pipeline-operator">pipeline operator</a> as:</p><div class="exampleInner"><pre>(1, 2, 3) -&gt; avg(.)</pre></div><p>By contrast, an expression using the <a title="mapping arrow operator" class="termref" href="#dt-mapping-arrow-operator">mapping arrow operator</a>:</p><div class="exampleInner"><pre>(1, 2, 3) =!&gt; avg()</pre></div><p>would return the original sequence of three items, <code>(1, 2, 3)</code>, 
            each item being the average of itself.</p><p>There are two significant differences between the <a title="pipeline operator" class="termref" href="#dt-pipeline-operator">pipeline operator</a>
               <code>-&gt;</code>
            and the <a title="sequence arrow operator" class="termref" href="#dt-sequence-arrow-operator">sequence arrow operator</a>
               <code>=&gt;</code>:</p><ul><li><p>The <code>-&gt;</code> operator takes an arbitrary expression as its right-hand operand,
            whereas the <code>=&gt;</code> operator only accepts a function call.</p></li><li><p>When the right hand operand is a function call, the first argument
            is omitted in the case of the <code>=&gt;</code> operator, but is included explicitly
            (as a context value expression, <code>.</code>) in the case of the <code>-&gt;</code> operator.</p></li></ul><p>The following example:</p><div class="exampleInner"><pre>"The cat sat on the mat"
=&gt; tokenize()
=!&gt; concat(".")
=!&gt; upper-case()
=&gt; string-join(" ")</pre></div><p>returns <code>"THE. CAT. SAT. ON. THE. MAT."</code>. The first arrow
            could be written either as <code>=&gt;</code> or <code>=!&gt;</code> because the operand is a 
            <a title="singleton" class="termref" href="#dt-singleton">singleton</a>; the next two

            arrows have to be <code>=!&gt;</code> because the function is applied to each item in the tokenized
            sequence individually; the final arrow must be <code>=&gt;</code> because the <code>string-join</code>
            function applies to the sequence as a whole.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>It may be useful to think of this as a map/reduce pipeline. The functions
            introduced by <code>=!&gt;</code> are mapping operations; the function introduced by <code>=&gt;</code>
            is a reduce operation.</p></div><p>The following example introduces an inline function to the pipeline:</p><div class="exampleInner"><pre>(1 to 5) =!&gt; xs:double() =!&gt; math:sqrt() =!&gt; fn($a) { $a + 1 }() =&gt; sum()</pre></div><p>This is equivalent to <code>sum((1 to 5) ! (math:sqrt(xs:double(.)) + 1))</code>.</p><p>The same effect can be achieved using a <a title="focus function" class="termref" href="#dt-focus-function">focus function</a>:</p><div class="exampleInner"><pre>(1 to 5) =!&gt; xs:double() =!&gt; math:sqrt() =!&gt; fn { . + 1 }() =&gt; sum()</pre></div><p>It could also be expressed using the mapping operator <code>!</code>:</p><div class="exampleInner"><pre>(1 to 5) ! xs:double(.) ! math:sqrt(.) ! (. + 1) =&gt; sum()</pre></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The <code>ArgumentList</code> may include <code>PlaceHolders</code>,
            though this is not especially useful. For example, the expression <code>"$" =&gt; concat(?)</code> is equivalent
            to <code>concat("$", ?)</code>: its value is a function that prepends a supplied string with
            a <code>$</code> symbol.</p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The <code>ArgumentList</code> may include keyword arguments if the
            function is identified statically (that is, by name). For example,
            the following is valid: <code>$xml =&gt; xml-to-json(indent := true()) =&gt; parse-json(escape := false())</code>.</p></div><p>The sequence arrow operator thus applies the supplied function to 
            the left-hand operand as a whole, while the mapping arrow operator applies the function to
            each item in the value of the left-hand operand individually. In the case where the result
            of the left-hand operand is a single item, the two operators have the same effect.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>The mapping arrow symbol <code>=!&gt;</code> is intended to suggest a combination of
            function application (<code>=&gt;</code>) and sequence mapping
            (<code>!</code>) combined in a single operation.</p></div><p>The construct on the right-hand side of the arrow operator (<code>=&gt;</code>) can
         either be a static function call, or a restricted form of dynamic function call. The
         restrictions are there to ensure that the two forms can be distinguished by the parser
         with limited lookahead. For a dynamic call, the function item to be called can be 
         expressed as a variable reference, an inline function expression, a named function reference, 
         a map constructor, or an array constructor. Any other expression used to return the required function
         item must be enclosed in parentheses.</p><div class="div3">
<h4><a id="id-sequence-arrow-expression"/>4.20.1 <a href="#id-sequence-arrow-expression" style="text-decoration: none">Sequence Arrow Expressions</a></h4><p>
                  [<a id="dt-sequence-arrow-operator" title="sequence arrow operator">Definition</a>: 
               The <b>sequence arrow operator</b>
                     <code>=&gt;</code> applies a function to a
               supplied sequence.] It is defined as follows:</p><ul><li><p>If the arrow is followed by a static <a href="#doc-xpath40-FunctionCall">FunctionCall</a>:</p><p>Given a  <a href="#doc-xpath40-UnaryExpr">UnaryExpr</a>
                        <var>U</var> and a <a href="#doc-xpath40-FunctionCall">FunctionCall</a>
                        <code>
                           <var>F</var>(<var>A</var>, <var>B</var>, <var>C</var>...)</code>, 
                  the expression <code>
                           <var>U</var> =&gt; <var>F</var>(<var>A</var>, <var>B</var>, <var>C</var>...)</code> 
                  is equivalent to the expression 
                  <code>
                           <var>F</var>(<var>U</var>, <var>A</var>, <var>B</var>, <var>C</var>...)</code>.</p></li><li><p>If the arrow is followed by a <a href="#prod-xpath40-RestrictedDynamicCall">RestrictedDynamicCall</a>:</p><p>Given a  <a href="#doc-xpath40-UnaryExpr">UnaryExpr</a>
                        <var>U</var>, and a <a href="#prod-xpath40-RestrictedDynamicCall">RestrictedDynamicCall</a>
                        <code>
                           <var>E</var>(<var>A</var>, <var>B</var>, <var>C</var>...)</code>, the expression 
                  <code>
                           <var>U</var> =&gt; <var>E</var>(<var>A</var>, <var>B</var>, <var>C</var>...)</code> is equivalent to the
                  dynamic function call <code>
                           <var>E</var>(<var>U</var>, <var>A</var>, <var>B</var>, <var>C</var>...)</code>.</p></li></ul></div><div class="div3">
<h4><a id="id-mapping-arrow-expression"/>4.20.2 <a href="#id-mapping-arrow-expression" style="text-decoration: none">Mapping Arrow Expressions</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-conformance">⬇</a> <a href="#id-arrow-operator">⬆</a></p><ol><li><p>
               The arrow operator <code>=&gt;</code> is now complemented by a “mapping arrow” operator <code>=!&gt;</code>
               which applies the supplied function to each item in the input sequence independently.
            </p></li></ol></div><p>
                  [<a id="dt-mapping-arrow-operator" title="mapping arrow operator">Definition</a>: 
            The <b>mapping arrow operator</b>
                     <code>=!&gt;</code> applies a function to each
            item in a sequence.] It is defined as follows:</p><ul><li><p>If the arrow is followed by a static <a href="#doc-xpath40-FunctionCall">FunctionCall</a>:</p><p>Given a <a href="#doc-xpath40-UnaryExpr">UnaryExpr</a>
                        <var>U</var> and a <a href="#doc-xpath40-FunctionCall">FunctionCall</a>
                        <code>
                           <var>F</var>(<var>A</var>, <var>B</var>, <var>C</var>...)</code>, 
                  the expression <code>
                           <var>U</var> =!&gt; <var>F</var>(<var>A</var>, <var>B</var>, <var>C</var>...)</code> 
                  is equivalent to the expression 
                  <code>for $u in <var>U</var> return 
                     <var>F</var>(<var>$u</var>, <var>A</var>, <var>B</var>, <var>C</var>...)</code>.</p></li><li><p>If the arrow is followed by a <a href="#prod-xpath40-RestrictedDynamicCall">RestrictedDynamicCall</a>:</p><p>Given a  <a href="#doc-xpath40-UnaryExpr">UnaryExpr</a>
                        <var>U</var>, and a <a href="#prod-xpath40-RestrictedDynamicCall">RestrictedDynamicCall</a>
                        <code>
                           <var>E</var>(<var>A</var>, <var>B</var>, <var>C</var>...)</code>, the expression 
               <code>
                           <var>U</var> =&gt; <var>E</var>(<var>A</var>, <var>B</var>, <var>C</var>...)</code> is equivalent to the
               expression <code>for $u in U return <var>E</var>(<var>$u</var>, <var>A</var>, <var>B</var>, <var>C</var>...)</code>.</p></li></ul></div></div></div><div class="div1">
<h2><a id="id-conformance"/>5 <a href="#id-conformance" style="text-decoration: none">Conformance</a></h2><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-grammar">⬇</a> <a href="#id-mapping-arrow-expression">⬆</a></p><ol><li><p>
           Support for higher-order functions is now a mandatory feature (in 3.1 it was optional).
        <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/205">205</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/326">326</a> 1 February 2023]</i></p></li><li><p>
          The static typing feature has been dropped.
        <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1343">1343</a> ]</i></p></li></ol></div><p>This section defines the conformance criteria for an XPath 4.0 processor. In this section, the
        following terms are used to indicate the requirement levels defined in <a href="#RFC2119">[RFC2119]</a>. [<a id="must" title="must">Definition</a>: 
               <b>MUST</b> means that the item is an absolute
            requirement of the specification.]
            [<a id="mustnot" title="must not">Definition</a>: 
               <b>MUST NOT</b> means that the item is an
            absolute prohibition of the specification.]
            [<a id="may" title="may">Definition</a>: 
               <b>MAY</b> means that an item is truly
            optional.]
         </p><p class="xpath">XPath is intended primarily as a component that can be used by
      other specifications. Therefore, XPath relies on specifications that
      use it (such as <a href="#XPTR">[XPointer]</a> and <a href="#xslt-40">[XSL Transformations (XSLT) Version 4.0]</a>) to
      specify conformance criteria for XPath in their respective
      environments. Specifications that set conformance criteria for their
      use of XPath <a title="must not" class="termref" href="#mustnot">MUST NOT</a> change the
      syntactic or semantic definitions of XPath as given in this
      specification, except by subsetting and/or compatible extensions.</p><p class="xpath">If a language is described as an extension of XPath, 
      then every expression that conforms to the XPath grammar <a title="must" class="termref" href="#must">MUST</a> 
          behave as described in this specification.
      </p></div></div><div class="back"><div class="div1">
<h2><a id="nt-bnf"/>A <a href="#nt-bnf" style="text-decoration: none">XPath 4.0 Grammar</a></h2><div class="div2">
<h3><a id="id-grammar"/>A.1 <a href="#id-grammar" style="text-decoration: none">EBNF</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#EBNFNotation">⬇</a> <a href="#id-conformance">⬆</a></p><ol><li><p>The EBNF operators <code>++</code> and <code>**</code>
      have been introduced, for more concise representation of sequences using a character
      such as <code>","</code> as a separator. The notation is borrowed from Invisible XML.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1366">1366</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1498">1498</a>]</i></p></li></ol></div><p>The grammar of XPath 4.0 uses the same simple Extended Backus-Naur Form (EBNF) notation as
        <a href="#XML">[XML 1.0]</a> with the following differences.</p><ul><li><p>The notation <code>XYZ ** ","</code> indicates a sequence of zero or more occurrences of
        <code>XYZ</code>, with a single comma between adjacent occurrences.</p></li><li><p>The notation <code>XYZ ++ ","</code> indicates a sequence of one or more occurrences of
        <code>XYZ</code>, with a single comma between adjacent occurrences.</p></li><li><p>All named symbols have a name that begins with an uppercase letter.</p></li><li><p>It adds a notation for referring to productions in external specifications.</p></li><li><p>Comments or extra-grammatical constraints on grammar productions are between '/*' and
          '*/' symbols.</p><ul><li><p>A 'xgc:' prefix is an extra-grammatical constraint, the details of which are
              explained in <a href="#extra-grammatical-constraints"><b>A.1.2 Extra-grammatical Constraints</b></a>
                        </p></li><li><p>A 'ws:' prefix explains the whitespace rules for the production, the details of which
              are explained in <a href="#whitespace-rules"><b>A.3.5 Whitespace Rules</b></a>
                        </p></li><li><p>A 'gn:' prefix means a 'Grammar Note', and is meant as a clarification for parsing
              rules, and is explained in <a href="#notes-on-parsing"><b>A.1.3 Grammar Notes</b></a>. These notes are not
              normative.</p></li></ul></li></ul><p>The terminal symbols for this grammar include the quoted strings used in the production rules
      below, and the terminal symbols defined in section <a href="#terminal-symbols"><b>A.3.1 Terminal Symbols</b></a>.
    <span>The grammar is a little unusual in that parsing and tokenization
    are somewhat intertwined: for more details see <a href="#lexical-structure"><b>A.3 Lexical structure</b></a>.</span>
            </p><p>The EBNF notation is described in more detail in <a href="#EBNFNotation"><b>A.1.1 Notation</b></a>.</p>
<a id="BNF-Grammar"/><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AbbrevForwardStep"/><code><a href="#doc-xpath40-AbbrevForwardStep">AbbrevForwardStep</a></code></td><td>::=</td><td><code>("@"  <a href="#doc-xpath40-NodeTest">NodeTest</a>)  |  <a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AbbrevReverseStep"/><code><a href="#doc-xpath40-AbbrevReverseStep">AbbrevReverseStep</a></code></td><td>::=</td><td><code>".."</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AdditiveExpr"/><code><a href="#doc-xpath40-AdditiveExpr">AdditiveExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-MultiplicativeExpr">MultiplicativeExpr</a>  (("+"  |  "-")  <a href="#prod-xpath40-MultiplicativeExpr">MultiplicativeExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AndExpr"/><code>AndExpr</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ComparisonExpr">ComparisonExpr</a>  ("and"  <a href="#prod-xpath40-ComparisonExpr">ComparisonExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AnyArrayType"/><code>AnyArrayType</code></td><td>::=</td><td><code>"array"  "("  "*"  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AnyFunctionType"/><code>AnyFunctionType</code></td><td>::=</td><td><code>("function"  |  "fn")  "("  "*"  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AnyItemTest"/><code>AnyItemTest</code></td><td>::=</td><td><code>"item"  "("  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AnyKindTest"/><code>AnyKindTest</code></td><td>::=</td><td><code>"node"  "("  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AnyMapType"/><code>AnyMapType</code></td><td>::=</td><td><code>"map"  "("  "*"  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AnyRecordType"/><code>AnyRecordType</code></td><td>::=</td><td><code>"record"  "("  "*"  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Argument"/><code>Argument</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ExprSingle">ExprSingle</a>  |  <a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholder</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ArgumentList"/><code>ArgumentList</code></td><td>::=</td><td><code>"("  ((<a href="#prod-xpath40-PositionalArguments">PositionalArguments</a>  (","  <a href="#prod-xpath40-KeywordArguments">KeywordArguments</a>)?)  |  <a href="#prod-xpath40-KeywordArguments">KeywordArguments</a>)?  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ArgumentPlaceholder"/><code>ArgumentPlaceholder</code></td><td>::=</td><td><code>"?"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ArrayConstructor"/><code><a href="#doc-xpath40-ArrayConstructor">ArrayConstructor</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-SquareArrayConstructor">SquareArrayConstructor</a>  |  <a href="#prod-xpath40-CurlyArrayConstructor">CurlyArrayConstructor</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ArrayType"/><code><a href="#doc-xpath40-ArrayType">ArrayType</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AnyArrayType">AnyArrayType</a>  |  <a href="#prod-xpath40-TypedArrayType">TypedArrayType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ArrowExpr"/><code><a href="#doc-xpath40-ArrowExpr">ArrowExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-UnaryExpr">UnaryExpr</a>  (<a href="#prod-xpath40-SequenceArrowTarget">SequenceArrowTarget</a>  |  <a href="#prod-xpath40-MappingArrowTarget">MappingArrowTarget</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ArrowTarget"/><code>ArrowTarget</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-FunctionCall">FunctionCall</a>  |  <a href="#prod-xpath40-RestrictedDynamicCall">RestrictedDynamicCall</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AttributeName"/><code>AttributeName</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EQName">EQName</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AttributeTest"/><code><a href="#doc-xpath40-AttributeTest">AttributeTest</a></code></td><td>::=</td><td><code>"attribute"  "("  (<a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>  (","  <a href="#prod-xpath40-TypeName">TypeName</a>)?)?  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AxisStep"/><code><a href="#doc-xpath40-AxisStep">AxisStep</a></code></td><td>::=</td><td><code>(<a href="#prod-xpath40-ReverseStep">ReverseStep</a>  |  <a href="#prod-xpath40-ForwardStep">ForwardStep</a>)  <a href="#prod-xpath40-Predicate">Predicate</a>*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-BracedAction"/><code>BracedAction</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-CastableExpr"/><code><a href="#doc-xpath40-CastableExpr">CastableExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-CastExpr">CastExpr</a>  ("castable"  "as"  <a href="#prod-xpath40-CastTarget">CastTarget</a>  "?"?)?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-CastExpr"/><code><a href="#doc-xpath40-CastExpr">CastExpr</a></code></td><td>::=</td><td><code>
                        <a href="#doc-xpath40-PipelineExpr">PipelineExpr</a>  ("cast"  "as"  <a href="#prod-xpath40-CastTarget">CastTarget</a>  "?"?)?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-CastTarget"/><code>CastTarget</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-TypeName">TypeName</a>  |  <a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a>  |  <a href="#prod-xpath40-EnumerationType">EnumerationType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ChoiceItemType"/><code>ChoiceItemType</code></td><td>::=</td><td><code>"("  (<a href="#prod-xpath40-ItemType">ItemType</a> ++ "|")  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-CommentTest"/><code>CommentTest</code></td><td>::=</td><td><code>"comment"  "("  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ComparisonExpr"/><code><a href="#doc-xpath40-ComparisonExpr">ComparisonExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-OtherwiseExpr">OtherwiseExpr</a>  ((<a href="#prod-xpath40-ValueComp">ValueComp</a>  |  <a href="#prod-xpath40-GeneralComp">GeneralComp</a>  |  <a href="#prod-xpath40-NodeComp">NodeComp</a>)  <a href="#prod-xpath40-OtherwiseExpr">OtherwiseExpr</a>)?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ContextValueRef"/><code><a href="#doc-xpath40-ContextValueRef">ContextValueRef</a></code></td><td>::=</td><td><code>"."</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-CurlyArrayConstructor"/><code>CurlyArrayConstructor</code></td><td>::=</td><td><code>"array"  <a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-DocumentTest"/><code>DocumentTest</code></td><td>::=</td><td><code>"document-node"  "("  (<a href="#prod-xpath40-ElementTest">ElementTest</a>  |  <a href="#prod-xpath40-SchemaElementTest">SchemaElementTest</a>  |  <a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>)?  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-DynamicFunctionCall"/><code><a href="#doc-xpath40-DynamicFunctionCall">DynamicFunctionCall</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-PostfixExpr">PostfixExpr</a>
                        <a href="#prod-xpath40-PositionalArgumentList">PositionalArgumentList</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ElementName"/><code>ElementName</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EQName">EQName</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ElementTest"/><code><a href="#doc-xpath40-ElementTest">ElementTest</a></code></td><td>::=</td><td><code>"element"  "("  (<a href="#prod-xpath40-NameTestUnion">NameTestUnion</a>  (","  <a href="#prod-xpath40-TypeName">TypeName</a>  "?"?)?)?  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-EnclosedExpr"/><code><a href="#doc-xpath40-EnclosedExpr">EnclosedExpr</a></code></td><td>::=</td><td><code>"{"  <a href="#prod-xpath40-Expr">Expr</a>?  "}"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-EnumerationType"/><code><a href="#doc-xpath40-EnumerationType">EnumerationType</a></code></td><td>::=</td><td><code>"enum"  "("  (<a href="#doc-xpath40-StringLiteral">StringLiteral</a> ++ ",")  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-EQName"/><code><a href="#doc-xpath40-EQName">EQName</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-QName">QName</a>  |  <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Expr"/><code>Expr</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-ExprSingle">ExprSingle</a> ++ ",")</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ExprSingle"/><code><a href="#doc-xpath40-ExprSingle">ExprSingle</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ForExpr">ForExpr</a>
                        <br/>|  <a href="#prod-xpath40-LetExpr">LetExpr</a>
                        <br/>|  <a href="#prod-xpath40-QuantifiedExpr">QuantifiedExpr</a>
                        <br/>|  <a href="#prod-xpath40-IfExpr">IfExpr</a>
                        <br/>|  <a href="#prod-xpath40-OrExpr">OrExpr</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ExtensibleFlag"/><code>ExtensibleFlag</code></td><td>::=</td><td><code>","  "*"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FieldDeclaration"/><code>FieldDeclaration</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-FieldName">FieldName</a>  "?"?  ("as"  <a href="#prod-xpath40-SequenceType">SequenceType</a>)?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FieldName"/><code>FieldName</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#doc-xpath40-StringLiteral">StringLiteral</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FilterExpr"/><code><a href="#doc-xpath40-FilterExpr">FilterExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-PostfixExpr">PostfixExpr</a>
                        <a href="#prod-xpath40-Predicate">Predicate</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FilterExprAM"/><code><a href="#doc-xpath40-FilterExprAM">FilterExprAM</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-PostfixExpr">PostfixExpr</a>  "?["  <a href="#prod-xpath40-Expr">Expr</a>  "]"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForBinding"/><code>ForBinding</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ForItemBinding">ForItemBinding</a>  |  <a href="#prod-xpath40-ForMemberBinding">ForMemberBinding</a>  |  <a href="#prod-xpath40-ForEntryBinding">ForEntryBinding</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForClause"/><code>ForClause</code></td><td>::=</td><td><code>"for"  (<a href="#prod-xpath40-ForBinding">ForBinding</a> ++ ",")</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForEntryBinding"/><code>ForEntryBinding</code></td><td>::=</td><td><code>((<a href="#prod-xpath40-ForEntryKeyBinding">ForEntryKeyBinding</a>
                        <a href="#prod-xpath40-ForEntryValueBinding">ForEntryValueBinding</a>?)  |  <a href="#prod-xpath40-ForEntryValueBinding">ForEntryValueBinding</a>)  <a href="#prod-xpath40-PositionalVar">PositionalVar</a>?  "in"  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForEntryKeyBinding"/><code>ForEntryKeyBinding</code></td><td>::=</td><td><code>"key"  <a href="#prod-xpath40-VarNameAndType">VarNameAndType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForEntryValueBinding"/><code>ForEntryValueBinding</code></td><td>::=</td><td><code>"value"  <a href="#prod-xpath40-VarNameAndType">VarNameAndType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForExpr"/><code><a href="#doc-xpath40-ForExpr">ForExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ForClause">ForClause</a>
                        <a href="#prod-xpath40-ForLetReturn">ForLetReturn</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForItemBinding"/><code>ForItemBinding</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-VarNameAndType">VarNameAndType</a>
                        <a href="#prod-xpath40-PositionalVar">PositionalVar</a>?  "in"  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForLetReturn"/><code>ForLetReturn</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ForExpr">ForExpr</a>  |  <a href="#prod-xpath40-LetExpr">LetExpr</a>  |  ("return"  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>)</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForMemberBinding"/><code>ForMemberBinding</code></td><td>::=</td><td><code>"member"  <a href="#prod-xpath40-VarNameAndType">VarNameAndType</a>
                        <a href="#prod-xpath40-PositionalVar">PositionalVar</a>?  "in"  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForwardAxis"/><code><a href="#doc-xpath40-ForwardAxis">ForwardAxis</a></code></td><td>::=</td><td><code>("attribute"<br/>|  "child"<br/>|  "descendant"<br/>|  "descendant-or-self"<br/>|  "following"<br/>|  "following-or-self"<br/>|  "following-sibling"<br/>|  "following-sibling-or-self"<br/>|  "namespace"<br/>|  "self")  "::"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ForwardStep"/><code>ForwardStep</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-ForwardAxis">ForwardAxis</a>
                        <a href="#doc-xpath40-NodeTest">NodeTest</a>)  |  <a href="#doc-xpath40-AbbrevForwardStep">AbbrevForwardStep</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FunctionBody"/><code>FunctionBody</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FunctionCall"/><code><a href="#doc-xpath40-FunctionCall">FunctionCall</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EQName">EQName</a>
                        <a href="#prod-xpath40-ArgumentList">ArgumentList</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                      */</td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-parens">gn: parens</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FunctionItemExpr"/><code>FunctionItemExpr</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-NamedFunctionRef">NamedFunctionRef</a>  |  <a href="#prod-xpath40-InlineFunctionExpr">InlineFunctionExpr</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FunctionSignature"/><code>FunctionSignature</code></td><td>::=</td><td><code>"("  <a href="#prod-xpath40-ParamList">ParamList</a>  ")"  <a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a>?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-FunctionType"/><code><a href="#doc-xpath40-FunctionType">FunctionType</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AnyFunctionType">AnyFunctionType</a>
                        <br/>|  <a href="#prod-xpath40-TypedFunctionType">TypedFunctionType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-GeneralComp"/><code>GeneralComp</code></td><td>::=</td><td><code>"="  |  "!="  |  "&lt;"  |  "&lt;="  |  "&gt;"  |  "&gt;="</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-IfExpr"/><code><a href="#doc-xpath40-IfExpr">IfExpr</a></code></td><td>::=</td><td><code>"if"  "("  <a href="#prod-xpath40-Expr">Expr</a>  ")"  (<a href="#prod-xpath40-UnbracedActions">UnbracedActions</a>  |  <a href="#prod-xpath40-BracedAction">BracedAction</a>)</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-InlineFunctionExpr"/><code><a href="#doc-xpath40-InlineFunctionExpr">InlineFunctionExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-MethodAnnotation">MethodAnnotation</a>*  ("function"  |  "fn")  <a href="#prod-xpath40-FunctionSignature">FunctionSignature</a>?  <a href="#prod-xpath40-FunctionBody">FunctionBody</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-InstanceofExpr"/><code><a href="#doc-xpath40-InstanceofExpr">InstanceofExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-TreatExpr">TreatExpr</a>  ("instance"  "of"  <a href="#prod-xpath40-SequenceType">SequenceType</a>)?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-IntersectExceptExpr"/><code>IntersectExceptExpr</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-InstanceofExpr">InstanceofExpr</a>  (("intersect"  |  "except")  <a href="#prod-xpath40-InstanceofExpr">InstanceofExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ItemType"/><code><a href="#doc-xpath40-ItemType">ItemType</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AnyItemTest">AnyItemTest</a>  |  <a href="#prod-xpath40-TypeName">TypeName</a>  |  <a href="#prod-xpath40-KindTest">KindTest</a>  |  <a href="#prod-xpath40-FunctionType">FunctionType</a>  |  <a href="#prod-xpath40-MapType">MapType</a>  |  <a href="#prod-xpath40-ArrayType">ArrayType</a>  |  <a href="#prod-xpath40-RecordType">RecordType</a>  |  <a href="#prod-xpath40-EnumerationType">EnumerationType</a>  |  <a href="#prod-xpath40-ChoiceItemType">ChoiceItemType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-KeySpecifier"/><code>KeySpecifier</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>  |  <a href="#doc-xpath40-StringLiteral">StringLiteral</a>  |  <a href="#prod-xpath40-VarRef">VarRef</a>  |  <a href="#prod-xpath40-ParenthesizedExpr">ParenthesizedExpr</a>  |  <a href="#prod-xpath40-LookupWildcard">LookupWildcard</a>  |  <a href="#prod-xpath40-TypeSpecifier">TypeSpecifier</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-KeywordArgument"/><code>KeywordArgument</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EQName">EQName</a>  ":="  <a href="#prod-xpath40-Argument">Argument</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-KeywordArguments"/><code>KeywordArguments</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-KeywordArgument">KeywordArgument</a> ++ ",")</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-KindTest"/><code>KindTest</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-DocumentTest">DocumentTest</a>
                        <br/>|  <a href="#prod-xpath40-ElementTest">ElementTest</a>
                        <br/>|  <a href="#doc-xpath40-AttributeTest">AttributeTest</a>
                        <br/>|  <a href="#prod-xpath40-SchemaElementTest">SchemaElementTest</a>
                        <br/>|  <a href="#prod-xpath40-SchemaAttributeTest">SchemaAttributeTest</a>
                        <br/>|  <a href="#prod-xpath40-PITest">PITest</a>
                        <br/>|  <a href="#prod-xpath40-CommentTest">CommentTest</a>
                        <br/>|  <a href="#prod-xpath40-TextTest">TextTest</a>
                        <br/>|  <a href="#prod-xpath40-NamespaceNodeTest">NamespaceNodeTest</a>
                        <br/>|  <a href="#prod-xpath40-AnyKindTest">AnyKindTest</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-LetBinding"/><code>LetBinding</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-VarNameAndType">VarNameAndType</a>  ":="  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-LetClause"/><code>LetClause</code></td><td>::=</td><td><code>"let"  (<a href="#prod-xpath40-LetBinding">LetBinding</a> ++ ",")</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-LetExpr"/><code><a href="#doc-xpath40-LetExpr">LetExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-LetClause">LetClause</a>
                        <a href="#prod-xpath40-ForLetReturn">ForLetReturn</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Literal"/><code><a href="#doc-xpath40-Literal">Literal</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-NumericLiteral">NumericLiteral</a>  |  <a href="#doc-xpath40-StringLiteral">StringLiteral</a>  |  <a href="#prod-xpath40-QNameLiteral">QNameLiteral</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Lookup"/><code>Lookup</code></td><td>::=</td><td><code>("?"  |  "??")  (<a href="#prod-xpath40-Modifier">Modifier</a>  "::")?  <a href="#prod-xpath40-KeySpecifier">KeySpecifier</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-LookupExpr"/><code><a href="#doc-xpath40-LookupExpr">LookupExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-PostfixExpr">PostfixExpr</a>
                        <a href="#prod-xpath40-Lookup">Lookup</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-LookupWildcard"/><code>LookupWildcard</code></td><td>::=</td><td><code>"*"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-MapConstructor"/><code><a href="#doc-xpath40-MapConstructor">MapConstructor</a></code></td><td>::=</td><td><code>"map"?  "{"  (<a href="#prod-xpath40-MapConstructorEntry">MapConstructorEntry</a> ** ",")  "}"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-MapConstructorEntry"/><code>MapConstructorEntry</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-MapKeyExpr">MapKeyExpr</a>  ":"  <a href="#prod-xpath40-MapValueExpr">MapValueExpr</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-MapKeyExpr"/><code>MapKeyExpr</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-MappingArrowTarget"/><code>MappingArrowTarget</code></td><td>::=</td><td><code>"=!&gt;"  <a href="#prod-xpath40-ArrowTarget">ArrowTarget</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-MapType"/><code><a href="#doc-xpath40-MapType">MapType</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AnyMapType">AnyMapType</a>  |  <a href="#prod-xpath40-TypedMapType">TypedMapType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-MapValueExpr"/><code>MapValueExpr</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-MethodAnnotation"/><code>MethodAnnotation</code></td><td>::=</td><td><code>"%method"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Modifier"/><code>Modifier</code></td><td>::=</td><td><code>"pairs"  |  "keys"  |  "values"  |  "items"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-MultiplicativeExpr"/><code>MultiplicativeExpr</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-UnionExpr">UnionExpr</a>  (("*"  |  "×"  |  "div"  |  "÷"  |  "idiv"  |  "mod")  <a href="#prod-xpath40-UnionExpr">UnionExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-NamedFunctionRef"/><code><a href="#doc-xpath40-NamedFunctionRef">NamedFunctionRef</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EQName">EQName</a>  "#"  <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-reserved-function-names">xgc: reserved-function-names</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-NamespaceNodeTest"/><code>NamespaceNodeTest</code></td><td>::=</td><td><code>"namespace-node"  "("  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-NameTest"/><code>NameTest</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EQName">EQName</a>  |  <a href="#prod-xpath40-Wildcard">Wildcard</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-NameTestUnion"/><code>NameTestUnion</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-NameTest">NameTest</a> ++ "|")</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-NodeComp"/><code>NodeComp</code></td><td>::=</td><td><code>"is"  |  "&lt;&lt;"  |  "&gt;&gt;"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-NodeTest"/><code><a href="#doc-xpath40-NodeTest">NodeTest</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-UnionNodeTest">UnionNodeTest</a>  |  <a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-NumericLiteral"/><code><a href="#doc-xpath40-NumericLiteral">NumericLiteral</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>  |  <a href="#prod-xpath40-HexIntegerLiteral">HexIntegerLiteral</a>  |  <a href="#prod-xpath40-BinaryIntegerLiteral">BinaryIntegerLiteral</a>  |  <a href="#prod-xpath40-DecimalLiteral">DecimalLiteral</a>  |  <a href="#prod-xpath40-DoubleLiteral">DoubleLiteral</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-OccurrenceIndicator"/><code>OccurrenceIndicator</code></td><td>::=</td><td><code>"?"  |  "*"  |  "+"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-occurrence-indicators">xgc: occurrence-indicators</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-OrExpr"/><code><a href="#doc-xpath40-OrExpr">OrExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AndExpr">AndExpr</a>  ("or"  <a href="#prod-xpath40-AndExpr">AndExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-OtherwiseExpr"/><code><a href="#doc-xpath40-OtherwiseExpr">OtherwiseExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-StringConcatExpr">StringConcatExpr</a>  ("otherwise"  <a href="#prod-xpath40-StringConcatExpr">StringConcatExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ParamList"/><code>ParamList</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-VarNameAndType">VarNameAndType</a> ** ",")</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ParenthesizedExpr"/><code><a href="#doc-xpath40-ParenthesizedExpr">ParenthesizedExpr</a></code></td><td>::=</td><td><code>"("  <a href="#prod-xpath40-Expr">Expr</a>?  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-PathExpr"/><code><a href="#doc-xpath40-PathExpr">PathExpr</a></code></td><td>::=</td><td><code>("/"  <a href="#prod-xpath40-RelativePathExpr">RelativePathExpr</a>?)<br/>|  ("//"  <a href="#prod-xpath40-RelativePathExpr">RelativePathExpr</a>)<br/>|  <a href="#prod-xpath40-RelativePathExpr">RelativePathExpr</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-leading-lone-slash">xgc: leading-lone-slash</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-PipelineExpr"/><code><a href="#doc-xpath40-PipelineExpr">PipelineExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-ArrowExpr">ArrowExpr</a>  ("-&gt;"  <a href="#prod-xpath40-ArrowExpr">ArrowExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-PITest"/><code>PITest</code></td><td>::=</td><td><code>"processing-instruction"  "("  (<a href="#prod-xpath40-NCName">NCName</a>  |  <a href="#doc-xpath40-StringLiteral">StringLiteral</a>)?  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-PositionalArgumentList"/><code>PositionalArgumentList</code></td><td>::=</td><td><code>"("  <a href="#prod-xpath40-PositionalArguments">PositionalArguments</a>?  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-PositionalArguments"/><code>PositionalArguments</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-Argument">Argument</a> ++ ",")</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-PositionalVar"/><code>PositionalVar</code></td><td>::=</td><td><code>"at"  <a href="#prod-xpath40-VarName">VarName</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-PostfixExpr"/><code><a href="#doc-xpath40-PostfixExpr">PostfixExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-PrimaryExpr">PrimaryExpr</a>  |  <a href="#prod-xpath40-FilterExpr">FilterExpr</a>  |  <a href="#prod-xpath40-DynamicFunctionCall">DynamicFunctionCall</a>  |  <a href="#prod-xpath40-LookupExpr">LookupExpr</a>  |  <a href="#prod-xpath40-FilterExprAM">FilterExprAM</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Predicate"/><code>Predicate</code></td><td>::=</td><td><code>"["  <a href="#prod-xpath40-Expr">Expr</a>  "]"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-PrimaryExpr"/><code><a href="#doc-xpath40-PrimaryExpr">PrimaryExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-Literal">Literal</a>
                        <br/>|  <a href="#prod-xpath40-VarRef">VarRef</a>
                        <br/>|  <a href="#prod-xpath40-ParenthesizedExpr">ParenthesizedExpr</a>
                        <br/>|  <a href="#prod-xpath40-ContextValueRef">ContextValueRef</a>
                        <br/>|  <a href="#prod-xpath40-FunctionCall">FunctionCall</a>
                        <br/>|  <a href="#prod-xpath40-FunctionItemExpr">FunctionItemExpr</a>
                        <br/>|  <a href="#prod-xpath40-MapConstructor">MapConstructor</a>
                        <br/>|  <a href="#prod-xpath40-ArrayConstructor">ArrayConstructor</a>
                        <br/>|  <a href="#prod-xpath40-StringTemplate">StringTemplate</a>
                        <br/>|  <a href="#prod-xpath40-UnaryLookup">UnaryLookup</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-QNameLiteral"/><code><a href="#doc-xpath40-QNameLiteral">QNameLiteral</a></code></td><td>::=</td><td><code>"#"  <a href="#prod-xpath40-EQName">EQName</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-QuantifiedExpr"/><code><a href="#doc-xpath40-QuantifiedExpr">QuantifiedExpr</a></code></td><td>::=</td><td><code>("some"  |  "every")  (<a href="#prod-xpath40-QuantifierBinding">QuantifierBinding</a> ++ ",")  "satisfies"  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-QuantifierBinding"/><code>QuantifierBinding</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-VarNameAndType">VarNameAndType</a>  "in"  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-RangeExpr"/><code><a href="#doc-xpath40-RangeExpr">RangeExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AdditiveExpr">AdditiveExpr</a>  ("to"  <a href="#prod-xpath40-AdditiveExpr">AdditiveExpr</a>)?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-RecordType"/><code><a href="#doc-xpath40-RecordType">RecordType</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AnyRecordType">AnyRecordType</a>  |  <a href="#prod-xpath40-TypedRecordType">TypedRecordType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-RelativePathExpr"/><code><a href="#doc-xpath40-RelativePathExpr">RelativePathExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-StepExpr">StepExpr</a>  (("/"  |  "//")  <a href="#prod-xpath40-StepExpr">StepExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-RestrictedDynamicCall"/><code>RestrictedDynamicCall</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-VarRef">VarRef</a>  |  <a href="#prod-xpath40-ParenthesizedExpr">ParenthesizedExpr</a>  |  <a href="#prod-xpath40-FunctionItemExpr">FunctionItemExpr</a>  |  <a href="#prod-xpath40-MapConstructor">MapConstructor</a>  |  <a href="#prod-xpath40-ArrayConstructor">ArrayConstructor</a>)  <a href="#prod-xpath40-PositionalArgumentList">PositionalArgumentList</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ReverseAxis"/><code><a href="#doc-xpath40-ReverseAxis">ReverseAxis</a></code></td><td>::=</td><td><code>("ancestor"<br/>|  "ancestor-or-self"<br/>|  "parent"<br/>|  "preceding"<br/>|  "preceding-or-self"<br/>|  "preceding-sibling"<br/>|  "preceding-sibling-or-self")  "::"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ReverseStep"/><code>ReverseStep</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-ReverseAxis">ReverseAxis</a>
                        <a href="#doc-xpath40-NodeTest">NodeTest</a>)  |  <a href="#doc-xpath40-AbbrevReverseStep">AbbrevReverseStep</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-SchemaAttributeTest"/><code><a href="#doc-xpath40-SchemaAttributeTest">SchemaAttributeTest</a></code></td><td>::=</td><td><code>"schema-attribute"  "("  <a href="#prod-xpath40-AttributeName">AttributeName</a>  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-SchemaElementTest"/><code><a href="#doc-xpath40-SchemaElementTest">SchemaElementTest</a></code></td><td>::=</td><td><code>"schema-element"  "("  <a href="#prod-xpath40-ElementName">ElementName</a>  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-SequenceArrowTarget"/><code>SequenceArrowTarget</code></td><td>::=</td><td><code>"=&gt;"  <a href="#prod-xpath40-ArrowTarget">ArrowTarget</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-SequenceType"/><code><a href="#doc-xpath40-SequenceType">SequenceType</a></code></td><td>::=</td><td><code>("empty-sequence"  "("  ")")<br/>|  (<a href="#prod-xpath40-ItemType">ItemType</a>
                        <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>?)</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-SimpleMapExpr"/><code><a href="#doc-xpath40-SimpleMapExpr">SimpleMapExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-PathExpr">PathExpr</a>  ("!"  <a href="#prod-xpath40-PathExpr">PathExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-SimpleNodeTest"/><code>SimpleNodeTest</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-KindTest">KindTest</a>  |  <a href="#prod-xpath40-NameTest">NameTest</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-SquareArrayConstructor"/><code>SquareArrayConstructor</code></td><td>::=</td><td><code>"["  (<a href="#prod-xpath40-ExprSingle">ExprSingle</a> ** ",")  "]"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-StepExpr"/><code><a href="#doc-xpath40-StepExpr">StepExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-PostfixExpr">PostfixExpr</a>  |  <a href="#prod-xpath40-AxisStep">AxisStep</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-StringConcatExpr"/><code><a href="#doc-xpath40-StringConcatExpr">StringConcatExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-RangeExpr">RangeExpr</a>  ("||"  <a href="#prod-xpath40-RangeExpr">RangeExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-StringTemplate"/><code><a href="#doc-xpath40-StringTemplate">StringTemplate</a></code></td><td>::=</td><td><code>"`"  (<a href="#prod-xpath40-StringTemplateFixedPart">StringTemplateFixedPart</a>  |  <a href="#prod-xpath40-StringTemplateVariablePart">StringTemplateVariablePart</a>)*  "`"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-StringTemplateFixedPart"/><code>StringTemplateFixedPart</code></td><td>::=</td><td><code>((<a href="#prod-xpath40-Char">Char</a> - ('{' | '}' | '`'))  |  "{{"  |  "}}"  |  "``")*</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-StringTemplateVariablePart"/><code>StringTemplateVariablePart</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EnclosedExpr">EnclosedExpr</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TextTest"/><code>TextTest</code></td><td>::=</td><td><code>"text"  "("  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TreatExpr"/><code><a href="#doc-xpath40-TreatExpr">TreatExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-CastableExpr">CastableExpr</a>  ("treat"  "as"  <a href="#prod-xpath40-SequenceType">SequenceType</a>)?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TypedArrayType"/><code>TypedArrayType</code></td><td>::=</td><td><code>"array"  "("  <a href="#prod-xpath40-SequenceType">SequenceType</a>  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TypeDeclaration"/><code>TypeDeclaration</code></td><td>::=</td><td><code>"as"  <a href="#prod-xpath40-SequenceType">SequenceType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TypedFunctionParam"/><code>TypedFunctionParam</code></td><td>::=</td><td><code>("$"  <a href="#prod-xpath40-EQName">EQName</a>  "as")?  <a href="#prod-xpath40-SequenceType">SequenceType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TypedFunctionType"/><code>TypedFunctionType</code></td><td>::=</td><td><code>("function"  |  "fn")  "("  (<a href="#prod-xpath40-TypedFunctionParam">TypedFunctionParam</a> ** ",")  ")"  "as"  <a href="#prod-xpath40-SequenceType">SequenceType</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TypedMapType"/><code>TypedMapType</code></td><td>::=</td><td><code>"map"  "("  <a href="#prod-xpath40-ItemType">ItemType</a>  ","  <a href="#prod-xpath40-SequenceType">SequenceType</a>  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TypedRecordType"/><code>TypedRecordType</code></td><td>::=</td><td><code>"record"  "("  (<a href="#prod-xpath40-FieldDeclaration">FieldDeclaration</a> ** ",")  <a href="#prod-xpath40-ExtensibleFlag">ExtensibleFlag</a>?  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TypeName"/><code>TypeName</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-EQName">EQName</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-TypeSpecifier"/><code>TypeSpecifier</code></td><td>::=</td><td><code>"~["  <a href="#prod-xpath40-SequenceType">SequenceType</a>  "]"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-UnaryExpr"/><code><a href="#doc-xpath40-UnaryExpr">UnaryExpr</a></code></td><td>::=</td><td><code>("-"  |  "+")*  <a href="#prod-xpath40-ValueExpr">ValueExpr</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-UnaryLookup"/><code><a href="#doc-xpath40-UnaryLookup">UnaryLookup</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-Lookup">Lookup</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-UnbracedActions"/><code>UnbracedActions</code></td><td>::=</td><td><code>"then"  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>  "else"  <a href="#prod-xpath40-ExprSingle">ExprSingle</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-UnionExpr"/><code><a href="#doc-xpath40-UnionExpr">UnionExpr</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-IntersectExceptExpr">IntersectExceptExpr</a>  (("union"  |  "|")  <a href="#prod-xpath40-IntersectExceptExpr">IntersectExceptExpr</a>)*</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-UnionNodeTest"/><code>UnionNodeTest</code></td><td>::=</td><td><code>"("  <a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a>  ("|"  <a href="#prod-xpath40-SimpleNodeTest">SimpleNodeTest</a>)*  ")"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ValueComp"/><code>ValueComp</code></td><td>::=</td><td><code>"eq"  |  "ne"  |  "lt"  |  "le"  |  "gt"  |  "ge"</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-ValueExpr"/><code>ValueExpr</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-SimpleMapExpr">SimpleMapExpr</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-VarName"/><code>VarName</code></td><td>::=</td><td><code>"$"  <a href="#prod-xpath40-EQName">EQName</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-VarNameAndType"/><code>VarNameAndType</code></td><td>::=</td><td><code>"$"  <a href="#prod-xpath40-EQName">EQName</a>
                        <a href="#prod-xpath40-TypeDeclaration">TypeDeclaration</a>?</code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-VarRef"/><code><a href="#doc-xpath40-VarRef">VarRef</a></code></td><td>::=</td><td><code>"$"  <a href="#prod-xpath40-EQName">EQName</a>
                     </code></td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Wildcard"/><code>Wildcard</code></td><td>::=</td><td><code>"*"<br/>|  (<a href="#prod-xpath40-NCName">NCName</a>  ":*")<br/>|  ("*:"  <a href="#prod-xpath40-NCName">NCName</a>)<br/>|  (<a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>  "*")</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-XPath"/><code><a href="#doc-xpath40-XPath">XPath</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-Expr">Expr</a>
                     </code></td></tr></tbody></table><div class="div3">
<h4><a id="EBNFNotation"/>A.1.1 <a href="#EBNFNotation" style="text-decoration: none">Notation</a></h4><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#lexical-structure">⬇</a> <a href="#id-grammar">⬆</a></p><ol><li><p>The EBNF operators <code>++</code> and <code>**</code>
        have been introduced, for more concise representation of sequences using a character
        such as <code>","</code> as a separator. The notation is borrowed from Invisible XML.<i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1366">1366</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1498">1498</a>]</i></p></li></ol></div><p>
                  [<a id="symbol" title="symbol">Definition</a>: Each rule in the grammar defines one <b>symbol</b>,
          using the following format: </p><div class="exampleInner"><pre>symbol ::= expression</pre></div><p>
                  ]
               </p><p>
                  [<a id="terminal" title="terminal">Definition</a>: A <b>terminal</b> is a symbol or string or
          pattern that can appear in the right-hand side of a rule, but never appears on the
          left-hand side in the main grammar, although it may appear on the left-hand side of a rule
          in the grammar for terminals.] The following constructs are used to match strings
        of one or more characters in a terminal:</p><dl><dt class="label">[a-zA-Z]</dt><dd><p>matches any <a href="#prod-xpath40-Char">Char</a> with a value in the range(s) indicated
              (inclusive).</p></dd><dt class="label">[abc]</dt><dd><p>matches any <a href="#prod-xpath40-Char">Char</a> with a value among the characters enumerated.
            </p></dd><dt class="label">[^abc]</dt><dd><p>matches any <a href="#prod-xpath40-Char">Char</a> with a value not among the characters given.</p></dd><dt class="label">"string" or 'string'</dt><dd><p>matches the sequence of characters that appear inside the double or single quotation marks.</p></dd><dt class="label"> [http://www.w3.org/TR/REC-example/#NT-Example]</dt><dd><p>matches any string matched by the production defined in the external specification as
              per the provided reference.</p></dd></dl><p>Patterns (including the above constructs) can be combined with grammatical operators to
        form more complex patterns, matching more complex sets of character strings. In the examples
        that follow, <var>A</var> and <var>B</var> represent (sub-)patterns.</p><dl><dt class="label">(A)</dt><dd><p>
                           <code>A</code> is treated as a unit and may be combined as described in this list.</p></dd><dt class="label">A?</dt><dd><p>matches <code>A</code> or nothing; optional <code>A</code>.</p></dd><dt class="label">A B</dt><dd><p>matches <code>A</code> followed by <code>B</code>. This implicit operator has higher
              precedence than the choice operator <code>|</code>; thus <code>A B | C D</code> is 
              interpreted as <code>(A B) | (C D)</code>.</p></dd><dt class="label">A | B</dt><dd><p>matches <code>A</code> or <code>B</code> but not both.</p></dd><dt class="label">A - B</dt><dd><p>matches any string that matches <code>A</code> but does not match <code>B</code>.</p></dd><dt class="label">A+</dt><dd><p>matches one or more occurrences of <code>A</code>. Concatenation has higher
              precedence than choice; thus <code>A+ | B+</code> is identical to <code>(A+) |
                (B+)</code>.</p></dd><dt class="label">A*</dt><dd><p>matches zero or more occurrences of <code>A</code>. Concatenation has higher
              precedence than choice; thus <code>A* | B*</code> is identical to <code>(A*) |
                (B*)</code>
                        </p></dd><dt class="label">(A ++ B)</dt><dd><p>matches one or more occurrences of <code>A</code>, with one occurrence of <code>B</code>
              between adjacent occurrences of <code>A</code>. 
              The notation <code>A ++ B</code> is a shorthand for <code>A (B A)*</code>. The construct
              is always parenthesized to avoid ambiguity, and although in principle
              <code>B</code> could be any pattern, in practice the notation is used only when it is a simple string literal
              (typically but not invariably <code>","</code>).</p><p>For example, <code>(Digit ++ ".")</code> matches <code>1</code> or <code>1.2</code>
            or <code>1.2.3</code>.</p></dd><dt class="label">(A ** B)</dt><dd><p>matches zero or more occurrences of <code>A</code>, with one occurrence of <code>B</code>
              between adjacent occurrences of <code>A</code>.  
              The notation <code>A ** B</code> is a shorthand for <code>(A (B A)*)?</code>. The construct
              is always parenthesized to avoid ambiguity, and although in principle
              <code>B</code> could be any pattern, in practice the notation is used only when it is a simple string literal
              (typically but not invariably <code>","</code>).</p><p>For example, <code>"[" (Digit ** "|") "]"</code> matches <code>[]</code> or <code>[1]</code> or <code>[1|2]</code>
            or <code>[1|2|3]</code>.</p></dd></dl></div><div class="div3">
<h4><a id="extra-grammatical-constraints"/>A.1.2 <a href="#extra-grammatical-constraints" style="text-decoration: none">Extra-grammatical Constraints</a></h4><p>This section contains constraints on the EBNF productions, which are required to parse
        syntactically valid sentences. The notes below are referenced from the right side of the
        production, with the notation: <em>/* xgc: &lt;id&gt; */</em>.</p><div class="constraint"><p class="prefix"><a id="parse-note-leading-lone-slash"/><b>Constraint: leading-lone-slash</b></p><p>A single slash may appear either as a complete path expression or as the first part of a
          path expression in which it is followed by a <a href="#doc-xpath40-RelativePathExpr">RelativePathExpr</a>. In some cases, the next terminal after the slash is insufficient to
          allow a parser to distinguish these two possibilities: a <code>*</code> symbol or a
          keyword like <code>union</code> could be either an operator or a <a href="#prod-xpath40-NameTest">NameTest</a>. For example, the expression <code>/union/*</code> could be parsed
          either as <code>(/) union (/*)</code> or as <code>/child::union/child::*</code> (the
          second interpretation is the one chosen).</p><p>The situation where <code>/</code> is followed by <code>&lt;</code> is a little more
          complicated. In XPath, this is unambiguous: the <code>&lt;</code> can only indicate
          one of the operators <code>&lt;</code>, <code>&lt;=</code>, or <code>&lt;&lt;</code>.
          In XQuery, however, it can also be the start of a direct constructor: specifically,
          a direct constructor for an element node, processing instruction node, or comment node.
          These constructs are identified by the tokenizer, independently of their syntactic
          context, as described in <a href="#lexical-structure"><b>A.3 Lexical structure</b></a>.</p><p>The rule adopted is as follows: if the terminal immediately following a slash
          can form the start of a <a href="#doc-xpath40-RelativePathExpr">RelativePathExpr</a>, then the slash
          must be the beginning of a <a href="#doc-xpath40-PathExpr">PathExpr</a>, not the entirety of it.</p><p>The terminals that can form the start of a <a href="#doc-xpath40-RelativePathExpr">RelativePathExpr</a>
        are: <code>NCName</code>, <code>QName</code>, <code>URIQualifiedName</code>,
          <code>StringLiteral</code>, <code>NumericLiteral</code>, 
          <code>Wildcard</code>, and <code>StringTemplate</code>; 
        plus <code>@</code>
                     <code>.</code>
                     <code>..</code>
                     <code>*</code>
                     <code>$</code>
                     <code>?</code>
                     <code>??</code>
                     <code>%</code>
                     <code>(</code>
                     <code>[</code>; and in XQuery <code>StringConstructor</code> and <code>DirectConstructor</code>.
        </p><p>A single slash may be used as the left-hand argument of an operator by parenthesizing it:
            <code>(/) * 5</code>. The expression <code>5 *
            /</code>, on the other hand, is syntactically valid without parentheses.</p></div><div class="constraint"><p class="prefix"><a id="parse-note-xml-version"/><b>Constraint: xml-version</b></p><p>The version of XML and XML Names (e.g. <a href="#XML">[XML 1.0]</a> and <a href="#XMLNAMES">[XML Names]</a>,
          or <a href="#XML1.1">[XML 1.1]</a> and <a href="#XMLNAMES11">[XML Names 1.1]</a>) is <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>. It is recommended that
          the latest applicable version be used (even if it is published later than this
          specification). The EBNF in this specification links only to the 1.0 versions. Note also
          that these external productions follow the whitespace rules of their respective
          specifications, and not the rules of this specification, in particular <a href="#DefaultWhitespaceHandling"><b>A.3.5.1 Default Whitespace Handling</b></a>. Thus <code>prefix : localname</code> is not a
          syntactically valid <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a> for purposes of this
          specification, just as it is not permitted in a XML document. Also, comments are not
          permissible on either side of the colon. Also extra-grammatical constraints such as
          well-formedness constraints must be taken into account.</p></div><div class="constraint"><p class="prefix"><a id="parse-note-reserved-function-names"/><b>Constraint: reserved-function-names</b></p><p>Unprefixed function names spelled the same way as language keywords could make the
          language impossible to parse. For instance, <code>element(foo)</code> could be taken either as
          a <a href="#doc-xpath40-FunctionCall">FunctionCall</a> or as an <a href="#doc-xpath40-ElementTest">ElementTest</a>. Therefore, an unprefixed function name must not be any of the names in
            <a href="#id-reserved-fn-names"><b>A.4 Reserved Function Names</b></a>.</p><p>A function named <code>if</code> can be called by binding its namespace to a prefix and using the
          prefixed form: <code>library:if(foo)</code> instead of <code>if(foo)</code>.</p></div><div class="constraint"><p class="prefix"><a id="parse-note-occurrence-indicators"/><b>Constraint: occurrence-indicators</b></p><p>As written, the grammar in <a href="#nt-bnf"><b>A XPath 4.0 Grammar</b></a> is ambiguous for some forms using the
          <code>"+"</code>, <code>"?"</code> and <code>"*"</code>
                     <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicators</a>. 
          The ambiguity is resolved as follows: these operators are
          tightly bound to the <a href="#doc-xpath40-SequenceType">SequenceType</a> expression, and have higher
          precedence than other uses of these symbols. Any occurrence of <code>"+"</code>, 
          <code>"?"</code> or <code>"*"</code>, that follows a sequence type is assumed to be an occurrence indicator, which binds to
          the last <a href="#doc-xpath40-ItemType">ItemType</a> in the <a href="#doc-xpath40-SequenceType">SequenceType</a>.</p><p>Thus, <code>4 treat as item() + - 5</code> must be interpreted as <code>(4 treat as item()+) - 5</code>, taking the '+' as an
          occurrence indicator and the '-' as a subtraction operator. To force the interpretation of
          "+" as an addition operator (and the corresponding interpretation of the "-" as a unary
          minus), parentheses may be used: the form <code>(4 treat as item()) +
            -5</code> surrounds the <a href="#doc-xpath40-SequenceType">SequenceType</a> expression with
          parentheses and leads to the desired interpretation.</p><p>
                     <code>function () as xs:string *</code> is interpreted as <code>function () as (xs:string
            *)</code>, not as <code>(function () as xs:string) *</code>. Parentheses can be used as
          shown to force the latter interpretation.</p><p>This rule has as a consequence that certain forms which would otherwise be syntactically
          valid and unambiguous are not recognized: in <code>4 treat as item() + 5</code>, the <code>"+"</code> is taken as
          an <a href="#prod-xpath40-OccurrenceIndicator">OccurrenceIndicator</a>, and not as an operator, which
          means this is not a syntactically valid expression.</p></div></div><div class="div3">
<h4><a id="notes-on-parsing"/>A.1.3 <a href="#notes-on-parsing" style="text-decoration: none">Grammar Notes</a></h4><p>This section contains general notes on the EBNF productions, which may be helpful in
        understanding how to interpret and implement the EBNF. These notes are not normative. The
        notes below are referenced from the right side of the production, with the notation:
          <em>/* gn: &lt;id&gt; */</em>.</p><div class="note"><p class="prefix"><b>Note:</b></p><dl><dt class="label"><a id="parse-note-parens"/>grammar-note: parens</dt><dd><p>Lookahead is required to distinguish a <a href="#doc-xpath40-FunctionCall">FunctionCall</a> from
                an EQName or keyword followed by a 
                <a href="#doc-xpath40-Comment">Comment</a>. For example: <code>address (: this
                  may be empty :)</code> may be mistaken for a call to a function named "address"
                unless this lookahead is employed. Another example is <code>for (:
                  whom the bell :) $tolls in 3 return $tolls</code>, where the keyword "for" must
                not be mistaken for a function name.</p></dd><dt class="label"><a id="parse-note-comments"/>grammar-note: comments</dt><dd><p>Comments are allowed everywhere that <a title="ignorable whitespace" class="termref" href="#IgnorableWhitespace">ignorable
                  whitespace</a> is allowed, and the <a href="#doc-xpath40-Comment">Comment</a> symbol
                does not explicitly appear on the right-hand side of the grammar (except in its own
                production). See <a href="#DefaultWhitespaceHandling"><b>A.3.5.1 Default Whitespace Handling</b></a>. </p><p>A comment can contain nested comments, as long as all <code>"(:"</code> and <code>":)"</code> patterns are
                balanced, no matter where they occur within the outer comment.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Lexical analysis may typically handle nested comments by incrementing a counter
                  for each <code>"(:"</code> pattern, and decrementing the counter for each <code>":)"</code> pattern. The
                  comment does not terminate until the counter is back to zero.</p></div><p>Some illustrative examples:</p><ul><li><p>
                                    <code>(: commenting out a (: comment :) may be confusing, but often helpful
                      :)</code> is a syntactically valid <a href="#doc-xpath40-Comment">Comment</a>, since balanced nesting of comments
                    is allowed.</p></li><li><p>
                                    <code>"this is just a string :)"</code> is a syntactically
                    valid expression. However, <code>(: "this is just a string :)" :)</code> will
                    cause a syntax error. Likewise, <code>"this is another string
                      (:"</code> is a syntactically valid expression, but <code>(: "this is another
                      string (:" :)</code> will cause a syntax error. It is a limitation of nested
                    comments that literal content can cause unbalanced nesting of comments.</p></li><li><p>
                                    <code>for (: set up loop :) $i in $x return $i</code> is
                    syntactically valid, ignoring the comment.</p></li><li><p>
                                    <code>5 instance (: strange place for a comment :) of
                      xs:integer</code> is also syntactically valid.</p></li></ul></dd></dl></div></div></div><div class="div2">
<h3><a id="productions-derived-from-XML"/>A.2 <a href="#productions-derived-from-XML" style="text-decoration: none">Productions Derived from XML</a></h3><p>Some productions are defined by reference to the XML and XML Names specifications (e.g.
      <a href="#XML">[XML 1.0]</a> and <a href="#XMLNAMES">[XML Names]</a>, or <a href="#XML1.1">[XML 1.1]</a> and <a href="#XMLNAMES11">[XML Names 1.1]</a>. <span class="xpath">A host language may choose</span> which version of these specifications is
      used; it is recommended that the latest applicable version be used (even if it is published
      later than this specification).</p><p class="xpath">A <b>host language</b> may choose whether the lexical rules of <a href="#XML">[XML 1.0]</a> and <a href="#XMLNAMES">[XML Names]</a> are followed, or alternatively, the lexical rules
      of <a href="#XML1.1">[XML 1.1]</a> and <a href="#XMLNAMES11">[XML Names 1.1]</a> are followed.</p></div><div class="div2">
<h3><a id="lexical-structure"/>A.3 <a href="#lexical-structure" style="text-decoration: none">Lexical structure</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#id-reserved-fn-names">⬇</a> <a href="#EBNFNotation">⬆</a></p><ol><li><p>
        The rules for tokenization have been largely rewritten. In some cases the revised specification may
        affect edge cases that were handled in different ways by different 3.1 processors, which could lead
        to incompatible behavior.
      <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/327">327</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/519">519</a> 30 May 2023]</i></p></li></ol></div><p>This section describes how an XPath 4.0 text is tokenized prior to parsing.</p><p>All keywords are case sensitive. Keywords are not reserved—that is, any <a title="lexical QName" class="termref" href="#dt-qname">lexical QName</a> may duplicate a keyword except as noted in <a href="#id-reserved-fn-names"><b>A.4 Reserved Function Names</b></a>.</p><p>Tokenizing an input string is a process that follows the following rules:</p><ul><li><p>
                     [<a id="dt-ordinary-production-rule" title="ordinary production rule">Definition</a>: An <b>ordinary production rule</b> 
        is a production rule in <a href="#id-grammar"><b>A.1 EBNF</b></a> that is not annotated <code>ws:explicit</code>.]
                  </p></li><li><p>
                     [<a id="dt-literal-terminal" title="literal terminal">Definition</a>: A <b>literal terminal</b> is a token appearing as a string 
        in quotation marks on the right-hand side of an <a title="ordinary production rule" class="termref" href="#dt-ordinary-production-rule">ordinary production rule</a>.]
                  </p><div class="note"><p class="prefix"><b>Note:</b></p><p>Strings that appear in other production rules do not qualify.
        
          <span class="xpath">For example, <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>
            does not quality because it appears only in <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>, and <code>"0x"</code> does not qualify
            because it appears only in <a href="#prod-xpath40-HexIntegerLiteral">HexIntegerLiteral</a>.</span>
                     </p></div><p>
          The <a title="literal terminal" class="termref" href="#dt-literal-terminal">literal terminals</a>  in XPath 4.0 are: <code>!</code>
                     <code>!=</code>
                     <code>#</code>
                     <code>$</code>
                     <code>(</code>
                     <code>)</code>
                     <code>*</code>
                     <code>+</code>
                     <code>,</code>
                     <code>.</code>
                     <code>..</code>
                     <code>/</code>
                     <code>//</code>
                     <code>:</code>
                     <code>::</code>
                     <code>:=</code>
                     <code>&lt;</code>
                     <code>&lt;&lt;</code>
                     <code>&lt;=</code>
                     <code>=</code>
                     <code>=!&gt;</code>
                     <code>=&gt;</code>
                     <code>&gt;</code>
                     <code>&gt;=</code>
                     <code>&gt;&gt;</code>
                     <code>?</code>
                     <code>??</code>
                     <code>?[</code>
                     <code>@</code>
                     <code>[</code>
                     <code>]</code>
                     <code>{</code>
                     <code>|</code>
                     <code>||</code>
                     <code>}</code>
                     <code>~[</code>
                     <code>×</code>
                     <code>÷</code>
                     <code>%method</code>
                     <code>-</code>
                     <code>-&gt;</code>
                     <code>ancestor</code>
                     <code>ancestor-or-self</code>
                     <code>and</code>
                     <code>array</code>
                     <code>as</code>
                     <code>at</code>
                     <code>attribute</code>
                     <code>cast</code>
                     <code>castable</code>
                     <code>child</code>
                     <code>comment</code>
                     <code>descendant</code>
                     <code>descendant-or-self</code>
                     <code>div</code>
                     <code>document-node</code>
                     <code>element</code>
                     <code>else</code>
                     <code>empty-sequence</code>
                     <code>enum</code>
                     <code>eq</code>
                     <code>every</code>
                     <code>except</code>
                     <code>fn</code>
                     <code>following</code>
                     <code>following-or-self</code>
                     <code>following-sibling</code>
                     <code>following-sibling-or-self</code>
                     <code>for</code>
                     <code>function</code>
                     <code>ge</code>
                     <code>gt</code>
                     <code>idiv</code>
                     <code>if</code>
                     <code>in</code>
                     <code>instance</code>
                     <code>intersect</code>
                     <code>is</code>
                     <code>item</code>
                     <code>items</code>
                     <code>key</code>
                     <code>keys</code>
                     <code>le</code>
                     <code>let</code>
                     <code>lt</code>
                     <code>map</code>
                     <code>member</code>
                     <code>mod</code>
                     <code>namespace</code>
                     <code>namespace-node</code>
                     <code>ne</code>
                     <code>node</code>
                     <code>of</code>
                     <code>or</code>
                     <code>otherwise</code>
                     <code>pairs</code>
                     <code>parent</code>
                     <code>preceding</code>
                     <code>preceding-or-self</code>
                     <code>preceding-sibling</code>
                     <code>preceding-sibling-or-self</code>
                     <code>processing-instruction</code>
                     <code>record</code>
                     <code>return</code>
                     <code>satisfies</code>
                     <code>schema-attribute</code>
                     <code>schema-element</code>
                     <code>self</code>
                     <code>some</code>
                     <code>text</code>
                     <code>then</code>
                     <code>to</code>
                     <code>treat</code>
                     <code>union</code>
                     <code>value</code>
                     <code>values</code>
                  </p></li><li><p>
                     [<a id="dt-variable-terminal" title="variable terminal">Definition</a>: A <b>variable terminal</b> is an instance
        of a production rule that is not itself an <a title="ordinary production rule" class="termref" href="#dt-ordinary-production-rule">ordinary production rule</a> but that is named (directly) on the right-hand
        side of an <a title="ordinary production rule" class="termref" href="#dt-ordinary-production-rule">ordinary production rule</a>.]
                  </p><p>
          The <a title="variable terminal" class="termref" href="#dt-variable-terminal">variable terminals</a> in XPath 4.0 are: <a href="#prod-xpath40-BinaryIntegerLiteral">BinaryIntegerLiteral</a>
                     <a href="#prod-xpath40-DecimalLiteral">DecimalLiteral</a>
                     <a href="#prod-xpath40-DoubleLiteral">DoubleLiteral</a>
                     <a href="#prod-xpath40-HexIntegerLiteral">HexIntegerLiteral</a>
                     <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>
                     <code>NCName</code>
                     <a href="#prod-xpath40-QName">QName</a>
                     <a href="#prod-xpath40-QNameLiteral">QNameLiteral</a>
                     <a href="#prod-xpath40-StringLiteral">StringLiteral</a>
                     <a href="#prod-xpath40-StringTemplate">StringTemplate</a>
                     <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                     <a href="#prod-xpath40-Wildcard">Wildcard</a>
                  </p></li><li><p>
                     [<a id="dt-complex-terminal" title="complex terminal">Definition</a>: A <b>complex terminal</b> is
        a <a title="variable terminal" class="termref" href="#dt-variable-terminal">variable terminal</a> whose production rule references, directly or indirectly, an 
        <a title="ordinary production rule" class="termref" href="#dt-ordinary-production-rule">ordinary production rule</a>.]
                  </p><p>
          The <a title="complex terminal" class="termref" href="#dt-complex-terminal">complex terminals</a>  in XPath 4.0 are: <a href="#prod-xpath40-QNameLiteral">QNameLiteral</a>
                     <a href="#prod-xpath40-StringTemplate">StringTemplate</a>
                  </p><div class="note"><p class="prefix"><b>Note:</b></p><p>The significance of complex terminals is that at one level, a complex terminal is treated as a single
      token, but internally it may contain arbitrary expressions that must be parsed using the full EBNF grammar.</p></div></li><li><p>Tokenization is the process of splitting the supplied input string into a sequence of terminals, where each
      terminal is either a <a title="literal terminal" class="termref" href="#dt-literal-terminal">literal terminal</a> or a <a title="variable terminal" class="termref" href="#dt-variable-terminal">variable terminal</a> (which may itself
      be a <a title="complex terminal" class="termref" href="#dt-complex-terminal">complex terminal</a>). Tokenization is done by repeating the following steps:</p><ol class="enumar"><li><p>Starting at the current position, skip any whitespace and comments.</p></li><li><p>If the current position is not the end of the input, then
          return the longest <a title="literal terminal" class="termref" href="#dt-literal-terminal">literal terminal</a> or <a title="variable terminal" class="termref" href="#dt-variable-terminal">variable terminal</a> 
        that can be matched starting at the current position, regardless whether this terminal is valid at this point
        in the grammar. If no such terminal can be identified starting at the current position, or if the terminal that
        is identified is not a valid continuation of the grammar rules, then a syntax error is reported.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Here are some examples showing the effect of the longest token rule:</p><ul><li><p>The expression <code>map{a:b}</code> is a syntax error. Although there is a 
              tokenization of this string that satisfies the grammar (by treating <code>a</code> and <code>b</code>
              as separate expressions), this tokenization does not satisfy the longest token rule,
              which requires that <code>a:b</code> is interpreted as a single <code>QName</code>.</p></li><li><p>The expression <code>10 div3</code> is a syntax error. The longest token rule requires that this
              be interpreted as two tokens (<code>"10"</code> and <code>"div3"</code>) even though it would
              be a valid expression if treated as three tokens (<code>"10"</code>, <code>"div"</code>, and <code>"3"</code>).</p></li><li><p>The expression <code>$x-$y</code> is a syntax error. This is interpreted as four tokens,
              (<code>"$"</code>, <code>"x-"</code>, <code>"$"</code>, and <code>"y"</code>).</p></li></ul></div><div class="note"><p class="prefix"><b>Note:</b></p><p>The lexical production rules for <a title="variable terminal" class="termref" href="#dt-variable-terminal">variable terminals</a>
        have been designed so that there is minimal need for backtracking. For example, if the next terminal
        starts with <code>"0x"</code>, then it can only be either a <a href="#prod-xpath40-HexIntegerLiteral">HexIntegerLiteral</a> or an error;
        if it starts with <code>"`"</code> (and not with <code>"```"</code>) then it can only be a
          <a href="#doc-xpath40-StringTemplate">StringTemplate</a> or an error. </p><p>This convention, together with the rules for whitespace separation of tokens (see <a href="#id-terminal-delimitation"><b>A.3.2 Terminal Delimitation</b></a>) 
          means that the longest-token rule does not normally result in any need for backtracking. For example, suppose 
          that a <a title="variable terminal" class="termref" href="#dt-variable-terminal">variable terminal</a> has been identified as a <a href="#doc-xpath40-StringTemplate">StringTemplate</a> by examining
          its first few characters. If the construct turns out not to be a valid <a href="#doc-xpath40-StringTemplate">StringTemplate</a>, 
          an error can be reported without first considering whether there is some shorter token that might be returned instead.</p></div></li></ol></li><li><p>Tokenization unambiguously identifies the boundaries of the terminals in the input, and this
        can be achieved without backtracking or lookahead. However, tokenization does
      not unambiguously classify each terminal. For example, it might identify the string <code>"div"</code> as a terminal, but it does not
      resolve whether this is the operator symbol <code>div</code>, or an <code>NCName</code> 
        or <code>QName</code> used as a 
      node test or as a variable or function name. Classification of terminals generally requires information about the
      grammatical context, and in some cases requires lookahead.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Operationally, classification of terminals may be done either in the tokenizer or the parser, or
      in some combination of the two. For example, according to the EBNF, the expression 
        <code>"parent::x"</code> is made up of three
      tokens, <code>"parent"</code>, <code>"::"</code>, and <code>"x"</code>. The name <code>"parent"</code>
        can be classified as an axis name as soon as the following token <code>"::"</code> is recognized, and this
      might be done either in the tokenizer or in the parser. (Note that whitespace and comments are allowed
      both before and after <code>"::"</code>.)</p></div></li><li><p>In the case of a <a title="complex terminal" class="termref" href="#dt-complex-terminal">complex terminal</a>, identifying the end of the complex terminal
      typically involves invoking the parser to process any embedded expressions. Tokenization, as described
      here, is therefore a recursive process. But other implementations are possible.</p></li></ul><div class="note"><p class="prefix"><b>Note:</b></p><p>Previous versions of this specification included the statement: 
      <em>When tokenizing, the longest possible match that is consistent with the EBNF is used.</em>
               </p><p>Different processors are known to have interpreted this in different ways. One interpretation,
      for example, was that the expression <code>10 div-3</code> should be split into four tokens (<code>10</code>,
      <code>div</code>, <code>-</code>, <code>3</code>) on the grounds that any other tokenization would give a
      result that was inconsistent with the EBNF grammar. Other processors report a syntax error on this example.</p><p>This rule has therefore been rewritten in version 4.0. Tokenization is now entirely insensitive to the
      grammatical context; <code>div-3</code> is recognized as a single token even though this results in a syntax
      error. For some implementations this may mean that expressions that were accepted in earlier releases
      are no longer accepted in 4.0.</p></div><div class="div3">
<h4><a id="terminal-symbols"/>A.3.1 <a href="#terminal-symbols" style="text-decoration: none">Terminal Symbols</a></h4>
<a id="d3e36739"/><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-IntegerLiteral"/><code>IntegerLiteral</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-Digits">Digits</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-HexIntegerLiteral"/><code>HexIntegerLiteral</code></td><td>::=</td><td><code>"0x"  <a href="#prod-xpath40-HexDigits">HexDigits</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-BinaryIntegerLiteral"/><code>BinaryIntegerLiteral</code></td><td>::=</td><td><code>"0b"  <a href="#prod-xpath40-BinaryDigits">BinaryDigits</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-DecimalLiteral"/><code>DecimalLiteral</code></td><td>::=</td><td><code>("."  <a href="#prod-xpath40-Digits">Digits</a>)  |  (<a href="#prod-xpath40-Digits">Digits</a>  "."  <a href="#prod-xpath40-Digits">Digits</a>?)</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-DoubleLiteral"/><code>DoubleLiteral</code></td><td>::=</td><td><code>(("."  <a href="#prod-xpath40-Digits">Digits</a>)  |  (<a href="#prod-xpath40-Digits">Digits</a>  ("."  <a href="#prod-xpath40-Digits">Digits</a>?)?))  [eE]  [+-]?  <a href="#prod-xpath40-Digits">Digits</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-StringLiteral"/><code><a href="#doc-xpath40-StringLiteral">StringLiteral</a></code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>  |  <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-AposStringLiteral"/><code>AposStringLiteral</code></td><td>::=</td><td><code>"'"  (<a href="#prod-xpath40-EscapeApos">EscapeApos</a>  |  [^'])*  "'"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-QuotStringLiteral"/><code>QuotStringLiteral</code></td><td>::=</td><td><code>'"'  (<a href="#prod-xpath40-EscapeQuot">EscapeQuot</a>  |  [^"])*  '"'</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-URIQualifiedName"/><code>URIQualifiedName</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>
                        <a href="#prod-xpath40-NCName">NCName</a>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-BracedURILiteral"/><code>BracedURILiteral</code></td><td>::=</td><td><code>"Q"  "{"  [^{}]*  "}"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-EscapeQuot"/><code>EscapeQuot</code></td><td>::=</td><td><code>'""'</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-EscapeApos"/><code>EscapeApos</code></td><td>::=</td><td><code>"''"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Comment"/><code><a href="#doc-xpath40-Comment">Comment</a></code></td><td>::=</td><td><code>"(:"  (<a href="#prod-xpath40-CommentContents">CommentContents</a>  |  <a href="#prod-xpath40-Comment">Comment</a>)*  ":)"</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-comments">gn: comments</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-QName"/><code>QName</code></td><td>::=</td><td><code>
                        <a href="https://www.w3.org/TR/REC-xml-names/#NT-QName">[http://www.w3.org/TR/REC-xml-names/#NT-QName]</a><sup><small>Names</small></sup>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-xml-version">xgc: xml-version</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-NCName"/><code>NCName</code></td><td>::=</td><td><code>
                        <a href="https://www.w3.org/TR/REC-xml-names/#NT-NCName">[http://www.w3.org/TR/REC-xml-names/#NT-NCName]</a><sup><small>Names</small></sup>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-xml-version">xgc: xml-version</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Char"/><code>Char</code></td><td>::=</td><td><code>
                        <a href="https://www.w3.org/TR/REC-xml/#NT-Char">[http://www.w3.org/TR/REC-xml#NT-Char]</a><sup><small>XML</small></sup>
                     </code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#parse-note-xml-version">xgc: xml-version</a>
                      */</td></tr></tbody></table><p>The following symbols are used only in the definition of terminal symbols; they are not
        terminal symbols in the grammar of <a href="#id-grammar"><b>A.1 EBNF</b></a>.</p>
<a id="d3e36973"/><table class="scrap"><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-Digits"/><code>Digits</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-DecDigit">DecDigit</a>  ((<a href="#prod-xpath40-DecDigit">DecDigit</a>  |  "_")*  <a href="#prod-xpath40-DecDigit">DecDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-DecDigit"/><code>DecDigit</code></td><td>::=</td><td><code>[0-9]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-HexDigits"/><code>HexDigits</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-HexDigit">HexDigit</a>  ((<a href="#prod-xpath40-HexDigit">HexDigit</a>  |  "_")*  <a href="#prod-xpath40-HexDigit">HexDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-HexDigit"/><code>HexDigit</code></td><td>::=</td><td><code>[0-9a-fA-F]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-BinaryDigits"/><code>BinaryDigits</code></td><td>::=</td><td><code>
                        <a href="#prod-xpath40-BinaryDigit">BinaryDigit</a>  ((<a href="#prod-xpath40-BinaryDigit">BinaryDigit</a>  |  "_")*  <a href="#prod-xpath40-BinaryDigit">BinaryDigit</a>)?</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-BinaryDigit"/><code>BinaryDigit</code></td><td>::=</td><td><code>[01]</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody><tbody><tr style="vertical-align:baseline;"><td><a id="prod-xpath40-CommentContents"/><code>CommentContents</code></td><td>::=</td><td><code>(<a href="#prod-xpath40-Char">Char</a>+ - (Char* ('(:' | ':)') Char*))</code></td></tr><tr><td/><td/><td style="text-align: right;" class="prodComment">/* 
                        <a href="#ws-explicit">ws: explicit</a>
                      */</td></tr></tbody></table></div><div class="div3">
<h4><a id="id-terminal-delimitation"/>A.3.2 <a href="#id-terminal-delimitation" style="text-decoration: none">Terminal Delimitation</a></h4><p>XPath 4.0 expressions consist of <a href="#terminal-symbols">terminal symbols</a> and
          <a title="symbol separators" class="termref" href="#symbolseparators">symbol separators</a>.</p><p>
                  <span>
                     <a title="literal terminal" class="termref" href="#dt-literal-terminal">Literal</a> 
        and <a title="variable terminal" class="termref" href="#dt-variable-terminal">variable</a>
                  </span>
        terminal symbols are of two kinds: delimiting and non-delimiting.</p><p>
                  [<a id="delimiting-token" title="delimiting terminal symbol">Definition</a>: The <b>delimiting
            terminal symbols</b> are: <code>!</code>
                     <code>!=</code>
                     <code>#</code>
                     <code>$</code>
                     <code>%method</code>
                     <code>(</code>
                     <code>)</code>
                     <code>*</code>
                     <code>*:</code>
                     <code>+</code>
                     <code>,</code>
                     <code>-</code>
                     <code>-&gt;</code>
                     <code>.</code>
                     <code>..</code>
                     <code>/</code>
                     <code>//</code>
                     <code>:</code>
                     <code>:*</code>
                     <code>::</code>
                     <code>:=</code>
                     <code>&lt;</code>
                     <code>&lt;&lt;</code>
                     <code>&lt;=</code>
                     <code>=</code>
                     <code>=!&gt;</code>
                     <code>=&gt;</code>
                     <code>&gt;</code>
                     <code>&gt;=</code>
                     <code>&gt;&gt;</code>
                     <code>?</code>
                     <code>??</code>
                     <code>?[</code>
                     <code>@</code>
                     <code>[</code>
                     <code>]</code>
                     <code>`</code>
                     <code>``</code>
                     <code>{</code>
                     <code>{{</code>
                     <code>|</code>
                     <code>||</code>
                     <code>}</code>
                     <code>}}</code>
                     <code>~[</code>
                     <code>×</code>
                     <code>÷</code>
                     <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>
                     <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>
                     <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                     <a href="#prod-xpath40-StringLiteral">StringLiteral</a>
                  ]
               </p><p>
                  [<a id="non-delimiting-token" title="non-delimiting terminal symbol">Definition</a>: The
            <b>non-delimiting terminal symbols</b> are: <code>ancestor</code>
                     <code>ancestor-or-self</code>
                     <code>and</code>
                     <code>array</code>
                     <code>as</code>
                     <code>at</code>
                     <code>attribute</code>
                     <code>cast</code>
                     <code>castable</code>
                     <code>child</code>
                     <code>comment</code>
                     <code>descendant</code>
                     <code>descendant-or-self</code>
                     <code>div</code>
                     <code>document-node</code>
                     <code>element</code>
                     <code>else</code>
                     <code>empty-sequence</code>
                     <code>enum</code>
                     <code>eq</code>
                     <code>every</code>
                     <code>except</code>
                     <code>fn</code>
                     <code>following</code>
                     <code>following-or-self</code>
                     <code>following-sibling</code>
                     <code>following-sibling-or-self</code>
                     <code>for</code>
                     <code>function</code>
                     <code>ge</code>
                     <code>gt</code>
                     <code>idiv</code>
                     <code>if</code>
                     <code>in</code>
                     <code>instance</code>
                     <code>intersect</code>
                     <code>is</code>
                     <code>item</code>
                     <code>items</code>
                     <code>key</code>
                     <code>keys</code>
                     <code>le</code>
                     <code>let</code>
                     <code>lt</code>
                     <code>map</code>
                     <code>member</code>
                     <code>mod</code>
                     <code>namespace</code>
                     <code>namespace-node</code>
                     <code>ne</code>
                     <code>node</code>
                     <code>of</code>
                     <code>or</code>
                     <code>otherwise</code>
                     <code>pairs</code>
                     <code>parent</code>
                     <code>preceding</code>
                     <code>preceding-or-self</code>
                     <code>preceding-sibling</code>
                     <code>preceding-sibling-or-self</code>
                     <code>processing-instruction</code>
                     <code>record</code>
                     <code>return</code>
                     <code>satisfies</code>
                     <code>schema-attribute</code>
                     <code>schema-element</code>
                     <code>self</code>
                     <code>some</code>
                     <code>text</code>
                     <code>then</code>
                     <code>to</code>
                     <code>treat</code>
                     <code>union</code>
                     <code>value</code>
                     <code>values</code>
                     <a href="#prod-xpath40-BinaryIntegerLiteral">BinaryIntegerLiteral</a>
                     <a href="#prod-xpath40-DecimalLiteral">DecimalLiteral</a>
                     <a href="#prod-xpath40-DoubleLiteral">DoubleLiteral</a>
                     <a href="#prod-xpath40-HexIntegerLiteral">HexIntegerLiteral</a>
                     <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>
                     <a href="#prod-xpath40-NCName">NCName</a>
                     <a href="#prod-xpath40-QName">QName</a>
                     <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                  ]
               </p><p>
                  [<a id="symbolseparators" title="symbol separators">Definition</a>: 
                     <a title="whitespace" class="termref" href="#Whitespace">Whitespace</a> and <a href="#doc-xpath40-Comment">Comments</a>
          function as <b>symbol separators</b>. For the most part, they are not mentioned in
          the grammar, and may occur between any two terminal symbols mentioned in the grammar,
          except where that is forbidden by the <a href="#ws-explicit">/* ws: explicit */</a>
          annotation in the EBNF, or by the <a href="#parse-note-xml-version">/* xgc: xml-version
            */</a> annotation.]
               </p><p>
                  <span>As a consequence of the longest token rule (see <a href="#lexical-structure"><b>A.3 Lexical structure</b></a>), </span>
        one or more <a title="symbol separators" class="termref" href="#symbolseparators">symbol separators</a>
        are required between two consecutive terminal symbols
        <var>T</var> and <var>U</var> (where <var>T</var> precedes <var>U</var>) when any of the following is true:
      </p><ul><li><p>
                        <var>T</var> and <var>U</var> are both <a title="non-delimiting terminal symbol" class="termref" href="#non-delimiting-token">non-delimiting terminal symbols</a>.</p></li><li><p>
                        <var>T</var> is a QName or an NCName and <var>U</var> is <code>"."</code> or <code>"-"</code>.</p></li><li><p>
                        <var>T</var> is a numeric literal and <var>U</var> is <code>"."</code>, or vice versa.</p></li></ul></div><div class="div3">
<h4><a id="id-lt-and-gt-characters"/>A.3.3 <a href="#id-lt-and-gt-characters" style="text-decoration: none">Less-Than and Greater-Than Characters</a></h4><p>The operator symbols <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code>,
        <code>&lt;&lt;</code>, <code>&gt;&gt;</code>, <code>=&gt;</code>, <code>-&gt;</code>, <code>=!&gt;</code>, and <code>=?&gt;</code>
        have alternative representations using the characters <span class="unicode-codepoint">U+FF1C</span> (<span class="unicode-name">FULL-WIDTH LESS-THAN SIGN</span>, <code>＜</code>)  and
        <span class="unicode-codepoint">U+FF1E</span> (<span class="unicode-name">FULL-WIDTH GREATER-THAN SIGN</span>, <code>＞</code>)  in place of <span class="unicode-codepoint">U+003C</span> (<span class="unicode-name">LESS-THAN SIGN</span>, <code>&lt;</code>)  
        and <span class="unicode-codepoint">U+003E</span> (<span class="unicode-name">GREATER-THAN SIGN</span>, <code>&gt;</code>) . The alternative tokens are respectively
        <code>＜</code>, <code>＜=</code>, <code>＞</code>, <code>＞=</code>,
        <code>＜＜</code>, <code>＞＞</code>, <code>=＞</code>, 
        and <code>=!＞</code>.
        In order to avoid visual confusion these alternatives are not shown explicitly in the grammar.</p><p>This option is provided to improve the readability of XPath expressions embedded in XML-based host languages such as XSLT; it
      enables these operators to be depicted using characters that do not require escaping as XML entities or character references.</p></div><div class="div3">
<h4><a id="id-eol-handling"/>A.3.4 <a href="#id-eol-handling" style="text-decoration: none">End-of-Line Handling</a></h4><p class="xpath">The host language must specify whether the XPath 4.0 processor normalizes all
        line breaks on input, before parsing, and if it does so, whether it uses the rules of
          <a href="#XML">[XML 1.0]</a> or <a href="#XML1.1">[XML 1.1]</a>. </p><div class="note"><p class="prefix"><b>Note:</b></p><p>XML-based host languages such as XSLT and XSD
      do not normalize line breaks at the XPath level, because it will already have been done by the host XML parser. 
      Use of character or entity references suppresses normalization of line breaks, so
      the string literal <code>&amp;#x0D;</code> written within an XSLT-hosted XPath expression
      represents a string containing a single <span class="unicode-codepoint">U+000D</span> (<span class="unicode-name">CARRIAGE RETURN</span>)  character.</p></div><div class="div4">
<h5><a id="id-xml10-eol-handling"/>A.3.4.1 <a href="#id-xml10-eol-handling" style="text-decoration: none">XML 1.0 End-of-Line Handling</a></h5><p>For <a href="#XML">[XML 1.0]</a> processing, all of the following must be translated to a single
          <span class="unicode-codepoint">U+000A</span> (<span class="unicode-name">NEWLINE</span>) :</p><ol class="enumar"><li><p>the two-character sequence <span class="unicode-codepoint">U+000D</span> (<span class="unicode-name">CARRIAGE RETURN</span>) , <span class="unicode-codepoint">U+000A</span> (<span class="unicode-name">NEWLINE</span>) ;</p></li><li><p>any <span class="unicode-codepoint">U+000D</span> (<span class="unicode-name">CARRIAGE RETURN</span>)  character that is not immediately followed by <span class="unicode-codepoint">U+000A</span> (<span class="unicode-name">NEWLINE</span>) .</p></li></ol></div><div class="div4">
<h5><a id="id-xml11-eol-handling"/>A.3.4.2 <a href="#id-xml11-eol-handling" style="text-decoration: none">XML 1.1 End-of-Line Handling</a></h5><p>For <a href="#XML1.1">[XML 1.1]</a> processing, all of the following must be translated to a
          single <span class="unicode-codepoint">U+000A</span> (<span class="unicode-name">NEWLINE</span>)  character:</p><ol class="enumar"><li><p>the two-character sequence <span class="unicode-codepoint">U+000D</span> (<span class="unicode-name">CARRIAGE RETURN</span>) , <span class="unicode-codepoint">U+000A</span> (<span class="unicode-name">NEWLINE</span>) ;</p></li><li><p>the two-character sequence <span class="unicode-codepoint">U+000D</span> (<span class="unicode-name">CARRIAGE RETURN</span>) , <span class="unicode-codepoint">U+0085</span> (<span class="unicode-name">NEXT LINE, NEL</span>) ;</p></li><li><p>the single character <span class="unicode-codepoint">U+0085</span> (<span class="unicode-name">NEXT LINE, NEL</span>) ;</p></li><li><p>the single character <span class="unicode-codepoint">U+2028</span> (<span class="unicode-name">LINE SEPARATOR</span>) ;</p></li><li><p>any <span class="unicode-codepoint">U+000D</span> (<span class="unicode-name">CARRIAGE RETURN</span>)  character that is not immediately followed by <span class="unicode-codepoint">U+000A</span> (<span class="unicode-name">NEWLINE</span>)  or <span class="unicode-codepoint">U+0085</span> (<span class="unicode-name">NEXT LINE, NEL</span>) .</p></li></ol></div></div><div class="div3">
<h4><a id="whitespace-rules"/>A.3.5 <a href="#whitespace-rules" style="text-decoration: none">Whitespace Rules</a></h4><div class="div4">
<h5><a id="DefaultWhitespaceHandling"/>A.3.5.1 <a href="#DefaultWhitespaceHandling" style="text-decoration: none">Default Whitespace Handling</a></h5><p>
                     [<a id="Whitespace" title="whitespace">Definition</a>: A <b>whitespace</b> character is any of
            the characters defined by <a href="http://www.w3.org/TR/REC-xml/#NT-S">
              [http://www.w3.org/TR/REC-xml/#NT-S]</a>.]
                  </p><p>
                     [<a id="IgnorableWhitespace" title="ignorable whitespace">Definition</a>: 
                        <b>Ignorable whitespace</b> consists of any <a title="whitespace" class="termref" href="#Whitespace">whitespace</a> characters that may occur between <a title="terminal" class="termref" href="#terminal">terminals</a>, unless these characters occur in the context of a production
            marked with a <a href="#ExplicitWhitespaceHandling"> ws:explicit</a> annotation, in
            which case they can occur only where explicitly specified (see <a href="#ExplicitWhitespaceHandling"><b>A.3.5.2 Explicit Whitespace Handling</b></a>).] Ignorable whitespace characters are not
          significant to the semantics of an expression. Whitespace is allowed before the first
          terminal and after the last terminal <span class="xpath">of an XPath expression</span>. Whitespace is allowed between any two <a title="terminal" class="termref" href="#terminal">terminals</a>. <a href="#doc-xpath40-Comment">Comments</a> may also act as
          "whitespace" to prevent two adjacent terminals from being recognized as one. Some
          illustrative examples are as follows:</p><ul><li><p>
                           <code>foo- foo</code> results in a syntax error. "foo-" would be recognized as a
              QName.</p></li><li><p>
                           <code>foo -foo</code> is syntactically equivalent to <code>foo - foo</code>, two QNames separated by a subtraction
              operator.</p></li><li><p>
                           <code>foo(: This is a comment :)- foo</code> is syntactically
              equivalent to <code>foo - foo</code>. This is because the comment prevents the two
              adjacent terminals from being recognized as one.</p></li><li><p>
                           <code>foo-foo</code> is syntactically equivalent to single QName.
              This is because "-" is a valid character in a QName. When used as an operator after
              the characters of a name, the "-" must be separated from the name, e.g. by using
              whitespace or parentheses.</p></li><li><p>
                           <code>10div 3</code> results in a syntax error.</p></li><li><p>
                           <code>10 div3</code> also results in a syntax error.</p></li><li><p>
                           <code>10div3</code> also results in a syntax error.</p></li></ul></div><div class="div4">
<h5><a id="ExplicitWhitespaceHandling"/>A.3.5.2 <a href="#ExplicitWhitespaceHandling" style="text-decoration: none">Explicit Whitespace Handling</a></h5><p>Explicit whitespace notation is specified with the EBNF productions, when it is different
          from the default rules, using the notation shown below. This notation is not inherited. In
          other words, if an EBNF rule is marked as /* ws: explicit */, the notation does not
          automatically apply to all the 'child' EBNF productions of that rule.</p><dl><dt class="label"><a id="ws-explicit"/>ws: explicit</dt><dd><p>/* ws: explicit */ means that the EBNF notation explicitly notates, with
                  <code>S</code> or otherwise, where <a title="whitespace" class="termref" href="#Whitespace">whitespace
                  characters</a> are allowed. In productions with the /* ws: explicit */
                  annotation, <a href="#DefaultWhitespaceHandling"><b>A.3.5.1 Default Whitespace Handling</b></a> does not apply.
		  Comments are not allowed in these productions except where the <a href="#doc-xpath40-Comment">Comment</a> non-terminal appears.
	      </p></dd></dl></div></div></div><div class="div2">
<h3><a id="id-reserved-fn-names"/>A.4 <a href="#id-reserved-fn-names" style="text-decoration: none">Reserved Function Names</a></h3><div class="changes"><p class="changesHeading">Changes in 4.0 <a href="#lexical-structure">⬆</a></p><ol><li><p>
          XPath 3.0 included <code>empty-sequence</code> and <code>item</code>
            as reserved function names, and XPath 3.1 added <code>map</code> and <code>array</code>.
            This was unnecessary since these names never appear followed by a left parenthesis
            at the start of an expression. They have therefore been removed from the list.
            New keywords introducing item types, such as <code>record</code> and <code>enum</code>,
            have not been included in the list.
      <i>  [Issue <a href="https://github.com/qt4cg/qtspecs/issues/1208">1208</a> PR <a href="https://github.com/qt4cg/qtspecs/pull/1212">1212</a> 15 May 2024]</i></p></li></ol></div><p>The following names are not allowed as function names in an unprefixed form, because they
    can appear, followed by a left parenthesis, at the start of an XPath or XQuery expression that
    is not a function call.</p><p>Names used in <a href="#prod-xpath40-KindTest">KindTests</a>:</p><blockquote><p>attribute<br/>comment<br/>document-node<br/>element<br/>namespace-node<br/>node<br/>schema-attribute<br/>schema-element<br/>processing-instruction<br/>text</p></blockquote><p>Names used as syntactic keywords:</p><blockquote><p>fn<br/>function<br/>if<br/>switch<br/>typeswitch</p></blockquote><div class="note"><p class="prefix"><b>Note:</b></p><p> Although the keywords <code>switch</code> and <code>typeswitch</code> are not used in
        XPath, they are considered reserved function names for compatibility with XQuery. </p></div><div class="note"><p class="prefix"><b>Note:</b></p><p>As the language evolves in the future, it may become necessary to reserve additional
      names. Furthermore, use of common programming terms like <code>return</code> and
      <code>while</code> as function names may cause confusion even though they are not reserved. 
        The easiest way to avoid problems is to use an explicit namespace prefix in all calls 
        to user-defined functions.</p></div></div><div class="div2">
<h3><a id="id-precedence-order"/>A.5 <a href="#id-precedence-order" style="text-decoration: none">Precedence Order (Non-Normative)</a></h3><p>The grammar in <a href="#id-grammar"><b>A.1 EBNF</b></a> normatively defines built-in precedence among the
      operators of <span class="xpath">XPath</span>. These operators are summarized here to make clear the order of their
      precedence from lowest to highest. The associativity column indicates the order in which
      operators of equal precedence in an expression are applied (further details below).</p><table class="medium"><tbody><tr><th>#</th><th>Operator</th><th>Associativity</th></tr><tr><td>1</td><td>
                        <a href="#prod-xpath40-Expr">, (comma)</a>
                     </td><td>associative</td></tr><tr><td>2</td><td>
                        <span class="xpath">
                           <a href="#doc-xpath40-ForExpr">for</a>,</span>
                        <span class="xpath">
                           <a href="#doc-xpath40-LetExpr">let</a>,</span>
                        <a href="#doc-xpath40-QuantifiedExpr">some, every</a>, 
            <a href="#doc-xpath40-IfExpr">if</a>
                     </td><td>NA</td></tr><tr><td>3</td><td>
                        <a href="#doc-xpath40-OrExpr">or</a>
                     </td><td>associative</td></tr><tr><td>4</td><td>
                        <a href="#prod-xpath40-AndExpr">and</a>
                     </td><td>associative</td></tr><tr><td>5</td><td>
                        <a href="#prod-xpath40-ValueComp">eq, ne, lt, le, gt, ge</a>, <a href="#prod-xpath40-GeneralComp">=, !=, &lt;,
              &lt;=, &gt;, &gt;=</a>, <a href="#prod-xpath40-NodeComp">is, &lt;&lt;, &gt;&gt;</a>
                     </td><td>NA</td></tr><tr><td>6</td><td>
                        <a href="#doc-xpath40-OtherwiseExpr">otherwise</a>
                     </td><td>associative</td></tr><tr><td>7</td><td>
                        <a href="#doc-xpath40-StringConcatExpr">||</a>
                     </td><td>associative</td></tr><tr><td>8</td><td>
                        <a href="#doc-xpath40-RangeExpr">to</a>
                     </td><td>NA</td></tr><tr><td>9</td><td>
                        <a href="#doc-xpath40-AdditiveExpr">+, - (binary)</a>
                     </td><td>left-to-right</td></tr><tr><td>10</td><td>
                        <a href="#prod-xpath40-MultiplicativeExpr">*, div, idiv, mod</a>
                     </td><td>left-to-right</td></tr><tr><td>11</td><td>
                        <a href="#doc-xpath40-UnionExpr">union, |</a>
                     </td><td>associative</td></tr><tr><td>12</td><td>
                        <a href="#prod-xpath40-IntersectExceptExpr">intersect, except</a>
                     </td><td>left-to-right</td></tr><tr><td>13</td><td>
                        <a href="#doc-xpath40-InstanceofExpr">instance of</a>
                     </td><td>NA</td></tr><tr><td>14</td><td>
                        <a href="#doc-xpath40-TreatExpr">treat as</a>
                     </td><td>NA</td></tr><tr><td>15</td><td>
                        <a href="#doc-xpath40-CastableExpr">castable as</a>
                     </td><td>NA</td></tr><tr><td>16</td><td>
                        <a href="#doc-xpath40-CastExpr">cast as</a>
                     </td><td>NA</td></tr><tr><td>17</td><td>
                        <a href="#doc-xpath40-ArrowExpr">=&gt;, =!&gt;</a>
                     </td><td>left-to-right</td></tr><tr><td>18</td><td>
                        <a href="#doc-xpath40-UnaryExpr">-, + (unary)</a>
                     </td><td>right-to-left</td></tr><tr><td>19</td><td>
                        <a href="#doc-xpath40-SimpleMapExpr">!</a>
                     </td><td>left-to-right</td></tr><tr><td>20</td><td>
                        <a href="#doc-xpath40-PathExpr">/, //</a>
                     </td><td>left-to-right</td></tr><tr><td>21</td><td>
                        <a href="#prod-xpath40-Predicate">a[b]</a>, <a href="#doc-xpath40-FilterExprAM">a?[b]</a>, <a href="#prod-xpath40-Lookup">a?b, a??b</a>,
            <a href="#doc-xpath40-DynamicFunctionCall">a(b)</a>
                     </td><td>left-to-right</td></tr><tr><td>22</td><td>
                        <a href="#doc-xpath40-UnaryLookup">? (unary)</a>
                     </td><td>NA</td></tr></tbody></table><p>Entries in the <b>Associativity</b> column have the following meaning (where the symbol <code>⊙</code>
      represents any operator):</p><ul><li><p>
                     <b>associative</b> means that the order of evaluation is immaterial: for example <code>a, b, c</code>
      can be evaluated either as <code>(a, b), c</code> or as <code>a, (b, c)</code>, producing the same
      result either way.</p></li><li><p>
                     <b>NA</b> indicates that it is not possible to write an expression of the form <code>a ⊙ b ⊙ c</code>
      for this particular operator: for example, the grammar does not allow <code>a = b = c</code>.</p></li><li><p>
                     <b>left-to-right</b> means that for expressions using these operators, 
      <code>a ⊙ b ⊙ c</code> is evaluated as <code>(a ⊙ b) ⊙ c</code>. As a special case, the operators <code>+</code>
      and <code>*</code> are associative provided they are not mixed with other operators of the same precedence.</p></li><li><p>
                     <b>right-to-left</b> is used only for unary operators, and indicates that <code>⊙ ⊙ a</code>
      is evaluated as <code>⊙ (⊙ a)</code>
                  </p></li></ul><p>These rules do not constrain the order in which the operands of an expression are evaluated (which might affect
    error behavior). See also <a href="#errors"><b>2.4 Error Handling</b></a>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>Parentheses can be used to override the operator precedence in the usual way. Square
        brackets in an expression such as <code>A[B]</code>
                  <code>A?[B]</code>or serve two roles: they act as an operator causing B to
        be evaluated once for each item in the value of A, and they act as parentheses enclosing the
        expression B.</p></div></div></div><div class="div1 xpath">
<h2><a id="id-xp-context-components"/>B <a href="#id-xp-context-components" style="text-decoration: none">Context Components</a></h2><p>The tables in this
section describe the scope (range of applicability) of the various
components in a module's static context and dynamic context.</p><div class="div2">
<h3><a id="id-xp-static-context-components"/>B.1 <a href="#id-xp-static-context-components" style="text-decoration: none">Static Context
Components</a></h3><p>The following table describes the components of
the <b>static context</b>. For each component, “global”
indicates that the value of the component applies throughout an XPath
expression, whereas “lexical” indicates that the value of the
component applies only within the subexpression in which it is
defined.</p><table style="width:60%; border:1px solid" class="small"><caption>Static Context Components</caption><tbody><tr><th>Component</th><th>Scope</th></tr><tr><td>XPath 1.0 Compatibility Mode</td><td>global</td></tr><tr><td>Statically known namespaces</td><td>global</td></tr><tr><td>Default element/type namespace</td><td>global</td></tr><tr><td>Default function namespace</td><td>global</td></tr><tr><td>In-scope schema types</td><td>global</td></tr><tr><td>In-scope element declarations</td><td>global</td></tr><tr><td>In-scope attribute declarations</td><td>global</td></tr><tr><td>In-scope variables</td><td>lexical; for-expressions, let-expressions, and quantified expressions can bind new variables</td></tr><tr><td>Context value static type</td><td>lexical</td></tr><tr><td>Statically known function signatures</td><td>global</td></tr><tr><td>Statically known collations</td><td>global</td></tr><tr><td>Default collation</td><td>global</td></tr><tr><td>Base URI</td><td>global</td></tr><tr><td>Statically known documents</td><td>global</td></tr><tr><td>Statically known collections</td><td>global  </td></tr><tr><td>Statically known default collection type</td><td>global</td></tr></tbody></table></div><div class="div2">
<h3><a id="id-xp-evaluation-context-components"/>B.2 <a href="#id-xp-evaluation-context-components" style="text-decoration: none">Dynamic Context Components</a></h3><p>The following table describes how values are assigned to the various components of the
    <b>dynamic context</b>. All these components are initialized by mechanisms
    defined by the host language. For each component, “global” indicates that the value
    of the component remains constant throughout evaluation of the XPath expression,
    whereas “dynamic” indicates that the value of the component can be modified by the
    evaluation of subexpressions.</p><table style="width:60%; border:1px solid" class="small"><caption>Dynamic Context Components</caption><tbody><tr><th>Component</th><th>Scope</th></tr><tr><td>Context value</td><td>dynamic; changes during evaluation of path expressions and predicates</td></tr><tr><td>Context position</td><td>dynamic; changes during evaluation of path expressions and predicates</td></tr><tr><td>Context size</td><td>dynamic; changes during evaluation of path expressions and predicates</td></tr><tr><td>Variable values</td><td>dynamic; for-expressions, let-expressions, and quantified expressions can bind new variables</td></tr><tr><td>Current date and time</td><td>global; must be initialized </td></tr><tr><td>Implicit timezone</td><td>global; must be initialized </td></tr><tr><td>Available documents</td><td>global; must be initialized </td></tr><tr><td>Available node collections</td><td>global; must be initialized </td></tr><tr><td>Default collection</td><td>global; overwriteable by implementation</td></tr><tr><td>Available URI collections</td><td>global; must be initialized </td></tr><tr><td>Default URI collection</td><td>global; overwriteable by implementation</td></tr></tbody></table></div></div><div class="div1">
<h2><a id="id-impl-defined-items"/>C <a href="#id-impl-defined-items" style="text-decoration: none">Implementation-Defined Items</a></h2><p>The following items in this specification 
  are <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>:</p><ol class="enumar"><li><p>The version of Unicode that is used to construct expressions.</p></li><li><p>The <a title="statically known collations" class="termref" href="#dt-static-collations">statically-known collations</a>.</p></li><li><p>The <a title="implicit timezone" class="termref" href="#dt-timezone">implicit timezone</a>.</p></li><li><p>The circumstances in which <a title="warning" class="termref" href="#dt-warning">warnings</a> are raised, and the ways in which warnings are handled.</p></li><li><p>The method by which errors are reported to the external processing environment.</p></li><li><p>Which version of XML and XML Names (e.g. <a href="#XML">[XML 1.0]</a> and <a href="#XMLNAMES">[XML Names]</a> or <a href="#XML1.1">[XML 1.1]</a> and <a href="#XMLNAMES11">[XML Names 1.1]</a>) and which version of XML Schema (e.g. <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a>) is used for the definitions of primitives such as characters and names, and for the definitions  of operations such as normalization of line endings and normalization of whitespace in attribute values. It is recommended that the latest applicable version be used (even if it is published later than this specification).</p></li><li><p>How XDM instances are created from sources other than an Infoset or PSVI.</p></li><li class="xpath"><p>Whether the implementation supports the namespace axis.</p></li><li><p>Whether the type system is based on <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a>. An implementation that has based its type system on XML Schema 1.0 is not required to support the use of the <code>xs:dateTimeStamp</code> constructor or the use of <code>xs:dateTimeStamp</code> or <code>xs:error</code> as <a href="#prod-xpath40-TypeName">TypeName</a> in any expression.</p></li><li><p>The signatures of functions provided by the implementation or via an implementation-defined API (see <a href="#static_context"><b>2.2.1 Static Context</b></a>).</p></li><li><p>Any <a title="environment variables" class="termref" href="#dt-environment-variables">environment variables</a> provided by the implementation.</p></li><li><p>Any rules used for static typing (see <a href="#id-static-analysis"><b>2.3.3.1 Static Analysis Phase</b></a>).</p></li><li><p>Any serialization parameters provided by the implementation</p></li><li><p>What error, if any, is returned if an external function's implementation does not return the declared result type (see <a href="#id-consistency-constraints"><b>2.3.6 Consistency Constraints</b></a>).</p></li></ol><div class="note"><p class="prefix"><b>Note:</b></p><p>Additional <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a> 
  items are listed in <a href="#xpath-datamodel-40">[XQuery and XPath Data Model (XDM) 4.0]</a> and <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.</p></div></div><div class="div1">
<h2><a id="id-references"/>D <a href="#id-references" style="text-decoration: none">References</a></h2><div class="div2">
<h3><a id="id-normative-references"/>D.1 <a href="#id-normative-references" style="text-decoration: none">Normative References</a></h3><dl><dt class="label"><span><a id="RFC2119"/>RFC2119</span></dt><dd><div>S. Bradner.
      <em>Key Words for use in RFCs to Indicate Requirement Levels.</em> IETF RFC 2119.
      See <a href="http://www.ietf.org/rfc/rfc2119.txt">http://www.ietf.org/rfc/rfc2119.txt</a>.</div></dd><dt class="label"><span><a id="RFC3986"/>RFC3986</span></dt><dd><div>T. Berners-Lee, R. Fielding, and
      L. Masinter.  <em>Uniform Resource Identifiers (URI): Generic
      Syntax</em>. IETF RFC 3986.
      See <a href="http://www.ietf.org/rfc/rfc3986.txt">http://www.ietf.org/rfc/rfc3986.txt</a>.</div></dd><dt class="label"><span><a id="RFC3987"/>RFC3987</span></dt><dd><div>M. Duerst and M. Suignard.
      <em>Internationalized Resource Identifiers (IRIs)</em>.
      IETF RFC 3987. See <a href="http://www.ietf.org/rfc/rfc3987.txt">http://www.ietf.org/rfc/rfc3987.txt</a>.</div></dd><dt class="label"><span><a id="ISO10646"/>ISO/IEC 10646</span></dt><dd><div>ISO (International Organization for Standardization).
      <em>ISO/IEC 10646:2003. Information technology—Universal Multiple-Octet Coded Character Set (UCS)</em>,
      as, from time to time, amended, replaced by a new edition, or expanded by the addition of new parts.
      [Geneva]: International Organization for Standardization.
      (See <a href="http://www.iso.org">http://www.iso.org</a> for the latest version.)</div></dd><dt class="label"><span><a id="Unicode"/>Unicode</span></dt><dd><div>The Unicode Consortium. <em>The Unicode Standard.</em>
      Reading, Mass.: Addison-Wesley, 2003, as updated from time to time by the publication of new versions.
      See <a href="http://www.unicode.org/standard/versions/">http://www.unicode.org/standard/versions/</a>
      for the latest version and additional information on versions of the standard and of the Unicode Character Database.
      The version of Unicode to be used is <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>,
      but implementations are recommended to use the latest Unicode version.</div></dd><dt class="label"><span><a id="XML"/>XML 1.0</span></dt><dd><div>World Wide Web Consortium.
      <em>Extensible Markup Language (XML) 1.0.</em>
      W3C Recommendation.
      See <a href="http://www.w3.org/TR/REC-xml/">http://www.w3.org/TR/REC-xml/</a>.
      The edition of XML 1.0 must be no earlier than the Third Edition;
      the edition used is <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>,
      but we recommend that implementations use the latest version.
</div></dd><dt class="label"><span><a id="XML1.1"/>XML 1.1</span></dt><dd><div>World Wide Web Consortium.
      <em>Extensible Markup Language (XML) 1.1.</em>
      W3C Recommendation.
      See <a href="http://www.w3.org/TR/xml11/">http://www.w3.org/TR/xml11/</a>
               </div></dd><dt class="label"><span><a id="XMLBASE"/>XML Base</span></dt><dd><div>World Wide Web Consortium.
      <em>XML Base.</em> W3C Recommendation. See
      <a href="http://www.w3.org/TR/xmlbase/">http://www.w3.org/TR/xmlbase/</a>
               </div></dd><dt class="label"><span><a id="XMLNAMES"/>XML Names</span></dt><dd><div>World Wide Web Consortium.
      <em>Namespaces in XML.</em> W3C Recommendation. See
      <a href="http://www.w3.org/TR/REC-xml-names/">http://www.w3.org/TR/REC-xml-names/</a>
               </div></dd><dt class="label"><span><a id="XMLNAMES11"/>XML Names 1.1</span></dt><dd><div>World Wide Web Consortium.
      <em>Namespaces in XML 1.1.</em> W3C Recommendation. See
      <a href="http://www.w3.org/TR/xml-names11/">http://www.w3.org/TR/xml-names11/</a>
               </div></dd><dt class="label"><span><a id="XMLID"/>XML ID</span></dt><dd><div>World Wide Web Consortium. <em>xml:id Version 1.0.</em>
      W3C Recommendation. See <a href="http://www.w3.org/TR/xml-id/">http://www.w3.org/TR/xml-id/</a>
               </div></dd><dt class="label"><span><a id="XMLSchema10"/>XML Schema 1.0</span></dt><dd><div>World Wide Web Consortium.
      <em>XML Schema, Parts 0, 1, and 2 (Second Edition)</em>. W3C Recommendation, 28 October 2004.
      See <a href="http://www.w3.org/TR/xmlschema-0/">http://www.w3.org/TR/xmlschema-0/</a>,
      <a id="schema1" href="http://www.w3.org/TR/xmlschema-1/">http://www.w3.org/TR/xmlschema-1/</a>,
      and <a id="schema2" href="http://www.w3.org/TR/xmlschema-2/">http://www.w3.org/TR/xmlschema-2/</a>.</div></dd><dt class="label"><span><a id="XMLSchema11"/>XML Schema 1.1</span></dt><dd><div>World Wide Web Consortium.
      <em>XML Schema, Parts 1, and 2</em>. W3C Recommendation 5 April 2012.
      See <a id="schema1-11" href="http://www.w3.org/TR/xmlschema11-1/">http://www.w3.org/TR/xmlschema11-1/</a>,
      and <a id="schema2-11" href="http://www.w3.org/TR/xmlschema11-2/">http://www.w3.org/TR/xmlschema11-2/</a>.</div></dd><dt class="label"><span><a id="xpath-datamodel-40"/>XQuery and XPath Data Model (XDM) 4.0</span></dt><dd><div>
<a href="https://qt4cg.org/specifications/xpath-datamodel-40/Overview.html"><cite>XQuery and XPath Data Model (XDM) 4.0</cite></a>,
XSLT Extensions Community Group,
World Wide Web Consortium.</div></dd><dt class="label"><span><a id="xpath-functions-40"/>XQuery and XPath Functions and Operators 4.0</span></dt><dd><div>
<a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html"><cite>XQuery and XPath Functions and Operators 4.0</cite></a>,
XSLT Extensions Community Group,
World Wide Web Consortium.</div></dd><dt class="label"><span><a id="xpath-40"/>XPath 4.0</span></dt><dd><div>
<a href="https://qt4cg.org/specifications/xquery-40/xpath-40.html"><cite>XML Path Language (XPath) 4.0</cite></a>,
XSLT Extensions Community Group,
World Wide Web Consortium.</div></dd><dt class="label"><span><a id="xslt-xquery-serialization-40"/>XSLT and XQuery Serialization 4.0</span></dt><dd><div>
<a href="https://qt4cg.org/specifications/xslt-xquery-serialization-40/Overview.html"><cite>XSLT and XQuery Serialization 4.0</cite></a>,
XSLT Extensions Community Group,
World Wide Web Consortium.</div></dd></dl></div><div class="div2">
<h3><a id="id-non-normative-references"/>D.2 <a href="#id-non-normative-references" style="text-decoration: none">Non-normative References</a></h3><dl><dt class="label"><span class="xpath"><a id="xquery-31"/>XQuery 3.1: An XML Query Language</span></dt><dd><div class="xpath">
<a href="https://www.w3.org/TR/xquery-31/"><cite>XQuery 3.1: An XML Query Language</cite></a>,
Jonathan Robie, Michael Dyck and Josh Spiegel, Editors.
World Wide Web Consortium,
21 March 2017. 
This version is https://www.w3.org/TR/2017/REC-xquery-31-20170321/.
The <a href="https://www.w3.org/TR/xquery-31/">latest version</a>
is available at https://www.w3.org/TR/xquery-31/.</div></dd><dt class="label"><span><a id="xquery-semantics"/>XQuery 1.0 and XPath 2.0 Formal Semantics</span></dt><dd><div>
<a href="https://www.w3.org/TR/xquery-semantics/"><cite>XQuery 1.0 and XPath 2.0 Formal Semantics (Second Edition)</cite></a>,
Jérôme Siméon, Denise Draper, Peter Frankhauser, <em>et. al.</em>, Editors.
World Wide Web Consortium,
14 December 2010.
This version is https://www.w3.org/TR/2010/REC-xquery-semantics-20101214/.
The <a href="https://www.w3.org/TR/xquery-semantics/">latest version</a>
is available at https://www.w3.org/TR/xquery-semantics/.</div></dd><dt class="label"><span><a id="xslt-40"/>XSL Transformations (XSLT) Version 4.0</span></dt><dd><div>
<a href="https://qt4cg.org/specifications/xslt-40/Overview.html"><cite>XSL Transformations (XSLT) Version 4.0</cite></a>,
XSLT Extensions Community Group,
World Wide Web Consortium.</div></dd><dt class="label"><span><a id="XINFO"/>XML Infoset</span></dt><dd><div>World Wide Web
Consortium. <em>XML Information Set (Second Edition).</em> W3C Recommendation 4 February 2004. See
<a href="http://www.w3.org/TR/xml-infoset/">http://www.w3.org/TR/xml-infoset/</a>
               </div></dd><dt class="label"><span><a id="xpath"/>XML Path Language (XPath) Version 1.0</span></dt><dd><div>
      <a href="http://www.w3.org/TR/xpath/"><cite>XML Path Language (XPath) Version 1.0</cite></a>, James Clark and Steven DeRose, Editors. World Wide Web Consortium, 16 Nov 1999. This version is http://www.w3.org/TR/1999/REC-xpath-19991116. The <a href="http://www.w3.org/TR/xpath/">latest version</a> is available at http://www.w3.org/TR/xpath.</div></dd><dt class="label"><span><a id="xpath20"/>XML Path Language (XPath) Version 2.0</span></dt><dd><div>
<a href="https://www.w3.org/TR/xpath20/"><cite>XML Path Language (XPath) 2.0 (Second Edition)</cite></a>,
Don Chamberlin, Anders Berglund, Scott Boag, <em>et. al.</em>, Editors.
World Wide Web Consortium,
14 December 2010.
This version is https://www.w3.org/TR/2010/REC-xpath20-20101214/.
The <a href="https://www.w3.org/TR/xpath20/">latest version</a>
is available at https://www.w3.org/TR/xpath20/.</div></dd><dt class="label"><span><a id="xpath-30"/>XML Path Language (XPath) Version 3.0</span></dt><dd><div>
<a href="https://www.w3.org/TR/xpath-30/"><cite>XML Path Language (XPath) 3.0</cite></a>,
Jonathan Robie, Don Chamberlin, Michael Dyck, John Snelson, Editors.
World Wide Web Consortium,
08 April 2014. 
This version is https://www.w3.org/TR/2014/REC-xpath-30-20140408/.
The <a href="https://www.w3.org/TR/xpath-30/">latest version</a>
is available at https://www.w3.org/TR/xpath-30/.</div></dd><dt class="label"><span><a id="XPTR"/>XPointer</span></dt><dd><div>World Wide Web Consortium. <em>XML
Pointer Language (XPointer).</em> W3C Last Call Working Draft 8 January 2001.
See <a href="http://www.w3.org/TR/WD-xptr">http://www.w3.org/TR/WD-xptr</a>
               </div></dd></dl></div><div class="div2">
<h3><a id="id-background-material"/>D.3 <a href="#id-background-material" style="text-decoration: none">Background Material</a></h3><dl><dt class="label"><span><a id="CHARMOD"/>Character Model</span></dt><dd><div>World Wide Web Consortium.
<em>Character Model for the World Wide Web.</em> W3C Working
Draft. See <a href="http://www.w3.org/TR/charmod/">http://www.w3.org/TR/charmod/</a>.</div></dd><dt class="label"><span><a id="xslt"/>XSL Transformations (XSLT) Version 1.0</span></dt><dd><div>
      <a href="http://www.w3.org/TR/xslt"><cite>XSL Transformations (XSLT) Version 1.0</cite></a>, James Clark, Editor. World Wide Web Consortium, 16 Nov 1999. This version is http://www.w3.org/TR/1999/REC-xslt-19991116. The <a href="http://www.w3.org/TR/xslt">latest version</a> is available at http://www.w3.org/TR/xslt.</div></dd></dl></div></div><div class="div1">
<h2><a id="id-errors"/>E <a href="#id-errors" style="text-decoration: none">Error Conditions</a></h2><dl>
            <dt><a id="ERRXPST0001"/>err:XPST0001</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if analysis of an
            expression relies on some component of the <a title="static context" class="termref" href="#dt-static-context">static
               context</a> that 
                  <span>is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>
                  </span>.</p>
            </dd>
            <dt><a id="ERRXPDY0002"/>err:XPDY0002</dt><dd>
               <p>It is a <a title="type error" class="termref" href="#dt-type-error">type error</a> if evaluation of an
            expression relies on some part of the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic
               context</a> that is <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>.</p>
               <div class="note"><p class="prefix"><b>Note:</b></p><p>In version 4.0 this has been reclassified as a type error rather than
         a dynamic error. This change allows a processor to report the error during static
         analysis where possible; for example if the body of a user-defined
         function is written as <code>fn($x) { @code }</code>.
         The error code is prefixed <code>XPDY</code> rather than <code>XPTY</code>
         for backwards compatibility reasons.</p></div>
            </dd>
            <dt><a id="ERRXPST0003"/>err:XPST0003</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if an expression is not a
            valid instance of the grammar defined in <a href="#id-grammar"><b>A.1 EBNF</b></a>.</p>
            </dd>
            <dt><a id="ERRXPTY0004"/>err:XPTY0004</dt><dd>
               <p>It is a <a title="type error" class="termref" href="#dt-type-error">type error</a> if, during the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis phase</a>, an expression is found to
            have a <a title="static type" class="termref" href="#dt-static-type">static type</a> that is not appropriate for
            the context in which the expression occurs, or during the <a title="dynamic evaluation phase" class="termref" href="#dt-dynamic-evaluation">dynamic evaluation phase</a>, the <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> of a value does not match a required
            type as specified by the matching rules in <a href="#id-sequencetype-matching"><b>3.1.2 Sequence Type Matching</b></a>.</p>
            </dd>
            
            <dt><a id="ERRXPTY0006"/>err:XPTY0006</dt><dd>
               <p>During the analysis phase, an expression is classified as <a title="implausible" class="termref" href="#dt-implausible">implausible</a>
            if the inferred <a title="static type" class="termref" href="#dt-static-type">static type</a>
                  <var>S</var> and
            the required type <var>R</var> are substantively disjoint; more specifically, if neither
            of the types is a subtype of the other, and if the only values
            that are instances of both types are one or more of: the empty sequence, the empty map, 
            and the empty array.</p>
            </dd>
            <dt><a id="ERRXPST0008"/>err:XPST0008</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if an expression refers
            to an element name, attribute name, schema type name, or variable name
            that is not defined in the <a title="static context" class="termref" href="#dt-static-context">static context</a>,
            except for an ElementName in an <a href="#doc-xpath40-ElementTest">ElementTest</a> or an
            AttributeName in an <a href="#doc-xpath40-AttributeTest">AttributeTest</a>.</p>
            </dd>
            <dt><a id="ERRXPST0010"/>err:XPST0010</dt><dd>
               <p> An implementation that does not support the namespace axis must raise a <a title="static error" class="termref" href="#dt-static-error">static error</a> if it encounters a reference to the
            namespace axis and XPath 1.0 compatibility mode is <code>false</code>. </p>
            </dd>
            
            
            <dt><a id="ERRXPST0017"/>err:XPST0017</dt><dd>
               <p>It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if the <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>
            and number of arguments in a static function call do not match the name and
            <a title="arity range" class="termref" href="#dt-arity-range">arity range</a> of a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> in the
            <a title="static context" class="termref" href="#dt-static-context">static context</a>, or if an argument keyword in the function call
            does not match a parameter name in that function definition, or if two arguments
            in the function call bind to the same parameter in the function definition.</p>
            </dd>
            <dt><a id="ERRXPTY0018"/>err:XPTY0018</dt><dd>
               <p>It is a <a title="type error" class="termref" href="#dt-type-error">type error</a> if the result of a path
            operator contains both nodes and non-nodes.</p>
            </dd>
            <dt><a id="ERRXPTY0019"/>err:XPTY0019</dt><dd>
               <p> It is a <a title="type error" class="termref" href="#dt-type-error">type error</a> if <code>E1</code> in a path
            expression <code>E1/E2</code> does not evaluate to a sequence of nodes.</p>
            </dd>
            <dt><a id="ERRXPTY0020"/>err:XPTY0020</dt><dd>
               <p> It is a <a title="type error" class="termref" href="#dt-type-error">type error</a> if, in an axis step, the
            context item is not a node.</p>
            </dd>
            <dt><a id="ERRXPST0021"/>err:XPST0021</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if two fields in a record declaration
            have the same name.</p>
            </dd>
            <dt><a id="ERRXPST0023"/>err:XPST0023</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if a recursive record type
            cannot be instantiated (typically because it contains a self-reference that is neither optional
            nor emptiable). Processors are not required to detect this error.</p>
            </dd>
            
            
            
            
            <dt><a id="ERRXQST0039"/>err:XQST0039</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> for 
            <span>an inline function expression</span> to have more than one
            parameter with the same name. </p>
            </dd>
            
            
            <dt><a id="ERRXQST0046"/>err:XQST0046</dt><dd>
               <p>An implementation <span class="xpath">MAY</span> raise a <a title="static error" class="termref" href="#dt-static-error">static
               error</a> if the value of a <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a> is
            of nonzero length and is 
                  <span>neither an absolute URI nor a
               relative URI</span>.</p>
            </dd>
            <dt><a id="ERRXPDY0050"/>err:XPDY0050</dt><dd>
               <p>It is a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> if the <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> of the operand of a <code>treat</code>
            expression does not match the <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>
            designated by the <code>treat</code> expression. This error might also be raised by a
            path expression beginning with <code>/</code> or <code>//</code> if the context node
            is not in a tree that is rooted at a document node. This is because a leading
               <code>/</code> or <code>//</code> in a path expression is an abbreviation for an
            initial step that includes the clause <code>treat as document-node()</code>.</p>
            </dd>
            <dt><a id="ERRXPST0051"/>err:XPST0051</dt><dd>
               <p>It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> used as an <a href="#doc-xpath40-ItemType"/>
               in a <a href="#doc-xpath40-SequenceType"/> is not defined
            in the <a title="static context" class="termref" href="#dt-static-context">static context</a> either as a <a title="named item type" class="termref" href="#dt-named-item-type">named item type</a>
            in the <a title="in-scope named item types" class="termref" href="#dt-in-scope-named-item-types">in-scope named item types</a>,
            or as a <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>
            in the <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope schema type</a>.</p>
            </dd>
            <dt><a id="ERRXQST0052"/>err:XQST0052</dt><dd>
               <p>The type named in a cast or castable expression must be the name of a type defined in the <a title="in-scope schema type" class="termref" href="#dt-is-types">in-scope
               schema types</a>, and the type must be
               <code>simple</code>.</p>
            </dd>
            
            
            
            
            <dt><a id="ERRXQST0070"/>err:XQST0070</dt><dd>
               <p class="xpath">A <a title="static error" class="termref" href="#dt-static-error">static error</a> is raised if any of
            the following conditions is statically detected in any expression: </p>
               <ul><li><p>The prefix <code>xml</code> is bound to some namespace URI other than
                     <code>http://www.w3.org/XML/1998/namespace</code>. </p></li><li><p>A prefix other than <code>xml</code> is bound to the namespace URI
                     <code>http://www.w3.org/XML/1998/namespace</code>. </p></li><li><p>The prefix <code>xmlns</code> is bound to any namespace URI. </p></li><li><p>A prefix other than <code>xmlns</code> is bound to the namespace URI
                     <code>http://www.w3.org/2000/xmlns/</code>. </p></li></ul>
               
            </dd>
            
            
            
            <dt><a id="ERRXPST0080"/>err:XPST0080</dt><dd>
               <p>It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if the target type of a
               <code>cast</code> or <code>castable</code> expression is
               <code>xs:NOTATION</code>, <span>
                     <code>xs:anySimpleType</code>,</span> or
               <code>xs:anyAtomicType</code>.</p>
            </dd>
            <dt><a id="ERRXPST0081"/>err:XPST0081</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if a QName used in
               <span class="xpath">an expression</span>
            contains a namespace prefix that cannot be expanded into a namespace URI by using the
               <a title="statically known namespaces" class="termref" href="#dt-static-namespaces">statically known namespaces</a>.</p>
            </dd>
            
            
            <dt><a id="ERRXQST0089"/>err:XQST0089</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if a variable bound in a
            <code>for</code> expression, and its
            associated positional variable, do not have distinct names (<a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a>).</p>
            </dd>
            
            
            
            
            <dt><a id="ERRXPST0107"/>err:XPST0107</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if the annotation 
            <code>%method</code> is used on a <a title="focus function" class="termref" href="#dt-focus-function">focus function</a>.</p>
            </dd>
            
            <dt><a id="ERRXPTY0117"/>err:XPTY0117</dt><dd>
               <p>When applying the <a title="coercion rules" class="termref" href="#dt-coercion-rules">coercion rules</a>, if an item is of type <code>xs:untypedAtomic</code> and the
            expected type is <a title="namespace-sensitive" class="termref" href="#dt-namespace-sensitive">namespace-sensitive</a>, a
               <a title="type error" class="termref" href="#dt-type-error">type error</a>
                  [<a href="#ERRXPTY0117" title="err:XPTY0117">err:XPTY0117</a>] is raised. </p>
            </dd>
            
            
            
            
            <dt><a id="ERRXPDY0130"/>err:XPDY0130</dt><dd>
               <p>An implementation-dependent limit has been exceeded.</p>
            </dd>
            
            
            <dt><a id="ERRXQST0134"/>err:XQST0134</dt><dd>
               <p>The namespace axis is not supported.</p>
            </dd>
            
            
            <dt><a id="ERRXQDY0137"/>err:XQDY0137</dt><dd>
               <p>No two keys in a map may have the  <a title="same key" class="termref" href="#dt-same-key">same key value</a>.</p>
            </dd>
            
            <dt><a id="ERRXQST0140"/>err:XQST0140</dt><dd>
               <p> It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> if a named item type declaration 
            is recursive, unless it satisfies the conditions defined in <a href="#id-recursive-record-tests"><b>3.2.8.3.1 Recursive Record Types</b></a>.</p>
            </dd>
            <dt><a id="ERRXPTY0141"/>err:XPTY0141</dt><dd>
               <p>In a <code>for</code>
                  <span class="xpath">expression</span>,
         when the keyword <code>member</code> is present, the value of the binding collection
         must be a single array; and when either or both of the keywords <code>key</code> and <code>value</code>
         are present, the value of the binding collection must be a single map.</p>
            </dd>
            <dt><a id="ERRXPTY0144"/>err:XPTY0144</dt><dd>
               <p>During the analysis phase, an axis step is classified as <a title="implausible" class="termref" href="#dt-implausible">implausible</a>
            if the combination of the inferred context item type, the choice of axis, and the
            supplied node test, is such that the axis step will always return an empty sequence.</p>
            </dd>
            <dt><a id="ERRXPTY0145"/>err:XPTY0145</dt><dd>
               <p>During the analysis phase, a unary or postfix lookup expression is classified as <a title="implausible" class="termref" href="#dt-implausible">implausible</a>
            if the combination of the inferred type of the left-hand operand (or the context item type
            in the case of a unary expression) and the choice of key specifier is such that the lookup expression
            will always return an empty sequence.</p>
            </dd>
            
            
            <dt><a id="ERRXPST0152"/>err:XPST0152</dt><dd>
               <p>It is a <a title="static error" class="termref" href="#dt-static-error">static error</a> 
            if a key type named in a <a href="#prod-xpath40-TypedMapType"/> is not a
            <a title="generalized atomic type" class="termref" href="#dt-generalized-atomic-type">generalized atomic type</a>.</p>
            </dd>
         </dl></div><div class="div1">
<h2><a id="id-glossary"/>F <a href="#id-glossary" style="text-decoration: none">Glossary</a> (Non-Normative)</h2><dl><dt><a href="#dt-anonymous-function">anonymous function</a></dt><dd><p>
             An <b>anonymous function</b> is a <a title="function item" class="termref" href="#dt-function-item">function item</a> with no name.  
             Anonymous functions may be created, for example, by evaluating an inline function 
             expression or by partial function application.</p></dd><dt><a href="#dt-application-function">application function</a></dt><dd><p>
                                    <b>Application functions</b> are function definitions written in a 
                           host language such as XQuery or XSLT whose syntax and
                           semantics are defined in this family of specifications. Their behavior 
                           (including the rules determining the static and dynamic context) follows the
                           rules for such functions in the relevant host language specification.</p></dd><dt><a href="#dt-arg-expr">argument expression</a></dt><dd><p>An argument to a function call is either an
               <b>argument expression</b> or an <a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholder</a> 
               (<code>?</code>); in both cases it may
            either be supplied positionally, or identified by a name (called a keyword).</p></dd><dt><a href="#dt-arity-range">arity range</a></dt><dd><p>A <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> has an <b>arity range</b>,
                        which is a range of consecutive non-negative integers. If the function definition has <var>M</var> required parameters
                        and <var>N</var> optional parameters, then its arity range is from <var>M</var> to <var>M</var>+<var>N</var>
                        inclusive.</p></dd><dt><a href="#dt-array">array</a></dt><dd><p>An <b>array</b> is
      a <a title="function item" class="termref" href="#dt-function-item">function item</a> that associates a set of positions, represented as
      positive integer keys, with values.</p></dd><dt><a href="#dt-associated-value">associated value</a></dt><dd><p>The value
  associated with a given key is called the <b>associated
  value</b> of the key.</p></dd><dt><a href="#dt-atomic-item">atomic item</a></dt><dd><p>An <b>atomic
	 item</b> is a value in the value space of an <b>atomic
	 type</b>, as defined in <a href="#XMLSchema10">[XML Schema 1.0]</a>  or <a href="#XMLSchema11">[XML Schema 1.1]</a>.</p></dd><dt><a href="#dt-atomic-type">atomic type</a></dt><dd><p>An <b>atomic type</b>
            is a simple <a title="schema type" class="termref" href="#dt-schema-type">schema type</a> whose 
            <a href="https://www.w3.org/TR/xmlschema11-1/#std-variety">{variety}<sup><small>XS11-1</small></sup></a>
            is <code>atomic</code>.</p></dd><dt><a href="#dt-atomization">atomization</a></dt><dd><p>
                     <b>Atomization</b> of a sequence
is defined as the result of invoking the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-data"><code>fn:data</code></a> function, as defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-data">Section 2.1.4 fn:data</a><sup><small>FO</small></sup>.</p></dd><dt><a href="#dt-available-binary-resources">available binary resources</a></dt><dd><p>
                           <b>Available binary resources</b>. 
  This is a mapping of strings to binary resources. Each string
  represents the absolute URI of a resource. The resource is returned
  by the <code>fn:unparsed-binary</code> function when applied to that
  URI.</p></dd><dt><a href="#dt-available-docs">available documents</a></dt><dd><p>
                           <b>Available
    documents.</b> This is a mapping of strings to document nodes.  Each 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" class="termref" href="#dt-datamodel">data model</a>. The document node is returned by the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-doc"><code>fn:doc</code></a> 
    function when applied to that URI.</p></dd><dt><a href="#dt-available-collections">available item collections</a></dt><dd><p>
                           <b>Available
                           collections.</b> This is a mapping of
                         strings to sequences of items. Each string
                         represents the absolute URI of a
                         resource. The sequence of items represents
                         the result of the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a>
                         function when that URI is supplied as the
                         argument. </p></dd><dt><a href="#dt-available-text-resources">available text resources</a></dt><dd><p>
                           <b>Available text resources</b>. 
  This is a mapping of strings to text resources. Each string
  represents the absolute URI of a resource. The resource is returned
  by the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-unparsed-text"><code>fn:unparsed-text</code></a> function when applied to that
  URI.</p></dd><dt><a href="#dt-available-uri-collections">available uri collections</a></dt><dd><p>
                           <b>Available
    URI collections.</b> This is a mapping of
    strings to sequences of URIs. The string
    represents the absolute URI of a
    resource which can be interpreted as an aggregation of a number of individual resources each of which
    has its own URI. The sequence of URIs represents
    the result of the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-uri-collection"><code>fn:uri-collection</code></a>
    function when that URI is supplied as the
    argument. </p></dd><dt><a href="#dt-axis-step">axis step</a></dt><dd><p>An <b>axis step</b> returns a sequence of nodes that are 
                  reachable from a starting node via a specified axis. Such a step has two parts: an
		<b>axis</b>, which defines the "direction of
		movement" for the step, and a <a title="node test" class="termref" href="#dt-node-test">node test</a>,
		which selects nodes based on their kind, name, and/or
		<a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> .</p></dd><dt><a href="#dt-binding-collection-xp">binding collection</a></dt><dd><p>The 
                  result of evaluating the <b>binding expression</b> in a 
                  <code>for</code> expression is called the 
                  <b>binding collection</b>
                        </p></dd><dt><a href="#dt-choice-item-type">choice item type</a></dt><dd><p>A 
                  <b>choice item type</b> defines an item type that is the union
               of a number of alternatives. For example the type 
               <code>(xs:hexBinary | xs:base64Binary)</code> defines the union of 
                  these two primitive <a title="atomic type" class="termref" href="#dt-atomic-type">atomic types</a>, 
                  while the type <code>(map(*) | array(*))</code>
               matches any item that is either a map or an array.</p></dd><dt><a href="#dt-coercion-rules">coercion rules</a></dt><dd><p>The <b>coercion rules</b> are rules used to convert a supplied value to a required type,
                     for example when converting an argument of a function call to the declared type of the function parameter.
                  </p></dd><dt><a href="#dt-collation">collation</a></dt><dd><p>A <b>collation</b> is a specification of the manner in which strings and URIs are compared and, by extension, ordered. For a more complete definition of collation, see <a href="https://qt4cg.org/specifications/xpath-functions-40/#string-compare">Section 5.3 Comparison of strings</a><sup><small>FO</small></sup>.</p></dd><dt><a href="#dt-comma-operator">comma operator</a></dt><dd><p>One way to construct a sequence is by using the <b>comma operator</b>, 
                  which evaluates each of its operands and concatenates the resulting sequences, in order, 
                  into a single result sequence.</p></dd><dt><a href="#dt-complex-terminal">complex terminal</a></dt><dd><p>A <b>complex terminal</b> is
        a <a title="variable terminal" class="termref" href="#dt-variable-terminal">variable terminal</a> whose production rule references, directly or indirectly, an 
        <a title="ordinary production rule" class="termref" href="#dt-ordinary-production-rule">ordinary production rule</a>.</p></dd><dt><a href="#dt-constructor-function">constructor function</a></dt><dd><p>The <b>constructor function</b> for a given simple type is used to convert instances of other  simple types into the given type. 
                  The semantics of the constructor function call <code>T($arg)</code> are defined to be equivalent to the expression <code>$arg cast as T?</code>.</p></dd><dt><a href="#dt-content-expression">content expression</a></dt><dd><p>In an  <a title="enclosed expression" class="termref" href="#dt-enclosed-expression">enclosed expression</a>, the optional expression enclosed in curly brackets is called the <b>content expression</b>.</p></dd><dt><a href="#dt-context-dependent">context dependent</a></dt><dd><p>A 
                        <a title="function definition" class="termref" href="#dt-function-definition">function definition</a> is said to be <b>context dependent</b>
                        if its result depends on the static or dynamic context of its caller.
                        A function definition may
                        be context-dependent for some arities in its arity range, and context-independent
                        for others: for example <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-name"><code>fn:name#0</code></a> is context-dependent
                        while <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-name"><code>fn:name#1</code></a> is context-independent.</p></dd><dt><a href="#dt-context-node">context node</a></dt><dd><p>When the context value is a single item, it can also be referred
                        to as the <b>context item</b>; when it is a single node,
it can also be referred to as the <b>context
node</b>.</p></dd><dt><a href="#dt-context-position">context position</a></dt><dd><p>The <b>context
position</b> is the position of the context value within the
series of values currently being processed.</p></dd><dt><a href="#dt-context-size">context size</a></dt><dd><p>The <b>context
size</b> is the number of values in the series of values currently
being processed.</p></dd><dt><a href="#dt-context-value">context value</a></dt><dd><p>The <b>context value</b>
is the <a title="value" class="termref" href="#dt-value">value</a> currently being processed.</p></dd><dt><a href="#dt-date-time">current dateTime</a></dt><dd><p>
                           <b>Current dateTime.</b> This information represents
				an <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> point in time during the processing of 
                        <span class="xpath">an expression</span>, and includes an explicit timezone. It can be retrieved by the  <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-current-dateTime"><code>fn:current-dateTime</code></a> function. 
                        If called multiple times during the execution of 
                        <span class="xpath">an expression</span>,
				this function always returns the same result.</p></dd><dt><a href="#dt-datamodel">data model</a></dt><dd><p>XPath 4.0 operates on the abstract, logical
			structure of an XML document or JSON object rather than its surface syntax. This logical structure,
			known as the <b>data model</b>, is defined in <a href="#xpath-datamodel-40">[XQuery and XPath Data Model (XDM) 4.0]</a>.</p></dd><dt><a href="#id-static-decimal-format-decimal-separator">decimal-separator</a></dt><dd><p>
                              <b>decimal-separator</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is
                                 used to separate the integer part of the number from the fractional part.
                                 The default value for both the marker and the rendition is <span class="unicode-codepoint">U+002E</span> (<span class="unicode-name">FULL STOP, PERIOD</span>, <code>.</code>) .</p></dd><dt><a href="#dt-default-calendar">default calendar</a></dt><dd><p>
                           <b>Default calendar.</b>
    This is the calendar used when formatting dates in human-readable output
    (for example, by the functions <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a> and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-dateTime"><code>fn:format-dateTime</code></a>)
    if no other calendar is requested. 
    The value is a string.</p></dd><dt><a href="#dt-def-collation">default collation</a></dt><dd><p>
                           <b>Default
                           collation.</b> This identifies one of the collations in <a title="statically known collations" class="termref" href="#dt-static-collations">statically known collations</a> as the  collation to be
                        used by functions and operators for comparing and ordering values of type <code>xs:string</code> and <code>xs:anyURI</code> (and types derived from them) when no
                        explicit collation is
                        specified.</p></dd><dt><a href="#dt-default-collection">default collection</a></dt><dd><p>
                           <b>Default  collection.</b>
    This is the sequence of items  that would result from calling the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a> function
    with no arguments.</p></dd><dt><a href="#dt-default-element-namespace-rule">default element namespace rule</a></dt><dd><p>When 
                  an unprefixed lexical QName
               is expanded using the <b>default element namespace rule</b>, then it uses the 
                  <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>. If this is absent, or if it takes
                  the special value <code>##any</code>, then the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>
                  is used.</p></dd><dt><a href="#dt-default-function-namespace">default function namespace</a></dt><dd><p>
                           <b>Default function namespace.</b> This is either a namespace URI, or <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>. The namespace URI, if present, is used for any unprefixed QName appearing
                     in a position where a function name is expected.</p></dd><dt><a href="#dt-default-function-namespace-rule">default function namespace rule</a></dt><dd><p>When 
                  an unprefixed lexical QName
               is expanded using the <b>default function namespace rule</b>, it uses the <a title="default function namespace" class="termref" href="#dt-default-function-namespace">default function namespace</a>
                  from the <a title="static context" class="termref" href="#dt-static-context">static context</a>.</p></dd><dt><a href="#dt-default-in-scope-namespace">default in-scope namespace</a></dt><dd><p>The <b>default in-scope namespace</b>
         of an element node</p></dd><dt><a href="#dt-default-language">default language</a></dt><dd><p>
                           <b>Default language.</b>
  This is the natural language used when creating human-readable output
  (for example, by the functions <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a> and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-integer"><code>fn:format-integer</code></a>)
  if no other language is requested. 
  The value is a language code as defined by the type <code>xs:language</code>.</p></dd><dt><a href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a></dt><dd><p>
                           <b>Default namespace for elements and types.</b> This is either a
				namespace URI, or the special value <code>"##any"</code>, or <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-absent">absent</a><sup><small>DM</small></sup>. This indicates how unprefixed QNames are interpreted when
                        they appear in a position  where an element name or type name is expected.</p></dd><dt><a href="#dt-default-place">default place</a></dt><dd><p>
                           <b>Default place.</b>
    This is a geographical location used to identify the place where events happened (or will happen) when
    processing dates and times using functions such as <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-date"><code>fn:format-date</code></a>, <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-dateTime"><code>fn:format-dateTime</code></a>,
     and <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-civil-timezone"><code>fn:civil-timezone</code></a>,
    if no other place is specified. It is used when translating timezone offsets to civil timezone names,
    and when using calendars where the translation from ISO dates/times to a local representation is dependent
    on geographical location. Possible representations of this information are an ISO country code or an
    Olson timezone name, but implementations are free to use other representations from which the above
    information can be derived. The only requirement is that it should uniquely identify a civil timezone,
    which means that country codes for countries with multiple timezones, such as the United States,
    are inadequate.</p></dd><dt><a href="#dt-default-type-namespace-rule">default type namespace rule</a></dt><dd><p>When 
                  an unprefixed lexical QName
               is expanded using the <b>default type namespace rule</b>, it uses the 
                  <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>. If this is absent, the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>
                  is used. If the <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a> has the special value <code>##any</code>,
                  then the lexical QName refers to a name in the namespace <code>http://www.w3.org/2001/XMLSchema</code>.</p></dd><dt><a href="#dt-default-uri-collection">default URI collection</a></dt><dd><p>
                           <b>Default URI collection.</b>
    This is the sequence of URIs that would result from calling the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-uri-collection"><code>fn:uri-collection</code></a> function
    with no arguments.</p></dd><dt><a href="#delimiting-token">delimiting terminal symbol</a></dt><dd><p>The <b>delimiting
            terminal symbols</b> are: <code>!</code>
                     <code>!=</code>
                     <code>#</code>
                     <code>$</code>
                     <code>%method</code>
                     <code>(</code>
                     <code>)</code>
                     <code>*</code>
                     <code>*:</code>
                     <code>+</code>
                     <code>,</code>
                     <code>-</code>
                     <code>-&gt;</code>
                     <code>.</code>
                     <code>..</code>
                     <code>/</code>
                     <code>//</code>
                     <code>:</code>
                     <code>:*</code>
                     <code>::</code>
                     <code>:=</code>
                     <code>&lt;</code>
                     <code>&lt;&lt;</code>
                     <code>&lt;=</code>
                     <code>=</code>
                     <code>=!&gt;</code>
                     <code>=&gt;</code>
                     <code>&gt;</code>
                     <code>&gt;=</code>
                     <code>&gt;&gt;</code>
                     <code>?</code>
                     <code>??</code>
                     <code>?[</code>
                     <code>@</code>
                     <code>[</code>
                     <code>]</code>
                     <code>`</code>
                     <code>``</code>
                     <code>{</code>
                     <code>{{</code>
                     <code>|</code>
                     <code>||</code>
                     <code>}</code>
                     <code>}}</code>
                     <code>~[</code>
                     <code>×</code>
                     <code>÷</code>
                     <a href="#prod-xpath40-AposStringLiteral">AposStringLiteral</a>
                     <a href="#prod-xpath40-BracedURILiteral">BracedURILiteral</a>
                     <a href="#prod-xpath40-QuotStringLiteral">QuotStringLiteral</a>
                     <a href="#prod-xpath40-StringLiteral">StringLiteral</a>
                  </p></dd><dt><a href="#dt-derives-from">derives from</a></dt><dd><p>A 
               <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>
                     <var>S<sub>1</sub></var> is said to <b>derive from</b>
                     <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>
                     <var>S<sub>2</sub></var> if any of the following
               conditions is true:</p><ul><li><p>
                        <var>S<sub>1</sub></var> is the same type as <var>S<sub>2</sub></var>.</p></li><li><p>
                        <var>S<sub>2</sub></var> is the base type of <var>S<sub>1</sub></var>.</p></li><li><p>
                        <var>S<sub>2</sub></var> is a <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union type</a>
                     of which <var>S<sub>1</sub></var> is a member type.</p></li><li><p>There is a <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>
                        <var>M</var> such that 
                     <var>S<sub>1</sub></var>
                        <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                        <var>M</var>
                     and <var>M</var>
                        <a title="derives from" class="termref" href="#dt-derives-from">derives from</a>
                        <var>S<sub>2</sub></var>.</p></li></ul></dd><dt><a href="#id-static-decimal-format-digit">digit</a></dt><dd><p>
                              <b>digit</b>
                              <em>(<var>M</var>)</em>
                                 is a character used in the picture string to represent an optional digit; 
                                 the default value is <span class="unicode-codepoint">U+0023</span> (<span class="unicode-name">NUMBER SIGN</span>, <code>#</code>) .</p></dd><dt><a href="#dt-document-order">document order</a></dt><dd><p>Informally, <b>document order</b> is the order in which nodes appear in the XML serialization of a document.</p></dd><dt><a href="#dt-dynamically-known-function-definitions">dynamically known function definitions</a></dt><dd><p>
                           <b>Dynamically known function definitions</b>.
        This is a set of <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a>. It includes the
                        <a title="statically known function definitions" class="termref" href="#dt-statically-known-function-definitions">statically known function definitions</a> as a subset, but may include
                        other function definitions that are not known statically.
      </p></dd><dt><a href="#dt-dynamic-context">dynamic context</a></dt><dd><p>The <b>dynamic
context</b> of an expression is defined as information that is needed for the dynamic evaluation of an expression,
                  beyond any information that is needed from the <a title="static context" class="termref" href="#dt-static-context">static context</a>.</p></dd><dt><a href="#dt-dynamic-error">dynamic error</a></dt><dd><p>A <b>dynamic
error</b> is an error that
must be detected during the dynamic evaluation phase and may be detected
during the static analysis phase.</p></dd><dt><a href="#dt-dynamic-evaluation">dynamic evaluation phase</a></dt><dd><p>The <b>dynamic evaluation phase</b> is the phase during which the value of an expression is computed.</p></dd><dt><a href="#dt-dynamic-function-invocation">dynamic function call</a></dt><dd><p>A <b>dynamic function call</b>
                     consists of a base expression that returns the function and a
                     parenthesized list of zero or more arguments (<a title="argument expression" class="termref" href="#dt-arg-expr">argument expressions</a> or
                     ArgumentPlaceholders).</p></dd><dt><a href="#dt-dynamic-function-call">dynamic function call</a></dt><dd><p>A <b>dynamic function call</b>
            is an expression that is evaluated by calling a <a title="function item" class="termref" href="#dt-function-item">function item</a>, which is
            typically obtained dynamically.</p></dd><dt><a href="#dt-dynamic-type">dynamic type</a></dt><dd><p>
                  Every value matches one or more <a title="sequence type" class="termref" href="#dt-sequence-type">sequence types</a>. 
                     A value is said to have a <b>dynamic type</b>
                        <var>T</var> if it matches (or <b>is an instance of</b>) 
                     the sequence type <var>T</var>.</p></dd><dt><a href="#dt-ebv">effective boolean value</a></dt><dd><p>The
<b>effective boolean value</b> of a value is defined as the result
of applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-boolean"><code>fn:boolean</code></a> function to the value, as
defined in <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-boolean">Section 8.3.1 fn:boolean</a><sup><small>FO</small></sup>.</p></dd><dt><a href="#dt-element-name-matching-rule">element name matching rule</a></dt><dd><p>When 
                  an unprefixed lexical QName
               is expanded using the <b>element name matching rule</b> rule, then it uses the 
                  <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a>. If this is absent, then 
                  it uses the <a title="no-namespace rule" class="termref" href="#dt-no-namespace-rule">no-namespace rule</a>. But if it takes the special value <code>##any</code>,
                  then the name is taken as matching any <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> with the corresponding local part,
                  regardless of namespace: that is, the  unprefixed name <code>local</code> is interpreted
                  as <code>*:local</code>.</p></dd><dt><a href="#dt-empty-sequence">empty sequence</a></dt><dd><p>A sequence containing zero items is called an <b>empty sequence</b>.</p></dd><dt><a href="#dt-enclosed-expression">enclosed expression</a></dt><dd><p>An <b>enclosed expression</b> is an instance of the <a href="#doc-xpath40-EnclosedExpr">EnclosedExpr</a> production, which allows an optional expression within curly brackets.</p></dd><dt><a href="#dt-entry">entry</a></dt><dd><p>Each key / value pair in a map
  is called an <b>entry</b>.</p></dd><dt><a href="#dt-enumeration-type">enumeration type</a></dt><dd><p>An <b>EnumerationType</b>
                  accepts a fixed set of string values.</p></dd><dt><a href="#dt-environment-variables">environment variables</a></dt><dd><p>
                           <b>Environment variables.</b>
  This is a mapping from names to values. 
    Both the names and the values are strings. The names are compared using an
    <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a> collation, and are unique under this collation. The set of environment variables is
  <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a> and <span class="verb">may</span> be empty.</p></dd><dt><a href="#dt-error-value">error value</a></dt><dd><p>In addition to its identifying QName, a dynamic error may also carry a descriptive string and one or more additional values called <b>error values</b>.</p></dd><dt><a href="#dt-executable-base-uri">Executable Base URI</a></dt><dd><p>
                           <b>Executable Base URI.</b> This is an absolute URI used
                     to resolve relative URIs during the evaluation of expressions;
                     it is used, for example, to resolve a relative URI supplied
                     to the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-doc"><code>fn:doc</code></a> or <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-unparsed-text"><code>fn:unparsed-text</code></a>
                     functions.
                  </p></dd><dt><a href="#dt-expanded-qname">expanded QName</a></dt><dd><p>An <b>expanded QName</b> is a
      triple: its components are a prefix, a local name, and a
      namespace URI. In the case of a name in no namespace, the
      namespace URI and prefix are both absent. In the case of a name
      in the default namespace, the prefix is absent.</p></dd><dt><a href="#id-static-decimal-format-exponent-separator">exponent-separator</a></dt><dd><p>
                              <b>exponent-separator</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is
                                 used to separate the mantissa from the exponent in
                                 scientific notation. The default value 
                                 for both the marker and the rendition is <span class="unicode-codepoint">U+0065</span> (<span class="unicode-name">LATIN SMALL LETTER E</span>, <code>e</code>) .</p></dd><dt><a href="#dt-expression-context">expression context</a></dt><dd><p>The <b>expression
		context</b> for a given expression consists of all
		the information that can affect the result of the
		expression.</p></dd><dt><a href="#dt-external-function">external function</a></dt><dd><p>
                                    <b>External functions</b> 
                           can be characterized as functions that are neither
                           part of the processor implementation, nor written in a language whose semantics
                           are under the control of this family of specifications. The semantics of external
                           functions, including any context dependencies, are entirely implementation-defined. 
                           In XSLT, external functions are called 
                           <a href="https://www.w3.org/TR/xslt-30/#extension-functions">Section 
            
               24.1 Extension Functions
         </a><sup><small>XT30</small></sup>. </p></dd><dt><a href="#dt-filter-expression">filter expression</a></dt><dd><p>
               A <b>filter expression</b> is an expression in the form <code>E1[E2]</code>: 
               its effect is
               to return those items from the value of <code>E1</code> that
               satisfy the predicate in E2.</p></dd><dt><a href="#dt-fixed-focus">fixed focus</a></dt><dd><p>A <b>fixed focus</b> is a focus for an expression that is evaluated once, 
                  rather than being applied to a series of values; in a fixed focus, 
                  the context value is set to one specific value, the context position is 1, and the context size is 1.</p></dd><dt><a href="#dt-focus">focus</a></dt><dd><p>The first three components of
the <a title="dynamic context" class="termref" href="#dt-dynamic-context">dynamic context</a>
(context value, context position, and context size) are called the
<b>focus</b> of the expression. </p></dd><dt><a href="#dt-focus-function">focus function</a></dt><dd><p>A <b>focus function</b>
               is an inline function expression in which the function signature is implicit: the function takes
               a single argument of type <code>item()*</code> (that is, any value), and binds this to the 
                  context value when evaluating
               the function body, which returns a result of type <code>item()*</code>.</p></dd><dt><a href="#dt-function-coercion">function coercion</a></dt><dd><p>
                     <b>Function coercion</b> wraps a <a title="function item" class="termref" href="#dt-function-item">function item</a>
        in a new function whose signature is the same as the expected type.
        This effectively delays the checking
        of the argument and return types
        until the function is called.</p></dd><dt><a href="#dt-function-definition">function definition</a></dt><dd><p>A <b>function definition</b>
                  contains information used to evaluate a static function call, including the name, parameters,
                  and return type of the function.</p></dd><dt><a href="#dt-function-item">function item</a></dt><dd><p>A <b>function item</b> is an item that can
         be called using a <a title="dynamic function call" class="termref" href="#dt-dynamic-function-call">dynamic function call</a>.</p></dd><dt><a href="#dt-generalized-atomic-type">generalized atomic type</a></dt><dd><p>A <b>generalized atomic type</b> is an 
                        <a title="item type" class="termref" href="#dt-item-type">item type</a> whose instances are all
                        atomic items. Generalized atomic types include (a) 
                        <a title="atomic type" class="termref" href="#dt-atomic-type">atomic types</a>, either built-in
                        (for example <code>xs:integer</code>) or imported from a schema, 
                        (b) <a title="pure union type" class="termref" href="#dt-pure-union-type">pure union types</a>, either built-in
                        (<code>xs:numeric</code> and <code>xs:error</code>) or imported from a schema,
                        (c) <a title="choice item type" class="termref" href="#dt-choice-item-type">choice item types</a> if their alternatives
                        are all generalized atomic types, and 
                        (d) <a title="enumeration type" class="termref" href="#dt-enumeration-type">enumeration types</a>.
                     </p></dd><dt><a href="#id-static-decimal-format-grouping-separator">grouping-separator</a></dt><dd><p>
                              <b>grouping-separator</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is used to 
                                 separate groups of digits (for example as a thousands separator).
                                 The default value for both the marker and the rendition is <span class="unicode-codepoint">U+002C</span> (<span class="unicode-name">COMMA</span>, <code>,</code>) .</p></dd><dt><a href="#dt-guarded">guarded</a></dt><dd><p>An expression <var>E</var> is said to be <b>guarded</b>
               by some governing condition <var>C</var> if evaluation of <var>E</var> is not allowed to fail
               with a <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic error</a> except when <var>C</var> applies.</p></dd><dt><a href="#dt-host-language">host language</a></dt><dd><p>
		A <b>host language</b> for XPath is any environment that provides capabilities
		for XPath expressions to be defined and evaluated, and that supplies a static and dynamic
		context for their evaluation.
	</p></dd><dt><a href="#IgnorableWhitespace">ignorable whitespace</a></dt><dd><p>
                        <b>Ignorable whitespace</b> consists of any <a title="whitespace" class="termref" href="#Whitespace">whitespace</a> characters that may occur between <a title="terminal" class="termref" href="#terminal">terminals</a>, unless these characters occur in the context of a production
            marked with a <a href="#ExplicitWhitespaceHandling"> ws:explicit</a> annotation, in
            which case they can occur only where explicitly specified (see <a href="#ExplicitWhitespaceHandling"><b>A.3.5.2 Explicit Whitespace Handling</b></a>).</p></dd><dt><a href="#dt-implausible">implausible</a></dt><dd><p>Certain expressions, while not
            erroneous, are classified as being <b>implausible</b>,
            because they achieve no useful effect.</p></dd><dt><a href="#dt-implementation-defined">implementation defined</a></dt><dd><p>
                        <b>Implementation-defined</b> indicates an aspect that may differ
					between implementations, but must be specified by the implementer for each
					particular implementation.</p></dd><dt><a href="#dt-implementation-dependent">implementation   dependent</a></dt><dd><p>
                        <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 implementer for any particular
					implementation.</p></dd><dt><a href="#dt-timezone">implicit timezone</a></dt><dd><p>
                           <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 arithmetic operation. The implicit timezone is an  <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a> value of type
<code>xs:dayTimeDuration</code>. See <a href="https://www.w3.org/TR/xmlschema-2/#dateTime-timezones">Section 
                  
                     3.2.7.3 Timezones
               </a><sup><small>XS1-2</small></sup> or
<a href="https://www.w3.org/TR/xmlschema11-2/#dateTime">Section 
               
                  3.3.7 dateTime
            </a><sup><small>XS11-2</small></sup> for the range of valid values of a timezone.</p></dd><dt><a href="#id-static-decimal-format-infinity">infinity</a></dt><dd><p>
                              <b>infinity</b>
                              <em>(<var>R</var>)</em>
                                 is the string used to represent the double value infinity (<code>INF</code>); the
                                 default value is the string <code>"Infinity"</code>
                           </p></dd><dt><a href="#dt-inline-func">inline function expression</a></dt><dd><p>An 
                  <b>inline function expression</b>
                     <span>, when evaluated,</span> creates
          an <a title="anonymous function" class="termref" href="#dt-anonymous-function">anonymous function</a>
          defined directly in the inline function expression.</p></dd><dt><a href="#dt-is-attrs">in-scope attribute declarations</a></dt><dd><p>
                                 <b>In-scope attribute
declarations.</b> Each attribute declaration is identified either
by an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> (for a top-level attribute declaration) or by an
<a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> attribute identifier (for a local attribute
declaration).  
                           </p></dd><dt><a href="#dt-is-elems">in-scope element declarations</a></dt><dd><p>
                                 <b>In-scope element declarations.</b> Each element
declaration is identified either by an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> (for a top-level element
declaration) or by an <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> element identifier (for a
local element declaration). 
                           </p></dd><dt><a href="#dt-in-scope-named-item-types">in-scope named item types</a></dt><dd><p>
                           <b>In-scope named item types.</b> This is a mapping from 
                        <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a> to 
                        <a title="named item type" class="termref" href="#dt-named-item-type">named item types</a>.</p></dd><dt><a href="#dt-in-scope-namespaces">in-scope namespaces</a></dt><dd><p>The <b>in-scope namespaces</b> property of an element node is a set of 
         <a title="namespace binding" class="termref" href="#dt-namespace-binding">namespace bindings</a>, each of which associates a namespace prefix with a URI.</p></dd><dt><a href="#dt-issd">in-scope schema definitions</a></dt><dd><p>
                           <b>In-scope schema
			 definitions</b> is a generic term
			 for all the element declarations, attribute declarations, and schema type
			 definitions that are in scope during
			 static analysis of an expression.</p></dd><dt><a href="#dt-is-types">in-scope schema type</a></dt><dd><p>
                                 <b>In-scope schema types.</b> Each schema type
			        definition is identified either by an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded
			        QName</a> (for a <b>named type</b>)
			        or by an <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a> type
			        identifier (for an <b>anonymous
			        type</b>). The in-scope schema types include the predefined schema types described in <a href="#id-predefined-types"><b>3.5 Schema Types</b></a>.

                                
                           </p></dd><dt><a href="#dt-in-scope-variables">in-scope variables</a></dt><dd><p>
                           <b>In-scope variables.</b> 
                           This is a mapping from <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a> to sequence types. It defines the
                           set of variables that are available for reference within an
                           expression. The <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> is the name of the variable, and the type is the
                           <a title="static type" class="termref" href="#dt-static-type">static type</a> of the
                           variable.</p></dd><dt><a href="#dt-item">item</a></dt><dd><p>
  An <b>item</b> is either an <a title="atomic item" class="termref" href="#dt-atomic-item">atomic item</a>, a <a title="node" class="termref" href="#dt-node">node</a>,
or a <a title="function item" class="termref" href="#dt-function-item">function item</a>.</p></dd><dt><a href="#dt-item-type">item type</a></dt><dd><p>An <b>item type</b> is a type that can be expressed using the <a href="#doc-xpath40-ItemType">ItemType</a> syntax, which forms part of the <a href="#doc-xpath40-SequenceType">SequenceType</a>
            syntax. Item types match individual <a title="item" class="termref" href="#dt-item">items</a>.</p></dd><dt><a href="#dt-item-type-designator">item type designator</a></dt><dd><p>An
         <b>item type designator</b> is a syntactic construct conforming to the grammar rule
         <a href="#doc-xpath40-ItemType">ItemType</a>. An item type designator is said
         to <b>designate</b> an <a title="item type" class="termref" href="#dt-item-type">item type</a>.</p></dd><dt><a href="#dt-kind-test">kind test</a></dt><dd><p>An alternative
		  form of a node test called a
		  <b>kind test</b> can select nodes based
		  on their kind, name, and <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a>.</p></dd><dt><a href="#dt-qname">lexical QName</a></dt><dd><p>A
          <b>lexical QName</b> is a name that conforms to the syntax of the
          <a href="#prod-xpath40-QName">QName</a> production</p></dd><dt><a href="#dt-literal">literal</a></dt><dd><p>A <b>literal</b> is a direct syntactic representation of an
		atomic item.</p></dd><dt><a href="#dt-literal-terminal">literal terminal</a></dt><dd><p>A <b>literal terminal</b> is a token appearing as a string 
        in quotation marks on the right-hand side of an <a title="ordinary production rule" class="termref" href="#dt-ordinary-production-rule">ordinary production rule</a>.</p></dd><dt><a href="#dt-map">map</a></dt><dd><p>A <b>map</b> is a function
  that associates a set of keys with values, resulting in a collection
  of key / value pairs.</p></dd><dt><a href="#dt-mapping-arrow-operator">mapping arrow operator</a></dt><dd><p>
            The <b>mapping arrow operator</b>
                     <code>=!&gt;</code> applies a function to each
            item in a sequence.</p></dd><dt><a href="#may">may</a></dt><dd><p>
               <b>MAY</b> means that an item is truly
            optional.</p></dd><dt><a href="#dt-member">member</a></dt><dd><p>The values of an array are called
      its <b>members</b>.</p></dd><dt><a href="#dt-method">method</a></dt><dd><p>A <b>method</b> is a
               function item that has the annotation <code>%method</code>.</p></dd><dt><a href="#id-static-decimal-format-minus-sign">minus-sign</a></dt><dd><p>
                              <b>minus-sign</b>
                              <em>(<var>R</var>)</em> is the string used to mark negative numbers; the
                                 default value is <span class="unicode-codepoint">U+002D</span> (<span class="unicode-name">HYPHEN-MINUS</span>, <code>-</code>) .</p></dd><dt><a href="#must">must</a></dt><dd><p>
               <b>MUST</b> means that the item is an absolute
            requirement of the specification.</p></dd><dt><a href="#mustnot">must not</a></dt><dd><p>
               <b>MUST NOT</b> means that the item is an
            absolute prohibition of the specification.</p></dd><dt><a href="#dt-named-function-ref">named function reference</a></dt><dd><p>
          A <b>named function reference</b> is an expression (written <code>name#arity</code>)
                  which evaluates to a <a title="function item" class="termref" href="#dt-function-item">function item</a>, <span>the details
                  of the function item being based on the properties of a <a title="function definition" class="termref" href="#dt-function-definition">function definition</a>
                  in the <a title="static context" class="termref" href="#dt-static-context">static context</a>
                     </span>.</p></dd><dt><a href="#dt-named-item-type">named item type</a></dt><dd><p>A <b>named item type</b>
                      is an <code>ItemType</code> identified by an <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a>.</p></dd><dt><a href="#dt-namespace-binding">namespace binding</a></dt><dd><p>A <b>namespace binding</b>
         is a pair comprising a namespace prefix (which is either an <code>xs:NCName</code> or empty),
         and a namespace URI.</p></dd><dt><a href="#dt-namespace-sensitive">namespace-sensitive</a></dt><dd><p>The <b>namespace-sensitive</b>
                     types are <code>xs:QName</code>, <code>xs:NOTATION</code>, types
                     derived by restriction from <code>xs:QName</code> or
                     <code>xs:NOTATION</code>, list types that have a namespace-sensitive
                     item type, and union types with a namespace-sensitive type in their
                     transitive membership.</p></dd><dt><a href="#dt-name-test">name test</a></dt><dd><p>A node test that consists only of an EQName or a
		  Wildcard is called a <b>name test</b>.</p></dd><dt><a href="#id-static-decimal-format-NaN">NaN</a></dt><dd><p>
                              <b>NaN</b>
                              <em>(<var>R</var>)</em>
                                 is the string used to
                                 represent the double value <code>NaN</code> (not a number); the default value is the string <code>"NaN"</code>
                           </p></dd><dt><a href="#dt-node">node</a></dt><dd><p>A <b>node</b> is an instance of one of the
	  <b>node kinds</b> defined in <span class="markup-error">[TITLE OF DM40 SPEC, TITLE OF Node SECTION]<sup><small>DM40</small></sup></span>.</p></dd><dt><a href="#dt-node-test">node test</a></dt><dd><p>A <b>node test</b> is a condition on the name, kind (element, attribute, text, document, comment,
		  or processing instruction), and/or  <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of a node. 
      A node test determines which nodes contained by an axis are selected by a <a title="step" class="termref" href="#dt-step">step</a>.</p></dd><dt><a href="#dt-no-namespace-rule">no-namespace rule</a></dt><dd><p>When an unprefixed lexical QName
               is expanded using the <b>no-namespace rule</b>, it is interpreted as having an absent namespace URI.</p></dd><dt><a href="#non-delimiting-token">non-delimiting terminal symbol</a></dt><dd><p>The
            <b>non-delimiting terminal symbols</b> are: <code>ancestor</code>
                     <code>ancestor-or-self</code>
                     <code>and</code>
                     <code>array</code>
                     <code>as</code>
                     <code>at</code>
                     <code>attribute</code>
                     <code>cast</code>
                     <code>castable</code>
                     <code>child</code>
                     <code>comment</code>
                     <code>descendant</code>
                     <code>descendant-or-self</code>
                     <code>div</code>
                     <code>document-node</code>
                     <code>element</code>
                     <code>else</code>
                     <code>empty-sequence</code>
                     <code>enum</code>
                     <code>eq</code>
                     <code>every</code>
                     <code>except</code>
                     <code>fn</code>
                     <code>following</code>
                     <code>following-or-self</code>
                     <code>following-sibling</code>
                     <code>following-sibling-or-self</code>
                     <code>for</code>
                     <code>function</code>
                     <code>ge</code>
                     <code>gt</code>
                     <code>idiv</code>
                     <code>if</code>
                     <code>in</code>
                     <code>instance</code>
                     <code>intersect</code>
                     <code>is</code>
                     <code>item</code>
                     <code>items</code>
                     <code>key</code>
                     <code>keys</code>
                     <code>le</code>
                     <code>let</code>
                     <code>lt</code>
                     <code>map</code>
                     <code>member</code>
                     <code>mod</code>
                     <code>namespace</code>
                     <code>namespace-node</code>
                     <code>ne</code>
                     <code>node</code>
                     <code>of</code>
                     <code>or</code>
                     <code>otherwise</code>
                     <code>pairs</code>
                     <code>parent</code>
                     <code>preceding</code>
                     <code>preceding-or-self</code>
                     <code>preceding-sibling</code>
                     <code>preceding-sibling-or-self</code>
                     <code>processing-instruction</code>
                     <code>record</code>
                     <code>return</code>
                     <code>satisfies</code>
                     <code>schema-attribute</code>
                     <code>schema-element</code>
                     <code>self</code>
                     <code>some</code>
                     <code>text</code>
                     <code>then</code>
                     <code>to</code>
                     <code>treat</code>
                     <code>union</code>
                     <code>value</code>
                     <code>values</code>
                     <a href="#prod-xpath40-BinaryIntegerLiteral">BinaryIntegerLiteral</a>
                     <a href="#prod-xpath40-DecimalLiteral">DecimalLiteral</a>
                     <a href="#prod-xpath40-DoubleLiteral">DoubleLiteral</a>
                     <a href="#prod-xpath40-HexIntegerLiteral">HexIntegerLiteral</a>
                     <a href="#prod-xpath40-IntegerLiteral">IntegerLiteral</a>
                     <a href="#prod-xpath40-NCName">NCName</a>
                     <a href="#prod-xpath40-QName">QName</a>
                     <a href="#prod-xpath40-URIQualifiedName">URIQualifiedName</a>
                  </p></dd><dt><a href="#dt-numeric">numeric</a></dt><dd><p>The type <code>xs:numeric</code>
               is defined as a union type with member types <code>xs:double</code>, 
               <code>xs:float</code>, and <code>xs:decimal</code>. An item that
               is an instance of any of these types is referred to as a <b>numeric value</b>,
               and a type that is a subtype of <code>xs:numeric</code> is referred to
               as a <b>numeric type</b>.</p></dd><dt><a href="#dt-ordinary-production-rule">ordinary production rule</a></dt><dd><p>An <b>ordinary production rule</b> 
        is a production rule in <a href="#id-grammar"><b>A.1 EBNF</b></a> that is not annotated <code>ws:explicit</code>.</p></dd><dt><a href="#dt-partial-function-application">partial function application</a></dt><dd><p>
                     A static or <a title="dynamic function call" class="termref" href="#dt-dynamic-function-invocation">dynamic</a>
                     function call is a <b>partial function application</b>
                     if one or more arguments is an <a href="#prod-xpath40-ArgumentPlaceholder">ArgumentPlaceholder</a>.</p></dd><dt><a href="#dt-partially-applied-function">partially applied function</a></dt><dd><p>A <b>partially applied function</b>
                  is a function created by  <a title="partial function application" class="termref" href="#dt-partial-function-application">partial function application</a>.</p></dd><dt><a href="#dt-path-expression">path expression</a></dt><dd><p>A path expression consists of a series of one or more
	              <a title="step" class="termref" href="#dt-step">steps</a>, separated by <code>/</code> or
	 <code>//</code>, and optionally beginning with
               <code>/</code> or <code>//</code>.  
               A <b>path expression</b> is typically used to locate nodes
               within trees. </p></dd><dt><a href="#id-static-decimal-format-pattern-separator">pattern-separator</a></dt><dd><p>
                              <b>pattern-separator</b>
                              <em>(<var>M</var>)</em> is a character used
                                 to separate positive and negative sub-pictures
                                 in a picture string; the default value is <span class="unicode-codepoint">U+003B</span> (<span class="unicode-name">SEMICOLON</span>, <code>;</code>) .</p></dd><dt><a href="#id-static-decimal-format-percent">percent</a></dt><dd><p>
                              <b>percent</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is used to 
                                 indicate that the number is written as a per-hundred fraction; the default
                                 value for both the marker and the rendition is <span class="unicode-codepoint">U+0025</span> (<span class="unicode-name">PERCENT SIGN</span>, <code>%</code>) .</p></dd><dt><a href="#id-static-decimal-format-per-mille">per-mille</a></dt><dd><p>
                              <b>per-mille</b>
                              <em>(<var>M</var>, <var>R</var>)</em> is used to 
                                 indicate that the number is written as a per-thousand fraction; the default
                                 value for both the marker and the rendition is <span class="unicode-codepoint">U+2030</span> (<span class="unicode-name">PER MILLE SIGN</span>, <code>‰</code>) .</p></dd><dt><a href="#dt-pipeline-operator">pipeline operator</a></dt><dd><p>
            The <b>pipeline operator</b>
                  <code>-&gt;</code> evaluates an expression and
            binds the result to the context value before evaluating another expression.</p></dd><dt><a href="#dt-predicate-truth-value">predicate truth value</a></dt><dd><p>The
            <b>predicate truth value</b> of a value <code>$V</code>
            is the result of the expression <code>if ($V instance of xs:numeric+)
            then ($V = position()) else fn:boolean($V)</code>.</p></dd><dt><a href="#dt-primary-expression">primary expression</a></dt><dd><p>
                  <b>Primary expressions</b> are the basic primitives of the
	 language. They include literals, variable references, context value references,  and function calls. A primary expression may also be created by enclosing any expression in parentheses, which is sometimes helpful in controlling the precedence of operators.</p></dd><dt><a href="#dt-principal-node-kind">principal node kind</a></dt><dd><p>Every axis has a <b>principal node kind</b>. If an axis can
		  contain elements, then the principal node kind is element; otherwise, it is the
		  kind of nodes that the axis can contain.</p></dd><dt><a href="#dt-pure-union-type">pure union type</a></dt><dd><p>A <b>pure union type</b> is a <span>
                        <b>simple type</b>
                     </span> 
                     that satisfies the following constraints:
                     (a) <a href="https://www.w3.org/TR/xmlschema11-1/#std-variety">{variety}<sup><small>XS11-1</small></sup></a> is <code>union</code>, 
                     (b) the <a href="https://www.w3.org/TR/xmlschema11-1/#std-facets">{facets}<sup><small>XS11-1</small></sup></a> property is empty, 
                     (c) no type in the transitive membership of the union type has 
                     <a href="https://www.w3.org/TR/xmlschema11-1/#std-variety">{variety}<sup><small>XS11-1</small></sup></a>
                     <code>list</code>, and 
                     (d) no type in the transitive membership of the union type is a type with 
                     <a href="https://www.w3.org/TR/xmlschema11-1/#std-variety">{variety}<sup><small>XS11-1</small></sup></a>
                     <code>union</code> having a non-empty <a href="https://www.w3.org/TR/xmlschema11-1/#std-facets">{facets}<sup><small>XS11-1</small></sup></a> property</p></dd><dt><a href="#dt-resolve-relative-uri">resolve</a></dt><dd><p>To
      <b>resolve a relative URI</b>
                     <code>$rel</code> against a
      base URI <code>$base</code> is to expand it to an absolute URI,
      as if by calling the function <code>fn:resolve-uri($rel,
      $base)</code>.</p></dd><dt><a href="#dt-reverse-document-order">reverse document order</a></dt><dd><p>The node ordering that is the reverse of document order is called <b>reverse document order</b>.</p></dd><dt><a href="#dt-same-key">same key</a></dt><dd><p>Two atomic items <code>K1</code> and
    <code>K2</code> have the <b>same key value</b> if
    
    
      <code>fn:atomic-equal(K1, K2)</code> returns <code>true</code>, as specified in <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-atomic-equal">Section 14.2.1 fn:atomic-equal</a><sup><small>FO</small></sup>
                     </p></dd><dt><a href="#dt-schema-type">schema type</a></dt><dd><p>A <b>schema type</b>
            is a complex type or simple type as defined in the <a href="#XMLSchema10">[XML Schema 1.0]</a> or <a href="#XMLSchema11">[XML Schema 1.1]</a> specifications, including built-in types as well as user-defined types.</p></dd><dt><a href="#dt-sequence">sequence</a></dt><dd><p>A
<b>sequence</b> is an ordered collection of zero or more
<a title="item" class="termref" href="#dt-item">items</a>.</p></dd><dt><a href="#dt-sequence-arrow-operator">sequence arrow operator</a></dt><dd><p>
               The <b>sequence arrow operator</b>
                     <code>=&gt;</code> applies a function to a
               supplied sequence.</p></dd><dt><a href="#dt-sequence-concatenation">sequence concatenation</a></dt><dd><p>The 
               <b>sequence concatenation</b> of a number of sequences <var>S<sub>1</sub></var>, <var>S<sub>2</sub></var>, ... <var>S<sub>n</sub></var>
               is defined to be the sequence formed from the items of <var>S<sub>1</sub></var>, followed by the items
               from <var>S<sub>2</sub></var>, and so on, retaining order.</p></dd><dt><a href="#dt-sequence-type">sequence type</a></dt><dd><p>A <b>sequence type</b> is a type that can be expressed using the <a href="#doc-xpath40-SequenceType">SequenceType</a>
                  syntax. Sequence types are used whenever it is necessary to refer to a type in an XPath 4.0 expression. 
                  Since all values are sequences, every value matches one or more <b>sequence types</b>.</p></dd><dt><a href="#dt-sequence-type-designator">sequence type designator</a></dt><dd><p>A
      <b>sequence type designator</b> is a syntactic construct conforming to the grammar rule
         <a href="#doc-xpath40-SequenceType">SequenceType</a>. A sequence type designator is said
      to <b>designate</b> a <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>.</p></dd><dt><a href="#dt-sequencetype-matching">SequenceType matching</a></dt><dd><p>
                     <b>SequenceType matching</b> compares a value with an expected <a title="sequence type" class="termref" href="#dt-sequence-type">sequence type</a>. </p></dd><dt><a href="#dt-serialization">serialization</a></dt><dd><p>
                     <b>Serialization</b> is the process of
converting an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM
instance</a> to a sequence of octets (step DM4 in Figure 1.),
as described in <a href="#xslt-xquery-serialization-40">[XSLT and XQuery Serialization 4.0]</a>.</p></dd><dt><a href="#dt-singleton">singleton</a></dt><dd><p>A sequence containing exactly one item is called a
	 <b>singleton</b>.</p></dd><dt><a href="#dt-singleton-enumeration-type">singleton enumeration type</a></dt><dd><p>An 
                     enumeration type with a single enumerated value (such as
                     <code>enum("red")</code>) is an anonymous <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a>
                     derived from <code>xs:string</code> by restriction using an enumeration facet
                     that permits only the value <code>"red"</code>. This is referred to
                     as a <b>singleton enumeration type</b>.</p></dd><dt><a href="#dt-singleton-focus">singleton focus</a></dt><dd><p>A <b>singleton focus</b> is a <a title="fixed focus" class="termref" href="#dt-fixed-focus">fixed focus</a> in which the
                  <a title="context value" class="termref" href="#dt-context-value">context value</a> is a <a title="singleton" class="termref" href="#dt-singleton">singleton</a> item.</p></dd><dt><a href="#stable">stable</a></dt><dd><p>Document order is <b>stable</b>, which means that the relative order of two nodes will not change during the processing of a given 
                  <span class="xpath">expression</span>, even if this order is <a title="implementation   dependent" class="termref" href="#dt-implementation-dependent">implementation-dependent</a>.</p></dd><dt><a href="#dt-static-collations">statically known collations</a></dt><dd><p>
                           <b>Statically known collations.</b> This is an <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>
        mapping from URI to collation. It defines the names of the collations that are available for
				use in processing  expressions.</p></dd><dt><a href="#dt-static-decimal-formats">statically known decimal formats</a></dt><dd><p>
                           <b>Statically known decimal
		      formats.</b> This is a mapping from QNames to decimal formats, with one default format that has no visible name,
		      referred to as the unnamed decimal format. Each
		      format is available for use when formatting numbers using the <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-format-number"><code>fn:format-number</code></a> function.</p></dd><dt><a href="#dt-statically-known-function-definitions">statically known function definitions</a></dt><dd><p>
                           <b>Statically known function definitions.</b> This is a set of 
                           <a title="function definition" class="termref" href="#dt-function-definition">function definitions</a>.</p></dd><dt><a href="#dt-static-namespaces">statically known namespaces</a></dt><dd><p>
                           <b>Statically known namespaces.</b> 
        This is a mapping from prefix to namespace URI that defines all the namespaces 
        that are known during static processing of a given expression.</p></dd><dt><a href="#dt-static-analysis">static analysis phase</a></dt><dd><p>The
<b>static analysis phase</b> depends on the expression itself
and on the <a title="static context" class="termref" href="#dt-static-context">static context</a>. The <b>static analysis phase</b> does
not depend on input data (other than schemas).</p></dd><dt><a href="#dt-static-base-uri">Static Base URI</a></dt><dd><p>
                           <b>Static Base URI.</b>
        This is an absolute URI, used to resolve relative URIs during static analysis.
                     </p></dd><dt><a href="#dt-static-context">static context</a></dt><dd><p>The <b>static context</b> of an expression is
		  the information that is available during static analysis of the expression, prior
		  to its evaluation.</p></dd><dt><a href="#dt-static-error">static error</a></dt><dd><p>
An error that can be detected during the static analysis phase, and is not a type error, is a <b>static error</b>.</p></dd><dt><a href="#dt-static-function-call">static function call</a></dt><dd><p>A <b>static function call</b> 
               consists of an EQName followed by a parenthesized list of zero or more arguments.</p></dd><dt><a href="#dt-static-type">static type</a></dt><dd><p>The
<b>static type</b> of an expression is the best inference that
the processor is able to make statically about the type of the result
of the expression.</p></dd><dt><a href="#dt-step">step</a></dt><dd><p>A <b>step</b> is a part of a <a title="path expression" class="termref" href="#dt-path-expression">path expression</a> that generates a sequence of items
		and then filters the sequence by zero or more
		<a title="" class="termref" href="#dt-predicate">predicates</a>. The value of the step
		consists of those items that satisfy the
		predicates, working from left to right. A step may be either an <a title="axis step" class="termref" href="#dt-axis-step">axis step</a> or a postfix expression.</p></dd><dt><a href="#dt-string-value">string value</a></dt><dd><p>The
                  <b>string value</b> of a node is a string and can be extracted
                  by applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-string">Section 2.1.3 fn:string</a><sup><small>FO</small></sup>
                  function to the node.</p></dd><dt><a href="#dt-substantively-disjoint">substantively disjoint</a></dt><dd><p>Two 
               <a title="sequence type" class="termref" href="#dt-sequence-type">sequence types</a> are
                     deemed to be <b>substantively disjoint</b> if (a) neither is a subtype
                     of the other (see <a href="#id-seqtype-subtype"><b>3.3.1 Subtypes of Sequence Types</b></a>) and 
                     (b) the only values that
                     are instances of both types are one or more of the following:</p><ul><li><p>The empty sequence, <code>()</code>.</p></li><li><p>The <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-empty-map">empty map</a><sup><small>DM</small></sup>, <code>{}</code>.</p></li><li><p>The <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#dt-empty-array">empty array</a><sup><small>DM</small></sup>, <code>[]</code>.</p></li></ul></dd><dt><a href="#dt-substitution-group">substitution group</a></dt><dd><p>
                                 <b>Substitution groups</b> are defined in <a href="https://www.w3.org/TR/xmlschema-1/#Element_Equivalence_Class">Section 
                  
                     2.2.2.2 Element Substitution Group
               </a><sup><small>XS1-1</small></sup> and 
<a href="https://www.w3.org/TR/xmlschema11-1/#Element_Equivalence_Class">Section 
                  
                     2.2.2.2 Element Substitution Group
               </a><sup><small>XS11-1</small></sup>. 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></dd><dt><a href="#dt-subtype">subtype</a></dt><dd><p>Given two 
               <a title="sequence type" class="termref" href="#dt-sequence-type">sequence types</a>
               or <a title="item type" class="termref" href="#dt-item-type">item types</a>, the rules in this section determine if one 
               is a <b>subtype</b> of the other. If a type <var>A</var> is a subtype of type <var>B</var>,
               it follows that every value matched by <var>A</var> is also matched by <var>B</var>.</p></dd><dt><a href="#dt-subtype-substitution">subtype substitution</a></dt><dd><p>The use of a value that has a <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a>
                      that is a <a title="subtype" class="termref" href="#dt-subtype">subtype</a> of the
		  expected type is known as <b>subtype substitution</b>.</p></dd><dt><a href="#symbol">symbol</a></dt><dd><p>Each rule in the grammar defines one <b>symbol</b>,
          using the following format: </p><div class="exampleInner"><pre>symbol ::= expression</pre></div></dd><dt><a href="#symbolseparators">symbol separators</a></dt><dd><p>
                     <a title="whitespace" class="termref" href="#Whitespace">Whitespace</a> and <a href="#doc-xpath40-Comment">Comments</a>
          function as <b>symbol separators</b>. For the most part, they are not mentioned in
          the grammar, and may occur between any two terminal symbols mentioned in the grammar,
          except where that is forbidden by the <a href="#ws-explicit">/* ws: explicit */</a>
          annotation in the EBNF, or by the <a href="#parse-note-xml-version">/* xgc: xml-version
            */</a> annotation.</p></dd><dt><a href="#dt-system-function">system function</a></dt><dd><p>
                                    <b>System functions</b> include the functions defined in <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>, functions defined by the specifications 
                           of a host language, <a title="constructor function" class="termref" href="#dt-constructor-function">constructor functions</a> 
                           for atomic types, and any additional functions provided
                           by the implementation. System functions are sometimes called built-in
                           functions.</p></dd><dt><a href="#terminal">terminal</a></dt><dd><p>A <b>terminal</b> is a symbol or string or
          pattern that can appear in the right-hand side of a rule, but never appears on the
          left-hand side in the main grammar, although it may appear on the left-hand side of a rule
          in the grammar for terminals.</p></dd><dt><a href="#dt-type-annotation">type annotation</a></dt><dd><p>Each element node and attribute node in an <a title="XDM instance" class="termref" href="#dt-data-model-instance">XDM instance</a> has a <b>type annotation</b> (described in <a href="https://qt4cg.org/specifications/xpath-datamodel-40/#types">Section 4.1 Schema Information</a><sup><small>DM</small></sup>). 
The type annotation of a node is a reference to a <a title="schema type" class="termref" href="#dt-schema-type">schema type</a>. 
</p></dd><dt><a href="#dt-typed-value">typed value</a></dt><dd><p>The <b>typed
                     value</b> of a node is a sequence of atomic items and can be
                  extracted by applying the <a href="https://qt4cg.org/specifications/xpath-functions-40/#func-data">Section 2.1.4 fn:data</a><sup><small>FO</small></sup> function to the
                  node.</p></dd><dt><a href="#dt-type-error">type error</a></dt><dd><p>A <b>type
error</b> may be raised during the static analysis phase or the dynamic evaluation phase.
During the static analysis phase, a <a title="type error" class="termref" href="#dt-type-error">type error</a> occurs
when the <a title="static type" class="termref" href="#dt-static-type">static type</a> of an expression does not match the expected type
of the context in which the expression occurs.
During the dynamic evaluation phase, a <a title="type error" class="termref" href="#dt-type-error">type error</a> occurs
when the <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a> of a value does not match the expected type of
the context in which the value occurs.</p></dd><dt><a href="#dt-URI">URI</a></dt><dd><p>Within this specification, the term <b>URI</b> refers to a Universal Resource Identifier as defined in <a href="#RFC3986">[RFC3986]</a> and extended in <a href="#RFC3987">[RFC3987]</a> with the new name <b>IRI</b>.</p></dd><dt><a href="#dt-value">value</a></dt><dd><p>In the <a title="data model" class="termref" href="#dt-datamodel">data model</a>, a <b>value</b> is always a <a title="sequence" class="termref" href="#dt-sequence">sequence</a>.</p></dd><dt><a href="#dt-variable-reference">variable reference</a></dt><dd><p>A <b>variable reference</b> is an EQName preceded by a $-sign.</p></dd><dt><a href="#dt-variable-terminal">variable terminal</a></dt><dd><p>A <b>variable terminal</b> is an instance
        of a production rule that is not itself an <a title="ordinary production rule" class="termref" href="#dt-ordinary-production-rule">ordinary production rule</a> but that is named (directly) on the right-hand
        side of an <a title="ordinary production rule" class="termref" href="#dt-ordinary-production-rule">ordinary production rule</a>.</p></dd><dt><a href="#dt-variable-values">variable values</a></dt><dd><p>
                           <b>Variable values</b>. 
        This is a mapping from <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a> to values. 
        It contains the
				same <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QNames</a> as the <a title="in-scope variables" class="termref" href="#dt-in-scope-variables">in-scope variables</a> in the
				<a title="static context" class="termref" href="#dt-static-context">static context</a> for the expression. The <a title="expanded QName" class="termref" href="#dt-expanded-qname">expanded QName</a> is the name of the variable and the value is the dynamic value of the variable, which includes its <a title="dynamic type" class="termref" href="#dt-dynamic-type">dynamic type</a>.</p></dd><dt><a href="#dt-warning">warning</a></dt><dd><p>In addition to <a title="static error" class="termref" href="#dt-static-error">static errors</a>, <a title="dynamic error" class="termref" href="#dt-dynamic-error">dynamic errors</a>, and <a title="type error" class="termref" href="#dt-type-error">type
errors</a>, an XPath 4.0
implementation may raise <b>warnings</b>, either during the <a title="static analysis phase" class="termref" href="#dt-static-analysis">static analysis
phase</a> or the
<a title="dynamic evaluation phase" class="termref" href="#dt-dynamic-evaluation">dynamic evaluation
phase</a>. The circumstances in which warnings are raised, and
the ways in which warnings are handled, are <a title="implementation defined" class="termref" href="#dt-implementation-defined">implementation-defined</a>.</p></dd><dt><a href="#Whitespace">whitespace</a></dt><dd><p>A <b>whitespace</b> character is any of
            the characters defined by <a href="http://www.w3.org/TR/REC-xml/#NT-S">
              [http://www.w3.org/TR/REC-xml/#NT-S]</a>.</p></dd><dt><a href="#dt-wildcard-matches">wildcard-matches</a></dt><dd><p>In these rules, if <var>MU</var> and <var>NU</var> 
                     are <a href="#prod-xpath40-NameTestUnion">NameTestUnions</a>, 
                     then <var>MU</var>
                           <b>wildcard-matches</b>
                           <var>NU</var> is true if every name that matches <var>MU</var>
                     also matches <var>NU</var>.</p></dd><dt><a href="#dt-data-model-instance">XDM instance</a></dt><dd><p>The term <b>XDM instance</b> is used,
    synonymously with the term <a title="value" class="termref" href="#dt-value">value</a>, to denote an unconstrained
    <a title="sequence" class="termref" href="#dt-sequence">sequence</a> of <a title="item" class="termref" href="#dt-item">items</a>.</p></dd><dt><a href="#dt-xpath-compat-mode">XPath 1.0 compatibility     mode</a></dt><dd><p>
                           <b>XPath 1.0 compatibility
			 mode.</b>
                           <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>
                        </p></dd><dt><a href="#dt-anyAtomicType">xs:anyAtomicType</a></dt><dd><p>
                        <code>xs:anyAtomicType</code> is an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a> 
                     that includes all atomic items (and no values that
                     are not atomic). Its base type is
                     <code>xs:anySimpleType</code> from which all simple types, including atomic,
                     list, and union types, are derived. All primitive atomic types, such as
                     <code>xs:decimal</code> and <code>xs:string</code>, have <code>xs:anyAtomicType</code> as their base type.</p></dd><dt><a href="#dt-dayTimeDuration">xs:dayTimeDuration</a></dt><dd><p>
                        <code>xs:dayTimeDuration</code> is derived by restriction from <code>xs:duration</code>. The  lexical representation of <code>xs:dayTimeDuration</code>
                     is restricted to contain only day, hour, minute, and second
                     components.</p></dd><dt><a href="#dt-xs-error">xs:error</a></dt><dd><p>
                        <code>xs:error</code> is a simple type with no value space.  It is defined in <a href="https://www.w3.org/TR/xmlschema11-1/#xsd-error">Section 
                  
                     3.16.7.3 xs:error
                  
               </a><sup><small>XS11-1</small></sup> and  can be used in the <a href="#id-sequencetype-syntax"><b>3.1 Sequence Types</b></a> to raise errors.</p></dd><dt><a href="#dt-untyped">xs:untyped</a></dt><dd><p>
                        <code>xs:untyped</code> is  used as the <a title="type annotation" class="termref" href="#dt-type-annotation">type annotation</a> of an element node that has not been validated, or has been validated in <code>skip</code> mode.</p></dd><dt><a href="#dt-untypedAtomic">xs:untypedAtomic</a></dt><dd><p>
                        <code>xs:untypedAtomic</code>
                     is an <a title="atomic type" class="termref" href="#dt-atomic-type">atomic type</a> that is used to denote untyped atomic data, 
                     such as text that has not been assigned a more specific type.</p></dd><dt><a href="#dt-yearMonthDuration">xs:yearMonthDuration</a></dt><dd><p>
                        <code>xs:yearMonthDuration</code> is derived by restriction from <code>xs:duration</code>. The lexical representation of <code>xs:yearMonthDuration</code> is
                     restricted to contain only year and month
                     components.</p></dd><dt><a href="#id-static-decimal-format-zero-digit">zero-digit</a></dt><dd><p>
                              <b>zero-digit</b>
                              <em>(<var>M</var>)</em>
                                 is the character used in the picture string to represent the digit zero; the default
                                 value is <span class="unicode-codepoint">U+0030</span> (<span class="unicode-name">DIGIT ZERO</span>, <code>0</code>) . This character must be a digit
                                 (category Nd in the Unicode property database), and it must have
                                 the numeric value zero. This property implicitly defines the
                                 ten Unicode characters that are used to represent the values 0
                                 to 9 in the function output: Unicode is organized so that each
                                 set of decimal digits forms a contiguous block of characters in
                                 numerical sequence. Within the picture string any of these ten character 
                                 can be used (interchangeably) as a place-holder for a mandatory digit.
                                 Within the final result string, these ten characters are used to represent
                                 the digits zero to nine.</p></dd></dl></div><div class="div1">
<h2><a id="id-atomic-comparisons"/>G <a href="#id-atomic-comparisons" style="text-decoration: none">Atomic Comparisons: An Overview</a> (Non-Normative)</h2><p>This appendix provides a non-normative summary of the various functions and operators used for comparison
    of atomic items, with some background on the history and rationale.</p><div class="div2">
<h3><a id="id-equality-comparison"/>G.1 <a href="#id-equality-comparison" style="text-decoration: none">Equality Comparisons</a></h3><p>In XPath 4.0 there are essentially four ways of comparing two atomic items for equality:</p><ul><li><p>
                     <code>$A = $B</code>
                  </p><p>This operator was introduced in XPath 1.0. The semantics were changed slightly in XPath 2.0, but the
          original semantics remain available when XPath 1.0 compatibility mode is enabled.</p><p>With a general comparison in XPath 2.0 or later (and in XQuery), the following rules are observed:</p><ul><li><p>Either operand may be a sequence; the result is true if any pair of items from the
            two sequences compares equal.</p><p>In consequence, if either operand is an empty sequence, the result is false.</p></li><li><p>If nodes are supplied, they are atomized.</p></li><li><p>Untyped atomic items appearing in one operand are converted to the type of the
            other operand (if both operands are untyped atomic, they are compared as strings).</p></li><li><p>As a result, the operator is not transitive: the untyped atomic items <code>"4.0"</code>
            and <code>"4"</code> are not equal to each other, but both compare equal to the integer value 
            <code>4</code>.</p></li><li><p>Comparison of certain values is context-sensitive. In particular, comparison of strings
            uses the default collation from the static context, while comparison of date/time values lacking
            an explicit timezone takes the timezone from the dynamic context.</p></li><li><p>NaN is not equal to NaN; negative zero is equal to positive zero.</p></li><li><p>
                           <code>xs:hexBinary</code> and <code>xs:base64Binary</code> values are mutually comparable:
          they are equal if they represent the same sequence of octets.</p></li><li><p>Comparing incompatible values (for example <code>xs:integer</code> and <code>xs:date</code>)
            raises an error.</p></li></ul></li><li><p>
                     <code>$A eq $B</code>
                  </p><p>Value comparisons were introduced in XPath 2.0 and XQuery 1.0. One of the aims was to make
          the comparison transitive (a precondition for a wide variety of optimizations), however in edge
          cases involving comparisons across different numeric types this was not entirely achieved.</p><p>With a value comparison, the rules are:</p><ul><li><p>Each operand must either be a single atomic item, or an empty sequence.</p></li><li><p>If either operand is an empty sequence, the result is an empty sequence; in most
            contexts this has the same effect as returning false.</p></li><li><p>If nodes are supplied, they are atomized.</p></li><li><p>Untyped atomic items are converted to strings (regardless of the type of the other operand).</p></li><li><p>Numeric values of types <code>xs:integer</code>, <code>xs:decimal</code>, or <code>xs:float</code>
            are converted to <code>xs:double</code>.</p><p>This can lead to problems with implementations of <code>xs:decimal</code> that support more precision 
              than can be held in an <code>xs:double</code>.</p></li><li><p>As with general comparisons, the default collation and implicit timezone are taken from the
            context.</p></li><li><p>NaN is not equal to NaN; negative zero is equal to positive zero.</p></li><li><p>
                           <code>xs:hexBinary</code> and <code>xs:base64Binary</code> values are mutually comparable:
            they are equal if they represent the same sequence of octets.</p></li><li><p>Comparing incompatible values (for example <code>xs:integer</code> and <code>xs:date</code>)
            raises an error.</p></li></ul></li><li><p>
                     <code>deep-equal($A, $B)</code>
                  </p><p>As the name implies, the <code>deep-equal</code> function was introduced primarily for comparing nodes,
          or sequences of nodes; however in its simplest form it can also be used to compare two atomic items. The semantics
          of the comparison used by <code>deep-equal($A, $B)</code> are also invoked by a wide variety of other functions
          including <code>distinct-values</code>, <code>all-equal</code>, and <code>all-different</code>; it is also
          used to underpin grouping constructs in both XQuery 4.0 and XSLT 4.0.</p><p>Some of the relevant rules are:</p><ul><li><p>Because <code>deep-equal</code> is used to compare sequences, if one of the operands is an empty
            sequence the result is false; but if both operands are empty sequences, the result is true.</p></li><li><p>If nodes are supplied, they are not atomized; they are compared as nodes.</p></li><li><p>Strings can be compared using the default collation or using an explicitly specified collation;
            there are also options to compare after normalizing whitespace or unicode.</p></li><li><p>Comparisons of dates and times lacking a timezone uses the implicit timezone from the dynamic
            context.</p></li><li><p>Numeric values are converted to <code>xs:decimal</code> prior to comparison, not to <code>xs:double</code>.
            This represents a departure in 4.0 from previous versions of the specification. The conversion must use
            an implementation of <code>xs:decimal</code> that does not cause loss of precision. As a result, the comparison
            is now truly transitive, which makes it suitable to underpin grouping operations.</p></li><li><p>To ensure that every value is equal to itself, comparing NaN to NaN returns true.</p></li><li><p>
                           <code>xs:hexBinary</code> and <code>xs:base64Binary</code> values are mutually comparable:
            they are equal if they represent the same sequence of octets.</p></li><li><p>Comparing incompatible values (for example <code>xs:integer</code> and <code>xs:date</code>)
            returns false; it does not raise an error.</p></li></ul></li><li><p>
                     <code>atomic-equal($A, $B)</code>
                  </p><p>This comparison operation was introduced in XPath 3.0 (and XQuery 3.0) for comparing keys in maps;
          the 4.0 specifications expose it directly as a function that can be called from user applications.
          The dominant requirements for keys in maps were that the comparison should be transitive, error-free, 
          and context-independent. The relevant rules are:</p><ul><li><p>The type signature of the function ensures that it can only be used to compare
            single items; empty sequences do not arise.</p></li><li><p>If nodes are supplied, they are atomized.</p></li><li><p>Strings are compared codepoint-by-codepoint, without reference to any collation or
            normalization.</p></li><li><p>Dates and times lacking a timezone are never equal to dates and times that have a timezone.
            However, when comparing two dates or times that both have a timezone, the timezone is normalized.</p></li><li><p>As with <code>deep-equal</code>, numeric values are converted to <code>xs:decimal</code> 
            prior to comparison, not to <code>xs:double</code>.</p></li><li><p>Comparing NaN to NaN returns true.</p></li><li><p>
                           <code>xs:hexBinary</code> and <code>xs:base64Binary</code> values are distinct:
            both can co-exist as distinct keys in a map even if the underlying sequence of octets is the same.</p></li><li><p>Comparing incompatible values (for example <code>xs:integer</code> and <code>xs:date</code>)
            returns false; it does not raise an error.</p></li></ul></li></ul><p>The following table summarizes these differences. For all these examples it is assumed that
      (a) the default collation is the HTML case-blind collation, (b) the implicit timezone is
      +01:00, and (c) nodes are untyped.</p><table style="border:1px solid" class="small"><thead><tr><th>
                        <code>$A</code>
                     </th><th>
                        <code>$B</code>
                     </th><th>
                        <code>$A = $B</code>
                     </th><th>
                        <code>$A eq $B</code>
                     </th><th>
                        <code>deep-equal(​$A, $B)</code>
                     </th><th>
                        <code>atomic-equal(​$A, $B)</code>
                     </th></tr></thead><tbody><tr><td>
                        <p>
                           <code>()</code>
                        </p>
                     </td><td>
                        <p>
                           <code>()</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>()</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>error</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>12</code>
                        </p>
                     </td><td>
                        <p>
                           <code>()</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>()</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>error</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>(1,2)</code>
                        </p>
                     </td><td>
                        <p>
                           <code>(2,3)</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>error</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>error</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>12</code>
                        </p>
                     </td><td>
                        <p>
                           <code>12e0</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>0.2</code>
                        </p>
                     </td><td>
                        <p>
                           <code>0.2e0</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>NaN</code>
                        </p>
                     </td><td>
                        <p>
                           <code>NaN</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>+0e0</code>
                        </p>
                     </td><td>
                        <p>
                           <code>-0e0</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>"A"</code>
                        </p>
                     </td><td>
                        <p>
                           <code>"a"</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>"A"</code>
                        </p>
                     </td><td>
                        <p>
                           <code>12</code>
                        </p>
                     </td><td>
                        <p>
                           <code>error</code>
                        </p>
                     </td><td>
                        <p>
                           <code>error</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>&lt;a&gt;A&lt;/a&gt;</code>
                        </p>
                     </td><td>
                        <p>
                           <code>"A"</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>&lt;a&gt;12&lt;/a&gt;</code>
                        </p>
                     </td><td>
                        <p>
                           <code>12</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>error</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>xs:time(​'12:00:00Z')</code>
                        </p>
                     </td><td>
                        <p>
                           <code>xs:time(​'13:00:00+01:00')</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>xs:time(​'12:00:00Z')</code>
                        </p>
                     </td><td>
                        <p>
                           <code>xs:time(​'13:00:00')</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td></tr><tr><td>
                        <p>
                           <code>xs:hexBinary(​"0000")</code>
                        </p>
                     </td><td>
                        <p>
                           <code>xs:base64Binary(​"AAA=")</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>true</code>
                        </p>
                     </td><td>
                        <p>
                           <code>false</code>
                        </p>
                     </td></tr></tbody></table></div><div class="div2">
<h3><a id="id-ordering-comparison"/>G.2 <a href="#id-ordering-comparison" style="text-decoration: none">Ordering Comparisons</a></h3><p>In XPath 4.0 there are essentially three ways of comparing two atomic items for their relative ordering:</p><ul><li><p>
                     <code>$A &lt; $B</code>
                  </p></li><li><p>
                     <code>$A lt $B</code>
                  </p></li><li><p>
                     <code>Sorting</code>
                  </p></li></ul><p>TODO: to be expanded.</p></div></div><div class="div1">
<h2><a id="id-incompatibilities"/>H <a href="#id-incompatibilities" style="text-decoration: none">Backwards Compatibility</a> (Non-Normative)</h2><div class="div2">
<h3><a id="id-incompatibilities-31"/>H.1 <a href="#id-incompatibilities-31" style="text-decoration: none">Incompatibilities relative to XPath 3.1</a></h3><p>In <code>fn:format-integer</code>, certain formatting pictures using a circumflex as a grouping separator might
    be interpreted differently in 4.0: for example <code>format-integer(1234, "9^999")</code> would output <code>"1^234"</code>
      in 3.1, but will output <code>"1621"</code> (1234 in base 9) with 4.0. As a workaround, this can be rewritten as 
      <code>format-integer(1234, "0^000")</code>.</p><p>In XPath 4.0, certain expressions are classified as <a title="implausible" class="termref" href="#dt-implausible">implausible</a>: an example
    is <code>@code/text()</code>, which will always return an empty sequence. A processor may report
    a static error when such expressions are encountered; however, processors are <span class="verb">required</span>
    to provide a mode of operation in which such expressions are accepted, thus retaining backwards
    compatibility.</p><p>In expressions that deliver a function item, notably partial function applications, named function references,
    and the <code>fn:function-lookup</code> function, errors may now be detected at the point where the function item
    is created when they were previously detected at the point where the function item was called. This was underspecified
    in previous versions. For example, the partial function application <code>contains(?, 42)</code> is now required to
    raise a type error (because the second argument should be a string, not an integer) at the point where the partial
    function application occurs, not at the point where the resulting function is called.</p><p>As explained in <a href="#id-function-coercion"><b>3.4.4 Function Coercion</b></a>, the fact that coercion rules are now applied
    to global variables and local variable bindings introduces an incompatibility in the case of variables
    whose value is a function item. Previously it was possible to supply a function item that accepted a wider
    range of argument values than those declared in the variable's type declaration; this is no longer the case.</p></div><div class="div2">
<h3><a id="id-incompatibilities-30"/>H.2 <a href="#id-incompatibilities-30" style="text-decoration: none">Incompatibilities relative to XPath 3.0</a></h3><p>The following names are now reserved, and cannot appear as function names (see <a href="#id-reserved-fn-names"><b>A.4 Reserved Function Names</b></a>):</p><ul><li><p>
                     <code>map</code>
                  </p></li><li><p>
                     <code>array</code>
                  </p></li></ul></div><div class="div2">
<h3><a id="id-incompatibilities-10"/>H.3 <a href="#id-incompatibilities-10" style="text-decoration: none">Incompatibilities relative to XPath <span class="xpath">2.0</span>
            </a></h3><p>The following names are now reserved, and cannot appear as function names (see <a href="#id-reserved-fn-names"><b>A.4 Reserved Function Names</b></a>):</p><ul><li><p>
                     <code>function</code>
                  </p></li><li><p>
                     <code>namespace-node</code>
                  </p></li><li><p>
                     <code>switch</code>
                  </p></li></ul><p>If <code>U</code> is a union type with <code>T</code> as one of its members,
  and if <code>E</code> is an element with <code>T</code> as its type annotation,
  the expression <code>E instance of element(*, U)</code>
  returns <code>true</code> in both XPath 3.0 and 3.1.
  In <span class="xpath">XPath 2.0</span>,
  it returns <code>false</code>.</p><div class="note"><p class="prefix"><b>Note:</b></p><p>This is not an incompatibility with XPath 3.0.
 It should be included in XPath 3.0 as an incompatibility with
 <span class="xpath">XPath 2.0</span>,
 but it was discovered after publication.</p></div></div><div class="div2 xpath">
<h3><a id="id-backwards-compatibility"/>H.4 <a href="#id-backwards-compatibility" style="text-decoration: none">Incompatibilities relative to XPath 1.0</a></h3><p>This appendix provides a summary of the areas of incompatibility between XPath 4.0 and
            <a href="#xpath">[XML Path Language (XPath) Version 1.0]</a>. In each of these cases, an XPath 4.0 processor is compatible
        with an XPath 2.0, 3.0, or 3.1 processor.</p><p>Three separate cases are considered:</p><ol class="enumar"><li><p>Incompatibilities that exist when source documents have no schema, and when running
                with XPath 1.0 compatibility mode set to <code>true</code>. This specification has been designed
                to reduce the number of incompatibilities in this situation to an absolute minimum,
                but some differences remain and are listed individually.</p></li><li><p>Incompatibilities that arise when XPath 1.0 compatibility mode is set to <code>false</code>. In
                this case, the number of expressions where compatibility is lost is rather
                greater.</p></li><li><p>Incompatibilities that arise when the source document is processed using a schema
                (whether or not XPath 1.0 compatibility mode is set to <code>true</code>). Processing the
                document with a schema changes the way that the values of nodes are interpreted, and
                this can cause an XPath expression to return different results.</p></li></ol><div class="div3">
<h4><a id="id-incompat-in-true-mode"/>H.4.1 <a href="#id-incompat-in-true-mode" style="text-decoration: none">Incompatibilities when Compatibility Mode is true</a></h4><p>The list below contains all known areas, within the scope of this specification, where an
            XPath 4.0 processor running with compatibility mode set to <code>true</code> will produce different
            results from an XPath 1.0 processor evaluating the same expression, assuming that the
            expression was valid in XPath 1.0, and that the nodes in the source document have no
            type annotations other than <code>xs:untyped</code> and
            <code>xs:untypedAtomic</code>.</p><p>Incompatibilities in the behavior of individual functions are not listed here, but are
            included in an appendix of <a href="#xpath-functions-40">[XQuery and XPath Functions and Operators 4.0]</a>.</p><p>Since both XPath 1.0 and <span>XPath 4.0</span> leave some aspects of the specification
            implementation-defined, there may be incompatibilities in the behavior of a particular
            implementation that are outside the scope of this specification. Equally, some aspects
            of the behavior of XPath are defined by the host language.</p><ol class="enumar"><li><p>Consecutive comparison operators such as <code>A &lt; B &lt; C</code> were
                    supported in XPath 1.0, but are not permitted by the <span>XPath 4.0</span> grammar. In most
                    cases such comparisons in XPath 1.0 did not have the intuitive meaning, so it is
                    unlikely that they have been widely used in practice. If such a construct is
                    found, an <span>XPath 4.0</span> processor will report a syntax error, and the construct can
                    be rewritten as <code>(A &lt; B) &lt; C</code>
                     </p></li><li><p>When converting strings to numbers (either explicitly when using the
                        <code>number</code> function, or implicitly say on a function call), certain
                    strings that converted to the special value <code>NaN</code> under XPath 1.0
                    will convert to values other than <code>NaN</code> under <span>XPath 4.0</span>. These
                    include any number written with a leading <code>+</code> sign, any number in
                    exponential floating point notation (for example <code>1.0e+9</code>), and the
                    strings <code>INF</code> and <code>-INF</code>.</p><p>Furthermore, the strings <code>Infinity</code> and <code>-Infinity</code>, which
                    were accepted by XPath 1.0 as representations of the floating-point values
                    positive and negative infinity, are no longer recognized. They are converted to
                        <code>NaN</code> when running under <span>XPath 4.0</span> with compatibility mode set to
                    <code>true</code>, and cause a dynamic error when compatibility mode is set to <code>false</code>.</p></li><li><p>
                        <span>XPath 4.0</span> does not allow a token starting with a letter to follow immediately
                    after a numeric literal, without intervening whitespace. For example,
                        <code>10div 3</code> was permitted in XPath 1.0, but in <span>XPath 4.0</span> must be
                    written as <code>10 div 3</code>.</p></li><li><p>The namespace axis is deprecated as of XPath 2.0. Implementations may support the
                    namespace axis for backward compatibility with XPath 1.0, but they are not
                    required to do so. (XSLT 2.0 requires that if XPath backwards compatibility mode
                    is supported, then the namespace axis must also be supported; but other host
                    languages may define the conformance rules differently.)</p></li><li><p>In XPath 1.0, the expression <code>-x|y</code> parsed as <code>-(x|y)</code>, and
                    returned the negation of the numeric value of the first node in the union of
                        <code>x</code> and <code>y</code>. In <span>XPath 4.0</span>, this expression parses as
                        <code>(-x)|y</code>. When XPath 1.0 Compatibility Mode is true, this will
                    always cause a type error.</p></li><li><p>The rules for converting numbers to strings have changed. These may affect the
                    way numbers are displayed in the output of a stylesheet. For numbers whose
                    absolute value is in the range <code>1E-6</code> to <code>1E+6</code>, the
                    result should be the same, but outside this range, scientific format is used for
                    non-integral <code>xs:float</code> and <code>xs:double</code> values.</p></li><li><p>If one operand in a general comparison is a single atomic item of type
                        <code>xs:boolean</code>, the other operand is converted to
                        <code>xs:boolean</code> when XPath 1.0 compatibility mode is set to <code>true</code>. In
                    XPath 1.0, if neither operand of a comparison operation using the &lt;, &lt;=,
                    &gt; or &gt;= operator was a node set, both operands were converted to numbers.
                    The result of the expression <code>true() &gt; number('0.5')</code> is therefore
                    <code>true</code> in XPath 1.0, but is <code>false</code> in <span>XPath 4.0</span> even when compatibility mode is set
                    to <code>true</code>.</p></li><li><p>In <span>XPath 4.0</span>, a type error is raised if the PITarget specified in a SequenceType
                    of form <code>processing-instruction(PITarget)</code> is not a valid NCName. In
                    XPath 1.0, this condition was not treated as an error.</p></li></ol></div><div class="div3">
<h4><a id="id-incompat-in-false-mode"/>H.4.2 <a href="#id-incompat-in-false-mode" style="text-decoration: none">Incompatibilities when Compatibility Mode is <code>false</code>
               </a></h4><p>Even when the setting of the XPath 1.0 compatibility mode is <code>false</code>, many XPath
            expressions will still produce the same results under XPath 4.0 as under XPath 1.0. The
            exceptions are described in this section.</p><p>In all cases it is assumed that the expression in question was valid under XPath 1.0,
            that XPath 1.0 compatibility mode is <code>false</code>, and that all elements and attributes are
            annotated with the types <code>xs:untyped</code> and <code>xs:untypedAtomic</code>
            respectively.</p><p>In the description below, the terms <em>node-set</em> and <em>number</em> are
            used with their XPath 1.0 meanings, that is, to describe expressions which according to
            the rules of XPath 1.0 would have generated a node-set or a number respectively.</p><ol class="enumar"><li><p>When a node-set containing more than one node is supplied as an argument to a
                    function or operator that expects a single node or value, the XPath 1.0 rule was
                    that all nodes after the first were discarded. Under XPath 4.0, a type error
                    occurs if there is more than one node. The XPath 1.0 behavior can always be
                    restored by using the predicate <code>[1]</code> to explicitly select the first
                    node in the node-set.</p></li><li><p>In XPath 1.0, the <code>&lt;</code> and <code>&gt;</code> operators, when applied
                    to two strings, attempted to convert both the strings to numbers and then made a
                    numeric comparison between the results. In XPath 4.0, these operators perform a
                    string comparison using the default collating sequence. (If either value is
                    numeric, however, the results are compatible with XPath 1.0)</p></li><li><p>When an empty node-set is supplied as an argument to a function or operator that
                    expects a number, the value is no longer converted implicitly to <code>NaN</code>. The XPath
                    1.0 behavior can always be restored by using the <code>number</code> function to
                    perform an explicit conversion.</p></li><li><p>More generally, the supplied arguments to a function or operator are no longer
                    implicitly converted to the required type, except in the case where the supplied
                    argument is of type <code>xs:untypedAtomic</code> (which will commonly be the
                    case when a node in a schemaless document is supplied as the argument). For
                    example, the function call <code>substring-before(10 div 3,
                        ".")</code> raises a type error under XPath 4.0, because the arguments to
                    the <code>substring-before</code> function must be strings rather than numbers.
                    The XPath 1.0 behavior can be restored by performing an explicit conversion to
                    the required type using a constructor function or cast.</p></li><li><p>The rules for comparing a node-set to a boolean have changed. In XPath 1.0, an
                    expression such as <code>$node-set = true()</code> was
                    evaluated by converting the node-set to a boolean and then performing a boolean
                    comparison: so this expression would return <code>true</code> if
                        <code>$node-set</code> was non-empty. In XPath 4.0, this expression is
                    handled in the same way as other comparisons between a sequence and a singleton:
                    it is <code>true</code> if <code>$node-set</code> contains at least one node
                    whose value, after atomization and conversion to a boolean using the casting
                    rules, is <code>true</code>.</p><p>This means that if <code>$node-set</code> is empty, the result under XPath 4.0
                    will be <code>false</code> regardless of the value of the boolean operand, and
                    regardless of which operator is used. If <code>$node-set</code> is non-empty,
                    then in most cases the comparison with a boolean is likely to fail, giving a
                    dynamic error. But if a node has the value <code>"0"</code>, <code>"1"</code>,
                    <code>"true"</code>, or <code>"false"</code>, evaluation of the expression
                    may succeed.</p></li><li><p>Comparisons of a number to a boolean, a number to a string, or a string to a
                    boolean are not allowed in XPath 4.0: they result in a type error. In XPath 1.0
                    such comparisons were allowed, and were handled by converting one of the
                    operands to the type of the other. So for example in XPath 1.0
                    <code>4 = true()</code> returned <code>true</code>;
                    <code>4 ="+4"</code> returned <code>false</code>
                    (because the string <code>"+4"</code> converts to <code>NaN</code>),
                    and <code>false = "false"</code> returned <code>false</code> (because the
                    string <code>"false"</code> converts to the boolean <code>true</code>). In XPath
                    3.0 all these comparisons are type errors.</p></li><li><p>Additional numeric types have been introduced, with the effect that arithmetic
                    may now be done as an integer, decimal, or single- or double-precision floating
                    point calculation where previously it was always performed as double-precision
                    floating point. The result of the <code>div</code> operator when dividing two
                    integers is now a value of type decimal rather than double. The expression <code>10 div 0</code> raises an error rather than returning
                    positive infinity.</p></li><li><p>The rules for converting strings to numbers have changed. The implicit conversion
                    that occurs when passing an <code>xs:untypedAtomic</code> value as an argument
                    to a function that expects a number no longer converts unrecognized strings to
                    the value <code>NaN</code>; instead, it reports a dynamic error. This is in
                    addition to the differences that apply when backwards compatibility mode is set
                    to <code>true</code>.</p></li><li><p>Many operations in XPath 4.0 produce an empty sequence as their result when one
                    of the arguments or operands is an empty sequence. Where the operation expects a
                    string, an empty sequence is usually considered equivalent to a zero-length
                    string, which is compatible with the XPath 1.0 behavior. Where the operation
                    expects a number, however, the result is not the same. For example, if
                        <code>@width</code> returns an empty sequence, then in XPath 1.0 the result
                    of <code>@width+1</code> was <code>NaN</code>, while with
                    XPath 4.0 it is <code>()</code>. This has the effect that a filter expression
                    such as <code>item[@width+1 != 2]</code> will select items
                    having no <code>width</code> attribute under XPath 1.0, and will not select them
                    under XPath 4.0.</p></li><li><p>The typed value of a comment node, processing instruction node, or namespace node
                    under XPath 4.0 is of type <code>xs:string</code>, not
                        <code>xs:untypedAtomic</code>. This means that no implicit conversions are
                    applied if the value is used in a context where a number is expected. If a
                    processing-instruction node is used as an operand of an arithmetic operator, for
                    example, XPath 1.0 would attempt to convert the string value of the node to a
                    number (and deliver <code>NaN</code> if unsuccessful), while XPath 4.0 will
                    report a type error.</p></li><li><p>In XPath 1.0, it was defined that with an expression of the form <code>A and
                        B</code>, B would not be evaluated if A was false. Similarly in the case of
                        <code>A or B</code>, B would not be evaluated if A was true. This is no
                    longer guaranteed with XPath 4.0: the implementation is free to evaluate the two
                    operands in either order or in parallel. This change has been made to give more
                    scope for optimization in situations where XPath expressions are evaluated
                    against large data collections supported by indexes. Implementations may choose
                    to retain backwards compatibility in this area, but they are not obliged to do
                    so.</p></li><li><p>In XPath 1.0, the expression <code>-x|y</code> parsed as <code>-(x|y)</code>, and
                    returned the negation of the numeric value of the first node in the union of
                        <code>x</code> and <code>y</code>. In XPath 4.0, this expression parses as
                        <code>(-x)|y</code>. When XPath 1.0 Compatibility Mode is false, this will
                    cause a type error, except in the situation where <code>x</code> evaluates to an
                    empty sequence. In that situation, XPath 4.0 will return the value of
                        <code>y</code>, whereas XPath 1.0 returned the negation of the numeric value
                    of <code>y</code>.</p></li></ol></div><div class="div3">
<h4><a id="id-incompat-when-using-schema"/>H.4.3 <a href="#id-incompat-when-using-schema" style="text-decoration: none">Incompatibilities when using a Schema</a></h4><p>An XPath expression applied to a document that has been processed against a schema will
            not always give the same results as the same expression applied to the same document in
            the absence of a schema. Since schema processing had no effect on the result of an XPath
            1.0 expression, this may give rise to further incompatibilities. This section gives a
            few examples of the differences that can arise.</p><p>Suppose that the context node is an element node derived from the following markup:
                <code>&lt;background color="red green blue"/&gt;</code>. In XPath 1.0, the predicate
                <code>[@color="blue"]</code> would return <code>false</code>. In XPath 4.0, if the
                <code>color</code> attribute is defined in a schema to be of type
                <code>xs:NMTOKENS</code>, the same predicate will return <code>true</code>.</p><p>Similarly, consider the expression <code>@birth &lt; @death</code>
            applied to the element <code>&lt;person birth="1901-06-06"
                death="1991-05-09"/&gt;</code>. With XPath 1.0, this expression would return <code>false</code>,
            because both attributes are converted to numbers, which returns <code>NaN</code> in each
            case. With XPath 4.0, in the presence of a schema that annotates these attributes as
            dates, the expression returns <code>true</code>.</p><p>Once schema validation is applied, elements and attributes cannot be used as operands and
            arguments of expressions that expect a different data type. For example, it is no longer
            possible to apply the <code>substring</code> function to a date to extract the year
            component, or to a number to extract the integer part. Similarly, if an attribute is
            annotated as a boolean then it is not possible to compare it with the strings
                <code>"true"</code> or <code>"false"</code>. All such operations lead to type
            errors. The remedy when such errors occur is to introduce an explicit conversion, or to
            do the computation in a different way. For example, <code>substring-after(@temperature, "-")</code> might be rewritten as <code>abs(@temperature)</code>.</p><p>In the case of an XPath 4.0 implementation that provides the static typing feature, many
            further type errors will be reported in respect of expressions that worked under XPath
            1.0. For example, an expression such as <code>round(../@price)</code>
            might lead to a static type error because the processor cannot infer statically that
                <code>../@price</code> is guaranteed to be numeric.</p><p>Schema validation will in many cases perform whitespace normalization on the contents of
            elements (depending on their type). This will change the result of operations such as
            the <code>string-length</code> function.</p><p>Schema validation augments the data model by adding default values for omitted attributes
            and empty elements.</p></div></div></div><div class="div1">
<h2><a id="id-revision-log"/>I <a href="#id-revision-log" style="text-decoration: none">Change Log</a> (Non-Normative)</h2><ol><li><p>Use the arrows to browse significant changes since the 3.1 version of this specification.</p><p>See <a href="#id-introduction"><b>1 Introduction</b></a></p></li><li><p>Sections with significant changes are marked Δ in the table of contents.</p><p>See <a href="#id-introduction"><b>1 Introduction</b></a></p></li><li><p>
                     Setting the default namespace for elements and types to the special value
                     <code>##any</code> causes an unprefixed element name to act as a wildcard,
                     matching by local name regardless of namespace.
                  </p><p>See <a href="#id-element-test"><b>3.2.7.2 Element Types</b></a></p></li><li><p>The terms <b>FunctionType</b>, <b>ArrayType</b>,
                     <b>MapType</b>, and <b>RecordType</b> replace 
                     <b>FunctionTest</b>, <b>ArrayTest</b>,
                     <b>MapTest</b>, and <b>RecordTest</b>, with no
                  change in meaning.</p><p>See <a href="#id-function-test"><b>3.2.8.1 Function Types</b></a></p></li><li><p>
                     Record types are added as a new kind of <code>ItemType</code>, constraining
                     the value space of maps.
                  </p><p>See <a href="#id-record-test"><b>3.2.8.3 Record Types</b></a></p></li><li><p>
                     Function coercion now allows a function with arity <var>N</var> to be supplied where a function of arity 
                     greater than <var>N</var> is expected. For example this allows the function <code>true#0</code> 
                     to be supplied where a predicate function is required.
                  </p><p>See <a href="#id-function-coercion"><b>3.4.4 Function Coercion</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1817 1853">1817 1853 </a></p><p>An inline function may be annotated
               as a <code>%method</code>, giving it access to its containing map.</p><p>See <a href="#id-inline-func"><b>4.5.6 Inline Function Expressions</b></a></p><p>See <a href="#id-methods"><b>4.5.6.1 Methods</b></a></p><p>See <a href="#id-lookup"><b>4.13.3 Lookup Expressions</b></a></p></li><li><p>
               The symbols <code>×</code> and <code>÷</code> can be used for multiplication and division.
            </p><p>See <a href="#id-arithmetic"><b>4.8 Arithmetic Expressions</b></a></p></li><li><p>
                  The rules for value comparisons when comparing values of different types (for example, decimal and double)
                  have changed to be transitive. A decimal value is no longer converted to double, instead the double is converted
                  to a decimal without loss of precision. This may affect compatibility in edge cases involving comparison of
                  values that are numerically very close.
               </p><p>See <a href="#id-value-comparisons"><b>4.10.1 Value Comparisons</b></a></p></li><li><p>
                  Operators such as <code>&lt;</code> and <code>&gt;</code> can use the full-width forms
                  <code>＜</code> and <code>＞</code> to avoid the need for XML escaping.
               </p><p>See <a href="#id-general-comparisons"><b>4.10.2 General Comparisons</b></a></p></li><li><p>
                  The lookup operator <code>?</code> can now be followed by a string literal, for cases where
                     map keys are strings other than NCNames. It can also be followed by a variable
                     reference.
               </p><p>See <a href="#id-lookup"><b>4.13.3 Lookup Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1864 1877">1864 1877 </a></p><p>
                  The key specifier can reference an item type or sequence type, to select
                  values of that type only. This is especially useful when processing
                  trees of maps and arrays, as encountered when processing JSON input.
               </p><p>See <a href="#id-lookup"><b>4.13.3 Lookup Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1763 1830">1763 1830 </a></p><p>The syntax on the right-hand side of an arrow operator
               has been relaxed; a dynamic function call no longer needs to start with a variable reference
            or a parenthesized expression, it can also be (for example) an inline function expression
            or a map or array constructor.</p><p>See <a href="#id-arrow-operator"><b>4.20 Arrow Expressions</b></a></p></li><li><p>
               The arrow operator <code>=&gt;</code> is now complemented by a “mapping arrow” operator <code>=!&gt;</code>
               which applies the supplied function to each item in the input sequence independently.
            </p><p>See <a href="#id-mapping-arrow-expression"><b>4.20.2 Mapping Arrow Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1023 1128">1023 1128 </a></p><p>
                     It has been clarified that function coercion applies even when the supplied function item
                     matches the required function type. This is to ensure that arguments supplied when calling
                     the function are checked against the signature of the required function type, which might
                     be stricter than the signature of the supplied function item.
                  </p><p>See <a href="#id-function-coercion"><b>3.4.4 Function Coercion</b></a></p></li><li><p>A dynamic function call can now be applied to a sequence of functions, and in particular
               to an empty sequence. This makes it easier to chain a sequence of calls.</p><p>See <a href="#id-eval-dynamic-function-call"><b>4.5.3.1 Evaluating Dynamic Function Calls</b></a></p></li><li><p>
          The static typing feature has been dropped.
        </p><p>See <a href="#id-conformance"><b>5 Conformance</b></a></p></li><li><p>
                     The syntax <code>document-node(<var>N</var>)</code>, where <var>N</var> is a <code>NameTestUnion</code>,
                     is introduced as an abbreviation for <code>document-node(element(<var>N</var>))</code>. For example,
                     <code>document-node(*)</code> matches any well-formed XML document (as distinct from a document fragment).
                  </p><p>See <a href="#node-types"><b>3.2.7 Node Types</b></a></p></li><li><p>QName literals are new in 4.0.</p><p>See <a href="#id-qname-literals"><b>4.2.1.3 QName Literals</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/28">28 </a></p><p>
               Multiple <code>for</code> and <code>let</code> clauses can be combined
               in an expression without an intervening <code>return</code> keyword.
            </p><p>See <a href="#id-for-expressions"><b>4.12.1 For Expressions</b></a></p><p>See <a href="#id-let-expressions"><b>4.12.2 Let Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/159">159 </a></p><p>
                  Keyword arguments are allowed on static function calls, as well as positional arguments.
               </p><p>See <a href="#id-function-calls"><b>4.5.1.1 Static Function Call Syntax</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/202">202 </a></p><p>The presentation of the rules for the subtype relationship between 
                  sequence types and item types has been substantially
                  rewritten to improve clarity; no change to the semantics is intended.</p><p>See <a href="#id-sequencetype-subtype"><b>3.3 Subtype Relationships</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/230">230 </a></p><p>
                  The rules for “errors and optimization” have been tightened up to disallow
                  many cases of optimizations that alter error behavior. In particular
                  there are restrictions on reordering the operands of <code>and</code> and <code>or</code>,
                  and of predicates in filter expressions, in a way that might allow the processor to raise dynamic
                  errors that the author intended to prevent.
               </p><p>See <a href="#id-guarded-expressions"><b>2.4.5 Guarded Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/254">254 </a></p><p>
                  The term "function conversion rules" used in 3.1 has been replaced by the term "coercion rules".
               </p><p>See <a href="#id-coercion-rules"><b>3.4 Coercion Rules</b></a></p><p>
                  The coercion rules allow “relabeling” of a supplied atomic item where
                  the required type is a derived atomic type: for example, it is now permitted to supply
                  the value 3 when calling a function that expects an instance of <code>xs:positiveInteger</code>.
               </p><p>See <a href="#id-coercion-rules"><b>3.4 Coercion Rules</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/284">284 </a></p><p>
               Alternative syntax for conditional expressions is available: <code>if (condition) { X }</code>.
            </p><p>See <a href="#id-conditionals"><b>4.14 Conditional Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/286">286 </a></p><p>
                     Element and attribute tests can include alternative names: <code>element(chapter|section)</code>,
                     <code>attribute(role|class)</code>.
                  </p><p>See <a href="#node-types"><b>3.2.7 Node Types</b></a></p><p>
                     The <code>NodeTest</code> in an <code>AxisStep</code> now allows alternatives: 
                     <code>ancestor::(section|appendix)</code>
                  </p><p>See <a href="#node-types"><b>3.2.7 Node Types</b></a></p><p>
                     Element and attribute tests of the form <code>element(N)</code>
                     and <code>attribute(N)</code> now allow <code>N</code> to be any <code>NameTest</code>,
                     including a wildcard.
                  </p><p>See <a href="#id-element-test"><b>3.2.7.2 Element Types</b></a></p><p>See <a href="#id-attribute-test"><b>3.2.7.3 Attribute Types</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/324">324 </a></p><p>
                  String templates provide a new way of constructing strings: for example <code>`{$greeting}, {$planet}!`</code>
                  is equivalent to <code>$greeting || ', ' || $planet || '!'</code>
                  </p><p>See <a href="#id-string-templates"><b>4.9.2 String Templates</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/326">326 </a></p><p>
           Support for higher-order functions is now a mandatory feature (in 3.1 it was optional).
        </p><p>See <a href="#id-conformance"><b>5 Conformance</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/344">344 </a></p><p>A <code>for member</code> clause is added to FLWOR expressions to allow iteration over
               an array.</p><p>See <a href="#id-for-expressions"><b>4.12.1 For Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/368">368 </a></p><p>
                  The concept of the context item has been generalized, so it is now a context value. That is,
                  it is no longer constrained to be a single item.
               </p><p>See <a href="#eval_context"><b>2.2.2 Dynamic Context</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/433">433 </a></p><p>
                     Numeric literals can now be written in hexadecimal or binary notation; 
                     and underscores can be included for readability.
                  </p><p>See <a href="#id-numeric-literals"><b>4.2.1.1 Numeric Literals</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/519">519 </a></p><p>
        The rules for tokenization have been largely rewritten. In some cases the revised specification may
        affect edge cases that were handled in different ways by different 3.1 processors, which could lead
        to incompatible behavior.
      </p><p>See <a href="#lexical-structure"><b>A.3 Lexical structure</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/521">521 </a></p><p>New abbreviated syntax is introduced 
                     (<a title="focus function" class="termref" href="#dt-focus-function">focus function</a>) 
                     for simple inline functions taking a single argument. 
                     An example is <code>fn { ../@code }</code>
                  </p><p>See <a href="#id-inline-func"><b>4.5.6 Inline Function Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/603">603 </a></p><p>
                  The rules for reporting type errors during static analysis have been changed
                  so that a processor has more freedom to report errors in respect of constructs that
                  are evidently wrong, such as <code>@price/@value</code>, even though dynamic evaluation
                  is defined to return an empty sequence rather than an error.
               </p><p>See <a href="#id-implausible-expressions"><b>2.4.6 Implausible Expressions</b></a></p><p>See <a href="#implausible-axis-steps"><b>4.6.4.3 Implausible Axis Steps</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/606">606 </a></p><p>
                     Element and attribute tests of the form <code>element(A|B)</code>
                     and <code>attribute(A|B)</code> are now allowed.
                  </p><p>See <a href="#id-element-test"><b>3.2.7.2 Element Types</b></a></p><p>See <a href="#id-attribute-test"><b>3.2.7.3 Attribute Types</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/691">691 </a></p><p>Enumeration types are added as a new kind of <code>ItemType</code>, constraining
                     the value space of strings.</p><p>See <a href="#id-enumeration-types"><b>3.2.6 Enumeration Types</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/728">728 </a></p><p>
                     The syntax <code>record(*)</code> is allowed; it matches any map.
                  </p><p>See <a href="#id-record-test"><b>3.2.8.3 Record Types</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/815">815 </a></p><p>
                  The coercion rules now allow conversion in either direction between <code>xs:hexBinary</code>
                  and <code>xs:base64Binary</code>.
               </p><p>See <a href="#id-coercion-rules"><b>3.4 Coercion Rules</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/837">837 </a></p><p>
                  A deep lookup operator <code>??</code> is provided for searching
                  trees of maps and arrays.
               </p><p>See <a href="#id-lookup"><b>4.13.3 Lookup Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/911">911 </a></p><p>
                  The coercion rules now allow any numeric type to be implicitly converted to any other, for example
                  an <code>xs:double</code> is accepted where the required type is <code>xs:double</code>.
               </p><p>See <a href="#id-coercion-rules"><b>3.4 Coercion Rules</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/996">996 </a></p><p>
                  The value of a predicate in a filter expression can now be a sequence of integers.
               </p><p>See <a href="#id-filter-expression"><b>4.4 Filter Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1031">1031 </a></p><p>
               An <code>otherwise</code> operator is introduced: <code>A otherwise B</code> returns the
               value of <code>A</code>, unless it is an empty sequence, in which case it returns the value of <code>B</code>.
            </p><p>See <a href="#id-otherwise"><b>4.15 Otherwise Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1071">1071 </a></p><p>
                     In map constructors, the keyword <code>map</code> is now optional, so 
                     <code>map { 0: false(), 1: true() }</code> can now be written <code>{ 0: false(), 1: true() }</code>,
                     provided it is used in a context where this creates no ambiguity.
                  </p><p>See <a href="#id-map-constructors"><b>4.13.1.1 Map Constructors</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1125">1125 </a></p><p>
                  Lookup expressions can now take a modifier (such as <code>keys</code>,
                  <code>values</code>, or <code>pairs</code>) enabling them to return
                  structured results rather than a flattened sequence. 
               </p><p>See <a href="#id-lookup"><b>4.13.3 Lookup Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1131">1131 </a></p><p>
               A positional variable can be defined in a <code>for</code> expression.
            </p><p>See <a href="#id-for-expressions"><b>4.12.1 For Expressions</b></a></p><p>
               The type of a variable used in a <code>for</code> expression can be declared.
               </p><p>See <a href="#id-for-expressions"><b>4.12.1 For Expressions</b></a></p><p>
               The type of a variable used in a <code>let</code> expression can be declared.
            </p><p>See <a href="#id-let-expressions"><b>4.12.2 Let Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1132">1132 </a></p><p>
                     Choice item types (an item type allowing a set of alternative item types)
                     are introduced.
                  </p><p>See <a href="#id-choice-item-types"><b>3.2.5 Choice Item Types</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1163">1163 </a></p><p>
                  Filter expressions for maps and arrays are introduced.
               </p><p>See <a href="#id-filter-maps-and-arrays"><b>4.13.4 Filter Expressions for Maps and Arrays</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1181">1181 </a></p><p>
                  The <a title="default namespace for elements and types" class="termref" href="#dt-default-namespace-elements-and-types">default namespace for elements and types</a> can be set to the value <code>##any</code>,
                  allowing unprefixed names in axis steps to match elements with a given local name in any namespace.
               </p><p>See <a href="#static_context"><b>2.2.1 Static Context</b></a></p><p>
                     If the default namespace for elements and types has the special value <code>##any</code>,
                     then an unprefixed name in a <code>NameTest</code> acts as a wildcard, matching
                     names in any namespace or none.
                  </p><p>See <a href="#node-tests"><b>4.6.4.2 Node Tests</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1197">1197 </a></p><p>The keyword <code>fn</code> is allowed as a synonym for <code>function</code>
                  in function types, to align with changes to inline function declarations.</p><p>See <a href="#id-function-test"><b>3.2.8.1 Function Types</b></a></p><p>
                  In inline function expressions, the keyword <code>function</code> may be abbreviated
                  as <code>fn</code>.
               </p><p>See <a href="#id-inline-func"><b>4.5.6 Inline Function Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1212">1212 </a></p><p>
          XPath 3.0 included <code>empty-sequence</code> and <code>item</code>
            as reserved function names, and XPath 3.1 added <code>map</code> and <code>array</code>.
            This was unnecessary since these names never appear followed by a left parenthesis
            at the start of an expression. They have therefore been removed from the list.
            New keywords introducing item types, such as <code>record</code> and <code>enum</code>,
            have not been included in the list.
      </p><p>See <a href="#id-reserved-fn-names"><b>A.4 Reserved Function Names</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1217">1217 </a></p><p>
                  Predicates in filter expressions for maps and arrays can now be numeric.
               </p><p>See <a href="#id-filter-maps-and-arrays"><b>4.13.4 Filter Expressions for Maps and Arrays</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1249">1249 </a></p><p>
               A <code>for key/value</code> clause is added to FLWOR expressions to allow iteration over
               maps.</p><p>See <a href="#id-for-expressions"><b>4.12.1 For Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1250">1250 </a></p><p>
                           Several decimal format properties, including minus sign, exponent separator, percent, and per-mille,
                           can now be rendered as arbitrary strings rather than being confined to a single
                           character.
                        </p><p>See <a href="#id-decimal-formats"><b>2.2.1.2 Decimal Formats</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1265">1265 </a></p><p>
                  The rules regarding the <code>document-uri</code> property of nodes returned by the
                  <a href="https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-collection"><code>fn:collection</code></a> function have been relaxed.
               </p><p>See <a href="#eval_context"><b>2.2.2 Dynamic Context</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1344">1344 </a></p><p>

                  Parts of the static context that were there purely to assist in static typing, such as the statically
                  known documents, were no longer referenced and have therefore been dropped.
               </p><p>See <a href="#static_context"><b>2.2.1 Static Context</b></a></p><p>
             The static typing option has been dropped.
           </p><p>See <a href="#id-processing-model"><b>2.3 Processing Model</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1361">1361 </a></p><p>
               The term <b>atomic value</b> has been replaced by <a title="atomic item" class="termref" href="#dt-atomic-item">atomic item</a>.
            </p><p>See <a href="#id-values"><b>2.1.2 Values</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1384">1384 </a></p><p>
               If a type declaration is present, the supplied values in the input sequence are now
               coerced to the required type. Type declarations are now permitted in XPath as well as XQuery.
            </p><p>See <a href="#id-quantified-expressions"><b>4.16 Quantified Expressions</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1496">1496 </a></p><p>
                  The context value static type, which was there purely to assist in static typing, has been dropped.
               </p><p>See <a href="#static_context"><b>2.2.1 Static Context</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1498">1498 </a></p><p>The EBNF operators <code>++</code> and <code>**</code>
      have been introduced, for more concise representation of sequences using a character
      such as <code>","</code> as a separator. The notation is borrowed from Invisible XML.</p><p>See <a href="#id-terminology"><b>2.1 Terminology</b></a></p><p>
                  The EBNF notation has been extended to allow the constructs <code>(A ++ ",")</code>
                  (one or more occurrences of <code>A</code>, comma-separated, and <code>(A ** ",")</code>
                  (zero or more occurrences of <code>A</code>, comma-separated.
               </p><p>See <a href="#id-ebnf-introduction"><b>2.1.1 Grammar Notation</b></a></p><p>The EBNF operators <code>++</code> and <code>**</code>
      have been introduced, for more concise representation of sequences using a character
      such as <code>","</code> as a separator. The notation is borrowed from Invisible XML.</p><p>See <a href="#id-grammar"><b>A.1 EBNF</b></a></p><p>See <a href="#EBNFNotation"><b>A.1.1 Notation</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1501">1501 </a></p><p>
                  The coercion rules now apply recursively to the members of an array and the entries in a map.
               </p><p>See <a href="#id-coercion-rules"><b>3.4 Coercion Rules</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1532">1532 </a></p><p>
                     Four new axes have been defined: <code>preceding-or-self</code>, <code>preceding-sibling-or-self</code>,
                     <code>following-or-self</code>, and <code>following-sibling-or-self</code>.
                  </p><p>See <a href="#axes"><b>4.6.4.1 Axes</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1577">1577 </a></p><p>
                     The syntax <code>record()</code> is allowed; the only thing it matches is an empty map.
                  </p><p>See <a href="#id-record-test"><b>3.2.8.3 Record Types</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1686">1686 </a></p><p>
               With the pipeline operator <code>-&gt;</code>, the result of an expression
               can be bound to the context value before evaluating another expression.
            </p><p>See <a href="#id-pipeline-operator"><b>4.18 Pipeline operator</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1696">1696 </a></p><p>
                     Parameter names may be included in a function signature; they are purely
                     documentary.
                  </p><p>See <a href="#id-function-test"><b>3.2.8.1 Function Types</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1703">1703 </a></p><p>Ordered maps are introduced.</p><p>See <a href="#id-maps"><b>4.13.1 Maps</b></a></p><p>The order of key-value
                     pairs in the map constructor is now retained in the constructed map.</p><p>See <a href="#id-map-constructors"><b>4.13.1.1 Map Constructors</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1874">1874 </a></p><p>
                  The coercion rules now reorder the entries in a map when the required type is a record type.
               </p><p>See <a href="#id-coercion-rules"><b>3.4 Coercion Rules</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1898">1898 </a></p><p>The rules for subtyping of document node types have been refined.</p><p>See <a href="#id-item-subtype-documents"><b>3.3.2.4.2 Subtyping Nodes: Document Nodes</b></a></p></li><li><p>PR <a href="https://github.com/qt4cg/qtspecs/pull/1991">1991 </a></p><p>
                  Named record types used in the signatures of built-in functions are now available as standard
                  in the static context.
               </p><p>See <a href="#static_context"><b>2.2.1 Static Context</b></a></p></li></ol></div></div><script src="https://www.w3.org/scripts/TR/2016/fixup.js"/><script src="js/toc.js"/></body></html>