<spec xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax" w3c-doctype="rec">
  <!-- W3C header, disclaimers and abstracts -->
       <header>
  <title>XQuery 1.0 and XPath 2.0 Formal Semantics</title>
  <w3c-designation>REC-xquery-semantics-20070123</w3c-designation>
  <w3c-doctype>W3C Recommendation</w3c-doctype>

  <pubdate>
   <day>23</day>
   <month>January</month>
   <year>2007</year>
  </pubdate>

  <publoc>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/</loc>
  </publoc>

  <altlocs>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/xquery-semantics.xml" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML</loc>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="diff-from-20061121.html" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Recent revisions</loc>
  </altlocs>

  <latestloc>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xquery-semantics/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/xquery-semantics/</loc>
  </latestloc>

  <prevlocs>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2006/PR-xquery-semantics-20061121/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
<!--* <loc 
    href="http://www.w3.org/TR/2005/CR-xquery-semantics-20051103/"/>
    <loc
    href="http://www.w3.org/TR/2005/WD-xquery-semantics-20050915/"/> *-->
<!--
    <loc
    href="http://www.w3.org/TR/2005/WD-xquery-semantics-20050603/"/>
    <loc
    href="http://www.w3.org/TR/2005/WD-xquery-semantics-20050404/"/>
    <loc
    href="http://www.w3.org/TR/2005/WD-xquery-semantics-20050211/">http://www.w3.org/TR/2005/WD-xquery-semantics-20050211/</loc>
    <loc
    href="http://www.w3.org/TR/2004/WD-xquery-semantics-20040220/">http://www.w3.org/TR/2004/WD-xquery-semantics-20040220/</loc>
    <loc
    href="http://www.w3.org/TR/2003/WD-xquery-semantics-20031112/">http://www.w3.org/TR/2003/WD-xquery-semantics-20031112/</loc>
    <loc
    href="http://www.w3.org/TR/2003/WD-xquery-semantics-20030822/">http://www.w3.org/TR/2003/WD-xquery-semantics-20030822/</loc>
    <loc
    href="http://www.w3.org/TR/2003/WD-xquery-semantics-20030502/">http://www.w3.org/TR/2003/WD-xquery-semantics-20030502/</loc>
    <loc
    href="http://www.w3.org/TR/2002/WD-query-semantics-20021115/">http://www.w3.org/TR/2002/WD-query-semantics-20021115/</loc>
    <loc
    href="http://www.w3.org/TR/2002/WD-query-semantics-20020816/">http://www.w3.org/TR/2002/WD-query-semantics-20020816/</loc>
    <loc
    href="http://www.w3.org/TR/2002/WD-query-semantics-20020326/">http://www.w3.org/TR/2002/WD-query-semantics-20020326/</loc>
    <loc
    href="http://www.w3.org/TR/2001/WD-query-semantics-20010607/">http://www.w3.org/TR/2001/WD-query-semantics-20010607/</loc>
    <loc
    href="http://www.w3.org/TR/2001/WD-query-algebra-20010215/">http://www.w3.org/TR/2001/WD-query-algebra-20010215/</loc>
    <loc
    href="http://www.w3.org/TR/2000/WD-query-algebra-20001204/">http://www.w3.org/TR/2000/WD-query-algebra-20001204/</loc>
-->
  </prevlocs>

  <authlist>
    <author>
      <name>Denise Draper</name>
      <affiliation>Microsoft</affiliation>
      <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:denised@microsoft.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">denised@microsoft.com</email>
    </author>
    <author>
      <name>Peter Fankhauser (XML Query WG)</name>
      <affiliation>Infonyte GmbH</affiliation>
      <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:fankhaus@infonyte.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">fankhaus@infonyte.com</email>
    </author>
    <author>
      <name>Mary Fernández (XML Query WG)</name>
      <affiliation>AT&amp;T Labs - Research</affiliation>
      <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:mff@research.att.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">mff@research.att.com</email>
    </author>
    <author>
      <name>Ashok Malhotra (XML Query and XSL WGs)</name>
      <affiliation>Oracle Corporation</affiliation>
      <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:ashok.malhotra@oracle.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">ashok.malhotra@oracle.com</email>
    </author>
    <author>
      <name>Kristoffer Rose (XSL WG)</name>
      <affiliation>IBM T.J. Watson Research Center</affiliation>
      <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:krisrose@us.ibm.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">krisrose@us.ibm.com</email>
    </author>
    <author>
      <name>Michael Rys (XML Query WG)</name>
      <affiliation>Microsoft</affiliation>
      <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:mrys@microsoft.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">mrys@microsoft.com</email>
    </author>
    <author>
      <name>Jérôme Siméon (XML Query WG)</name>
      <affiliation>IBM T.J. Watson Research Center</affiliation>
      <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:simeon@us.ibm.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">simeon@us.ibm.com</email>
    </author>
    <author>
      <name>Philip Wadler (XML Query WG)</name>
      <affiliation>University of Edinburgh</affiliation>
      <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:wadler@inf.ed.ac.uk" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">wadler@inf.ed.ac.uk</email>
    </author>
  </authlist>

   <errataloc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/2007/qt-errata/xquery-semantics-errata.html" xlink:type="simple"/>

  <translationloc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2003/03/Translations/byTechnology?technology=xquery-semantics" xlink:type="simple"/>



  <abstract>
    <p>This document defines formally the semantics of <phrase role="xquery">XQuery 1.0 <bibref ref="xquery"/></phrase> <phrase role="shared">and</phrase> <phrase role="xpath">XPath 2.0
    <bibref ref="xpath20"/></phrase>.</p>
  </abstract>

<!--* Common status section for QT specs.
    * Use is currently not required, but it simplifies things.
    * 
    * Revisions:
    * 2007-01-15 : CMSMcQ : made file, to simplify publication of Rec.
    *-->

    <status id="status">

      <p><emph>This section describes the status of this
         document at the time of its publication.
         Other documents may supersede this document.
         A list of current W3C publications and the latest
         revision of this technical report can be found in the
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">W3C technical reports index</loc>
         at http://www.w3.org/TR/.</emph></p>

      <p>This is one document in a set of eight documents that have progressed to
         Recommendation together (XQuery 1.0, XQueryX 1.0, XSLT 2.0,
         Data Model, Functions and Operators, Formal Semantics,
         Serialization, XPath 2.0). </p>

      <p>This is a <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2004/02/Process-20040205/tr.html#RecsW3C" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Recommendation</loc>
         of the W3C.  It has been jointly developed by the W3C 
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/Query" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Query Working Group</loc> and the W3C <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Style/XSL" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XSL Working Group</loc>, each of which is part of the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/Activity" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Activity</loc>.</p>
 
      <p>This document has been reviewed by W3C Members, by
         software developers, and by other W3C groups and interested parties,
         and is endorsed by the Director as a W3C Recommendation. It is a
         stable document and may be used as reference material or cited from
         another document. W3C's role in making the Recommendation is to draw
         attention to the specification and to promote its widespread
         deployment. This enhances the functionality and interoperability of
         the Web.</p>

         <p>No substantive changes have
been made to this specification since its publication as a
Proposed Recommendation.</p>

         <!--* &implementation-para; *-->

         <p>Please report errors in this document using W3C's
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public Bugzilla system</loc>
         (instructions can be found at
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/2005/04/qt-bugzilla" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/XML/2005/04/qt-bugzilla</loc>).
         If access to that system is not feasible, you may send your comments
         to the W3C XSLT/XPath/XQuery public comments mailing list,
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:public-qt-comments@w3.org" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public-qt-comments@w3.org</loc>.
         It will be very helpful if you include the string 
         “[FS]”
         in the subject line of your report, whether made in Bugzilla or in email.
         Each Bugzilla entry and email message should contain only one error report.
         Archives of the comments and responses are available at
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://lists.w3.org/Archives/Public/public-qt-comments/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://lists.w3.org/Archives/Public/public-qt-comments/</loc>. </p>

           <p>This document was produced by groups operating under the
   <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">5 February 2004
   W3C Patent Policy</loc>.
   W3C maintains a <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2004/01/pp-impl/18797/status#disclosures" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public list of any 
   patent disclosures</loc> made in connection with the deliverables of the 
   XML Query Working Group and also maintains a <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2004/01/pp-impl/19552/status#disclosures" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public list of any patent 
   disclosures</loc> made in connection with the deliverables of the XSL 
   Working Group; those pages also include instructions for
   disclosing a patent.
   An individual who has actual knowledge of a patent which the individual believes
   contains
   <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Essential Claim(s)</loc>
   must disclose the information in accordance with
   <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">section 6 of the W3C Patent Policy</loc>. </p>


    </status>


<!--*
<status id="status">

<p><emph>This section describes the status of this document at the
time of its publication.  Other documents may supersede this document.
A list of current W3C publications and the latest revision of this
technical report can be found in the <loc
href="http://www.w3.org/TR/">W3C technical reports index</loc> at
http://www.w3.org/TR/.</emph></p>

<p>This is one document in a set of eight documents that have
progressed to Recommendation together (XQuery 1.0, XQueryX 1.0, XSLT
2.0, Data Model, Functions and Operators, Formal Semantics,
Serialization, XPath 2.0). </p>

<p>This is a <loc
href="http://www.w3.org/2004/02/Process-20040205/tr.html#RecsREC
">Recommendation</loc> of the W3C.  This document has been jointly
developed by the W3C XML Query Working Group and the W3C XSL Working
Group, each part of the XML Activity. It has been reviewed by W3C
Members and other interested parties and has been endorsed by the
Director. It is a stable document and may be used as reference
material or cited as a normative reference from another
document. W3C's role in making the Recommendation is to draw attention
to the specification and to promote its widespread deployment.  This
enhances the functionality and interoperability of the Web.</p>

<p>This document incorporates minor changes made against the <loc
href="http://www.w3.org/2004/02/Process-20040205/tr.html#RecsPR">Proposed
Recommendation</loc> of 21 November 2006; please see the public
disposition of comments for details. Changes to this document since
the <loc
href="http://www.w3.org/2004/02/Process-20040205/tr.html#RecsPR">Proposed
Recommendation</loc> are detailed in the <specref
ref="id-fs-revisions-log"/>.</p>

<p>Please report errors in this document using W3C's <loc
href="http://www.w3.org/Bugs/Public/">public Bugzilla system</loc>
(instructions can be found at <loc
href="http://www.w3.org/XML/2005/04/qt-bugzilla
">http://www.w3.org/XML/2005/04/qt-bugzilla</loc>).  If access to that
system is not feasible, you may send your comments to the W3C
XSLT/XPath/XQuery public comments mailing list, <loc
href="mailto:public-qt-comments@w3.org">public-qt-comments@w3.org</loc>.
It will be very helpful if you include the string [FS] in the subject
line of your report, whether made in Bugzilla or in email.  Each
Bugzilla entry and email message should contain only one error report.
Archives of the comments and responses are available at <loc
href="http://lists.w3.org/Archives/Public/public-qt-comments/
">http://lists.w3.org/Archives/Public/public-qt-comments/</loc>.</p>

<p>This document was produced by a group operating under the <loc
href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February
2004 W3C Patent Policy</loc>.  The Working Groups maintain a public
list of patent disclosures relevant to this document; that page also
includes instructions for disclosing [and excluding] a patent.  An
individual who has actual knowledge of a patent which the individual
believes contains <loc
href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential
">Essential Claim(s)</loc> must disclose the information in accordance
with <loc
href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure
">section 6 of the W3C Patent Policy</loc>. </p>

  </status>
*-->

  <langusage>
    <language id="EN">English</language>
    <language id="ebnf">EBNF</language>
  </langusage>

  <revisiondesc>
    <p>JS January 2005. Major revision, aligning the Formal Semantics
    with decisions taken up to October 2004.</p>

    <p>JS April 22/2003: Revised version implementing decisions from
    the December 2002 and March 2003 f2f meetings.</p>

    <p>JS November 4/2002: Revised version implementing decisions from
    the September 2002 f2f meeting in North Carolina.</p>

    <p>JS July 26/2002: Revised version which implements most of the
    recent decisions of the XML Query and XSLT working groups. Notably
    implements semantics of element constructors.</p>

    <p>JS June 15/2002: Revised version which fixes many problems
    pointed out by reviewers. Revised version of Sections 4.3 and
    4.13.</p>

    <p>JS May 05/2002: New major revision which integrates named
    typing, mapping from XML Schema and the semantics of XPath
    2.0.</p>

    <p>JS March 22/2002: Most of the document is new. This is the
    basis for the new Formal Semantics of XPath 2.0 and XQuery
    1.0. The current version is up to date and aligned with the
    December 2002 XQuery 1.0 and XPath 2.0 working drafts.</p>
  
    <p>JS January 2002: Comprehensive editorial rewrite. Update to
    align the formal semantics with the new [XPath/XQuery] language
    document. Update to align the formal semantics with the new
    <bibref ref="xpath-functions"/> document. Update to align the formal semantics with
    XML Schema.</p>

    <p>DD August 2001: Comprehensive editorial rewrite.  Introduced
    new section <specref ref="sec_preliminaries"/> with new text and
    amalgamated material that was previously spread over the entire
    document.  Merged previously separate static, dynamic, and core
    rewrite sections into a single section.  Switched to the type
    system and syntax as defined Jérôme's presentation
    from the last face-to-face. Text is marked according to various
    properties:</p>
    <slist>
      <sitem role="newtext">Substantially new text.</sitem>
      <sitem role="iffy">Text that seems likely to be somewhat controversial.</sitem>
      <sitem role="missing">Areas where there is known to be missing text.</sitem>
      <sitem role="notdone">Old text, dropped into position, but not yet incorporated.</sitem>
    </slist>
  
    <p>MF June 06/2001: Added FS-Issue-0099</p>
  
    <p>PF, MF May 31/2001: Added FS-Issue-0094, FS-Issue-0095, FS
     Issue-0096, FS-Issue-0097, FS-Issue-0098.
    </p>
  
    <p>PF, MF May 28/2001: Large number of syntax errors and editorial
    fixes.</p>
  
    <p>JS, May 11/2001: Large number of syntax errors and editorial
    fixes. Added FS-Issue-0092.</p>
  
    <p>MF, Apr 30/2001: Resolved FS-Issue-0076.  Added FS-Issue-0089,
    FS-Issue-0090, FS-Issue-0091.</p>
  
    <p>JS, Apr 21/2001: Populated Section 6: [XPath/XQuery] Mapping to the
    Core</p>
  
    <p>PF, Apr 18/2001: (Re)introduced example on joins on unordered
    sequences in Section 2, a section on typing descendant-or-self in
    Section 4, and some new optimization rules for unordered().</p>
  
    <p>MF, Apr 04/2001: Syntax overhaul through Sec 4.8 : changed all
    examples to [XPath/XQuery] syntax.</p>
  
    <p>PF, Jan 30/2001: Simplified design for dealing with unordered
    collections along the lines presented at the F2F in Nov 2000.</p>
  
    <p>MF, Jan 15/2000: Addressed many issues raised by Micheal
    Brundage.Added FS-Issue-0081, FS-Issue-0082, FSIssue-0083, FS
    Issue-0084, FS-Issue-0085, FS-Issue-0086 and FS-Issue-0087.
    Resolved FS-Issue-0038, FS-Issue-0062, FS-Issue-0066, and FS
    Issue-0071.
    </p>
  
    <p>MF, Jan 5/2000: Added introductory paragraph on static and
    dynamic semantics and a section on dynamic/operational semantics.
    WARNING: This material has not been reviewed by Algebra editors
    yet.  Commented out unnecessary references.</p>
  
    <p>MF, Dec 19/2000: Addendum to FS-Issue-0011.  Added FS
    Issue-0081.</p>
  
    <p>MF, Dec 11/2000: Added sections on parent and dynamic and
    static casts, and on references and node identity.  Separated
    built-in functions and operators from core syntax and moved into
    own section.  Improved correspondence with data model (See
    accompanying release of data model document.)  Added Jerome's text
    on querying order.  Clarified that semantics of operators will be
    defined by joint task force.  Closed FS-Issue-0003; added FS
    Issue-0079.  Pending approval of group, can also close FS
    Issue-0062, FS-Issue-0071.</p>
  
    <p>PF, Dec 11/2000: Introduced unordered forests, implying changes
    to Section 2 (example), Section 3 (expressions and amended type
    system), and Section 4 (amended type equivalences and type
    inference rules). Resolved related issues, and raised new issue
    "unordered types".</p>
  
    <p>MF, Dec 9/2000: Added sections on PIs/comments and mixed
    content.  Added <code>nodes</code> and <code>string</code>
    built-in functions.  Changed <code>bag</code> operator to a
    built-in function.  Substituted ^ for &amp; for consistency.</p>
  
    <p>PF, Dec 4/2000: Minor editorial corrections for W3C pubrules,
    some clean-up of associated stylesheets</p>
  
    <p>PF, Nov 20/2000: Added abstract, status section from Paul, and
    Issue-0075 "Overloading user defined functions". Integrated Phil's
    motivation for the type system into the introduction.  Slightly
    edited (anonymized) some issues, and corrected some XML-stuff to
    generate valid HTML.</p>
  
    <p>MF, Nov 16/2000: Corrected many typos.  Added Issue-0074
    "Operational semantics for expressions".</p>
  
    <p>MF, Oct 23/2000: Major changes: Added support for attributes,
    namespaces, all-groups. Changed syntax (and semantics) of case,
    for, sort. Introduced new built-in and derived operators
    (children, data). Refined approach for wildcards. Introduced
    concrete syntax for special kinds of types (ContentType,
    ElementGroup, etc.). Adapted type inference rules to these
    changes, and introduced more specific type system for iteration by
    for. For more details see the resolutions around Sep/Oct 2000 in
    Issue list in Appendix B.</p>
  
    <p>PF, Oct 22/2000: Discarded obsolete issue clustering.  Resolved
    some issues, amended some issue descriptions, and added some
    issues, see Issues List in Appendix B for details.</p>
  
    <p>PF, MF: Oct 16/2000. WARNING. This document has not been
    thoroughly reviewed by the editorial team, and may thus contain a
    number of editorial inconsistencies.  The editorial team plans to
    correct them by about October 24. During this time the body of the
    document may undergo some changes. Nevertheless, the editorial
    team welcomes comments.</p>
  
    <p>PF, Oct 15/2000: Major changes: substantial update of the
    issues list. Resolved some, added some, see Issues list in
    Appendix B for details.</p>
  
    <p>PF, Aug 23/2000. Added Issue-0061 from Group 3 F2F in Redmond,
     added reference to I18N character doc.</p>
  
    <p>MF, Aug 11/2000: Proofread, typographical and editorial
    corrections</p>
  
    <p>PF, Aug 09/2000: Added more issues by LA-Team and I18N, added
    links to dtd, xml, and stylesheets.</p>
      
    <p>PF, Aug 05/2000: Major changes: Added Appendix on Issues, moved
    issues list in Discussion to Appendix on Issues.</p>
   
    <p>PF, July 21/2000: First version by Fernández,
    Siméon, Wadler. All copy-editing mistakes resulting from
    the transformation to the XML spec.dtd are due to me.</p>
  
    <p>Fernández, Siméon, Wadler: June/July 2000:
    Original Version</p>
  
    <p>Fankhauser, July 21 2000. Transformed original version to
    conform to XML spec.dtd</p>
  
    <p>Fankhauser, August 05 2000. Made various editorial corrections
    (in particular whitespace-magic for mixed content...). Compiled
    list of issues for appendix. Merged Discussion Section with list
    of issues.</p>
  
    <p>Fernandez, August 11 2000. Made various typographical
    corrections.</p>
  </revisiondesc>

</header>


  <body>
     <!-- *********************************************************************
      Section 1. Introduction
     ********************************************************************* -->

<div1 id="introduction">
  <head>Introduction</head>

  <p>This document defines the formal semantics of XQuery 1.0 and
  XPath 2.0. The present document is part of a set of documents that
  together define the XQuery 1.0 and XPath 2.0 languages:</p>

  <ulist>
    <item>
      <p><bibref ref="xquery"/> introduces the XQuery 1.0 language, defines
      its capabilities from a user-centric view, and defines the
      language syntax.</p>
    </item>
    <item>
      <p><bibref ref="xpath20"/> introduces the XPath 2.0 language, defines its
      capabilities from a user-centric view, and defines the language
      syntax.</p>
    </item>
    <item>
      <p><bibref ref="xpath-functions"/> lists the functions and operators defined for
      the [XPath/XQuery] language and specifies the required types of their 
      parameters and return value.  </p>
    </item>
    <item>
      <p><bibref ref="xpath-datamodel"/> formally specifies the data model used by
      [XPath/XQuery] to represent the content of XML documents.  The
      [XPath/XQuery] language is formally defined by operations on this
      data model.</p>
    </item>
    <item>
      <p><bibref ref="xslt-xquery-serialization"/> specifies how [XPath/XQuery] data
      model values are serialized into XML.</p>
    </item>
  </ulist>

  <p>The scope and goals for the [XPath/XQuery] language are discussed in
  the charter of the W3C [XSL/XML Query] Working Group and in the [XPath/XQuery]
  requirements <bibref ref="xquery-requirements"/>.</p>

  <p>This document defines the semantics of [XPath/XQuery] by giving a
  precise formal meaning to each of the expressions of the [XPath/XQuery]
  specification in terms of the [XPath/XQuery] data model. This document
  assumes that the reader is already familiar with the [XPath/XQuery]
  language. This document defines the formal semantics for XPath 2.0
  only when the XPath 1.0 backward compatibility rules are not in
  effect.</p>

  <p>Two important design aspects of [XPath/XQuery] are that it is
  <emph>functional</emph> and that it is <emph>typed</emph>. These two
  aspects play an important role in the [XPath/XQuery] Formal
  Semantics.</p>

  <p><term>[XPath/XQuery] is a functional language</term>. [XPath/XQuery] is
  built from expressions, rather than statements. Every construct in
  the language (except for the XQuery query prolog) is an expression
  and expressions can be composed arbitrarily. The result of one
  expression can be used as the input to any other expression, as long
  as the type of the result of the former expression is compatible
  with the input type of the latter expression with which it is
  composed. Another characteristic of a functional language is that
  variables are always passed by value, and a variable's value cannot
  be modified through side effects.</p>

  <p><term>[XPath/XQuery] is a typed language</term>. Types can be
  imported from one or more XML Schemas that describe the input
  documents and the output document, and the [XPath/XQuery] language can
  then perform operations based on these types. In addition,
  [XPath/XQuery] supports <emph>static type analysis</emph>.  Static type
  analysis infers the output type of an expression based on the type
  of its input expressions.  In addition to inferring the type of an
  expression for the user, static typing allows early detection of
  type errors, and can be used as the basis for certain classes of
  optimization. The [XPath/XQuery] type system captures most of the
  features of <bibref ref="xmlschema-1"/>, including global and local element and
  attribute declarations, complex and simple type definitions, named
  and anonymous types, derivation by restriction, extension, list and
  union, substitution groups, and wildcard types. It does not model
  uniqueness constraints and facet constraints on simple types.</p>

  <p>This document is organized as follows. <specref ref="sec_preliminaries"/> introduces the notations used to define
  the [XPath/XQuery] Formal Semantics.  These include the formal notations
  for values in the [XPath/XQuery] data model and for types in XML Schema.
  The next three sections: <specref ref="id-basics"/>, <specref ref="id-expressions"/>, and <specref ref="id-query-prolog"/> have
  the same structure as the corresponding sections in the
  <bibref ref="xquery"/> and <bibref ref="xpath20"/> documents.  This allows the reader to
  quickly find the formal definition of a particular language
  construct.  <specref ref="id-basics"/> defines the semantics for
  basic [XPath/XQuery] concepts, and <specref ref="id-expressions"/>
  defines the dynamic and static semantics of each [XPath/XQuery]
  expression.  <specref ref="id-query-prolog"/> defines the semantics
  of the [XPath/XQuery] prolog.

  <specref ref="sec_special_functions"/> defines the static semantics
  of several functions in <bibref ref="xpath-functions"/> and gives the dynamic and
  static semantics of several supporting functions used in this
  document.  The remaining sections, <specref ref="sec_auxiliary_judgments"/> and <specref ref="sec_importing_schema"/>, contain material that supports the
  formal semantics of [XPath/XQuery].  <specref ref="sec_auxiliary_judgments"/> defines formal judgments that relate
  data model values to types, that relate types to types, and that
  support the formal definition of validation.  These judgments are
  used in the definition of expressions in <specref ref="id-expressions"/>.

  Lastly, <specref ref="sec_importing_schema"/>, specifies how XML Schema documents are
  imported into the [XPath/XQuery] type system and relates XML Schema
  types to the [XPath/XQuery] type system.</p>

  <div2 id="id-normativity"><head>Normative and Informative
  Sections</head>

  <p>Certain aspects of language processing are described in this
  specification as <term>implementation-defined</term> or
  <term>implementation-dependent</term>.</p>

  <ulist>
    <item>
      <p><termdef id="dt-implementation-defined" term="implementation       defined"><term>Implementation-defined</term> indicates an aspect
      that may differ between implementations, but must be specified
      by the implementor for each particular
      implementation.</termdef></p>
    </item>
    <item>
      <p><termdef id="dt-implementation-dependent" term="implementation       dependent"><term>Implementation-dependent</term> indicates an
      aspect that may differ between implementations, is not specified
      by this or any W3C specification, and is not required to be
      specified by the implementor for any particular
      implementation.</termdef></p>
    </item>
  </ulist>

  <p role="xpath">A language aspect described in this specification as
  <term>implementation-defined</term> or <term>implementation
  dependent</term> may be further constrained by the specifications of
  a host language in which XPath or XQuery is embedded.</p>

  <p>This document contains the normative static semantics of
  [XPath/XQuery]. The static semantics rules in <specref ref="id-basics"/>, <specref ref="id-expressions"/>, <specref ref="id-query-prolog"/>, and <specref ref="sec_special_functions"/>
  are normative. <specref ref="static_context"/> is normative, because
  it defines the static context used in the static typing rules.
  <specref ref="sec_auxiliary_judgments"/> is normative, because it
  contains all the judgments necessary for defining SequenceType
  Matching.</p>

  <p>The dynamic semantics of [XPath/XQuery] are normatively defined in
  <bibref ref="xquery"/> and <bibref ref="xpath20"/>. In this document, the dynamic
  semantic rules in <specref ref="id-basics"/>, <specref ref="id-expressions"/>, and <specref ref="id-query-prolog"/>, the
  examples, and the material labeled as "Note" are provided for
  explanatory purposes and are not normative.</p>

  <p>The mapping rules from XML Schema to the XQuery type system
  provided in <specref ref="sec_importing_schema"/>, and the formal
  semantics of XML Schema validation in <specref ref="sec_validation_judgments"/> are informative and do not handle
  every feature of XML Schema.</p>
</div2>

</div1>


     <!-- ===================================================================== -->
<!--  Section 2. Preliminaries                                             -->
<!-- ===================================================================== -->

<div1 id="sec_preliminaries">
  <head>Preliminaries</head>

  <p>This section provides the background necessary to understand the
  Formal Semantics, introduces the notations that are used, and
  explains its relationship to other documents.</p>

  <div2 id="sec_intro">
    <head>Introduction to the Formal Semantics</head>

    <p><term>Why a Formal Semantics?</term> The goal of the formal
    semantics is to complement the [XPath/XQuery] specification
    (<bibref ref="xquery"/> and <bibref ref="xpath20"/>), by defining the meaning of
    [XPath/XQuery] expressions with mathematical rigor.</p>

    <p>A rigorous formal semantics clarifies the intended meaning of
    the English specification, ensures that no corner cases are left
    out, and provides a reference for implementation.</p>

    <p><term>Why use formal notations?</term> Rigor is achieved by the
    use of formal notations to represent [XPath/XQuery] objects such as
    expressions, XML values, and XML Schema types, and by the
    systematic definition of the relationships between those objects
    to reflect the meaning of the language.  In particular, the
    dynamic semantics relates [XPath/XQuery] expressions to the XML value
    to which they evaluate, and the static semantics relates
    [XPath/XQuery] expressions to the XML Schema type that is inferred for
    that expression.</p>

    <p>The Formal Semantics uses several kinds of formal notations to
    define the relationships between [XPath/XQuery] expressions, XML
    values, and XML Schema types. This section introduces the
    notations for judgments, inference rules, and mapping rules as
    well as the notation for environments, which implement the dynamic
    and static contexts. The reader already familiar with these
    notations can skip this section and continue with <specref ref="sec_values"/>.</p>

    <!-- ***************** Grammar productions    ****** -->

    <div3 id="sec_grammar_notations">
      <head>Notations from grammar productions</head>

      <p>Grammar productions are used to describe
      <quote>objects</quote> (values, types, [XPath/XQuery] expressions,
      etc.) manipulated by the Formal Semantics. The Formal Semantics
      makes use of several kinds of grammar productions: productions
      from the [XPath/XQuery] grammar itself, productions for a subset of
      the [XPath/XQuery] language called the XQuery Core which is used
      throughout this document, and other productions used for formal
      specification only such as for the XQuery type system.</p>

      <p>XQuery grammar productions describe the XQuery language and
      expressions. XQuery productions are identified by a number,
      which corresponds to their number in the <bibref ref="xquery"/>
      document, and are marked with <quote>(XQuery)</quote>. For
      instance, the following production describes FLWOR expressions
      in XQuery.</p>
        
      <scrap headstyle="show">
        <head>[For/FLWOR] Expressions</head>
        <prod num="33 (XQuery)" id="noid_N103F0.doc-xquery-FLWORExpr"><lhs>FLWORExpr</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ForClause" xlink:type="simple">ForClause</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-LetClause" xlink:type="simple">LetClause</nt>)+  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-WhereClause" xlink:type="simple">WhereClause</nt>?  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-OrderByClause" xlink:type="simple">OrderByClause</nt>?  "return"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ExprSingle" xlink:type="simple">ExprSingle</nt></rhs></prod>
      </scrap>
      
      <p>For the purpose of this document, the differences between the
      XQuery 1.0 and the XPath 2.0 grammars are mostly irrelevant. By
      default, this document uses XQuery 1.0 grammar
      productions. Whenever the grammar for XPath 2.0 differs from the
      one for XQuery 1.0, the corresponding XPath 2.0 productions are
      also given. XPath productions are identified by a number, which
      corresponds to their number in <bibref ref="xpath20"/>, and are marked with
      <quote>(XPath)</quote>. For instance, the following production
      describes for expressions in XPath.</p>

      <scrap headstyle="show">
        <head>[For/FLWOR] Expressions</head>
        <prod num="4 (XPath)" id="noid_N10407.doc-xpath-ForExpr"><lhs>ForExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xpath-SimpleForClause" xlink:type="simple">SimpleForClause</nt>  "return"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xpath-ExprSingle" xlink:type="simple">ExprSingle</nt></rhs></prod>
      </scrap>
      
      <p>XQuery Core grammar productions describe the XQuery Core. The
      Core grammar is given in <specref ref="sec_core"/>. Core
      productions are identified by a number, which corresponds to
      their number in <specref ref="sec_core"/>, and are marked with
      <quote>(Core)</quote>. For instance, the following production
      describes the simpler form of the <quote>FLWOR</quote>
      expression in the XQuery Core.</p>

      <scrap headstyle="show">
        <head>Core FLWOR Expressions</head>
        <prod num="24 (Core)" id="noid_N10422.doc-core-FLWORExpr"><lhs>FLWORExpr</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ForClause" xlink:type="simple">ForClause</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-LetClause" xlink:type="simple">LetClause</nt>)  "return"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ExprSingle" xlink:type="simple">ExprSingle</nt></rhs></prod>
      </scrap>
      
      <p>The Formal Semantics manipulates <quote>objects</quote>
      (values, types, expressions, etc.)  for which there is no
      existing grammar production in the <bibref ref="xquery"/> document. In
      these cases, specific grammar productions are
      introduced. Notably, additional productions are used to describe
      values in the <bibref ref="xpath-datamodel"/>, and to describe the [XPath/XQuery]
      type system. Formal Semantics productions are identified by a
      number, and are marked by <quote>(Formal)</quote>. For instance,
      the following production describes global type definitions in
      the [XPath/XQuery] type system.</p>
      
      <scrap headstyle="show">
        <head>Type Definitions</head>
      
        <prod num="39 (Formal)" id="noid_N10443.doc-fs-Definition"><lhs>Definition</lhs><rhs>("define"  "element"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementName" xlink:type="simple">ElementName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-OptSubstitution" xlink:type="simple">OptSubstitution</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-OptNillable" xlink:type="simple">OptNillable</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeReference" xlink:type="simple">TypeReference</nt>)<br/>|  ("define"  "attribute"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeName" xlink:type="simple">AttributeName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeReference" xlink:type="simple">TypeReference</nt>)<br/>|  ("define"  "type"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeName" xlink:type="simple">TypeName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeDerivation" xlink:type="simple">TypeDerivation</nt>)</rhs></prod>
      </scrap>

      <p>Note that grammar productions that are specific to the Formal
      Semantics (i.e., marked with <quote>(Formal)</quote>) are not
      part of [XPath/XQuery]. They are not accessible to the user and are
      only used in the course of defining the languages'
      semantics.</p>

      <p>Grammar non-terminals are used extensively in this document
      to represent objects in judgments (see the next section). As a
      convenience, non-terminals used in judgments link to the
      appropriate grammar production.</p>
    </div3>

    <!-- ***************** Judgments              ****** -->

    <div3 id="sec_judgments">
      <head>Notations for judgments</head>

      <p>The basic building block of the formal specification is
      called a <emph>judgment</emph>. A judgment expresses whether a
      property holds or not.</p>

      <p>For example:</p>

      <smnotation>
        <p>The judgment</p>

        <display>
          <clause>
          <expression><emph>Object</emph> <term>is a positive
          integer</term></expression>
          </clause>
        </display>

        <p>holds if the object <emph>Object</emph> is a positive
        integer.</p>
      </smnotation>

      <p>A judgment may hold (if it is true) or not hold (if it is
      false). For instance '1 <term>is a positive integer</term>'
      holds and '-1 <term>is a positive integer</term>' does not
      hold.</p>

      <smnotation>
        <p>Here are two other example judgments.</p>

        <p>The judgment</p>

        <display>
          <clause>
          <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
          </clause>
        </display>

        <p>holds if the expression <emph>Expr</emph> yields (or evaluates to)
        the value <a href="#doc-fs-Value"><emph>Value</emph></a>.</p>

        <p>The judgment</p>

        <display>
        <clause>
          <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
        </clause>
        </display>

        <p>holds if the expression <emph>Expr</emph> has the type
        <a href="#doc-fs-Type"><emph>Type</emph></a>.</p>

        <p>Most other judgments used in this document are short
        English sentences intended to reflect their meaning. For
        instance, the judgment</p>

        <display>
        <clause>
          <expression><emph>Axis</emph> <a href="#jd_principal" class="judgment"><term>has principal</term></a> <emph>PrincipalNodeKind</emph></expression>
        </clause>
        </display>

        <p>holds if <emph>PrincipalNodeKind</emph> is the principal node kind
        for the axis <emph>Axis</emph>.</p>
      </smnotation>

      <p>A judgment can contain <emph>symbols</emph> and
      <emph>patterns</emph>.</p>

      <p>Symbols are purely syntactic and are used to write the
      judgment itself. Symbols are chosen to reflect a judgment's
      meaning, and are written in bold fonts. For example, '<term>is a
      positive integer</term>', '<term>=&gt;</term>' and
      '<term>:</term>' are symbols, the second and third of which
      should be read <quote>yields</quote>, and <quote>has
      type</quote> respectively.</p>

      <p>Patterns are used to represent objects, constructed from a
      given grammar production. In patterns, italicized words usually
      correspond to non-terminals in the grammar. The name of those
      non-terminals is significant, and may be instantiated only to an
      <quote>object</quote> (a value, a type, an expression, etc.)
      that can be substituted legally for that non-terminal. For
      example, '<emph>Expr</emph>' is a pattern that stands for every
      [XPath/XQuery] expressions, '<emph>Expr</emph><subscript>1</subscript> + <emph>Expr</emph><subscript>2</subscript>' is a pattern
      that stands for every addition expression, 'element a {
      <a href="#doc-fs-Value"><emph>Value</emph></a> }' is a pattern that stands for every value in the
      [XPath/XQuery] data model that is an 'a' element.</p>

      <p>Non-terminals in a pattern may appear with subscripts
      (e.g. <emph>Expr</emph><subscript>1</subscript>, <emph>Expr</emph><subscript>2</subscript>) to distinguish different instances
      of the same sort of pattern. In some cases, non-terminals in a
      pattern may have a name that is not exactly the name of that non
      terminal, but is based on it. For instance, a <a href="#doc-fs-TypeName"><emph>BaseTypeName</emph></a>
      is a pattern that stands for a type name, as would
      <a href="#doc-fs-TypeName"><emph>TypeName</emph></a>, or <a href="#doc-fs-TypeName"><emph>TypeName</emph></a><subscript>2</subscript>. This usage is limited, and
      only occurs to improve the readability of some of the inference
      rules.</p>

      <p>When <emph>instantiating</emph> the judgment, each pattern
      must be instantiated to an appropriate sort of
      <quote>object</quote> (value, type, expression, etc). For
      example, '3 <term>=&gt;</term> 3' and '$x+0 <term>=&gt;</term>
      3' are both instances of the judgment '<emph>Expr</emph>
      <term>=&gt;</term> <a href="#doc-fs-Value"><emph>Value</emph></a>'. Note that in the first
      judgment, '3' corresponds to both the expression '3' (on the
      left-hand side of the <term>=&gt;</term> symbol) and to the
      value '3' (on the right-hand side of the <term>=&gt;</term>
      symbol).</p>

      <p id="jd_not">In some cases, inference rules may need to use
      the fact that a certain judgment <emph>does not</emph> hold.
      <expression><a href="#jd_not" class="judgment"><term>not</term></a>(<emph>Judgment</emph>)</expression> holds
      if and only if <emph>Judgment</emph> does not hold.</p>

      <p id="jd_isin">In some cases, a pattern may be instantiated to
      a value within a finite set of pre-determined values. We may
      write that set of possible values using the <a href="#jd_isin" class="judgment"><term>in</term></a>
      judgment. For instance, the judgment</p>

        <display>
          <clause>
          <expression><emph>Color</emph> <a href="#jd_isin" class="judgment"><term>in</term></a> {
          <term>blue</term>, <term>green</term> }
          </expression>
          </clause>
        </display>

      <p>holds if the pattern <emph>Color</emph> has either the value
      <emph>blue</emph> or the value <emph>green</emph>.</p>

      <p>In some cases, a judgment may use the <quote>=</quote> sign
      to indicate that a given value is equal to another value, or
      that a pattern is equal to a given value. For instance, the
      judgment</p>

        <display>
          <clause>
          <expression><emph>Color</emph> = <term>blue</term>
          </expression>
          </clause>
        </display>

      <p>holds if the pattern <emph>Color</emph> has the value
      <emph>blue</emph>.</p>

      <p>An index to all the judgments used in this specification is
      provided in <specref ref="sec_indexes"/>.</p>
    </div3>

    <!-- ***************** Environments              ****** -->

    <div3 id="sec_environments">
      <head>Notations for environments</head>

      <p id="xq_environment">An environment component is a dictionary
      that maps a symbol (e.g., a function name or a variable name) to
      an <quote>object</quote> (e.g., a function body, a type, a
      value). One can access information in an environment component
      or update it.</p>

      <p>If <quote>envComp</quote> is an environment component, then
      <quote>envComp(<emph>symbol</emph>)</quote> denotes the
      <quote>object</quote> to which <emph>symbol</emph> is mapped.
      The notation is intentionally similar to function application,
      because an environment component can be considered a function
      from the argument <emph>symbol</emph> to the
      <quote>object</quote> to which the <emph>symbol</emph> is
      mapped.</p>

      <p>This document uses <emph>environments</emph> that group
      related environment components. If <quote>env</quote> is an
      environment containing the environment component
      <quote>envComp</quote>, that environment component is denoted
      <quote>env.envComp</quote>. The value that <emph>symbol</emph>
      is mapped to in that environment component is denoted
      <quote><expression>env.envComp(<emph>symbol</emph>)</expression></quote>.</p>

      <p>The two main environments used in the Formal Semantics are: a
      dynamic environment (<a href="#xq_dyn_env_def" class="env">dynEnv</a>), which models the [XPath/XQuery]
      dynamic context, and a static environment (<a href="#xq_stat_env_def" class="env">statEnv</a>), which
      models the [XPath/XQuery] static context. Both are defined in
      <specref ref="sec_context"/>.</p>

      <p>For example, <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a> denotes the dynamic environment
      component that maps variables to values and
      <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a>(<emph>Variable</emph>) denotes the value of the variable
      <emph>Variable</emph> in the dynamic context.</p>

      <p>Environments are used in a judgment to capture some of the
      context in which the judgment is computed, and most judgments
      are computed assuming that some environment is given. This
      <emph>assumption</emph> is denoted by prefixing the judgment
      with <quote><expression>env</expression> |-</quote>. The
      <quote>|-</quote> symbol is called a
      <quote>turnstile</quote> and is used in almost all inference
      rules.</p>

      <p>For instance, the judgment</p>

      <display>
        <clause>
          <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
          <expression>
      	    <emph>Expr</emph> <term>=&gt;</term> <a href="#doc-fs-Value"><emph>Value</emph></a>
          </expression>
        </clause>
      </display>

      <p>is read as: Assuming the dynamic environment <a href="#xq_dyn_env_def" class="env">dynEnv</a>,
      the expression <emph>Expr</emph> yields the value <a href="#doc-fs-Value"><emph>Value</emph></a>.</p>

      <p>Environments can be <emph>updated</emph>, using the following
      notation:</p>

      <ulist>
        <item>
          <p>
            <quote><expression><update><environment>env.envComp</environment>
            <expression><emph>symbol</emph> =&gt;
            <emph>object</emph></expression></update></expression></quote>
            denotes the new environment that is identical to
            <emph>env</emph> except that the environment component
            <emph>envComp</emph> has been updated to map
            <emph>symbol</emph> to <emph>object</emph>. The notation
            <expression><emph>symbol</emph> =&gt;
            <emph>object</emph></expression> indicates that
            <emph>symbol</emph> is mapped to <emph>object</emph> in
            the new environment.</p>
        </item>
        <item>
          <p>In case the environment component contains only a
            constant value (e.g., the ordering mode which can only be
            either ordered or unordered), the following notation is
            used to set its value.
            <quote><expression><update>
                  <environment>env.envComp</environment>
                  <expression>
                    <emph>object</emph>
                  </expression>
                </update></expression></quote>.</p>
        </item>
        <item>
          <p>
            The following shorthand is also allowed:
            <quote><expression><update>
                  <environment>env.envComp</environment>
                  <expression>
                    <emph>symbol</emph><subscript>1</subscript>
                    =&gt;
                    <emph>object</emph><subscript>1</subscript>
                    ; ... ;
                    <emph>symbol</emph><subscript>n</subscript>
                    =&gt;
                    <emph>object</emph><subscript>n</subscript>
                  </expression>
                </update></expression></quote> in which each symbol is
                mapped to a corresponding object in the new
                environment.</p>

            <p>This notation is equivalent to nested updates, as in
            <quote>
              <expression>
                (env + envComp(
                <emph>symbol</emph><subscript>1</subscript>
                =&gt;
                <emph>object</emph><subscript>1</subscript>)
                + 
                ...
                ) + env(<emph>symbol</emph><subscript>n</subscript>
                =&gt;
                <emph>object</emph><subscript>n</subscript>)</expression></quote>.
          </p>
        </item>
      </ulist>

      <p>Updating an environment creates a copy of the original
      environment and overrides any previous binding that might exist
      for the same name and the same component in that
      environment. Updating the environment is used to capture the
      <emph>scope</emph> of a symbol (e.g., for variables, namespace
      prefixes, etc). For instance, in the following expression</p>

<eg xml:space="preserve">
  let $x := 1 return
  let $x := $x + 2 return
  $x - 3
</eg>

      <p>each let expression changes the dynamic context by binding a
      new variable to a new value. Each different context is
      represented by a different environment. The original
      environment, in which the expression <code>1</code> is
      evaluated, does not contain any binding for variable
      <code>$x</code>. This environment is updated a first time with a
      binding of variable <code>$x</code> to the value <code>1</code>,
      and this new environment is used for the evaluation of the
      expression <code>$x + 2</code>. Then this second environment is
      updated with a binding of variable <code>$x</code> to the value
      <code>3</code>, and this environment is used for the evaluation
      of the expression <code>$x - 3</code>.</p>

      <p>Also, note that there are no operations to remove entries
      from environments. This is never necessary as updating an
      environment effectively creates a new extended copy of the
      original environment, leaving the original environment
      accessible wherever it is in scope along with the updated
      copy.</p>

    </div3>

    <!-- ***************** Inference rules ****** -->

    <div3 id="sec_inference">
      <head>Notations for inference rules</head>

      <p>Inference rules are used to specify how to infer whether a
      given judgment holds or not. Inference rules express the logical
      relation between judgments and describe how complex judgments
      can be concluded from simpler premise judgments. </p>

      <p>A logical inference rule is written as a collection of
      <emph>premise</emph>s and a <emph>conclusion</emph>, written
      respectively above and below a dividing line, as follows:</p>

      <infergr>
    	<infer>
    	 <prejudge>
    	  <clause>
    	   <expression>
    	       <emph>premise</emph><subscript>1</subscript>
    	       ...
    	       <emph>premise</emph><subscript>n</subscript>
    	   </expression>
    	  </clause>
    	 </prejudge>
    	 <postjudge>
    	  <clause>
    	   <expression>
    	       <emph>conclusion</emph>
    	   </expression>
    	  </clause>
    	 </postjudge>
    	</infer>
      </infergr>

      <p>All premises and the conclusion are judgments. From a logical
      point of view, an inference rule is a deduction that if the
      premises hold, then the conclusion holds as well. In that sense,
      the previous inference rule has a similar meaning as the
      following logical statement.</p>

      <p>IF <emph>premise</emph><subscript>1</subscript></p>
      <p>AND ...</p>
      <p>AND <emph>premise</emph><subscript>n</subscript></p>
      <p>THEN <emph>conclusion</emph></p>

      <p>Here is a simple example of inference rule, which uses
      specific instances of the example judgment '<emph>Expr</emph>
      <term>=&gt;</term> <a href="#doc-fs-Value"><emph>Value</emph></a>' from above:</p>

      <infergr>
    	<infer>
    	 <prejudge>
    	  <clause>
    	   <expression>
    	       $x <term>=&gt;</term> 0
    	   </expression>
    	  </clause>
    	  <clause>
    	   <expression>
    	       3 <term>=&gt;</term> 3
    	   </expression>
    	  </clause>
    	 </prejudge>
    	 <postjudge>
    	  <clause>
    	    <expression>
    	       $x + 3 <term>=&gt;</term> 3
    	    </expression>
    	  </clause>
    	 </postjudge>
    	</infer>
      </infergr>

      <p>This inference rule expresses the following property:
      <emph>if</emph> the variable expression '$x' yields the value
      '0', <emph>and</emph> the literal expression '3' yields the
      value '3', <emph>then</emph> the expression '$x + 3' yields the
      value '3'.</p>

      <p>An inference rule may have no premises above the line, which
      means that the expression below the line always holds. For
      instance:</p>

      <infergr>
    	<infer>
    	 <postjudge>
    	  <clause>
    	    <expression>
    	       3 <term>=&gt;</term> 3 
    	    </expression>
    	  </clause>
    	 </postjudge>
    	</infer>
      </infergr>

      <p>This inference rule expresses the following property:
      evaluating the literal expression '3' always yields the value
      '3'.</p>

      <p>The two above rules are expressed in terms of specific
      expressions and values, but usually rules are more abstract.
      That is, the judgments are not fully instantiated. Here is a
      rule that says that for any variable $<emph>VarName</emph> that yields
      the integer value <emph>Integer</emph>, adding '0' yields the same
      integer value:</p>

      <infergr>
        <infer>
         <prejudge>
           <clause>
             <expression>
                $<emph>VarName</emph> <term>=&gt;</term> <emph>Integer</emph>
             </expression>
           </clause>
         </prejudge>
         <postjudge>
          <clause>
            <expression>
              $<emph>VarName</emph> + 0 <term>=&gt;</term> <emph>Integer</emph>
            </expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>

      <p>Each occurrence of a given pattern in a particular inference
      rule must be instantiated to the same <quote>object</quote>
      within the entire rule.  This means that, in the context of a
      particular instantiation of a rule, one can talk about
      <quote>the value of $<emph>VarName</emph></quote> instead of the value
      bound to the first (second, etc) occurrence of
      $<emph>VarName</emph>.</p>

      <p>Here is an example of a rule occurring later in this
      document.</p>

      <infergr>
       <infer>
        <prejudge>
         <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>Expr</emph><subscript>1</subscript> <term>:</term> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
         </clause>
         <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>Expr</emph><subscript>2</subscript> <term>:</term> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
         </clause>
        </prejudge>
        <postjudge>
         <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>Expr</emph><subscript>1</subscript> , <emph>Expr</emph><subscript>2</subscript> <term>:</term> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
         </clause>
        </postjudge>
       </infer>
      </infergr>

      <p>This rule is read as follows: if two expressions <emph>Expr</emph><subscript>1</subscript>
      and <emph>Expr</emph><subscript>2</subscript> are known to have the static types <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> and
      <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> (the two premises above the line), then it is the
      case that the sequence expression <quote><emph>Expr</emph><subscript>1</subscript> ,
      <emph>Expr</emph><subscript>2</subscript></quote> has the static type <quote><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,
      <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></quote>, which is the sequence of types <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>
      and <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>. Note that this inference rule does not modify
      the static environment.</p>

      <p>The following rule defines the static semantics of a
      <quote>let</quote> expression. The binding of the new variable
      is captured by an update to the varType component of the
      original static environment.</p>

      <infergr>
       <infer>
    	<prejudge>
         <multiclause>
    	 <clause>
    	  <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
    	  <expression>
    		  <emph>VarName</emph> <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
    	   </expression>
    	 </clause>
         </multiclause>
         <multiclause>
    	 <clause>
    	  <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
    	  <expression>
    		  <emph>Expr</emph><subscript>1</subscript> <term>:</term> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>
    	   </expression>
    	 </clause>
    	 <clause>
    		<environment>
    		  <update>
    		    <environment><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a></environment>
    		    <expression><a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> =&gt; <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
    		  </update>
    		</environment>
    		<expression>
    		    <emph>Expr</emph><subscript>2</subscript> <term>:</term> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>
    	  </expression>
    	 </clause>
         </multiclause>
    	</prejudge>
    	<postjudge>
    	 <clause>
    	  <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
    	  <expression>
    		  <code>let</code>
    		  <code>$</code><emph>VarName</emph> := <emph>Expr</emph><subscript>1</subscript>
    		  <code>return</code>
    		  <emph>Expr</emph><subscript>2</subscript> <term>:</term> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>
    	  </expression>
    	 </clause>
    	</postjudge>
       </infer>
      </infergr>

      <p>This rule is read as follows: First, because the variable is
      a QName, it is first expanded into an expanded QName.  Second,
      the type <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> for the <quote>let</quote> input expression
      <emph>Expr</emph><subscript>1</subscript> is computed.  Then the <quote>let</quote> variable
      with expanded name, <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> with type <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> is added
      into the varType component of the static environment
      <a href="#xq_stat_env_def" class="env">statEnv</a>. Finally, the type <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> of <emph>Expr</emph><subscript>2</subscript> is
      computed in that new environment.</p>

      <p>In some cases, ellipses may be used in inference rules to
      handle an arbitrary number of judgments. In those cases, some of
      the patterns may have indices as subscript. If the same index is
      used several times within the same rule, the number of judgment
      in each case must be the same. For instance, the following rule
      holds for any number of expressions, from <emph>Expr</emph><subscript>1</subscript> to
      <emph>Expr</emph><subscript>n</subscript>, with the same number of types <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> to
      <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>QName</emph>
                  <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment> 
                  <expression>
                    <emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause>
                  <expression>
                    ...
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment> 
                  <expression>
                    <emph>Expr</emph><subscript>n</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <emph>QName</emph> (<emph>Expr</emph><subscript>1</subscript>,...,<emph>Expr</emph><subscript>n</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <p>This inference rule is equivalent to having an unbounded
        number of rules, the first of which has 1 judgment, the second
        of which has 2 judgments, etc. For instance, the above rule
        holds if and only if one of the following rules hold.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>QName</emph>
                  <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment> 
                  <expression>
                    <emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <emph>QName</emph> (<emph>Expr</emph><subscript>1</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <p>or</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>QName</emph>
                  <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment> 
                  <expression>
                    <emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment> 
                  <expression>
                    <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <emph>QName</emph> (<emph>Expr</emph><subscript>1</subscript>,<emph>Expr</emph><subscript>2</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <p>etc.</p>

        <p>When ellipses are used, the value for the index always
        ranges from 1 to an arbitrary number n.</p>
    </div3>

    <div3 id="sec_together">
      <head>Putting it together</head>

      <p>In isolation, each inference rule describes a fragment of the
      semantics for a given judgment. Put together, inference rules
      describe possible inferences that can be used to decide whether
      a particular judgment holds.</p>

      <p>For a given judgment, if that judgment can be inferred to be
      true by applying any sequence of inferences based on premises
      which are known to be true, the inference succeeds. In most
      cases, the inference will proceed by proving intermediate
      judgments, following the consequences from one judgment to the
      next by applying successive inference rules.</p>

      <p>Such inference is a mechanism which can be used to describe
      both static type analysis and dynamic evaluation. More
      specifically, performing static typing consists in proving that
      the following judgment holds for a given expression
      <emph>Expr</emph>.</p>

      <display>
      <clause>
        <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
        <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
      </clause>
      </display>

      <p>If the judgment holds for a given type <a href="#doc-fs-Type"><emph>Type</emph></a>, this type
      is a possible static type for the expression. If there exists no
      type for which this judgment holds, then static typing
      fails and a static type error is returned to the user.</p>

      <p>Consider the following expression.</p>

<eg xml:space="preserve">
  fn:count((1,2,3))
</eg>

      <p>Using the static typing rules given for expressions in the
      rest of this document, one can deduce that the expression is of
      type <code>xs:integer</code> through the following inference.</p>

<eg xml:space="preserve">
  statEnv |- 1 : xs:integer  (from typing of literals)
  statEnv |- 2 : xs:integer  (from typing of literals)
  --------------------------------------------------- (sequence)
    statEnv |- 1,2 : xs:integer, xs:integer
    statEnv |- 3 : xs:integer
    ----------------------------------------------------- (sequence)
    statEnv |- 1,2,3 : xs:integer, xs:integer, xs:integer

    declare function fn:count($x as item()*) as xs:integer
    statEnv |- xs:integer,xs:integer,xs:integer &lt;: item()*
    ---------------------------------------------------------- (function call)
    statEnv |- fn:count((1,2,3)) : xs:integer
</eg>

   <p>Conversly, consider the following expression.</p>

<eg xml:space="preserve">
  fn:nilled((1,2,3))
</eg>

   <p>Using the static typing rules given for expressions in the rest
   of this document, one can apply inference rules up to the
   following point.</p>

<eg xml:space="preserve">
    ....
    ----------------------------------------------------- (sequence)
    statEnv |- 1,2,3 : xs:integer, xs:integer, xs:integer
</eg>

      <p>However, there is no rule that can infer the type of
      <code>fn:nilled((1,2,3))</code>, because the static typing rules
      for function calls will only hold if the type of the function
      parameters is a subtype of the expected type. However, here
      <code>(xs:integer,xs:integer,xs:integer)</code> is not a node
      type, which is the expected type for the function
      <code>fn:nilled</code>.</p>

      <p>Note that in some cases, the inference can only proceed
      through the appropriate changes to the environment. For
      instance, consider the following expression.</p>

<eg xml:space="preserve">
  let $x := 1 return ($x,$x)
</eg>

      <p>Using the static typing rules given for expressions in the
      rest of this document, one can deduce that the expression is of
      type <code>(xs:integer,xs:integer)</code> through the following
      inference.</p>

<eg xml:space="preserve">
statEnv0.varType = ()

  -------------------------- (literal)
  statEnv0 |- 1 : xs:integer

statEnv1 = statEnv0 + varType($x =&gt; xs:integer)

     statEnv1.varType($x) = xs:integer
     --------------------------------- (variable reference)
     statEnv1 |- $x : xs:integer

     statEnv1.varType($x) = xs:integer
     --------------------------------- (variable reference)
     statEnv1 |- $x : xs:integer

     ------------------------------------------- (sequence)
     statEnv1 |- ($x,$x) : xs:integer,xs:integer

  -------------------------------------------------------------- (let)
  statEnv0 |- let $x := 1 return ($x,$x) : xs:integer,xs:integer
</eg>

      <p>This example illustrates how each rule is applied to
      individual sub-expressions, and how the environment is used to
      maintain the relevant context information.</p>
    </div3>

  </div2>

  <div2 id="sec_namespaces">
    <head>URIs, Namespaces, and Prefixes</head>

    <p>The Formal Semantics does not formally specify the adjustment
    of relative URIs according to a base URI. All URIs used in this
    document are assumed to be absolute URIs.</p>

    <p>The Formal Semantics uses the following namespace prefixes.</p>

    <ulist>
      <item><p><code>fn:</code> for functions and operators from the
      <bibref ref="xpath-functions"/> document.</p></item>
      <item><p><code>xs:</code> for XML Schema components and
      built-in types.</p></item>
    </ulist>

    <p>These prefixes are assumed to be bound to the appropriate
    URIs.</p>

    <p>In addition, the Formal Semantics uses the following special
    prefixes for specification purposes.</p>

    <ulist>
      <item><p><emph>dm:</emph> for accessors of the
      <bibref ref="xpath-datamodel"/>.</p></item>
      <item><p><emph>op:</emph> for operators in
      <bibref ref="xpath-functions"/>.</p></item>
      <item><p><emph>fs:</emph> for functions and types defined in
      the formal semantics.</p></item>
    </ulist>

    <p>These prefixes are always italicized to emphasize that the
    corresponding functions, variables, and types are abstract: they
    are not and cannot be made accessible in [XPath/XQuery]. None of these
    special prefixes are given an explicit URI, but they behave as if
    they had one for the purposes of namespace resolution.</p>
  </div2>

  <div2 id="sec_values">
    <head>XML Values</head>

    <p>The [XPath/XQuery] language is defined over values of the
    [XPath/XQuery] data model. The [XPath/XQuery] data model is defined
    normatively in <bibref ref="xpath-datamodel"/>. We define the formal notation that
    is used in this document to describe and manipulate values in
    inference rules. Formal values are used for specification purposes
    only and are not exposed to the [XPath/XQuery] user.</p>

    <p>This section gives the grammar for formal values, along with a
    summary of the corresponding data model properties. In the context
    of this document, all constraints on values that are specified in
    <bibref ref="xpath-datamodel"/> are assumed to hold.</p>

    <div3 id="sec_data_model_overview">
      <head>Formal values</head>

      <p>A value is a sequence of zero or more items.  An item is
      either an atomic value or a node.</p>

      <p>An atomic value is a value in the value space of an atomic
      type, labeled with the name of that atomic type. An atomic type
      is either a primitive or derived atomic type according to XML
      Schema <bibref ref="xmlschema-2"/>, <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>, or
      <a href="#dt-xs_anyAtomicType"><code>xs:anyAtomicType</code></a>.</p>

      <p>A node is either an element, an attribute, a document, a
      text, a comment, or a processing-instruction node.</p>

      <p>Element nodes have a <xtermref spec="XQ" ref="dt-type-annotation"/> and contain a complex value or a
      simple value. Attribute nodes have a <xtermref spec="XQ" ref="dt-type-annotation"/> and contain a simple value. Text
      nodes always contain one string value of type
      <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>, therefore the corresponding type annotation
      is omitted in the formal notation of a text node. Document nodes
      do not have a type annotation and contain a sequence of element,
      text, comment, or processing-instruction nodes.</p>

      <p>A simple value is a sequence of atomic values.</p>

      <p>A complex value is a sequence of attribute nodes followed by
      a sequence of element, text, comment, or processing-instruction
      nodes.</p>

      <p>A <xtermref spec="XQ" ref="dt-type-annotation"/> can be
      either the <emph>QName</emph> of a declared type or an anonymous
      type. An anonymous type corresponds to an XML Schema type for
      which the schema writer did not provide a name. Anonymous type
      names are not visible to the user, but are generated during
      schema validation and used to annotate nodes in the data
      model. By convention, anonymous type names are written using the
      <emph>fs:</emph> Formal Semantics prefix: <emph>fs:</emph>anon<subscript>0</subscript>,
      <emph>fs:</emph>anon<subscript>1</subscript>, etc.</p>

      <p>Formal values are defined by the following grammar.</p>

      <scrap headstyle="show">
        <head>Values</head>

        <prod num="7 (Formal)" id="doc-fs-Value"><lhs>Value</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Item" xlink:type="simple">Item</nt><br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Value" xlink:type="simple">Value</nt>  ","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Value" xlink:type="simple">Value</nt>)<br/>|  ("("  ")")</rhs></prod>
        <prod num="21 (Formal)" id="doc-fs-Item"><lhs>Item</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-NodeValue" xlink:type="simple">NodeValue</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AtomicValue" xlink:type="simple">AtomicValue</nt></rhs></prod>
        <prod num="22 (Formal)" id="doc-fs-AtomicValue"><lhs>AtomicValue</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AtomicValueContent" xlink:type="simple">AtomicValueContent</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeAnnotation" xlink:type="simple">TypeAnnotation</nt>?</rhs></prod>
        <prod num="1 (Formal)" id="doc-fs-AtomicValueContent"><lhs>AtomicValueContent</lhs><rhs>String<br/>|  Boolean<br/>|  Decimal<br/>|  Float<br/>|  Double<br/>|  Duration<br/>|  DateTime<br/>|  Time<br/>|  Date<br/>|  GYearMonth<br/>|  GYear<br/>|  GMonthDay<br/>|  GDay<br/>|  GMonth<br/>|  HexBinary<br/>|  Base64Binary<br/>|  AnyURI<br/>|  expanded-QName<br/>|  NOTATION</rhs></prod>
        <prod num="2 (Formal)" id="doc-fs-TypeAnnotation"><lhs>TypeAnnotation</lhs><rhs>"of"  "type"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeName" xlink:type="simple">TypeName</nt></rhs></prod>
        <prod num="9 (Formal)" id="doc-fs-ElementValue"><lhs>ElementValue</lhs><rhs>"element"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementName" xlink:type="simple">ElementName</nt>  "nilled"?  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeAnnotation" xlink:type="simple">TypeAnnotation</nt>?  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Value" xlink:type="simple">Value</nt>  "}"  ("{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-NamespaceBindings" xlink:type="simple">NamespaceBindings</nt>  "}")?</rhs></prod>
        <prod num="10 (Formal)" id="doc-fs-AttributeValue"><lhs>AttributeValue</lhs><rhs>"attribute"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeName" xlink:type="simple">AttributeName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeAnnotation" xlink:type="simple">TypeAnnotation</nt>?  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-SimpleValue" xlink:type="simple">SimpleValue</nt>  "}"</rhs></prod>
        <prod num="8 (Formal)" id="doc-fs-SimpleValue"><lhs>SimpleValue</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AtomicValue" xlink:type="simple">AtomicValue</nt><br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-SimpleValue" xlink:type="simple">SimpleValue</nt>  ","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-SimpleValue" xlink:type="simple">SimpleValue</nt>)<br/>|  ("("  ")")</rhs></prod>
        <prod num="11 (Formal)" id="doc-fs-DocumentValue"><lhs>DocumentValue</lhs><rhs>"document"  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Value" xlink:type="simple">Value</nt>  "}"</rhs></prod>
        <prod num="13 (Formal)" id="doc-fs-CommentValue"><lhs>CommentValue</lhs><rhs>"comment"  "{"  String  "}"</rhs></prod>
        <prod num="14 (Formal)" id="doc-fs-ProcessingInstructionValue"><lhs>ProcessingInstructionValue</lhs><rhs>"processing-instruction"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-NCName" xlink:type="simple">NCName</nt>  "{"  String  "}"</rhs></prod>
        <prod num="12 (Formal)" id="doc-fs-TextValue"><lhs>TextValue</lhs><rhs>"text"  "{"  String  "}"</rhs></prod>
        <prod num="20 (Formal)" id="doc-fs-NodeValue"><lhs>NodeValue</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementValue" xlink:type="simple">ElementValue</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeValue" xlink:type="simple">AttributeValue</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-DocumentValue" xlink:type="simple">DocumentValue</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TextValue" xlink:type="simple">TextValue</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-CommentValue" xlink:type="simple">CommentValue</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ProcessingInstructionValue" xlink:type="simple">ProcessingInstructionValue</nt></rhs></prod>
        <prod num="3 (Formal)" id="doc-fs-ElementName"><lhs>ElementName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt></rhs></prod>
        <prod num="6 (Formal)" id="doc-fs-AttributeName"><lhs>AttributeName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt></rhs></prod>
        <prod num="23 (Formal)" id="doc-fs-TypeName"><lhs>TypeName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt></rhs></prod>
        <prod num="15 (Formal)" id="doc-fs-NamespaceBindings"><lhs>NamespaceBindings</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-NamespaceBinding" xlink:type="simple">NamespaceBinding</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-NamespaceBinding" xlink:type="simple">NamespaceBinding</nt>)*</rhs></prod>
        <prod num="17 (Formal)" id="doc-fs-NamespaceBinding"><lhs>NamespaceBinding</lhs><rhs>"namespace"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-NCName" xlink:type="simple">NCName</nt>  "{"  AnyURI  "}"</rhs></prod>
      </scrap>

      <smnotation>
        <p>In the production for <a href="#doc-fs-AtomicValueContent"><emph>AtomicValueContent</emph></a>, each symbol
        in the right-hand side corresponds to one of the primitive
        datatypes. For example, <emph>String</emph> corresponds to
        <code>xs:string</code>, and <emph>Boolean</emph> corresponds to
        <code>xs:boolean</code>. (The mapping is obvious, except that
        "expanded-QName" corresponds to <code>xs:QName</code>) Although there are
        no explicit productions for these symbols, we assume that each
        is a non-terminal that derives a set of syntactic objects,
        each of which corresponds to a value in the value space of the
        corresponding datatype. For instance, the non-terminal
        <emph>String</emph> derives a set of syntactic objects, which appear
        in examples as <code>""</code>, <code>"a"</code>,
        <code>"John"</code>, etc.; each one corresponds to a string
        value in the <code>xs:string</code> value space. For familiarity, these
        objects have been given the same appearance as
        <emph>StringLiteral</emph>s from the XQuery and Core grammars;
        however, these are formal objects, with a distinct role in the
        FS.</p>
      </smnotation>

      <p>Element (resp. attributes) without type annotations, are
      assumed to have the type annotation <code>xs:anyType</code>
      (resp. <code>xs:anySimpleType</code>). Atomic values without type
      annotations, are assumed to have a type annotation which is the
      base type for the corresponding value. For instance,
      <code>"Hello, World!"</code> is equivalent to <code>"Hello,
      World!" of type xs:string</code>.</p>

      <p>Untyped elements (e.g., from well-formed documents) have the
      <xtermref spec="XQ" ref="dt-type-annotation"/> <a href="#dt-xs_untyped"><code>xs:untyped</code></a>,
      untyped attributes have the <xtermref spec="XQ" ref="dt-type-annotation"/> <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>, and untyped
      atomic values have the <xtermref spec="XQ" ref="dt-type-annotation"/> <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>.</p>

      <p>An element has an optional <quote>nilled</quote> marker. This
      marker is present only if the element has been validated against
      an element type in the schema which is <quote>nillable</quote>,
      and the element has no content and an attribute
      <code>xsi:nil</code> set to <code>"true"</code>.</p>

      <p>An element also has a sequence of namespace bindings, which
      are the set of in-scope namespaces for that element. Each
      namespace binding is a prefix, URI pair. Elements without
      namespace bindings are assumed to have an empty set of in-scope
      namespaces.</p>

      <note role="xquery"><p>In <bibref ref="XPath"/>, the in-scope
      namespaces of an element node are represented by a collection of
      <term>namespace nodes</term> arranged on a <term>namespace
      axis</term>, which is optional and deprecated in <bibref ref="xpath20"/>. XQuery does not support the namespace axis and
      does not represent namespace bindings in the form of
      nodes.</p></note>

    </div3>

    <div3 id="sec_example_values">
      <head>Examples of values</head>

      <p><term>A well-formed document</term></p>

<eg xml:space="preserve">
  &lt;fact&gt;The cat weighs &lt;weight units="lbs"&gt;12&lt;/weight&gt; pounds.&lt;/fact&gt;
</eg>

      <p>In the absence of a Schema, this document is represented
      as</p>

<eg xml:space="preserve">
  element fact of type xs:untyped {
    text { "The cat weighs " },
    element weight of type xs:untyped {
      attribute units of type xs:untypedAtomic {
        "lbs" of type xs:untypedAtomic
      }
      text { "12" }
    },
    text { " pounds." }
  }
</eg>

      <p><term>A document before and after validation.</term></p>

<eg xml:space="preserve">
  &lt;weight xsi:type="xs:integer"&gt;42&lt;/weight&gt;
</eg>

      <p>The formal model for values can represent values before and
      after validation. Before validation, this element is represented
      as:</p>

<eg xml:space="preserve">
  element weight of type xs:untyped {
    attribute xsi:type of type xs:untypedAtomic {
      "xs:integer" of type xs:untypedAtomic
    },
    text { "42" }
  }
</eg>

      <p>After validation, this element is represented as:</p>

<eg xml:space="preserve">
  element weight of type xs:integer {
    attribute xsi:type of type xs:QName {
      "xs:integer" of type xs:QName
    },
    42 of type xs:integer
  }
</eg>

      <p><term>An element with a list type</term></p>

<eg xml:space="preserve">
  &lt;sizes&gt;1 2 3&lt;/sizes&gt;
</eg>

      <p>Before validation, this element is represented as:</p>

<eg xml:space="preserve">
  element sizes of type xs:untyped {
    text { "1 2 3" }
  }
</eg>

      <p>Assume the following Schema.</p>

<eg xml:space="preserve">
  &lt;xs:element name="sizes" type="sizesType"/&gt;
  &lt;xs:simpleType name="sizesType"&gt;
    &lt;xs:list itemType="sizeType"/&gt;
  &lt;/xs:simpleType&gt;
  &lt;xs:simpleType name="sizeType"&gt;
    &lt;xs:restriction base="xs:integer"/&gt;
  &lt;/xs:simpleType&gt;
</eg>

      <p>After validation against this Schema, the element is
      represented as:</p>

<eg xml:space="preserve">
  element sizes of type sizesType {
    1 of type sizeType,
    2 of type sizeType,
    3 of type sizeType
  }
</eg>

      <p><term>An element with an anonymous type</term></p>

<eg xml:space="preserve">
  &lt;sizes&gt;1 2 3&lt;/sizes&gt;
</eg>

      <p>Before validation, this element is represented as:</p>

<eg xml:space="preserve">
  element sizes of type xs:untyped {
    text { "1 2 3" }
  }
</eg>

      <p>Assume the following Schema.</p>

<eg xml:space="preserve">
  &lt;xs:element name="sizes"&gt;
    &lt;xs:simpleType&gt;
      &lt;xs:list itemType="xs:integer"/&gt;
    &lt;/xs:simpleType&gt;
  &lt;/xs:element&gt;
</eg>

      <p>After validation, this element is represented as:</p>

<eg xml:space="preserve">
  element sizes of type fs:anon1 {
    1 of type xs:integer,
    2 of type xs:integer,
    3 of type xs:integer
  }
</eg>

      <p>where <emph>fs:</emph>anon<subscript>1</subscript> stands for the internal anonymous name
      generated by the system for the <code>sizes</code> element.</p>

      <p><term>A nillable element with</term> <code>xsi:type</code> <term>set to
      true</term></p>

<eg xml:space="preserve">
  &lt;sizes xsi:nil="true"/&gt;
</eg>

      <p>Before validation, this element is represented as:</p>

<eg xml:space="preserve">
  element sizes of type xs:untyped {
    attribute xsi:nil of type xs:untypedAtomic { "true" of type xs:untypedAtomic }
  }
</eg>

      <p>Assume the following Schema.</p>

<eg xml:space="preserve">
  &lt;xs:element name="sizes" type="sizesType" nillable="true"/&gt;
</eg>

      <p>After validation against this Schema, the element is
      represented as:</p>

<eg xml:space="preserve">
  element sizes nilled of type sizesType {
    attribute xsi:nil of type xs:boolean { true of type xs:boolean }
  }
</eg>

      <p><term>An element with a union type</term></p>

<eg xml:space="preserve">
  &lt;sizes&gt;1 two 3 four&lt;/sizes&gt;
</eg>

      <p>Before validation, this element is represented as:</p>

<eg xml:space="preserve">
  element sizes of type xs:untyped {
    text { "1 two 3 four" }
  }
</eg>

      <p>Assume the following Schema:</p>

<eg xml:space="preserve">
  &lt;xs:element name="sizes" type="sizesType"/&gt;
  &lt;xs:simpleType name="sizesType"&gt;
    &lt;xs:list itemType="sizeType"/&gt;
  &lt;/xs:simpleType&gt;
  &lt;xs:simpleType name="sizeType"&gt;
    &lt;xs:union memberType="xs:integer xs:string"/&gt;
  &lt;/xs:simpleType&gt;
</eg>

      <p>After validation against this Schema, the element is
      represented as:</p>

<eg xml:space="preserve">
  element sizes of type sizesType {
    1 of type xs:integer,
    "two" of type xs:string,
    3 of type xs:integer,
    "four" of type xs:string
  }
</eg>

    </div3>

  </div2>

  <div2 id="sec_types">
    <head>The [XPath/XQuery] Type System</head>

    <p>The [XPath/XQuery] type system is used in the specification of the
    dynamic and of the static semantics of [XPath/XQuery]. This section
    introduces formal notations for describing types.</p>

    <div3 id="sec_schema">
      <head>XML Schema and the [XPath/XQuery] Type System</head>

      <p>The [XPath/XQuery] type system is based on <bibref ref="xmlschema-1"/> and
      <bibref ref="xmlschema-2"/>.  <bibref ref="xmlschema-1"/> and <bibref ref="xmlschema-2"/> specify
      normatively the type information available in [XPath/XQuery].  We
      define the formal notation that is used in this document to
      describe and manipulate types in inference rules. Formal types
      are used for specification purposes only and are not exposed to
      the [XPath/XQuery] user.</p>

      <p><term>Representation of content models.</term> For the
      purpose of static typing, the [XPath/XQuery] type system only
      describes minOccurs, maxOccurs, and minLength, maxLength on list
      types for the occurrences that correspond to the DTD operators
      <code>+</code>, <code>*</code>, and <code>?</code>. Choices are
      represented using the DTD operator <code>|</code>. <term>All
      groups</term> are represented using the interleaving operator
      (<code>&amp;</code>).</p>

      <p><term>Representation of anonymous types.</term> To clarify
      the semantics, the [XPath/XQuery] type system makes all anonymous
      types explicit.</p>

      <p><term>Representation of XML Schema simple type facets and
      identity constraints.</term> For simplicity, XML Schema simple
      type facets and identity constraints are not formally
      represented in the [XPath/XQuery] type system. However, an
      [XPath/XQuery] implementation supporting XML Schema import and
      validation must take simple type facets and identity constraints
      into account.</p>

      <p>This document describe types in the [XPath/XQuery] types system,
      as well as the operations and properties over those types which
      are used to define the [XPath/XQuery] static typing feature. The two
      most important properties are whether a data instances matches a
      type, and whether a type is a subtype of another. Those
      properties are described in <specref ref="sec_type_matching"/>. This document does not describe all
      other possible properties over those types.</p>

      <p>The mapping from XML Schema into the [XPath/XQuery] type system
      is given in <specref ref="sec_importing_schema"/>. The rest of
      this section is organized as follows. <specref ref="sec_item_types"/> describes item types, <specref ref="sec_content_models"/> describes content models, and
      <specref ref="sec_top_level_definitions"/> describe top-level
      type declarations.</p>

    </div3>

    <div3 id="sec_item_types">
      <head>Item types</head>

      <p>An item type is either an atomic type, an element type, an
      attribute type, a document node type, a text node type, a
      comment node type, or a processing instruction type.  We
      distinguish between document nodes, attribute nodes, and nodes
      that can occur in element content (elements, comments,
      processing instructions, and text nodes), as we need to refer to
      element content types later in the formal semantics.</p>

  <scrap headstyle="show">
  <head>Item Types</head>
  <prod num="25 (Formal)" id="doc-fs-FormalItemType"><lhs>FormalItemType</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AtomicTypeName" xlink:type="simple">AtomicTypeName</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-NodeType" xlink:type="simple">NodeType</nt></rhs></prod>
  <prod num="28 (Formal)" id="doc-fs-AtomicTypeName"><lhs>AtomicTypeName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeName" xlink:type="simple">TypeName</nt></rhs></prod>
  <prod num="26 (Formal)" id="doc-fs-NodeType"><lhs>NodeType</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-DocumentType" xlink:type="simple">DocumentType</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeType" xlink:type="simple">AttributeType</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementContentType" xlink:type="simple">ElementContentType</nt></rhs></prod>
  <prod num="27 (Formal)" id="doc-fs-ElementContentType"><lhs>ElementContentType</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementType" xlink:type="simple">ElementType</nt><br/>|  "comment"<br/>|  "processing-instruction"<br/>|  "text"</rhs></prod>
  <prod num="29 (Formal)" id="doc-fs-ElementType"><lhs>ElementType</lhs><rhs>"element"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementNameOrWildcard" xlink:type="simple">ElementNameOrWildcard</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-OptTypeSpecifier" xlink:type="simple">OptTypeSpecifier</nt></rhs></prod>
  <prod num="4 (Formal)" id="doc-fs-ElementNameOrWildcard"><lhs>ElementNameOrWildcard</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt>  |  "*"</rhs></prod>
  <prod num="5 (Formal)" id="doc-fs-AttributeNameOrWildcard"><lhs>AttributeNameOrWildcard</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt>  |  "*"</rhs></prod>
  <prod num="77 (Formal)" id="doc-fs-OptTypeSpecifier"><lhs>OptTypeSpecifier</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeSpecifier" xlink:type="simple">TypeSpecifier</nt>?</rhs></prod>
  <prod num="30 (Formal)" id="doc-fs-TypeSpecifier"><lhs>TypeSpecifier</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-OptNillable" xlink:type="simple">OptNillable</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeReference" xlink:type="simple">TypeReference</nt></rhs></prod>
  <prod num="31 (Formal)" id="doc-fs-AttributeType"><lhs>AttributeType</lhs><rhs>"attribute"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeNameOrWildcard" xlink:type="simple">AttributeNameOrWildcard</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-OptTypeReference" xlink:type="simple">OptTypeReference</nt></rhs></prod>
  <prod num="75 (Formal)" id="doc-fs-OptNillable"><lhs>OptNillable</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Nillable" xlink:type="simple">Nillable</nt>?</rhs></prod>
  <prod num="32 (Formal)" id="doc-fs-Nillable"><lhs>Nillable</lhs><rhs>"nillable"</rhs></prod>
  <prod num="78 (Formal)" id="doc-fs-OptTypeReference"><lhs>OptTypeReference</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeReference" xlink:type="simple">TypeReference</nt>?</rhs></prod>
  <prod num="36 (Formal)" id="doc-fs-TypeReference"><lhs>TypeReference</lhs><rhs>"of"  "type"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeName" xlink:type="simple">TypeName</nt></rhs></prod>
  <prod num="45 (Formal)" id="doc-fs-DocumentType"><lhs>DocumentType</lhs><rhs>"document"  ("{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>  "}")?</rhs></prod>
  </scrap>

      <p>An element or attribute type has a name or wildcard, and an
      optional type reference. A name alone corresponds to a reference
      to a global element or attribute declaration. A name with a type
      reference corresponds to a local element or attribute
      declaration. "element *" or "attribute *" alone refers to the
      wildcard types for any element or any attribute. In addition, an
      element type has an optional nillable flag that indicates
      whether the element can be nilled or not.</p>

      <p>A document type has an optional content type. If no content
      type is given, then the type is treated as being the wildcard
      type for documents, i.e., a sequence of text and element
      nodes. For consistency with element nodes, PIs and comments are
      not indicated in that wildcard type, but may occur in
      instances.</p>

      <smnote>
        <p>Generic node types (e.g., <code>node()</code>) such as used
        in the SequenceType production, are interpreted in the type
        system as a union of the corresponding node types (e.g.,
        element,attribute,text,comment and processing-instruction
        nodes) and therefore do not appear in the grammar. The
        semantics of sequence types is described in <specref ref="id-sequencetype-matching"/>.</p>
      </smnote>
      
      <smexamples>

        <p>The following is a text node type</p>

<eg xml:space="preserve">
  text
</eg>

        <p>The following is a type for all elements</p>

<eg xml:space="preserve">
  element * of type xs:anyType
</eg>

        <p>The following is a type for all elements of type string</p>

<eg xml:space="preserve">
  element * of type xs:string
</eg>

        <p>The following is a type for a nillable element of type
        string and with name <code>size</code></p>

<eg xml:space="preserve">
  element size nillable of type xs:string
</eg>

        <p>The following is a reference to a global attribute
        declaration</p>

<eg xml:space="preserve">
  attribute sizes
</eg>

        <p>The following is a type for elements with anonymous type
        <emph>fs:</emph>anon<subscript>1</subscript>:</p>

<eg xml:space="preserve">
  element sizes of type fs:anon1
</eg>

      </smexamples>

    </div3>

    <div3 id="sec_content_models">
      <head>Content models</head>

      <p>Following XML Schema, types in [XPath/XQuery] are composed from
      item types by optional, one or more, zero or more, <term>all
      group</term>, <term>sequence</term>, <term>choice</term>, empty
      sequence (written <code>empty</code>), or empty choice (written
      <code>none</code>).</p>

      <p>The type <code>empty</code> matches the empty sequence. The type
      <code>none</code> matches no values. <code>none</code> is the identity for
      choice, that is <expression>(<a href="#doc-fs-Type"><emph>Type</emph></a> | <code>none</code>) =
      <a href="#doc-fs-Type"><emph>Type</emph></a></expression>. The type <code>none</code> is the static type
      for <specref ref="sec_fnerror"/>.</p>

      <scrap headstyle="show">
        <head>Types</head>
      
        <prod num="24 (Formal)" id="doc-fs-Type"><lhs>Type</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-FormalItemType" xlink:type="simple">FormalItemType</nt><br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-OccurrenceIndicator" xlink:type="simple">OccurrenceIndicator</nt>)<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>  "&amp;"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>)<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>  ","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>)<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>  "|"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>)<br/>|  "empty"<br/>|  "none"<br/>|  ("("  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>  ")")</rhs></prod>
      </scrap>

      <p>The [XPath/XQuery] type system includes three binary operators on
      types: ",", "|" and "&amp;", corresponding respectively to
      sequence, choice and <term>all groups</term> in Schema. The
      [XPath/XQuery] type system includes three unary operators on types:
      "*", "+", and "?", corresponding respectively to zero or more
      instances of the type, one or more instances of the type, or an
      optional instance of the type.</p>

      <p>The "&amp;" operator builds the <quote>interleaved
      product</quote> of two types. The type <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> &amp;
      <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> matches any sequence that is an interleaving of two
      sequences of items, <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> and <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript>, with
      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> matching <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> and <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> matching
      <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>. The interleaving of two sequences of items
      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> and <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> is any sequence <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> such
      that there is an ordered partition of <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> into the two
      sub-sequences <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> and <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript>. The interleaved
      product captures the semantics of <term>all groups</term> in XML
      Schema, but is more general as it applies to arbitrary
      types. <term>All groups</term> in XML Schema are restricted to
      apply only on global or local element declarations with
      minOccurs 0 or 1, and maxOccurs 1.</p>

      <p>For example, consider the types <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> =
      <code>xs:integer</code>,<code>xs:integer</code>,<code>xs:integer</code> and <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> =
      <code>xs:string</code>,<code>xs:string</code>. <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <code>= (1,2,3)</code>
      matches the type <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> and <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> <code>=
      ("a","b")</code> matches the type <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>. Any of the
      following <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> are interleavings of <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> and
      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript>, and therefore match the type (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> &amp;
      <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>):</p>

<eg xml:space="preserve">
Value0 = (1,2,3,"a","b")
Value0 = (1,2,"a",3,"b")
Value0 = (1,2,"a","b",3)
Value0 = (1,"a",2,3,"b")
Value0 = (1,"a",2,"b",3)
Value0 = (1,"a","b",2,3)
Value0 = ("a",1,2,3,"b")
Value0 = ("a",1,2,"b",3)
Value0 = ("a",1,"b",2,3)
Value0 = ("a","b",1,2,3)
</eg>

      <p><term>Types precedence order</term>. To improve readability
      when writing types, we assume the following precedence order
      between operators on types.</p>

      <table border="1">
      <tbody>
  
      <tr><th rowspan="1" colspan="1">#</th><th rowspan="1" colspan="1">Operator</th></tr>
      <tr><td rowspan="1" colspan="1">1</td><td rowspan="1" colspan="1">| (choice)</td></tr>
      <tr><td rowspan="1" colspan="1">2</td><td rowspan="1" colspan="1">&amp; (interleaving)</td></tr>
      <tr><td rowspan="1" colspan="1">3</td><td rowspan="1" colspan="1">, (sequence)</td></tr>
      <tr><td rowspan="1" colspan="1">4</td><td rowspan="1" colspan="1">*, +, ? (occurrence)</td></tr>
      </tbody>
      </table>

      <p>Parenthesis can be used to enforce precedence. For
      instance</p>

<eg xml:space="preserve">
  xs:string | xs:integer, xs:float*
</eg>

      <p>is equivalent to</p>

<eg xml:space="preserve">
  xs:string | (xs:integer, (xs:float*))
</eg>

      <p>and a different precedence can be obtained by writing</p>

<eg xml:space="preserve">
  ((xs:string | xs:integer), xs:float)*
</eg>

      <smexamples>
        <p><term>A sequence of elements</term></p>

        <p>The "," operator builds the <quote>sequence</quote> of two
        types. For example,</p>

<eg xml:space="preserve">
  element title of type xs:string, element year of type xs:integer
</eg>

        <p>is a sequence of an element title of type string followed
        by an element year of type integer.</p>

        <p><term>The union of two element types</term></p>

        <p>The "|" operator builds the <quote>union</quote> of two
        types. For example,</p>

<eg xml:space="preserve">
  element editor of type xs:string | element bib:author
</eg>

        <p>means either an element editor of type string, or a
        reference to the global element <code>bib:author</code>.</p>

        <p><term>An all group of two elements</term></p>

        <p>The "&amp;" operator builds the <quote>interleaved
        product</quote> of two types. For example,</p>

<eg xml:space="preserve">
  (element a &amp; element b) =
    element a, element b
  | element b, element a
</eg>

        <p>which specifies that the <code>a</code> and <code>b</code>
        elements can occur in any order.</p>

        <p><term>An empty type</term></p>

        <p>The following type matches the empty sequence.</p>

<eg xml:space="preserve">
  empty
</eg>

        <p><term>A sequence of zero or more elements</term></p>

        <p>The following type matches zero or more elements each of
        which can be a <code>surgeon</code> or a
        <code>plumber</code>.</p>

<eg xml:space="preserve">
  (element surgeon | element plumber)*
</eg>

      </smexamples>      

      <smnotation>
        <p>The grammar for <a href="#doc-fs-Type"><emph>Type</emph></a> describe above is general enough
        to capture type infered for arbitrary expression, as well as
        to represent the content of an in <bibref ref="xmlschema-1"/>. In a few
        cases, inference rules rely on the fact that a given type is a
        type validly describing the content of an element. To capture
        those cases, we introduce the following auxiliary grammar
        productions to describe more precisely the attribute
        declarations and the content model for an element.</p>

      <scrap headstyle="show">
        <head/>
        <prod num="42 (Formal)" id="doc-fs-AttributeModel"><lhs>AttributeModel</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeType" xlink:type="simple">AttributeType</nt><br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeType" xlink:type="simple">AttributeType</nt>  "?")<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeModel" xlink:type="simple">AttributeModel</nt>  "&amp;"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeModel" xlink:type="simple">AttributeModel</nt>)<br/>|  ("("  ")")</rhs></prod>
        <prod num="43 (Formal)" id="doc-fs-ElementModel"><lhs>ElementModel</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementType" xlink:type="simple">ElementType</nt><br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementType" xlink:type="simple">ElementType</nt>  "?")<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementModel" xlink:type="simple">ElementModel</nt>  "&amp;"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementModel" xlink:type="simple">ElementModel</nt>)<br/>|  ("("  ")")<br/>|  "none"</rhs></prod>
      </scrap>
      </smnotation>

    </div3>

    <div3 id="sec_top_level_definitions">
      <head>Top level definitions</head>

      <p>Top level definitions correspond to global element
      declarations, global attribute declarations and type definitions
      in XML Schema.</p>

      <scrap headstyle="show">
      <head>Type Definitions</head>
      
      <prod num="40 (Formal)" id="doc-fs-Definitions"><lhs>Definitions</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Definition" xlink:type="simple">Definition</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Definitions" xlink:type="simple">Definitions</nt>)?</rhs></prod>
      <prod num="39 (Formal)" id="doc-fs-Definition"><lhs>Definition</lhs><rhs>("define"  "element"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementName" xlink:type="simple">ElementName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-OptSubstitution" xlink:type="simple">OptSubstitution</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-OptNillable" xlink:type="simple">OptNillable</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeReference" xlink:type="simple">TypeReference</nt>)<br/>|  ("define"  "attribute"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeName" xlink:type="simple">AttributeName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeReference" xlink:type="simple">TypeReference</nt>)<br/>|  ("define"  "type"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeName" xlink:type="simple">TypeName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeDerivation" xlink:type="simple">TypeDerivation</nt>)</rhs></prod>
      <prod num="76 (Formal)" id="doc-fs-OptSubstitution"><lhs>OptSubstitution</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Substitution" xlink:type="simple">Substitution</nt>?</rhs></prod>
      <prod num="41 (Formal)" id="doc-fs-Substitution"><lhs>Substitution</lhs><rhs>"substitutes"  "for"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ElementName" xlink:type="simple">ElementName</nt></rhs></prod>
      <prod num="33 (Formal)" id="doc-fs-TypeDerivation"><lhs>TypeDerivation</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-ComplexTypeDerivation" xlink:type="simple">ComplexTypeDerivation</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AtomicTypeDerivation" xlink:type="simple">AtomicTypeDerivation</nt></rhs></prod>
      <prod num="34 (Formal)" id="doc-fs-ComplexTypeDerivation"><lhs>ComplexTypeDerivation</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Derivation" xlink:type="simple">Derivation</nt>?  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-OptMixed" xlink:type="simple">OptMixed</nt>  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>?  "}"</rhs></prod>
      <prod num="35 (Formal)" id="doc-fs-AtomicTypeDerivation"><lhs>AtomicTypeDerivation</lhs><rhs>"restricts"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AtomicTypeName" xlink:type="simple">AtomicTypeName</nt></rhs></prod>
      <prod num="37 (Formal)" id="doc-fs-Derivation"><lhs>Derivation</lhs><rhs>("restricts"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeName" xlink:type="simple">TypeName</nt>)<br/>|  ("extends"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeName" xlink:type="simple">TypeName</nt>)</rhs></prod>
      <prod num="74 (Formal)" id="doc-fs-OptMixed"><lhs>OptMixed</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Mixed" xlink:type="simple">Mixed</nt>?</rhs></prod>
      <prod num="38 (Formal)" id="doc-fs-Mixed"><lhs>Mixed</lhs><rhs>"mixed"</rhs></prod>
      </scrap>

       <p>A type definition has a name (possibly anonymous) and a type
       derivation. In the case of a complex type, the derivation
       indicates whether it is derived by extension or restriction, its
       base type, and its content model, with an optional flag
       indicating if it has mixed content.</p>

       <p>Note the type system allows recursive types, following the
       rules defined in <bibref ref="xmlschema-1"/>.</p>

       <smexample>

       <p>For instance, the following complex type</p>

<eg xml:space="preserve">
 &lt;complexType name="UKAddress"&gt;
   &lt;complexContent&gt;
     &lt;extension base="ipo:Address"&gt;
       &lt;sequence&gt;
         &lt;element name="postcode" type="ipo:UKPostcode"/&gt;
       &lt;/sequence&gt;
       &lt;attribute name="exportCode" type="positiveInteger" fixed="1"/&gt;
     &lt;/extension&gt;
   &lt;/complexContent&gt;
 &lt;/complexType&gt;
</eg>

       <p>is represented as follows</p>

<eg xml:space="preserve">
  define type UKAddress extends ipo:Address {
    attribute exportCode of type ipo:UKPostcode,
    element postcode of type positiveInteger
  };
</eg>

       </smexample>
       <smexample>

       <p>In the case of simple types derived by union or list, the
       derivation is always a restriction from the base type
       <code>xs:anySimpleType</code>, and has a content which is a union of the
       member types, or a repetition of the item type. For instance,
       the two following simple type declarations</p>

<eg xml:space="preserve">
&lt;xsd:simpleType name="listOfMyIntType"&gt;
  &lt;xsd:list itemType="myInteger"/&gt;
&lt;/xsd:simpleType&gt;

&lt;xsd:simpleType name="zipUnion"&gt;
  &lt;xsd:union memberTypes="USState FrenchRegion"/&gt;
&lt;/xsd:simpleType&gt;
</eg>

       <p>are represented as follows</p>

<eg xml:space="preserve">
define type listOfMyIntType restricts xs:anySimpleType {
  myInteger*
}

define type zipUnion restricts xs:anySimpleType {
  USState | FrenchRegion
}
</eg>

       </smexample>
       <smexample>

       <p>In the case of an atomic type, it just indicates its base
       type. For instance, the following type definition</p>

<eg xml:space="preserve">
&lt;xsd:simpleType name="SKU"&gt;
 &lt;xsd:restriction base="xsd:string"&gt;
  &lt;xsd:pattern value="\d{3}-[A-Z]{2}"/&gt;
 &lt;/xsd:restriction&gt;
&lt;/xsd:simpleType&gt;
</eg>

       <p>is represented as follow</p>

<eg xml:space="preserve">
  define type SKU restrict xsd:string;
</eg>

       </smexample>
       <smexample>

       <p>When the type derivation is omitted, the type derives by
       restriction from <code>xs:anyType</code>. For instance:</p>

<eg xml:space="preserve">
  define type Bib { element book* } =
  define type Bib restricts xs:anyType { element book* }
</eg>

       </smexample>
       <smexample>

       <p>Empty content can be indicated with the explicit empty
       sequence, or omitted, as in:</p>

<eg xml:space="preserve">
  define type Bib { } =
  define type Bib { empty }
</eg>

       </smexample>

       <p>Global element and attribute declarations always have a name
       and a reference to a (possibly anonymous) type.  A global
       element declaration also may declare a substitution group for
       the element and whether the element is nillable.</p>

       <smexample>
         <p>A type declaration with one element name of type
         <code>xs:string</code> follows by one or more elements street of type
         <code>xs:string</code>.</p>

<eg xml:space="preserve">
  define type Address {
    element name of type xs:string,
    element street of type xs:string*
  }
</eg>

       </smexample>
       <smexample>

         <p>A type declaration with complex content derived by
         extension</p>

<eg xml:space="preserve">
  define type USAddress extends Address {
    element zip name of type xs:integer
  }
</eg>

       </smexample>
       <smexample>

         <p>A type declaration with mixed content</p>

<eg xml:space="preserve">
  define type Section mixed {
    (element h1 of type xs:string |
     element p of type xs:string |
     element div of type Section)*
  }
</eg>

       </smexample>
       <smexample>

         <p>A type declaration with simple content derived by
         restriction</p>

<eg xml:space="preserve">
  define type SKU restricts xs:string
</eg>

       </smexample>
       <smexample>

         <p>An element declaration</p>

<eg xml:space="preserve">
  define element address of type Address
</eg>

       </smexample>
       <smexample>

         <p>An element declaration with a substitution
         group</p>

<eg xml:space="preserve">
  define element usaddress substitutes for address of type USAddress
</eg>

       </smexample>
       <smexample>

         <p>An element declaration which is nillable</p>

<eg xml:space="preserve">
  define element zip nillable of type xs:integer
</eg>

       </smexample>

     </div3>

     <div3 id="sec_types_example">
       <head>Example of a complete Schema</head>

       <p>Here is a schema describing purchase orders from
       <bibref ref="xmlschema-0"/>.</p>

<eg xml:space="preserve">
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  
   &lt;xsd:annotation&gt;
    &lt;xsd:documentation xml:lang="en"&gt;
     Purchase order schema for Example.com.
     Copyright 2000 Example.com. All rights reserved.
    &lt;/xsd:documentation&gt;
   &lt;/xsd:annotation&gt;
  
   &lt;xsd:element name="purchaseOrder" type="PurchaseOrderType"/&gt;
  
   &lt;xsd:element name="comment" type="xsd:string"/&gt;
  
   &lt;xsd:complexType name="PurchaseOrderType"&gt;
    &lt;xsd:sequence&gt;
     &lt;xsd:element name="shipTo" type="USAddress"/&gt;
     &lt;xsd:element name="billTo" type="USAddress"/&gt;
     &lt;xsd:element ref="comment" minOccurs="0"/&gt;
     &lt;xsd:element name="items"  type="Items"/&gt;
    &lt;/xsd:sequence&gt;
    &lt;xsd:attribute name="orderDate" type="xsd:date"/&gt;
   &lt;/xsd:complexType&gt;
  
   &lt;xsd:complexType name="USAddress"&gt;
    &lt;xsd:sequence&gt;
     &lt;xsd:element name="name"   type="xsd:string"/&gt;
     &lt;xsd:element name="street" type="xsd:string"/&gt;
     &lt;xsd:element name="city"   type="xsd:string"/&gt;
     &lt;xsd:element name="state"  type="xsd:string"/&gt;
     &lt;xsd:element name="zip"    type="xsd:decimal"/&gt;
    &lt;/xsd:sequence&gt;
    &lt;xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/&gt;
   &lt;/xsd:complexType&gt;
  
   &lt;xsd:complexType name="Items"&gt;
    &lt;xsd:sequence&gt;
     &lt;xsd:element name="item" minOccurs="0" maxOccurs="unbounded"&gt;
      &lt;xsd:complexType&gt;
  	&lt;xsd:sequence&gt;
  	 &lt;xsd:element name="productName" type="xsd:string"/&gt;
  	 &lt;xsd:element name="quantity"&gt;
  	  &lt;xsd:simpleType&gt;
  	   &lt;xsd:restriction base="xsd:positiveInteger"&gt;
  	    &lt;xsd:maxExclusive value="100"/&gt;
  	   &lt;/xsd:restriction&gt;
  	  &lt;/xsd:simpleType&gt;
  	 &lt;/xsd:element&gt;
  	 &lt;xsd:element name="USPrice"  type="xsd:decimal"/&gt;
  	 &lt;xsd:element ref="comment"   minOccurs="0"/&gt;
  	 &lt;xsd:element name="shipDate" type="xsd:date" minOccurs="0"/&gt;
  	&lt;/xsd:sequence&gt;
  	&lt;xsd:attribute name="partNum" type="SKU" use="required"/&gt;
      &lt;/xsd:complexType&gt;
     &lt;/xsd:element&gt;
    &lt;/xsd:sequence&gt;
   &lt;/xsd:complexType&gt;
  
   &lt;!-- Stock Keeping Unit, a code for identifying products --&gt;
   &lt;xsd:simpleType name="SKU"&gt;
    &lt;xsd:restriction base="xsd:string"&gt;
     &lt;xsd:pattern value="\d{3}-[A-Z]{2}"/&gt;
    &lt;/xsd:restriction&gt;
   &lt;/xsd:simpleType&gt;
  
  &lt;/xsd:schema&gt;
</eg>

<p>Here is the mapping of the above schema into the [XPath/XQuery] type
system.</p>

<eg xml:space="preserve">
  declare namespace xsd = "http://www.w3.org/2001/XMLSchema";

  define element purchaseOrder of type PurchaseOrderType;
 
  define element comment of type xsd:string;
  
  define type PurchaseOrderType {
    attribute orderDate of type xsd:date?,
    element shipTo of type USAddress,
    element billTo of type USAddress,
    element comment?,
    element items of type Items
  };

  define type USAddress {
    attribute country of type xsd:NMTOKEN,
    element name of type xsd:string,
    element street of type xsd:string,
    element city of type xsd:string,
    element state of type xsd:string,
    element zip of type xsd:decimal
  };

  define type Items {
    attribute partNum of type SKU,
    element item of type fs:anon1*
  };

  define type fs:anon1 {
    element productName of type xsd:string,
    element quantity of type fs:anon2,
    element USPrice of type xsd:decimal,
    element comment?,
    element shipDate of type xsd:date?
  };

  define type fs:anon2 restricts xsd:positiveInteger;

  define type SKU restrict xsd:string;
</eg>

      <p>Note that the two anonymous types in the <code>item</code>
      element declarations are mapping to types with names <emph>fs:</emph>anon<subscript>1</subscript>
      and <emph>fs:</emph>anon<subscript>2</subscript>.</p>

      <p>The following additional definitions illustrate how more
      advanced XML Schema features (a complex type derived by
      extension, an anonymous simple type derived by restriction, and
      substitution groups) are represented in the [XPath/XQuery] type
      system.</p>

<eg xml:space="preserve">
  &lt;complexType name="NYCAddress"&gt;
    &lt;complexContent&gt;
     &lt;extension base="USAddress"&gt;
      &lt;sequence&gt;
       &lt;element ref="apt"/&gt;
      &lt;/sequence&gt;
     &lt;/extension&gt;
    &lt;/complexContent&gt;
  &lt;/complexType&gt;

  &lt;element name="apt"&gt;
    &lt;xsd:simpleType&gt;
     &lt;xsd:restriction base="xsd:positiveInteger"&gt;
      &lt;xsd:maxExclusive value="10000"/&gt;
     &lt;/xsd:restriction&gt;
    &lt;/xsd:simpleType&gt;
  &lt;/element&gt;

  &lt;element name="usaddress" substitutionGroup="address" type="USAddress"/&gt;
  &lt;element name="nycaddress" substitutionGroup="usaddress" type="NYCAddress"/&gt;
</eg>

<p>The above definitions are mapped into the [XPath/XQuery] type system as
follows:</p>

<eg xml:space="preserve">
  define type NYCAddress extends USAddress {
    element apt
  }

  define element apt of type fs:anon3

  define type fs:anon3 restricts xsd:positiveInteger

  define element usaddress  substitutes for address of type USAddress
  define element nycaddress substitutes for usaddress of type NYCAddress
</eg>

    </div3>

  </div2>

  <div2 id="sec_fando">
    <head>Functions and operators</head>

    <p>The <bibref ref="xpath-functions"/> document defines built-in functions
    available in [XPath/XQuery]. A number of these functions are used to
    define the [XPath/XQuery] semantics; those functions are listed in
    <specref ref="sec_used_functions"/>.</p>

    <p>Many functions in the <bibref ref="xpath-functions"/> document are
    <emph>generic</emph>: they perform operations on arbitrary
    components of the data model, e.g., any kind of node, or any
    sequence of items. For instance, the <code>fn:unordered</code>
    returns its input sequence in an implementation-dependent
    order. The signature of the <code>fn:unordered</code> function
    takes arbitrary items as input and output:</p>

<eg xml:space="preserve">
  fn:unordered($sourceSeq as item()*) as item()*
</eg>

    <p>As defined, this signature provides little useful type
    information. For such functions, better type information can often
    be obtained by having the output type depend on the type of input
    parameters. For instance, if the function
    <code>fn:unordered</code> is applied on a sequence of
    <code>a</code> elements, the result is also a sequence of
    <code>a</code> elements.</p>

    <p>In order to provide better static typing for those functions,
    specific static typing rules are given in <specref ref="sec_special_functions"/>.</p>
  </div2>

</div1>

<!-- Keep this comment at the end of the file for Emacs' XML mode to work.
 Local variables:
 mode: xml
 sgml-indent-step:2
 indent-tabs-mode:nil
 sgml-parent-document:("XPathXQuerySemantics.xml" "body" "div1")
 End:
-->

     <!-- *************************** &language; Semantics            **************** -->

<div1 id="id-basics">
  <head>Basics</head>

  <p>The organization of this section parallels the organization of
  <xspecref spec="XQ" ref="id-basics"/>.</p>

  <div2 id="sec_context">
    <head>Expression Context</head>

    <smintro>
      <p>The expression context for a given expression consists of all
      the information that can affect the result of the
      expression. This information is organized into the <emph>static
      context</emph> and the <emph>dynamic context</emph>.  This
      section specifies the environments that represent the context
      information used by [XPath/XQuery] expressions.</p>
    </smintro>

    <div3 id="static_context">
      <head>Static Context</head>

      <smnotation>
        <p>We introduce the following auxiliary grammar production to
        describe function signatures.</p>

      <scrap headstyle="show">
        <head/>
        <prod num="85 (Formal)" id="doc-fs-FunctionSig"><lhs>FunctionSig</lhs><rhs>"declare"  "function"  expanded-QName  "("  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-TypeList" xlink:type="simple">TypeList</nt>?  ")"  "as"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-SequenceType" xlink:type="simple">SequenceType</nt></rhs></prod>
        <prod num="86 (Formal)" id="doc-fs-TypeList"><lhs>TypeList</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-SequenceType" xlink:type="simple">SequenceType</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-Type" xlink:type="simple">Type</nt>)*</rhs></prod>
      </scrap>
      </smnotation>

      <p id="xq_stat_env"><a href="#xq_stat_env_def" class="env">statEnv</a> denotes the environment
      available during static analysis.  Static analysis may extend
      parts of the static environment. The static environment is also
      available during dynamic evaluation.</p>

      <p>If analysis of an expression relies on some component of the
      static context that has not been assigned a value, a static
      error is raised.</p>

      <p id="xq_stat_env_def">The following environment components are
      part of the static environment:</p>
  
      <table summary="" role="figure" cellpadding="5">
      <col width="10%" span="1"/><!-- empty column forces left indentation -->
      <col width="15%" span="1"/>
      <col width="65%" align="justify" span="1"/>
      <col width="10%" span="1"/><!-- empty column forces right indentation -->
      <tbody>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_xpath_compat_flag" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_xpath_compat_flag" class="env">xpath1.0_compatibility</a></td>
          <td rowspan="1" colspan="1">
             <table summary="">
             <tbody>
               <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_xpath_compat_flag" class="env">xpath1.0_compatibility</a> environment
               component designates the <term>XPath 1.0 compatibility
               flag</term> in the [XPath/XQuery] static context. It
               specifies whether the semantic rules for backward
               compatibility with XPath 1.0 are in effect. This
               document defines the formal semantics for XPath 2.0
               only when the XPath 1.0 backward compatibility rules
               are not in effect.</td></tr>
             </tbody>
             </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_ns_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a></td>
          <td rowspan="1" colspan="1">
            <table summary="" id="term_active">
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a> environment component
                designates the <term>statically known
                namespaces</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a> environment component maps a
                namespace prefix (<emph>NCName</emph>) onto a <term>namespace
                kind</term> and a namespace URI (<emph>AnyURI</emph>), the null
                namespace (#NULL-NAMESPACE), or (#UNDECLARED). The
                namespace kind is either <term>passive</term> or
                <term>active</term>.  The namespace kind determines
                whether a namespace node is created for an element
                during element construction. The (#UNDECLARED) value
                may be used to indicate that the prefix has been
                undeclared, and may occur only if the implementation
                supports <bibref ref="XMLNAMES11"/>.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_default_elem_ns_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_elem_ns_env" class="env">default_elem_namespace</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_elem_ns_env" class="env">default_elem_namespace</a> environment
                component designates the <term>default element/type
                namespace</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_elem_ns_env" class="env">default_elem_namespace</a> environment
                component contains a namespace URI (a <emph>AnyURI</emph>) or the
                null namespace (#NULL-NAMESPACE) and is used for any
                unprefixed QName appearing in a position where an
                element or type name is expected.</td></tr>
  
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_default_fn_ns_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_fn_ns_env" class="env">default_function_namespace</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_fn_ns_env" class="env">default_function_namespace</a> environment
                component designates the <term>default function
                namespace</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_fn_ns_env" class="env">default_function_namespace</a> environment
                component contains a namespace URI (a <emph>AnyURI</emph>) or the
                null namespace (#NULL-NAMESPACE) and is used for any
                unprefixed QName appearing as the function name in a
                function call.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_type_defn" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_defn" class="env">typeDefn</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_defn" class="env">typeDefn</a> environment component
                designates the <term>in-scope schema types</term> in
                the [XPath/XQuery] static context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_defn" class="env">typeDefn</a> environment component maps
                expanded type names (expanded <a href="#doc-fs-TypeName"><emph>TypeName</emph></a>s) onto
                their type definition (<a href="#sec_top_level_definitions"><emph>Definition</emph></a>).  A type name
                may be globally declared or anonymous.</td></tr>
             </tbody>
           </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_elem_decl" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_elem_decl" class="env">elemDecl</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_elem_decl" class="env">elemDecl</a> environment component
                designates the <term>in-scope element
                declarations</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_elem_decl" class="env">elemDecl</a> environment component maps
                expanded element names (expanded <a href="#doc-fs-ElementName"><emph>ElementName</emph></a>s)
                onto their declaration (<a href="#sec_top_level_definitions"><emph>Definition</emph></a>).</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_attr_decl" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_attr_decl" class="env">attrDecl</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_attr_decl" class="env">attrDecl</a> environment component
                designates the <term>in-scope attribute
                declarations</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_attr_decl" class="env">attrDecl</a> environment component maps
                expanded attribute names (expanded
                <a href="#doc-fs-AttributeName"><emph>AttributeName</emph></a>s) onto their declaration
                (<a href="#sec_top_level_definitions"><emph>Definition</emph></a>).</td></tr>
  
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_type_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a> environment component
                designates the <term>in-scope variables</term> in
                the [XPath/XQuery] static context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a> environment component maps
                expanded variable names (expanded <emph>VarName</emph>) to
                their static type (<a href="#doc-fs-Type"><emph>Type</emph></a>).</td></tr>

                <tr><td rowspan="1" colspan="1">The <term>context item static type</term> in
                the [XPath/XQuery] static context is represented by the
                binding of the variable <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> to its corresponding
                type in <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a>.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_functy_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a> environment component
                designates the <term>function signatures</term> in the
                [XPath/XQuery] static context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a> environment component
                stores the static type signatures of
                functions. Because [XPath/XQuery] allows multiple
                functions with the same name differing in the number
                of parameters, this environment component maps an
                expanded <emph>QName</emph> and an arity to a function
                signature <emph>FunctionSig</emph>.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_collations_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_collations_env" class="env">collations</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_collations_env" class="env">collations</a> environment component
                designates the <term>statically known
                collations</term> in the [XPath/XQuery] static
                context.</td></tr> <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_collations_env" class="env">collations</a>
                environment component maps a unique namespace URI (a
                <emph>AnyURI</emph>) to a pair of functions: the first function
                takes a set of strings and returns a sequence
                containing those strings in sorted order; and the
                second function takes two strings, returns true if
                they are considered equal, and false if not.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
         <td id="xq_default_collation_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_collation_env" class="env">defaultCollation</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_collation_env" class="env">defaultCollation</a> environment
                component designates the <term>default
                collation</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_collation_env" class="env">defaultCollation</a> environment
                component is a pair of functions as described in
                <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_collations_env" class="env">collations</a> above.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_construction_mode_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> environment
                component designates the <term>construction
                mode</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> environment
                component is one of <term>preserve</term> or
                <term>strip</term>.  </td></tr>
  
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_ordering_mode_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ordering_mode_env" class="env">orderingMode</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ordering_mode_env" class="env">orderingMode</a> environment
                component designates the <term>ordering mode</term> in
                the [XPath/XQuery] static context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ordering_mode_env" class="env">orderingMode</a> environment
                component is one of <term>ordered</term> or
                <term>unordered</term>.</td></tr>
  
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_default_empty_order" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_empty_order" class="env">defaultEmptySequenceOrder</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_empty_order" class="env">defaultEmptySequenceOrder</a> environment
                component designates the <term>default order for empty
                sequences</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1"> The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_empty_order" class="env">defaultEmptySequenceOrder</a> environment
                component controls whether an empty sequence is
                interpreted as the greatest value or as the least
                value during processing of an <code>order by</code>
                clause in a FLWOR expression. Its value may be
                <code>greatest</code> or <code>least</code>.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_boundary_space" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_boundary_space" class="env">boundarySpace</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_boundary_space" class="env">boundarySpace</a> environment component
                designates the <term>boundary-space policy</term>
                in the [XPath/XQuery] static context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_boundary_space" class="env">boundarySpace</a> environment component
                controls the processing of boundary whitespace by
                element constructors. Its value may be
                <code>preserve</code> or <code>strip</code>.</td></tr>
            </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_copy_namespaces_mode" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_copy_namespaces_mode" class="env">copyNamespacesMode</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_copy_namespaces_mode" class="env">copyNamespacesMode</a> environment
                component designates the <term>copy-namespaces
                mode</term> in the [XPath/XQuery] static
                context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_copy_namespaces_mode" class="env">copyNamespacesMode</a> environment
                component controls the namespace bindings that are
                assigned when an existing element node is copied by an
                element constructor. Its value consists of two parts:
                <code>preserve</code> or <code>no-preserve</code>, and
                <code>inherit</code> or
                <code>no-inherit</code>.</td></tr>
            </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_base_uri_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_base_uri_env" class="env">baseURI</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_base_uri_env" class="env">baseURI</a> environment component
                designates the <term>base URI</term> in the [XPath/XQuery]
                static context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_base_uri_env" class="env">baseURI</a> environment component
                contains a unique namespace URI (a
                <emph>AnyURI</emph>).</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_doc_type_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_doc_type_env" class="env">docType</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_doc_type_env" class="env">docType</a> environment component
                designates the <term>statically known documents</term>
                in the [XPath/XQuery] static context.  It contains the
                static type for the input documents, and is used to
                provide the static type to the <code>fn:doc</code>
                function.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_doc_type_env" class="env">docType</a> environment component
                contains bindings from input URIs (a <emph>AnyURI</emph>) to
                types (a <a href="#doc-fs-Type"><emph>Type</emph></a>).</td></tr>
  
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_collection_type_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_collection_type_env" class="env">collectionType</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_collection_type_env" class="env">collectionType</a> environment
                component designates the <term>statically known
                collections</term> in the [XPath/XQuery] static context.
                It contains the static type for the input collections,
                and is used to provide the static type to the
                <code>fn:collection</code> function.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_collection_type_env" class="env">collectionType</a> environment
                component contains bindings from input URIs (a
                <emph>AnyURI</emph>) to types (a <a href="#doc-fs-Type"><emph>Type</emph></a>).</td></tr>
  
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_default_collection_type_env" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_collection_type_env" class="env">defaultCollectionType</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_collection_type_env" class="env">defaultCollectionType</a>
                environment component designates the <term>statically
                known default collection type</term> in the [XPath/XQuery]
                static context. It contains the static type for the
                default collection, and is used to provide the static
                type to the <code>fn:collection</code> function when called with
                no arguments.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_collection_type_env" class="env">defaultCollectionType</a>
                environment component contains type (a
                <a href="#doc-fs-Type"><emph>Type</emph></a>).</td></tr>
  
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      </tbody>
      </table>

      <p>Note that the boundary-space behavior is not formally
      specified in this document.</p>

      <p id="xq_default_stat_env">An initial environment is set up
      when [expression/query] processing begins, containing, for example,
      the function signatures of all built-in functions.  The initial
      values for the static context are defined in <xspecref spec="XQ" ref="id-xq-context-components"/> and <xspecref spec="XP" ref="id-xp-context-components"/> and is denoted by
      <a href="#xq_default_stat_env" class="env">statEnvDefault</a> in the Formal Semantics.</p>

      <p>Here is an example that shows how the static environment is
      modified in response to a namespace definition.</p>

      <infergr>
        <infer>
         <prejudge>
 	  <multiclause>
 	    <clause>
            <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 	    <expression><emph>URILiteral</emph> <a href="#jd_has_atomic_value" class="judgment"><term>has atomic value</term></a> <emph>AnyURI</emph>
 	    </expression>
 	    </clause>
 	  </multiclause>
          <multiclause>
          <clause>
            <environment>
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>NCName</emph> =&gt; (passive, <emph>AnyURI</emph>)</expression>
              </update>
            </environment>
            <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
          </clause>
          </multiclause>
         </prejudge>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><code>declare namespace</code> <emph>NCName</emph> = <emph>URILiteral</emph>; <emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>
  
      <p>This rule reads as follows: <quote>the phrase on the bottom
      (a namespace declaration in the query prolog followed by an
      expression) is well-typed (accepted by the static typing rules)
      within an environment <a href="#xq_stat_env_def" class="env">statEnv</a> <emph>if</emph> the
      expression above the line is well-typed in the environment
      obtained from <a href="#xq_stat_env_def" class="env">statEnv</a> by adding the namespace
      declaration</quote>.</p>

      <p id="fs_activeNS">The helper function
      <a href="#fs_activeNS">fs:<code>active_ns</code></a>(<a href="#xq_stat_env_def" class="env">statEnv</a>) returns all the active in-scope
      namespaces in the given static environment.</p>

      <p id="fs_ns_from_items">For each attribute and element node in
      <a href="#doc-fs-Value"><emph>Value</emph></a>, such that the node has name <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> in the
      namespace <emph>AnyURI</emph>, the helper function
      <a href="#fs_ns_from_items">fs:<code>get_static_ns_from_items</code></a>(<a href="#xq_stat_env_def" class="env">statEnv</a>, <a href="#doc-fs-Value"><emph>Value</emph></a>) returns the
      in-scope namespace that corresponds to <emph>AnyURI</emph>.  This is a
      reverse-lookup on <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a> by <emph>AnyURI</emph>.</p>

    <div4 id="id-expanded-qnames">
      <head>Resolving QNames to Expanded QNames</head>

      <p>A common use of the static environment is to expand a
      <emph>QName</emph> by looking up the URI that corresponds to the QName's
      namespace prefix in the <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a> environment component and by
      constructing an <xtermref spec="DM" ref="dt-expanded-qname"/>,
      which contains the URI and the QName's local part. Element and
      type names may be in the null namespace, that is, there is no
      URI associated with their namespace prefix.  The null namespace
      is denoted by the special value
      <code>#NULL-NAMESPACE</code>.</p>

      <p>The auxiliary judgments below expand an element, type,
      attribute, variable, or function <emph>QName</emph> by looking up the
      namespace prefix in <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a> or, if the QName is unqualified,
      by using the appropriate default namespace. </p>
  
      <smnotation>
        <p id="jd_elem_qname_expands_to">The judgment</p>
        <display>
          <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>QName</emph> <a href="#jd_elem_qname_expands_to" class="judgment"><term>of elem/type expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
          </clause>
        </display>
        <p>holds when the element or type QName expands to the 
        given expanded QName.</p>
        <p id="jd_attr_qname_expands_to">The judgment</p>
        <display>
          <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>QName</emph> <a href="#jd_attr_qname_expands_to" class="judgment"><term>of attr expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
          </clause>
        </display>
        <p>holds when the attribute QName expands to the 
        given expanded QName.</p>

        <p>We use <emph>Variable</emph> to denote the expanded QNames of
        variables.</p>

        <p id="jd_var_qname_expands_to">The judgment</p>
        <display>
          <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>QName</emph> <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a> <emph>Variable</emph></expression>
          </clause>
        </display>
        <p>holds when the variable QName expands to the 
        given expanded QName.</p>

        <p id="jd_func_qname_expands_to">The judgment</p>
        <display>
          <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>QName</emph> <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
          </clause>
        </display>
        <p>holds when the function QName expands to the 
        given expanded QName.</p>
    </smnotation>

    <smrules>
      <p>Note that none of the inference rules can infer a resolved
      name in the case a given namespace prefix is bound to the
      (#UNDECLARED) value. As a result, namespace resolution will fail
      if the implementation supports <bibref ref="XMLNAMES11"/> and a
      given namespace prefix has been undeclared.</p>

      <p>An element or type QName consisting of a prefix NCName and a
      local part NCName expands to the URI (or the null namespace)
      corresponding to that prefix and the local part.</p>

      <infergr>
        <infer>
         <prejudge>
         <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a>(<emph>NCName</emph><subscript>1</subscript>) = (<a href="#xq_ns_env"><emph>NamespaceKind</emph></a>,<emph>AnyURI-or-#NULL-NAMESPACE</emph>)</expression>
         </clause>
         </prejudge>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><emph>NCName</emph><subscript>1</subscript>:<emph>NCName</emph><subscript>2</subscript> <a href="#jd_elem_qname_expands_to" class="judgment"><term>of elem/type expands to</term></a>
           (<emph>AnyURI-or-#NULL-NAMESPACE</emph>, <emph>NCName</emph><subscript>2</subscript>)</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>

      <p>An element or type QName consisting only of a local part
      NCName expands to the default element/type namespace and the
      local part.</p>

      <infergr>
        <infer>
         <prejudge>
         <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_elem_ns_env" class="env">default_elem_namespace</a> = <emph>AnyURI-or-#NULL-NAMESPACE</emph></expression>
         </clause>
         </prejudge>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><emph>NCName</emph> <a href="#jd_elem_qname_expands_to" class="judgment"><term>of elem/type expands to</term></a>
           (<emph>AnyURI-or-#NULL-NAMESPACE</emph>, <emph>NCName</emph>)</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>

      <p>An attribute QName consisting of a prefix NCName and a local
      part NCName expands to the URI (or the null namespace)
      corresponding to the prefix and the local part.</p>

      <infergr>
        <infer>
         <prejudge>
         <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a>(<emph>NCName</emph><subscript>1</subscript>) = (<a href="#xq_ns_env"><emph>NamespaceKind</emph></a>,<emph>AnyURI-or-#NULL-NAMESPACE</emph>)</expression>
         </clause>
         </prejudge>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><emph>NCName</emph><subscript>1</subscript>:<emph>NCName</emph><subscript>2</subscript>
           <a href="#jd_attr_qname_expands_to" class="judgment"><term>of attr expands to</term></a> (<emph>AnyURI-or-#NULL-NAMESPACE</emph>, <emph>NCName</emph><subscript>2</subscript>)</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>

      <p>An attribute QName consisting only of a local part NCName
      expands to the null namespace and the local part.</p>

      <infergr>
        <infer>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><emph>NCName</emph> <a href="#jd_attr_qname_expands_to" class="judgment"><term>of attr expands to</term></a>
           (#NULL-NAMESPACE, <emph>NCName</emph>)</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>

      <p>A variable QName consisting of a prefix NCName and a local
      part NCName expands to the URI that corresponds to the prefix
      and the local part.</p>

      <infergr>
        <infer>
         <prejudge>
         <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a>(<emph>NCName</emph><subscript>1</subscript>) = (<a href="#xq_ns_env"><emph>NamespaceKind</emph></a>,<emph>AnyURI</emph>)</expression>
         </clause>
         </prejudge>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><emph>NCName</emph><subscript>1</subscript>:<emph>NCName</emph><subscript>2</subscript>
           <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a> (<emph>AnyURI</emph>, <emph>NCName</emph><subscript>2</subscript>)</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>

      <p>A variable QName consisting only of a local part NCName
      expands to the null namespace and the local part.</p>

      <infergr>
        <infer>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><emph>NCName</emph> <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a>
           (#NULL-NAMESPACE, <emph>NCName</emph>)</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>

      <p>A function QName consisting of a prefix NCName and a local
      part NCName expands to the URI that corresponds to the prefix
      and the local part.</p>

      <infergr>
        <infer>
         <prejudge>
         <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a>(<emph>NCName</emph><subscript>1</subscript>) = (<a href="#xq_ns_env"><emph>NamespaceKind</emph></a>,<emph>AnyURI</emph>)</expression>
         </clause>
         </prejudge>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><emph>NCName</emph><subscript>1</subscript>:<emph>NCName</emph><subscript>2</subscript>
           <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> (<emph>AnyURI</emph>, <emph>NCName</emph><subscript>2</subscript>)</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>

      <p>A function QName consisting only of a local part NCName
      expands to the default function namespace URI and the local
      part.</p>

      <infergr>
        <infer>
         <prejudge>
         <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_default_fn_ns_env" class="env">default_function_namespace</a> = <emph>AnyURI</emph></expression>
         </clause>
         </prejudge>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><emph>NCName</emph> <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a>
           (<emph>AnyURI</emph>, <emph>NCName</emph>)</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>
    </smrules>
    </div4>  

    </div3>
   
    <div3 id="eval_context">
      <head>Dynamic Context</head>

      <p id="xq_dyn_env_def"><a href="#xq_dyn_env_def" class="env">dynEnv</a> denotes the environment
      available during dynamic evaluation. Dynamic evaluation may
      extend parts of the dynamic environment.</p>

      <p>If evaluation of an expression relies on some component of
      the dynamic context that has not been assigned a value, a
      dynamic error is raised.</p>

      <p>The following environment components are part of the dynamic
      environment:</p>
  
      <table summary="" role="figure" cellpadding="5">
      <col width="10%" span="1"/><!-- empty column forces left indentation -->
      <col width="15%" span="1"/>
      <col width="65%" align="justify" span="1"/>
      <col width="10%" span="1"/><!-- empty column forces right indentation -->
      <tbody>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_val_env" valign="top" rowspan="1" colspan="1"><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a></td>
          <td rowspan="1" colspan="1">
           <table><tbody>
            <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a> environment component corresponds to the
            <term>variable values</term>, the <term>context
            item</term>, the <term>context position</term> and the
            <term>context size</term> in the [XPath/XQuery] evaluation
            context.</td></tr>

            <tr><td rowspan="1" colspan="1">The dynamic value environment component maps an
            expanded variable name (expanded <emph>VarName</emph>) to the
            variable's value (<a href="#doc-fs-Value"><emph>Value</emph></a>) or to the value
            <code>#IMPORTED</code>(<emph>AnyURI</emph>), if the variable is
            defined in the imported module with namespace
            <emph>AnyURI</emph>.</td></tr> </tbody></table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_func_env" valign="top" rowspan="1" colspan="1"><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
               <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a> environment component corresponds to
               the <term>function implementations</term> (or
               definition) part of the [XPath/XQuery] dynamic
               context.</td></tr>

               <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a> environment component maps an
               expanded function name and parameter signature of the
               form <quote><a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ...,
               <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)</quote> to the remainder of the
               corresponding function definition. If the function is
               defined in <bibref ref="xpath-functions"/>, the function definition is
               the value <code>#BUILT-IN</code>. If the function is
               externally defined, the function definition is the
               value <code>#EXTERNAL</code>. If the function is
               defined in the imported module with namespace <emph>AnyURI</emph>,
               the function definition is the value
               <code>#IMPORTED</code>(<emph>AnyURI</emph>). If the function is
               locally declared, the function definition is of the
               form <quote>(<emph>Expr</emph>, <emph>Variable</emph><subscript>1</subscript>,...,
               <emph>Variable</emph><subscript>n</subscript>)</quote>, where <emph>Expr</emph> is the
               function body and <emph>Variable</emph><subscript>1</subscript>, ..., <emph>Variable</emph><subscript>n</subscript>
               are the function parameters.</td></tr>

               <tr><td rowspan="1" colspan="1"> The initial function environment component
               (<a href="#xq_default_dyn_env" class="env">dynEnvDefault</a>.funcDefn) maps the signatures of
               the internal functions defined in <specref ref="sec_operators"/> and the signatures of the
               functions defined in <bibref ref="xpath-functions"/> to
               <code>#BUILT-IN</code>.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_date_time_env" valign="top" rowspan="1" colspan="1"><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_date_time_env" class="env">dateTime</a></td>
          <td rowspan="1" colspan="1">
           <table><tbody>
            <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_date_time_env" class="env">dateTime</a> environment component corresponds to
            the <term>current dateTime</term> in the [XPath/XQuery]
            dynamic context.</td></tr> </tbody></table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_timezone_env" valign="top" rowspan="1" colspan="1"><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_timezone_env" class="env">timezone</a></td>
          <td rowspan="1" colspan="1">
           <table><tbody>

            <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_timezone_env" class="env">timezone</a> environment component corresponds to
            the <term>implicit timezone</term> in the [XPath/XQuery]
            dynamic context and is used by the timezone related
            functions in <bibref ref="xpath-functions"/>.</td></tr> </tbody></table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_doc_value_env" valign="top" rowspan="1" colspan="1"><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_doc_value_env" class="env">docValue</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_doc_value_env" class="env">docValue</a> environment component corresponds
                to the <term>available documents</term> in the
                [XPath/XQuery] dynamic context.  It contains the document
                nodes corresponding to input documents, and is used to
                provide the dynamic value of the <code>fn:doc</code>
                function.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_doc_value_env" class="env">docValue</a> environment component contains
                bindings from input URIs (a <emph>AnyURI</emph>) to documents (a
                <a href="#doc-fs-DocumentValue"><emph>DocumentValue</emph></a>).</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_collection_value_env" valign="top" rowspan="1" colspan="1"><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_collection_value_env" class="env">collectionValue</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_collection_value_env" class="env">collectionValue</a> environment component
                corresponds to the <term>available collections</term>
                in the [XPath/XQuery] dynamic context.  It contains the
                root nodes corresponding to the input collections, and
                is used to provide the dynamic value of the
                <code>fn:collection</code> function.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_collection_value_env" class="env">collectionValue</a> environment component
                contains bindings from input URIs (a <emph>AnyURI</emph>) to
                a sequence of nodes.</td></tr>
                </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_default_collection_value_env" valign="top" rowspan="1" colspan="1"><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_default_collection_value_env" class="env">defaultCollectionValue</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_default_collection_value_env" class="env">defaultCollectionValue</a>
                environment component corresponds to the <term>default
                collection</term> in the [XPath/XQuery] dynamic context.
                It contains the sequence of nodes corresponding to the
                default collection, and is used to provide the dynamic
                value of the <code>fn:collection</code> function when called with
                no arguments.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_default_collection_value_env" class="env">defaultCollectionValue</a>
                environment component contains a sequence of nodes.</td></tr>
  
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>
      </tbody>
      </table>

      <p id="xq_default_dyn_env">The initial values for the dynamic
      context are defined in <xspecref spec="XQ" ref="id-xq-context-components"/> and <xspecref spec="XP" ref="id-xp-context-components"/>. The corresponding initial
      dynamic environment is denoted by <a href="#xq_default_dyn_env" class="env">dynEnvDefault</a> in the
      Formal Semantics.</p>

      <p id="fs_builtin_vars">The following Formal Semantics variables
      represent the <term>context item</term>, <term>context
      position</term>, and <term>context size</term> properties of the
      dynamic context:</p>
  
      <table summary="" border="1">
       <tbody>
        <tr><td rowspan="1" colspan="1">Built-in Variable  </td><td rowspan="1" colspan="1">Represents:</td></tr>
        <tr><td rowspan="1" colspan="1"><a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a></td><td rowspan="1" colspan="1">context item</td></tr>
        <tr><td rowspan="1" colspan="1"><a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a></td><td rowspan="1" colspan="1">context position </td></tr>
        <tr><td rowspan="1" colspan="1"><a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a></td><td rowspan="1" colspan="1">context size</td></tr>
       </tbody>
      </table>
  
      <p>Within this document, variables with the "fs" prefix are
      reserved for use in the formal specification. Values of
      <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> and <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a> can be obtained by invoking the
      <code>fn:position</code> and <code>fn:last</code> functions, respectively. Note that
      the type for those variables is obtained as for any other
      variables. As expected the type of <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> and <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a>
      is always <code>xs:integer</code> while the type of <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> depends on the
      context in which it is being used.</p>
    </div3>
  </div2>

  <div2 id="id-processing-model">
    <head>Processing Model</head>

    <p>This section reviews the processing model for [XPath/XQuery]. The
    [XPath/XQuery] processing model is defined normatively in <xspecref spec="XQ" ref="id-processing-model"/>.  This section also explains
    how the main notations (normalization rules, static typing rules,
    and dynamic evaluation rules) relate to the phases in that
    processing model.</p>

    <div3 id="id-fs-processing-model">
      <head>Processing model</head>

      <p>The following figure depicts the [XPath/XQuery] processing
      model</p>

<graphic xmlns:xlink="http://www.w3.org/1999/xlink" source="ProcMod-XQuery.gif" alt="Processing Model Overview" role="xquery" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>

      <p>Figure 1: Processing Model Overview</p>

      <p>This processing model is not intended to describe an actual
      implementation, although a naive implementation might be based
      upon it. It does not prescribe an implementation technique, but
      any implementation should produce the same results as obtained
      by following this processing model and applying the rest of the
      Formal Semantics specification.</p>

      <p>Query processing consists of two phases: a static analysis
      phase and a dynamic evaluation phase.  Static analysis is further
      divided into four sub-phases.   Typically, each phase consumes the result of
      the previous phase and generates output for the next phase. When
      processing query prologs, these phases may be mutually dependent
      (See <specref ref="id-query-prolog"/>).
      For       each processing phase, we point to the relevant notations
      introduced later in the document.</p>

      <p><termdef id="dt-static-analysis" term="static analysis       phase">The <term>static analysis phase</term> depends on the
      expression itself and on the static context. The <term>static
      analysis phase</term> does not depend on input data (other than
      schemas).</termdef></p>

      <p>The purpose of the static analysis phase is to detect errors,
      e.g., syntax errors or type errors, at compile time rather than
      at run-time. If no error occurs, the result of static analysis
      could be some compiled form of [expression/query], suitable for
      execution by a compiled-[expression/query] processor. Static analysis
      consists of the following sub-phases:</p>

      <olist>
        <item>
          <p id="processing_parsing"><term>Parsing.</term> (Step SQ1
          in Figure 1). The grammar for the [XPath/XQuery] syntax is
          defined in <bibref ref="xquery"/>.  Parsing may generate syntax
          errors.  If no error occurs, an internal operation tree of
          the parsed query is created.</p>
        </item>

        <item>
          <p id="processing_context"><term>Static Context
          Processing.</term> (Steps SQ2, SQ3, and SQ4 in Figure
          1). The static semantics of [expression/query] depends on the
          input static context. The input static context needs to be
          generated before the [expression/query] can be analysed. In
          XQuery, the input static context may be defined by the
          processing environment and by declarations in the Query
          Prolog (See <specref ref="id-query-prolog"/>). In XPath, the
          input static context is defined by the processing
          environment. The static context is denoted by
          <a href="#xq_stat_env_def" class="env">statEnv</a>. </p>
        </item>

        <item>
          <p id="processing_normalization"><term>Normalization.</term>
          (Step SQ5 in Figure 1). To simplify the semantics
          specification, some normalization is performed on the
          [expression/query]. The [XPath/XQuery] language provides many powerful
          features that make [expression/query]s simpler to write and use,
          but are also redundant. For instance, a complex
          <code>for</code> expression might be rewritten as a
          composition of several simple <code>for</code>
          expressions. The language composed of these simpler
          [expression/query] is called the [XPath/XQuery] <emph>Core
          language</emph> and is described by a grammar which is a
          subset of the XQuery grammar. The grammar of the [XPath/XQuery]
          Core language is given in <specref ref="sec_core"/>.</p>

          <p>During the normalization phase, each [XPath/XQuery]
          [expression/query] is mapped into its equivalent [expression/query] in
          the Core. (Note that this has nothing to do with Unicode
          Normalization, which works on character strings.)
          Normalization works by recursive application of the
          normalization rules over a given expression.</p>

          <p>Specifically the normalization phase is defined in terms
          of the static part of the context (<a href="#xq_stat_env_def" class="env">statEnv</a>) and a
          [expression/query] (<emph>Expr</emph>) abstract syntax tree. Formal
          notations for the normalization phase are introduced in
          <specref ref="sec_normalization"/>.</p>

          <p>After normalization, the full semantics is obtained by
          giving a semantics to the normalized Core [expression/query]. This
          is done during the last two phases.</p>
        </item>
        <item>
          <p id="processing_static"><term>Static type analysis.</term>
          (Step SQ6 in Figure 1).  Static type analysis is optional.
          If this phase is not supported, then normalization is
          followed directly by dynamic evaluation.  </p> <p>Static
          type analysis checks whether each [expression/query] is
          well-typed, and if so, determines its static type. Static
          type analysis is defined only for Core [expression/query]. Static
          type analysis works by recursive application of the static
          typing rules over a given expression.</p>

          <p>If the [expression/query] is not well-typed, static type
          analysis yields a <emph>type error</emph>. For instance, a
          comparison between an integer value and a string value might
          be detected as an type error during the static type
          analysis. If static type analysis succeeds, it yields an
          abstract syntax tree where each sub-expression is associated
          with its static type.</p>

          <p>More precisely, the static analysis phase is defined in
          terms of the static context (<a href="#xq_stat_env_def" class="env">statEnv</a>) and a Core
          [expression/query] (<emph>CoreExpr</emph>).  Formal notations for
          the static analysis phase are introduced in <specref ref="sec_static"/>.</p>

          <p>Static typing does not imply that the content of XML
          documents must be rigidly fixed or even known in advance.
          The [XPath/XQuery] type system accommodates
          <quote>flexible</quote> types, such as elements that can
          contain any content.  Schema-less documents are handled in
          [XPath/XQuery] by associating a standard type with the document,
          such that it may include any legal XML content.</p>
        </item>
      </olist>

      <p>If the static analysis phase succeeds, the dynamic evaluation
      phase (sometimes also called <quote>execution</quote>) evaluates
      a query on input document(s).</p>

      <olist>
        <item>
          <p id="dyn_processing_context"><term>Dynamic Context
          Processing.</term> (Steps DQ2 and DQ3 in Figure 1).The
          dynamic semantics of [expression/query] depends on the dynamic
          input context. The dynamic input context needs to be
          generated before the [expression/query] can be evaluated. The
          dynamic input context may be defined by the processing
          environment and by statements in the Query Prolog (See
          <specref ref="id-query-prolog"/>). In XPath, the dynamic
          input context is defined by the processing environment. The
          static input context is denoted by <a href="#xq_dyn_env_def" class="env">dynEnv</a>.</p>
        </item>
        <item>

          <p id="processing_dynamic"><term>Dynamic Evaluation.</term>
          (Steps DQ4 and DQ5 in Figure 1). This phase computes the
          value of an [expression/query].  The semantics of evaluation is
          defined only for Core [expression/query] terms. The formal
          description of evaluation works by recursive application of
          the dynamic evaluation rules over a given
          expression. Evaluation may result in a value OR a dynamic
          error, which may be a non-type error or a type error.  If
          static typing of an expression does not raise a type error,
          then dynamic evaluation of the same expression will not
          raise a type error (and thus dynamic type checking can be
          avoided when static typing is enabled). Dynamic evaluation
          may still raise a non-type error.</p>

          <p>The dynamic evaluation phase is defined in terms of the
          static context (<a href="#xq_stat_env_def" class="env">statEnv</a>) and evaluation context
          (<a href="#xq_dyn_env_def" class="env">dynEnv</a>), and a Core [expression/query]
          (<emph>CoreExpr</emph>). Formal notations for the dynamic
          evaluation phase are introduced in <specref ref="sec_evaluation"/>.</p>
        </item>
      </olist>

      <p>Static type analysis catches only certain classes of
      errors. For instance, it can detect a comparison operation
      applied between incompatible types (e.g., <code>xs:int</code>
      and <code>xs:date</code>). Some other classes of errors cannot
      be detected by the static analysis and are only detected at
      evaluation time. For instance, whether an arithmetic expression
      on 32 bit integers (<code>xs:int</code>) yields an out-of-bound
      value can only be detected at run-time by looking at the
      data.</p>

      <p>While implementations are free to implement different
      processing models, the [XPath/XQuery] static semantics relies on the
      existence of a static type analysis phase that precedes any
      access to the input data.</p>

      <p>The above processing phases are all internal to the
      [XPath/XQuery] processor. They do not deal with how the [XPath/XQuery]
      processor interacts with the outside world, notably how it
      accesses actual documents and types. A typical [expression/query]
      engine would support at least three other important processing
      phases:</p>

      <olist>
        <item>
          <p><term>Schema Import Processing.</term> The [XPath/XQuery]
          type system is based on XML Schema. In order to perform
          dynamic or static typing, the [XPath/XQuery] processor needs to
          build type descriptions that correspond to the schema(s) of
          the input documents. This phase is achieved by mapping all
          schemas required by the [expression/query] into the [XPath/XQuery]
          type system. The XML Schema import phase is described in
          <specref ref="sec_importing_schema"/>.</p>
        </item>
        <item>
          <p><term>Data Model Generation.</term> Expressions are
          evaluated on values in the <bibref ref="xpath-datamodel"/>. XML documents
          must be loaded into the <bibref ref="xpath-datamodel"/> before the evaluation
          phase. This is described in the <bibref ref="xpath-datamodel"/> and is not
          discussed further here.</p>
        </item>
        <item>
          <p><term>Serialization.</term> Once the [expression/query] is
          evaluated, processors might want to serialize the result of
          the [expression/query] as actual XML documents. Serialization of
          data model instances is described in
          <bibref ref="xslt-xquery-serialization"/> and is not discussed further
          here.</p>
        </item>
      </olist>

      <p>The parsing phase is not specified formally; the formal
      semantics does not define a formal model for the syntax trees,
      but uses the [XPath/XQuery] concrete syntax directly.  More details
      about parsing for XQuery 1.0 can be found in the <bibref ref="xquery"/>
      document and more details about parsing for XPath 2.0 can be
      found in the <bibref ref="xpath20"/> document. No further discussion of
      parsing is included here.</p>

    </div3>

    <div3 id="sec_normalization">
      <head>Normalization mapping rules</head>

      <p>Normalization is specified using <emph>mapping</emph> rules,
      which describe how a [XPath/XQuery] expression is rewritten into an
      expression in the [XPath/XQuery] Core. Mapping rules are also used
      in <specref ref="sec_importing_schema"/> to specify how XML
      Schemas are imported into the [XPath/XQuery] type system.</p>

      <smnotation>
        <p>Mapping rules are written using a square bracket notation,
          as follows:</p>

        <mapping>
          <xquery>
            <expression> 
              <map><expression>Object</expression></map><subscript>Subscript</subscript>,
              <emph>premises</emph>
            </expression>
          </xquery>
          <core>
            <expression>
              <emph>Mapped Object</emph>
            </expression>
          </core>
        </mapping>
        
        <p>The original <quote>object</quote>, and an optional list of
        premises, is written above the <term>=</term> sign. The
        rewritten <quote>object</quote> is written beneath the
        <term>=</term> sign. The subscript is used to indicate what
        kind of <quote>object</quote> is mapped, and sometimes to pass
        some information between mapping rules. For instance, the
        mapping rule <map><emph>Expr</emph></map><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<a href="#doc-fs-Type"><emph>Type</emph></a>)</subscript></a> is
        used in the normalization of <specref ref="id-function-calls"/> and passes a sequence type as a
        parameter during normalization.</p>

        <p>Since normalization is always applied in the presence of a 
        static context, the above rule is a shorthand for:</p>

        <display>
         <infergr>
          <infer><prejudge><clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment><expression>premises</expression></clause></prejudge>
          <postjudge>
           <clause>
            <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
            <expression>
              <map><expression>Object</expression></map>
              <subscript>Subscript</subscript>
              =
              Mapped Object
            </expression>
          </clause>
          </postjudge>
         </infer>
         </infergr>
        </display>

        <p>Most normalization rules have no premises, so they are
        omitted.  The static environment is used in certain
        normalization rules (e.g. for normalization of function
        calls).  To keep the notation simpler, the static environment
        is not written in the normalization rules, but it is assumed
        to be available.</p>

        <p id="jd_map_expr">The normalization rule that is used to map
        <quote>top-level</quote> expressions in the [XPath/XQuery] syntax
        into expressions in the [XPath/XQuery] Core is:</p>

      	<mapping>
      	  <xquery>
      	    <expression>
      	      <map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
      	    </expression>
      	  </xquery>
      	  <core>
      	    <expression>
      	      <emph>CoreExpr</emph>
      	    </expression>
      	  </core>
      	</mapping>

        <p>which indicates that the expression <emph>Expr</emph> is normalized
        to the expression <emph>CoreExpr</emph> in the [XPath/XQuery] Core
        (with the implied <a href="#xq_stat_env_def" class="env">statEnv</a>). Note that <emph>Expr</emph> within
        the square brackets are the expression being normalized, while
        the <emph>Expr</emph> in the subscript indicate that this is the main
        normalization rule that applies to expressions. For instance,
        here is the normalization rule applied to the literal integer
        <code>1</code>.</p>

      	<mapping>
      	  <xquery>
      	    <expression>
      	      <map>1</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
      	    </expression>
      	  </xquery>
      	  <core>
      	    <expression>1</expression>
      	  </core>
      	</mapping>

        <p>To simplify the specification in some cases, some further
        normalization may be used on the right-hand side of a
        normalization rule. For instance, the following normalization
        rules for the <code>/</code> operator applies normalization to
        the expanded expression on the right-hand side.</p>

      <mapping>
       <xquery>
         <expression><map>/</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </xquery>
       <core>
         <expression><map>(<code>fn:root</code>(self::node()) treat as document-node())</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </core>
      </mapping>
      </smnotation>

      <smexample>
        <p>For instance, the following [expression/query]</p>

<eg xml:space="preserve">
    for $i in (1, 2),
        $j in (3, 4)
    return
      element pair { ($i,$j) }
</eg>

        <p>is normalized to the Core expression</p>

<eg xml:space="preserve">
    for $i in (1, 2) return
      for $j in (3, 4) return
          element pair { ($i,$j) }
</eg>

        <p>in which the <quote>FWLR</quote> expression is
        mapped into a composition of two simpler "for" expressions.</p>
      </smexample>

    </div3>

    <div3 id="sec_static">
      <head>Static typing judgment</head>

      <p>The static semantics is specified using <term>static typing
      rules</term>, which relate [XPath/XQuery] expressions to types and
      specify under what conditions an expression is well typed.</p>

      <smnotation>
        <p id="jd_has_type">The judgment</p>

        <display>
        <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
        </clause>
        </display>

        <p>holds when, in the static environment <a href="#xq_stat_env_def" class="env">statEnv</a>, the
        expression <emph>Expr</emph> has type <a href="#doc-fs-Type"><emph>Type</emph></a>.</p>
      </smnotation>

      <smexample>
        <p>The result of static type inference is to associate a
        static type with every [expression/query], such that any evaluation
        of that [expression/query] is guaranteed to yield a value that
        belongs to that type.</p>

        <p>For instance, the following expression.</p>

<eg xml:space="preserve">
   let $v := 3 return $v+5
</eg>

        <p>has type <code>xs:integer</code>. This can be inferred as follows: the
        literal '3' has type integer, so the variable
        $v also has type integer. Since the sum of two integers is an
        integer, the complete expression has type integer.</p>
      </smexample>

      <smnote>
        <p>The type of an expression is computed by inference. Static
        typing rules define for each kind of expression how to compute
        the type of the expression given the types of its
        sub-expressions.  Here is a simple example:</p>

      	<infergr>
      	  <infer>
      	   <prejudge>
      	    <clause>
      	     <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
      	     <expression> <emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code> </expression>
      	    </clause>
      	    <clause>
      	     <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
      	     <expression> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>   </expression>
      	    </clause>
      	    <clause>
      	     <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
      	     <expression> <emph>Expr</emph><subscript>3</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>3</subscript>    </expression>
      	    </clause>
      	   </prejudge>
      	   <postjudge>
      	    <clause>
      	     <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
      	     <expression>
      	  <code>if</code> (<emph>Expr</emph><subscript>1</subscript>)
      	  <code>then</code> <emph>Expr</emph><subscript>2</subscript>
      	  <code>else</code> <emph>Expr</emph><subscript>3</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> ( <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> | <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>3</subscript> )
      	    </expression>
      	    </clause>
      	   </postjudge>
      	  </infer>
      	</infergr>
  
      	<p>This rule states that if the conditional expression of an
      	<quote>if</quote> expression has type boolean, then the type
      	of the entire expression is one of the two types of its
      	<quote>then</quote> and <quote>else</quote> clauses.  Note
      	that the resulting type is represented as a union:
      	'(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>|<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>3</subscript>)'.</p>

        <p>The part after the <term>|-</term> and before
        <term>:</term> in the judgment <emph>below</emph> the line
        corresponds to some [expression/query], for which a type is
        computed. If the [expression/query] has been parsed into an internal
        abstract syntax tree, this usually corresponds to some node in
        that tree. The judgment usually has patterns in it (here
        <emph>Expr</emph><subscript>1</subscript>, <emph>Expr</emph><subscript>2</subscript>, and <emph>Expr</emph><subscript>3</subscript>) that need to be
        matched against the children of the node in the abstract
        syntax tree. The judgments <emph>above</emph> the line
        indicate things that need to be computed to use this rule; in
        this case, the types of the condition expression and the two
        branches of the if-then-else expression.  Once those types are
        computed (by further applying static typing rules recursively
        to those sub-expressions), then the type of the expression
        below the line can be computed.  This example illustrates a
        general feature of the [XPath/XQuery] type system: the type of an
        expression depends only on the type of its sub-expressions.
        Static type inference is recursive, following the abstract
        syntax of the [expression/query].  At each point in the recursion,
        an inference rule whose conclusion has a structure that
        matches that of the premise in question is sought.  If all the
        premises of a rule cannot be satisfied, then the static type
        inference has failed for the given expression, and the
        [expression/query] is not well-typed.</p>

     </smnote>

    </div3>

    <!-- ***************** Evaluation **************** -->

    <div3 id="sec_evaluation">
      <head>Dynamic evaluation judgment</head>

      <p>The dynamic, or operational, semantics is specified using
      <term>dynamic evaluation rules</term>, which relate [XPath/XQuery]
      expressions to values, and in some cases specify the order in
      which an [XPath/XQuery] expression is evaluated.</p>

      <smnotation>
        <p id="jd_yields">The judgment</p>

        <display>
        <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a>; <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
          <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
        </clause>
        </display>

        <p>holds when, in the static environment <a href="#xq_stat_env_def" class="env">statEnv</a> and
        dynamic environment <a href="#xq_dyn_env_def" class="env">dynEnv</a>, the expression <emph>Expr</emph>
        yields the value <a href="#doc-fs-Value"><emph>Value</emph></a>.</p>

      </smnotation>

      <p>The static environment is used in certain cases (e.g. for
      type matching) during evaluation. To keep the notation simpler,
      the static environment is not written in the dynamic evaluation
      rules, but it is assumed to be available.</p>

      <p>The inference rules used for dynamic evaluation, like those
      for static typing, follow a recursive structure, computing the
      value of expressions from the values of their
      sub-expressions.</p>

    </div3>

  </div2>

  <div2 id="sec_errors">
    <head>Error Handling</head>

    <p>Expressions can raise errors during static analysis or dynamic
    evaluation.  The <bibref ref="xpath-functions"/> <bibref ref="xquery"/>, and <bibref ref="xpath20"/>
    specify the conditions under which an expression or operator
    raises an error.  The user may raise an error explicitly by
    calling the <code>fn:error</code> function, which takes an optional item as
    an argument.</p>

    <p>This document does not describe formally the conditions under
    which dynamic errors are raised. Notably, it does not specify the
    error codes or the rules about errors and optimization, as
    described in <bibref ref="xquery"/>. Instead, this document
    describe the rules necessary to statically detect the subset of
    the [XPath/XQuery] dynamic errors known as <xtermref spec="XQ" ref="dt-type-error"/>.</p>

  </div2>

  <div2 id="id-important-concepts">
    <head>Concepts</head>

    <p>[XPath/XQuery] is most generally used to process
    <term>documents</term>. The representation of a document is
    normatively defined in <bibref ref="xpath-datamodel"/>. The functions used to
    access documents and collections are normatively defined in
    <bibref ref="xpath-functions"/>. </p>

    <div3 id="id-doc-order">
      <head>Document Order</head>

      <p>Document order is defined in <bibref ref="xpath-datamodel"/>.</p>
    </div3>


      <div3 id="id-atomization">
        <head>Atomization</head>

        <p>Atomization converts an item sequence into a sequence of
        atomic values and is implemented by the <code>fn:data</code> function.
        Atomization is applied to a value when the value is used in a
        context in which a sequence of atomic values is required.</p>
      </div3>
  
    <div3 id="id-ebv">
      <head>Effective Boolean Value</head>

      <p>If a sequence of items is encountered where a boolean value
      is expected, the item sequence's effective boolean value is
      used. The <code>fn:boolean</code> function returns the effective boolean
      value of an item sequence.</p>
    </div3>
  
    <div3 id="id-input-sources"><head>Input Sources</head>
      <p>[XPath/XQuery] has several functions that provide access to input
      data, described in <xspecref spec="XQ" ref="id-input-sources"/>. These functions are of particular
      importance because they provide a way in which an expression can
      reference a document or a collection of documents. The dynamic
      semantics of these input functions are described in more detail
      in <bibref ref="xpath-functions"/>.</p>
    </div3>

    <div3 id="id-uri-literals" role="xquery">
      <head>URI Literals</head>

      <p>In certain places in the XQuery grammar, a statically known
      valid absolute URI is required. These places are denoted by the
      grammatical symbol <emph>URILiteral</emph>, and are treated as described
      in <bibref ref="xquery"/>.</p>

    </div3>
  </div2>

  <div2 id="id-type-conversion">
    <head>Types</head>

    <div3 id="id-predefined-types">
      <head>Predefined Schema Types</head>

      <p>All the built-in types of XML Schema are recognized by
      [XPath/XQuery]. In addition, [XPath/XQuery] recognizes the predefined
      types <a href="#dt-xs_anyAtomicType"><code>xs:anyAtomicType</code></a>, <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a> and <a href="#dt-xs_untyped"><code>xs:untyped</code></a>
      and the duration subtypes <a href="#dt-xs_durations"><code>xs:yearMonthDuration</code></a> and
      <a href="#dt-xs_durations"><code>xs:dayTimeDuration</code></a>      . The definition of those types in the
      [XPath/XQuery] type system is given below.</p>

      <p><termdef id="dt-xs_anyType" term="xs:anyType"> The following
      type definition of <term><phrase><code>xs:anyType</code></phrase></term>
      reflects the semantics of the Ur type from Schema in the
      [XPath/XQuery] type system.</termdef></p>

<eg xml:space="preserve">
  define type xs:anyType restricts xs:anyType {
    ( attribute * of type xs:anySimpleType )*,
    ( xs:anyAtomicType* | ( element * of type xs:anyType | text | comment | processing-instruction )* )
  }
</eg>

      <p><termdef id="dt-xs_anySimpleType" term="xs:anySimpleType">
      The following type definition of
      <term><phrase><code>xs:anySimpleType</code></phrase></term> reflects the
      semantics of the Ur simple type from Schema in the [XPath/XQuery]
      type system.</termdef></p>

<eg xml:space="preserve">
  define type xs:anySimpleType restricts xs:anyType {
    xs:anyAtomicType*
  }
</eg>

      <p>The name of the Ur simple type is <code>xs:anySimpleType</code>. It is
      derived by restriction from <code>xs:anyType</code>, its content is a
      sequence any atomic types.</p>

      <p><termdef id="dt-xs_anyAtomicType" term="xs:anyAtomicType"><phrase>The following type definition
      of <term><phrase><a href="#dt-xs_anyAtomicType"><code>xs:anyAtomicType</code></a></phrase></term> reflects
      the semantics of <a href="#dt-xs_anyAtomicType"><code>xs:anyAtomicType</code></a> in the [XPath/XQuery] type
      system.</phrase></termdef></p>

<eg xml:space="preserve">
  define type xs:anyAtomicType restricts xs:anySimpleType {
    ( xs:string
    | xs:boolean
    | xs:decimal
    | xs:float
    | xs:double
    | xs:duration
    | xs:dateTime
    | xs:time
    | xs:date
    | xs:gYearMonth
    | xs:gYear
    | xs:gMonthDay
    | xs:gDay
    | xs:gMonth
    | xs:hexBinary
    | xs:base64Binary
    | xs:anyURI
    | xs:QName
    | xs:NOTATION
    | xs:untypedAtomic )
  }
</eg>

      <p><termdef id="dt-primitive-types" term="primitive types"> The
      following type definitions of <term> the XML Schema primitive
      types</term> reflect the semantics of the primitive types from
      Schema in the [XPath/XQuery] type system.</termdef></p>

<eg xml:space="preserve">
  define type xs:string       restricts xs:anyAtomicType
  define type xs:boolean      restricts xs:anyAtomicType
  define type xs:decimal      restricts xs:anyAtomicType
  define type xs:float        restricts xs:anyAtomicType
  define type xs:double       restricts xs:anyAtomicType
  define type xs:duration     restricts xs:anyAtomicType
  define type xs:dateTime     restricts xs:anyAtomicType
  define type xs:time         restricts xs:anyAtomicType
  define type xs:date         restricts xs:anyAtomicType
  define type xs:gYearMonth   restricts xs:anyAtomicType
  define type xs:gYear        restricts xs:anyAtomicType
  define type xs:gMonthDay    restricts xs:anyAtomicType
  define type xs:gDay         restricts xs:anyAtomicType
  define type xs:gMonth       restricts xs:anyAtomicType
  define type xs:hexBinary    restricts xs:anyAtomicType
  define type xs:base64Binary restricts xs:anyAtomicType
  define type xs:anyURI       restricts xs:anyAtomicType
  define type xs:QName        restricts xs:anyAtomicType
  define type xs:NOTATION     restricts xs:anyAtomicType
</eg>

      <p>All of those primitive types derive from
      <a href="#dt-xs_anyAtomicType"><code>xs:anyAtomicType</code></a>. Note that the value space of each atomic
      type (such as <code>xs:string</code>) does not appear. The value space for
      each type is built-in and is as defined in <bibref ref="xmlschema-2"/>.</p>

      <p><termdef id="dt-xs_untypedAtomic" term="xs:untypedAtomic">The type
      <term><phrase><a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a></phrase></term> is defined as
      follows.</termdef></p>

<eg xml:space="preserve">
  define type xs:untypedAtomic restricts xs:anyAtomicType
</eg>

      <p>Note that this rule does not indicate the value space of
      <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>. By definition, <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a> has the
      same value space as <code>xs:string</code>.</p>

      <p>The following example shows two atomic values. The first one
      is a value of type string containing "Database". The second one
      is an untyped atomic value containing "Database".</p>

<eg xml:space="preserve">
  "Databases" of type xs:string
  "Databases" of type xs:untypedAtomic
</eg>

      <p><termdef id="dt-xs_untyped" term="xs:untyped">The type
      <term><phrase><a href="#dt-xs_untyped"><code>xs:untyped</code></a></phrase></term> is defined as
      follows.</termdef></p>

<eg xml:space="preserve">
  define type xs:untyped restricts xs:anyType {
    attribute * of type xs:untypedAtomic*,
    ( element * of type xs:untyped | text | comment | processing-instruction )*
  }
</eg>

      <p><termdef id="dt-derived-types" term="derived types">The
      following type definitions of the <term>XML Schema derived
      types</term> reflect the semantics of the XML Schema types
      derived by restriction from another atomic type.</termdef></p>

<eg xml:space="preserve">
  define type xs:normalizedString   restricts xs:string
  define type xs:token              restricts xs:normalizedString
  define type xs:language           restricts xs:token
  define type xs:NMTOKEN            restricts xs:token
  define type xs:Name               restricts xs:token
  define type xs:NCName             restricts xs:Name
  define type xs:ID                 restricts xs:NCName
  define type xs:IDREF              restricts xs:NCName
  define type xs:ENTITY             restricts xs:NCName
  define type xs:integer            restricts xs:decimal
  define type xs:nonPositiveInteger restricts xs:integer
  define type xs:negativeInteger    restricts xs:nonPositiveInteger
  define type xs:long               restricts xs:integer
  define type xs:int                restricts xs:long
  define type xs:short              restricts xs:int
  define type xs:byte               restricts xs:short
  define type xs:nonNegativeInteger restricts xs:integer
  define type xs:unsignedLong       restricts xs:nonNegativeInteger
  define type xs:unsignedInt        restricts xs:unsignedLong
  define type xs:unsignedShort      restricts xs:unsignedInt
  define type xs:unsignedByte       restricts xs:unsignedShort
  define type xs:positiveInteger    restricts xs:nonNegativeInteger
</eg>

      <p>Three XML Schema built-in derived types are derived by list,
      as follows. Note that those derive directly from
      <code>xs:anySimpleType</code>, since they are derived by list, and that
      their value space is defined using a <quote>one or more</quote>
      occurrence indicator.</p>

<eg xml:space="preserve">
  define type xs:NMTOKENS restricts xs:anySimpleType { xs:NMTOKEN+ }
  define type xs:IDREFS   restricts xs:anySimpleType { xs:IDREF+ }
  define type xs:ENTITIES restricts xs:anySimpleType { xs:ENTITY+ }
</eg>

     <p>For example, here is an element whose content is of type
     <code>xs:IDREFS</code>.</p>

<eg xml:space="preserve">
  element a of type xs:IDREFS {
    "id1" of type xs:IDREF,
    "id2" of type xs:IDREF,
    "id3" of type xs:IDREF
  }
</eg>

      <p>Note that the type name <code>xs:IDREFS</code> derives from
      <code>xs:anySimpleType</code>, but not from <code>xs:IDREF</code>. As a consequence,
      calling the following three XQuery functions with the element
      <code>a</code> as a parameter succeeds for <code>f1</code> and
      <code>f2</code>, but raises a type error for
      <code>f3</code>.</p>

<eg xml:space="preserve">
  declare function f1($x as element(*,xs:anySimpleType)) { $x }
  declare function f2($x as element(*,xs:IDREFS)) { $x }
  declare function f3($x as element(*,xs:IDREF)) { $x }
</eg>

      <p><termdef id="dt-xs_durations" term="Totally ordered duration       types"><phrase>The <term>totally ordered duration types</term>,
      <a href="#dt-xs_durations"><code>xs:yearMonthDuration</code></a> and <a href="#dt-xs_durations"><code>xs:dayTimeDuration</code></a>      , are derived
      by restriction from <code>xs:duration</code>.</phrase></termdef></p>

<eg xml:space="preserve">
  define type xs:yearMonthDuration restricts xs:duration
  define type xs:dayTimeDuration   restricts xs:duration
</eg>

      <p><termdef id="dt-fs_numeric" term="fs:numeric">In addition,
      the Formal Semantics uses the additional type <emph>fs:</emph><code>numeric</code>. This
      type is necessary for the specification of some of XPath type
      conversion rules.  It is defined as follows.</termdef></p>

<eg xml:space="preserve">
  define type fs:numeric restricts xs:anyAtomicType { xs:decimal | xs:float | xs:double }
</eg>

    </div3>

    <div3 id="id-typed-value">
      <head>Typed Value and String Value</head>
      <p>The typed value of a node is computed by the <code>fn:data</code>
      function, and the string value of a node is computed by the
      <code>fn:string</code> function, defined in <bibref ref="xpath-functions"/>.
The normative definitions of typed value and string value are defined in <bibref ref="xpath-datamodel"/>.
</p>
    </div3>

    <div3 id="id-sequencetype"> 
      <head>SequenceType Syntax</head> 

      <smintro>
        <p>Sequence types can be used in [XPath/XQuery] to refer to an XML
        Schema type. Sequence types are used to declare the types of
        function parameters and in several [XPath/XQuery] expressions.</p>
  
        <p>The syntax of sequence types is described by the following
        grammar productions.</p>

    <scrap headstyle="show"> 
      <head>SequenceType</head> 
      <prod num="119 (XQuery)" id="doc-xquery-SequenceType"><lhs>SequenceType</lhs><rhs>("empty-sequence"  "("  ")")<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ItemType" xlink:type="simple">ItemType</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-OccurrenceIndicator" xlink:type="simple">OccurrenceIndicator</nt>?)</rhs></prod>
      <prod num="121 (XQuery)" id="doc-xquery-ItemType"><lhs>ItemType</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-KindTest" xlink:type="simple">KindTest</nt>  |  ("item"  "("  ")")  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AtomicType" xlink:type="simple">AtomicType</nt></rhs></prod>
      <prod num="120 (XQuery)" id="doc-xquery-OccurrenceIndicator"><lhs>OccurrenceIndicator</lhs><rhs>"?"  |  "*"  |  "+"</rhs></prod>
      <prod num="122 (XQuery)" id="doc-xquery-AtomicType"><lhs>AtomicType</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt></rhs></prod>
      <prod num="123 (XQuery)" id="doc-xquery-KindTest"><lhs>KindTest</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DocumentTest" xlink:type="simple">DocumentTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ElementTest" xlink:type="simple">ElementTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AttributeTest" xlink:type="simple">AttributeTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-SchemaElementTest" xlink:type="simple">SchemaElementTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-SchemaAttributeTest" xlink:type="simple">SchemaAttributeTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PITest" xlink:type="simple">PITest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CommentTest" xlink:type="simple">CommentTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-TextTest" xlink:type="simple">TextTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AnyKindTest" xlink:type="simple">AnyKindTest</nt></rhs></prod>
      <prod num="125 (XQuery)" id="doc-xquery-DocumentTest"><lhs>DocumentTest</lhs><rhs>"document-node"  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ElementTest" xlink:type="simple">ElementTest</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-SchemaElementTest" xlink:type="simple">SchemaElementTest</nt>)?  ")"</rhs></prod>
      <prod num="133 (XQuery)" id="doc-xquery-ElementTest"><lhs>ElementTest</lhs><rhs>"element"  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ElementNameOrWildcard" xlink:type="simple">ElementNameOrWildcard</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-TypeName" xlink:type="simple">TypeName</nt>  "?"?)?)?  ")"</rhs></prod>
      <prod num="135 (XQuery)" id="doc-xquery-SchemaElementTest"><lhs>SchemaElementTest</lhs><rhs>"schema-element"  "("  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ElementDeclaration" xlink:type="simple">ElementDeclaration</nt>  ")"</rhs></prod>
      <prod num="136 (XQuery)" id="doc-xquery-ElementDeclaration"><lhs>ElementDeclaration</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ElementName" xlink:type="simple">ElementName</nt></rhs></prod>
      <prod num="129 (XQuery)" id="doc-xquery-AttributeTest"><lhs>AttributeTest</lhs><rhs>"attribute"  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AttribNameOrWildcard" xlink:type="simple">AttribNameOrWildcard</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-TypeName" xlink:type="simple">TypeName</nt>)?)?  ")"</rhs></prod>
      <prod num="131 (XQuery)" id="doc-xquery-SchemaAttributeTest"><lhs>SchemaAttributeTest</lhs><rhs>"schema-attribute"  "("  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AttributeDeclaration" xlink:type="simple">AttributeDeclaration</nt>  ")"</rhs></prod>
      <prod num="132 (XQuery)" id="doc-xquery-AttributeDeclaration"><lhs>AttributeDeclaration</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AttributeName" xlink:type="simple">AttributeName</nt></rhs></prod>
      <prod num="134 (XQuery)" id="doc-xquery-ElementNameOrWildcard"><lhs>ElementNameOrWildcard</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ElementName" xlink:type="simple">ElementName</nt>  |  "*"</rhs></prod>
      <prod num="138 (XQuery)" id="doc-xquery-ElementName"><lhs>ElementName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt></rhs></prod>
      <prod num="130 (XQuery)" id="doc-xquery-AttribNameOrWildcard"><lhs>AttribNameOrWildcard</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AttributeName" xlink:type="simple">AttributeName</nt>  |  "*"</rhs></prod>
      <prod num="137 (XQuery)" id="doc-xquery-AttributeName"><lhs>AttributeName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt></rhs></prod>
      <prod num="139 (XQuery)" id="doc-xquery-TypeName"><lhs>TypeName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt></rhs></prod>
      <prod num="128 (XQuery)" id="doc-xquery-PITest"><lhs>PITest</lhs><rhs>"processing-instruction"  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-NCName" xlink:type="simple">NCName</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-StringLiteral" xlink:type="simple">StringLiteral</nt>)?  ")"</rhs></prod>
      <prod num="127 (XQuery)" id="doc-xquery-CommentTest"><lhs>CommentTest</lhs><rhs>"comment"  "("  ")"</rhs></prod>
      <prod num="126 (XQuery)" id="doc-xquery-TextTest"><lhs>TextTest</lhs><rhs>"text"  "("  ")"</rhs></prod>
      <prod num="124 (XQuery)" id="doc-xquery-AnyKindTest"><lhs>AnyKindTest</lhs><rhs>"node"  "("  ")"</rhs></prod>
    </scrap> 

        <smcore>
          <p>The Core grammar productions for sequence types are:</p>
      
  <scrap headstyle="show">
   <head/>
   <prod num="76 (Core)" id="doc-core-SequenceType"><lhs>SequenceType</lhs><rhs>("empty-sequence"  "("  ")")<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ItemType" xlink:type="simple">ItemType</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-OccurrenceIndicator" xlink:type="simple">OccurrenceIndicator</nt>?)</rhs></prod>
   <prod num="78 (Core)" id="doc-core-ItemType"><lhs>ItemType</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-KindTest" xlink:type="simple">KindTest</nt>  |  ("item"  "("  ")")  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AtomicType" xlink:type="simple">AtomicType</nt></rhs></prod>
   <prod num="77 (Core)" id="doc-core-OccurrenceIndicator"><lhs>OccurrenceIndicator</lhs><rhs>"?"  |  "*"  |  "+"</rhs></prod>
   <prod num="79 (Core)" id="doc-core-AtomicType"><lhs>AtomicType</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt></rhs></prod>
   <prod num="80 (Core)" id="doc-core-KindTest"><lhs>KindTest</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-DocumentTest" xlink:type="simple">DocumentTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ElementTest" xlink:type="simple">ElementTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AttributeTest" xlink:type="simple">AttributeTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-SchemaElementTest" xlink:type="simple">SchemaElementTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-SchemaAttributeTest" xlink:type="simple">SchemaAttributeTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-PITest" xlink:type="simple">PITest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CommentTest" xlink:type="simple">CommentTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-TextTest" xlink:type="simple">TextTest</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AnyKindTest" xlink:type="simple">AnyKindTest</nt></rhs></prod>
   <prod num="82 (Core)" id="doc-core-DocumentTest"><lhs>DocumentTest</lhs><rhs>"document-node"  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ElementTest" xlink:type="simple">ElementTest</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-SchemaElementTest" xlink:type="simple">SchemaElementTest</nt>)?  ")"</rhs></prod>
   <prod num="90 (Core)" id="doc-core-ElementTest"><lhs>ElementTest</lhs><rhs>"element"  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ElementNameOrWildcard" xlink:type="simple">ElementNameOrWildcard</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-TypeName" xlink:type="simple">TypeName</nt>  "?"?)?)?  ")"</rhs></prod>
   <prod num="92 (Core)" id="doc-core-SchemaElementTest"><lhs>SchemaElementTest</lhs><rhs>"schema-element"  "("  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ElementDeclaration" xlink:type="simple">ElementDeclaration</nt>  ")"</rhs></prod>
   <prod num="93 (Core)" id="doc-core-ElementDeclaration"><lhs>ElementDeclaration</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ElementName" xlink:type="simple">ElementName</nt></rhs></prod>
   <prod num="86 (Core)" id="doc-core-AttributeTest"><lhs>AttributeTest</lhs><rhs>"attribute"  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AttribNameOrWildcard" xlink:type="simple">AttribNameOrWildcard</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-TypeName" xlink:type="simple">TypeName</nt>)?)?  ")"</rhs></prod>
   <prod num="88 (Core)" id="doc-core-SchemaAttributeTest"><lhs>SchemaAttributeTest</lhs><rhs>"schema-attribute"  "("  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AttributeDeclaration" xlink:type="simple">AttributeDeclaration</nt>  ")"</rhs></prod>
   <prod num="89 (Core)" id="doc-core-AttributeDeclaration"><lhs>AttributeDeclaration</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AttributeName" xlink:type="simple">AttributeName</nt></rhs></prod>
   <prod num="91 (Core)" id="doc-core-ElementNameOrWildcard"><lhs>ElementNameOrWildcard</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ElementName" xlink:type="simple">ElementName</nt>  |  "*"</rhs></prod>
   <prod num="95 (Core)" id="doc-core-ElementName"><lhs>ElementName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt></rhs></prod>
   <prod num="87 (Core)" id="doc-core-AttribNameOrWildcard"><lhs>AttribNameOrWildcard</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AttributeName" xlink:type="simple">AttributeName</nt>  |  "*"</rhs></prod>
   <prod num="94 (Core)" id="doc-core-AttributeName"><lhs>AttributeName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt></rhs></prod>
   <prod num="96 (Core)" id="doc-core-TypeName"><lhs>TypeName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt></rhs></prod>
   <prod num="85 (Core)" id="doc-core-PITest"><lhs>PITest</lhs><rhs>"processing-instruction"  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-NCName" xlink:type="simple">NCName</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-StringLiteral" xlink:type="simple">StringLiteral</nt>)?  ")"</rhs></prod>
   <prod num="84 (Core)" id="doc-core-CommentTest"><lhs>CommentTest</lhs><rhs>"comment"  "("  ")"</rhs></prod>
   <prod num="83 (Core)" id="doc-core-TextTest"><lhs>TextTest</lhs><rhs>"text"  "("  ")"</rhs></prod>
   <prod num="81 (Core)" id="doc-core-AnyKindTest"><lhs>AnyKindTest</lhs><rhs>"node"  "("  ")"</rhs></prod>
  </scrap>
        </smcore>

        <p>The semantics of SequenceTypes is defined by means of
        normalization rules from SequenceTypes into types in the
        [XPath/XQuery] type system (See <specref ref="sec_types"/>).</p>

        <p>However, the [XPath/XQuery] type system not being part of the
        [XPath/XQuery] syntax, the SequenceType syntax is still part of
        the [XPath/XQuery] Core. Normalization from SequenceTypes to types
        is not applied during the normalization phase but whenever a
        dynamic evaluation or static typing rule requires it.</p>

      </smintro>
  
    </div3>
      <div3 id="id-sequencetype-matching">
        <head>SequenceType Matching</head>
    
        <smintro>
          <p>During processing of a query, it is sometimes necessary
          to determine whether a given value matches a type that was
          declared using the SequenceType syntax. This process is
          known as SequenceType matching, and is formally specified in
          <specref ref="sec_type_matching"/>.</p>
        </smintro>

        <smnotation>
          <p id="jd_map_sequencetype">To define normalization of
          SequenceTypes to the [XPath/XQuery] type system, the following
          auxiliary mapping rule is used.</p>
        
            <mapping>
             <xquery>
              <expression>
               <map><emph>SequenceType</emph></map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
               <a href="#doc-fs-Type"><emph>Type</emph></a>
              </expression>
             </core>
            </mapping>
        
          <p>specifies that <emph>SequenceType</emph> is mapped to a <a href="#doc-fs-Type"><emph>Type</emph></a>,
          in the [XPath/XQuery] type system.</p>
        </smnotation>
        
        <smnorm>
          <p>OccurenceIndicators are left unchanged when normalizing
          SequenceTypes into [XPath/XQuery] types. Each kind of
          SequenceType component is normalized separately into the
          [XPath/XQuery] type system.</p>

            <mapping>
             <xquery>
              <expression>
               <map><a href="#doc-core-ItemType"><emph>ItemType</emph></a> <emph>OccurrenceIndicator</emph></map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
               <map><a href="#doc-core-ItemType"><emph>ItemType</emph></a></map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a> <emph>OccurrenceIndicator</emph>
              </expression>
             </core>
            </mapping>
        
            <p>The <quote>empty-sequence()</quote> sequence type is
            mapped to the empty type.</p>
        
            <mapping>
             <xquery>
              <expression>
               <map>empty-sequence()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
               empty
              </expression>
             </core>
            </mapping>

            <p>An atomic type is normalized to itself in the
            [XPath/XQuery] type system.</p>

            <mapping>
             <xquery>
              <expression>
               <map><a href="#doc-core-AtomicType"><emph>AtomicType</emph></a></map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
               <a href="#doc-core-AtomicType"><emph>AtomicType</emph></a>
              </expression>
             </core>
            </mapping>

            <p>An <quote>element</quote> SequenceType without content
            or with a wildcard and no type name is normalized into a
            wildcard element type.</p>

            <mapping>
             <xquery>
              <expression>
               <map>element()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                element * of type <code>xs:anyType</code>
              </expression>
             </core>
            </mapping>
        
            <mapping>
             <xquery>
              <expression>
               <map>element(*)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                element * of type <code>xs:anyType</code>
              </expression>
             </core>
            </mapping>

            <p>An <quote>element</quote> SequenceType with a wildcard
            and a type name is normalized into a wildcard element type
            with a corresponding type name. The presence of a
            <quote>?</quote> after the type name indicates a nillable
            element.</p>

            <mapping>
             <xquery>
              <expression>
               <map>element(*,<a href="#doc-fs-TypeName"><emph>TypeName</emph></a>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                element * of type <a href="#doc-fs-TypeName"><emph>TypeName</emph></a>
              </expression>
             </core>
            </mapping>

            <mapping>
             <xquery>
              <expression>
               <map>element(*,<a href="#doc-fs-TypeName"><emph>TypeName</emph></a>?)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                element * nillable of type <a href="#doc-fs-TypeName"><emph>TypeName</emph></a>
              </expression>
             </core>
            </mapping>

            <p>An <quote>element</quote> SequenceType with a name and
            a type name is normalized into an element type with a
            corresponding type name. The presence of a
            <quote>?</quote> after the type name indicates a nillable
            element.</p>

            <mapping>
             <xquery>
              <expression>
               <map>element(<a href="#doc-fs-ElementName"><emph>ElementName</emph></a>,<a href="#doc-fs-TypeName"><emph>TypeName</emph></a>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                element <a href="#doc-fs-ElementName"><emph>ElementName</emph></a> of type <a href="#doc-fs-TypeName"><emph>TypeName</emph></a>
              </expression>
             </core>
            </mapping>

            <mapping>
             <xquery>
              <expression>
               <map>element(<a href="#doc-fs-ElementName"><emph>ElementName</emph></a>,<a href="#doc-fs-TypeName"><emph>TypeName</emph></a>?)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                element <a href="#doc-fs-ElementName"><emph>ElementName</emph></a> nillable of type <a href="#doc-fs-TypeName"><emph>TypeName</emph></a>
              </expression>
             </core>
            </mapping>

            <p>An <quote>element</quote> SequenceType with only a name
            is normalized into a <emph>nillable</emph> element type
            with a corresponding name. The reason for the
            normalization to allow nillable elements is because the
            semantics of SequenceTypes in that case allows it to match
            every possible element with that names, regardless of its
            type or nilled property.</p>

            <mapping>
             <xquery>
              <expression>
               <map>element(<a href="#doc-fs-ElementName"><emph>ElementName</emph></a>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                element <a href="#doc-fs-ElementName"><emph>ElementName</emph></a> nillable of type <code>xs:anyType</code>
              </expression>
             </core>
            </mapping>
       
            <p>A <quote>schema-element</quote> SequenceType with an
            element declaration is normalized into a reference to the
            corresponding global element declaration.</p>

            <mapping>
             <xquery>
              <expression>
               <map>schema-element(<a href="#doc-fs-ElementName"><emph>ElementName</emph></a>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                element <a href="#doc-fs-ElementName"><emph>ElementName</emph></a>
              </expression>
             </core>
            </mapping>
        
            <p>An <quote>attribute</quote> SequenceType without
            content or with a wildcard and no type name is normalized
            into a wildcard attribute type.</p>

            <mapping>
             <xquery>
              <expression>
               <map>attribute()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                attribute * of type <code>xs:anySimpleType</code> 
              </expression>
             </core>
            </mapping>
        
            <mapping>
             <xquery>
              <expression>
               <map>attribute(*)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                attribute * of type <code>xs:anySimpleType</code> 
              </expression>
             </core>
            </mapping>

            <p>An <quote>attribute</quote> SequenceType with a
            wildcard and a type name is normalized into a wildcard
            attribute type with a corresponding type name.</p>

            <mapping>
             <xquery>
              <expression>
               <map>attribute(*,<a href="#doc-fs-TypeName"><emph>TypeName</emph></a>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                attribute * of type <a href="#doc-fs-TypeName"><emph>TypeName</emph></a>
              </expression>
             </core>
            </mapping>

            <p>An <quote>attribute</quote> SequenceType with a name
            and a type name is normalized into an attribute type with
            a corresponding type name.</p>

            <mapping>
             <xquery>
              <expression>
               <map>attribute(<a href="#doc-fs-AttributeName"><emph>AttributeName</emph></a>,<a href="#doc-fs-TypeName"><emph>TypeName</emph></a>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                attribute <a href="#doc-fs-AttributeName"><emph>AttributeName</emph></a> of type <a href="#doc-fs-TypeName"><emph>TypeName</emph></a>
              </expression>
             </core>
            </mapping>

            <p>A <quote>schema-attribute</quote> SequenceType with an
            attribute declaration is normalized into a reference to
            the corresponding global attribute declaration.</p>

            <mapping>
             <xquery>
              <expression>
               <map>schema-attribute(<a href="#doc-fs-AttributeName"><emph>AttributeName</emph></a>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                attribute <a href="#doc-fs-AttributeName"><emph>AttributeName</emph></a>
              </expression>
             </core>
            </mapping>

            <p>A <quote>document-node()</quote> sequence types is
            normalized into the corresponding document type.</p>

            <mapping>
             <xquery>
              <expression>
               <map>document-node()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                document { (element * of type <code>xs:anyType</code> | text | comment | processing-instruction)* }
              </expression>
             </core>
            </mapping>

            <p>A <quote>document-node</quote> sequence type with an
            element test (resp. a schema element test) is normalized
            into the corresponding document type, whose content is the
            normalization of the element test (resp. schema element
            test), interleaved with an arbitrary sequence of
            processing instruction, comment, and text nodes.</p>

            <mapping>
             <xquery>
              <expression>
               <map>document-node(<emph>ElementTest</emph>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                document {
                <map><emph>ElementTest</emph></map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a> &amp;
                ( processing-instruction | comment ) *}
              </expression>
             </core>
            </mapping>

            <mapping>
             <xquery>
              <expression>
               <map>document-node(<emph>SchemaElementTest</emph>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                document {
                <map><emph>SchemaElementTest</emph></map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a> &amp;
                ( processing-instruction | comment ) *}
              </expression>
             </core>
            </mapping>

            <p>A <quote>processing-instruction()</quote> SequenceType
            is normalized into the corresponding
            processing-instruction type.</p>

            <mapping>
             <xquery>
              <expression>
               <map>processing-instruction()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>processing-instruction</expression>
             </core>
            </mapping>

            <p>The [XPath/XQuery] type system does not model the target of
            a processing-instruction, which is treated as a dynamic
            property. Therefore a
            <quote>processing-instruction</quote> SequenceType with a
            string or NCName parameter is normalized into an optional
            processing-instruction type.</p>

            <mapping>
             <xquery>
              <expression>
               <map>processing-instruction(<emph>StringLiteral</emph>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>processing-instruction?</expression>
             </core>
            </mapping>

            <mapping>
             <xquery>
              <expression>
               <map>processing-instruction(<emph>NCName</emph>)</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>processing-instruction?</expression>
             </core>
            </mapping>

            <p>A <quote>comment()</quote> SequenceType is normalized
            into the corresponding comment type.</p>
        
            <mapping>
             <xquery>
              <expression>
               <map>comment()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>comment</expression>
             </core>
            </mapping>

            <p>A <quote>text()</quote> SequenceType is normalized into
            the corresponding text type.</p>
        
            <mapping>
             <xquery>
              <expression>
               <map>text()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                text
              </expression>
             </core>
            </mapping>

            <p>The <quote>node()</quote> SequenceType denotes any
            node. It is normalized into a choice between the
            corresponding wildcard types for each kind of node.</p>

            <mapping>
             <xquery>
              <expression>
               <map>node()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                (element * of type <code>xs:anyType</code> | attribute * of type
                <code>xs:anySimpleType</code> | text | document { (element * of
                type <code>xs:anyType</code> | text | comment |
                processing-instruction)* } | comment | processing-instruction)
              </expression>
             </core>
            </mapping>

            <p>The <quote>item()</quote> SequenceType denotes any node
            or atomic value. It is normalized into a choice between
            the corresponding wildcard types for each kind of nodes or
            atomic values.</p>

            <mapping>
             <xquery>
              <expression>
               <map>item()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                (element * of type <code>xs:anyType</code> | attribute * of type <code>xs:anySimpleType</code> | text | document { (element * of
                type <code>xs:anyType</code> | text | comment |
                processing-instruction)* } | comment |
                processing-instruction | <a href="#dt-xs_anyAtomicType"><code>xs:anyAtomicType</code></a> )
              </expression>
             </core>
            </mapping>

        </smnorm>
      </div3>
  
  
  </div2>

  <div2 id="comments">
    <head>Comments</head>
  
    <scrap headstyle="show">
      <head/>
      <prod num="151 (XQuery)" id="doc-xquery-Comment"><lhs>Comment</lhs><rhs>"(:"  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CommentContents" xlink:type="simple">CommentContents</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Comment" xlink:type="simple">Comment</nt>)*  ":)"</rhs></prod>
      <prod num="159 (XQuery)" id="doc-xquery-CommentContents"><lhs>CommentContents</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-Char" xlink:type="simple">Char</nt>+ - (Char* ('(:' | ':)') Char*))</rhs></prod>
    </scrap>

    <p>Comments are lexical constructs only, and have no effect on the
    meaning of the query, and therefore do not have any formal
    semantics.</p>
  
  </div2>

  <div2 id="xml-terminals">
    <head>XML-defined Terminals</head>

    <p>The following terminals are defined by XML.</p>
	
	<scrap headstyle="show">  
		<head/>
		<prod num="152 (XQuery)" id="prod-xquery-PITarget"><lhs>PITarget</lhs><rhs><xnt xmlns:xlink="http://www.w3.org/1999/xlink" spec="XML" ref="NT-PITarget" xlink:type="simple">[http://www.w3.org/TR/REC-xml#NT-PITarget]</xnt></rhs><com><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#parse-note-xml-version" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">xgs: xml-version</loc></com></prod><prod num="153 (XQuery)" id="prod-xquery-CharRef"><lhs>CharRef</lhs><rhs><xnt xmlns:xlink="http://www.w3.org/1999/xlink" spec="XML" ref="NT-CharRef" xlink:type="simple">[http://www.w3.org/TR/REC-xml#NT-CharRef]</xnt></rhs><com><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#parse-note-xml-version" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">xgs: xml-version</loc></com></prod><prod num="154 (XQuery)" id="prod-xquery-QName"><lhs>QName</lhs><rhs><xnt xmlns:xlink="http://www.w3.org/1999/xlink" spec="Names" ref="NT-QName" xlink:type="simple">[http://www.w3.org/TR/REC-xml-names/#NT-QName]</xnt></rhs><com><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#parse-note-xml-version" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">xgs: xml-version</loc></com></prod><prod num="155 (XQuery)" id="prod-xquery-NCName"><lhs>NCName</lhs><rhs><xnt xmlns:xlink="http://www.w3.org/1999/xlink" spec="Names" ref="NT-NCName" xlink:type="simple">[http://www.w3.org/TR/REC-xml-names/#NT-NCName]</xnt></rhs><com><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#parse-note-xml-version" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">xgs: xml-version</loc></com></prod><prod num="156 (XQuery)" id="prod-xquery-S"><lhs>S</lhs><rhs><xnt xmlns:xlink="http://www.w3.org/1999/xlink" spec="XML" ref="NT-S" xlink:type="simple">[http://www.w3.org/TR/REC-xml#NT-S]</xnt></rhs><com><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#parse-note-xml-version" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">xgs: xml-version</loc></com></prod><prod num="157 (XQuery)" id="prod-xquery-Char"><lhs>Char</lhs><rhs><xnt xmlns:xlink="http://www.w3.org/1999/xlink" spec="XML" ref="NT-Char" xlink:type="simple">[http://www.w3.org/TR/REC-xml#NT-Char]</xnt></rhs><com><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#parse-note-xml-version" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">xgs: xml-version</loc></com></prod> 	   
	</scrap>

  </div2>

</div1>

<div1 id="id-expressions">
  <head>Expressions</head>

  <p>This section gives the semantics of all the [XPath/XQuery]
  expressions. The organization of this section parallels the
  organization of <xspecref spec="XQ" ref="id-expressions"/>.</p>

  <scrap headstyle="show"><head/>
    <prod num="31 (XQuery)" id="doc-xquery-Expr"><lhs>Expr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ExprSingle" xlink:type="simple">ExprSingle</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ExprSingle" xlink:type="simple">ExprSingle</nt>)*</rhs></prod>
    <prod num="32 (XQuery)" id="doc-xquery-ExprSingle"><lhs>ExprSingle</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-FLWORExpr" xlink:type="simple">FLWORExpr</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-QuantifiedExpr" xlink:type="simple">QuantifiedExpr</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-TypeswitchExpr" xlink:type="simple">TypeswitchExpr</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-IfExpr" xlink:type="simple">IfExpr</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-OrExpr" xlink:type="simple">OrExpr</nt></rhs></prod>
    <prod num="1 (XPath)" id="doc-xpath-XPath"><lhs>XPath</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xpath-Expr" xlink:type="simple">Expr</nt></rhs></prod>
  </scrap>

  <p>For each expression, a short description and the relevant grammar
  productions are given. The semantics of an expression includes the
  normalization, static analysis, and dynamic evaluation
  phases. Recall that normalization rules translate [XPath/XQuery] syntax
  into Core syntax. In the sections that contain normalization rules,
  the Core grammar productions into which the expression is normalized
  are also provided. After normalization, sections on static type
  inference and dynamic evaluation define the static type and dynamic
  value for the Core expression.</p>

  <smcore>
    <p>The Core grammar productions for expressions are:</p>

    <scrap headstyle="show"><head/>
      <prod num="22 (Core)" id="doc-core-Expr"><lhs>Expr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ExprSingle" xlink:type="simple">ExprSingle</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ExprSingle" xlink:type="simple">ExprSingle</nt>)*</rhs></prod>
      <prod num="23 (Core)" id="doc-core-ExprSingle"><lhs>ExprSingle</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-FLWORExpr" xlink:type="simple">FLWORExpr</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-QuantifiedExpr" xlink:type="simple">QuantifiedExpr</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-TypeswitchExpr" xlink:type="simple">TypeswitchExpr</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-IfExpr" xlink:type="simple">IfExpr</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-OrExpr" xlink:type="simple">OrExpr</nt></rhs></prod>
    </scrap>
  </smcore>

  <smtype>
    <p>During static analysis, it is a type error for an expression to have the empty
    type, except for the following expressions and function calls:

    <ulist>
      <item><p>Empty parentheses <code>()</code>, which denote the
      empty sequence.</p></item>
      <item><p>The <code>fn:data</code> function and all functions in the
      <emph>fs</emph> namespace applied to empty
      parentheses <code>()</code>.</p></item>
      <item><p>Any function which returns the empty type.</p></item>
    </ulist>

    The reason for these exceptions is that they are typically part of
    the result of normalizing a larger user-level expression and are
    used to capture the semantics of the user-level expression when
    applied to the empty sequence.</p>

    <p>The rule below enforces the above constraints. It is a static
    type error, if the following conditions hold for a given
    expression <emph>Expr</emph>.</p>

      <infergr>
    	<infer>
          <prejudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 		      <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
              <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 		      <expression><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <code>empty</code></expression></clause>
 	    </multiclause>
 	    <multiclause>
              <clause><expression><a href="#jd_not" class="judgment"><term>not</term></a>(<emph>Expr</emph> is the empty
              parentheses () or <code>fn:data</code> or any <emph>fs</emph>
              function applied to empty parentheses ())</expression></clause>
 	    </multiclause>
          </prejudge>
 	  <postjudge>
 	    <multiclause>
              <clause>
               <expression>A static type error is raised for expression <emph>Expr</emph></expression>
              </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>

      <p>In general, static type errors are raised whenever there is
      no static typing rules which can compute the type of a given
      expression. This is the reason for the absence of a formal
      post-condition in this rules. There is indeed a rule that infers
      the type for expression <emph>Expr</emph>, however the inferred type is
      empty and still a static type error must be raised.</p>

      <smexample>
        <p>The above rule is useful in catching common mistakes, such
        as the misspelling of an element or attribute name or
        referencing of an element or attribute that does not
        exist. For instance, the following path expression</p>
<eg xml:space="preserve">  $x/title
</eg>
    <p>raises a static type error if the type of variable
    <code>$x</code> does not include any
    <code>title</code> children elements.</p>
  </smexample>
  </smtype>

  <!-- ***************** Primary expressions              ****** -->

  <div2 id="id-primary-expressions">
    <head>Primary Expressions</head>
  
    <p><term>Primary expressions</term> are the basic primitives of
    the language. They include literals, variables, function calls,
    and the parenthesized expressions.</p>

    <scrap headstyle="show">
      <head>Primary Expressions</head>
      <prod num="84 (XQuery)" id="doc-xquery-PrimaryExpr"><lhs>PrimaryExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Literal" xlink:type="simple">Literal</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-VarRef" xlink:type="simple">VarRef</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ParenthesizedExpr" xlink:type="simple">ParenthesizedExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ContextItemExpr" xlink:type="simple">ContextItemExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-FunctionCall" xlink:type="simple">FunctionCall</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-OrderedExpr" xlink:type="simple">OrderedExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-UnorderedExpr" xlink:type="simple">UnorderedExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Constructor" xlink:type="simple">Constructor</nt></rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar production for primary expressions is:</p>
  
      <scrap headstyle="show">
        <head>Primary Expressions</head>
        <prod num="55 (Core)" id="doc-core-PrimaryExpr"><lhs>PrimaryExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Literal" xlink:type="simple">Literal</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-VarRef" xlink:type="simple">VarRef</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ParenthesizedExpr" xlink:type="simple">ParenthesizedExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-FunctionCall" xlink:type="simple">FunctionCall</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-OrderedExpr" xlink:type="simple">OrderedExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-UnorderedExpr" xlink:type="simple">UnorderedExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Constructor" xlink:type="simple">Constructor</nt></rhs></prod>
      </scrap>
    </smcore>

    <div3 id="id-literals">
      <head>Literals</head>

      <smintro>
        <p>A <term>literal</term> is a direct syntactic representation
        of an atomic value.  [XPath/XQuery] supports two kinds of literals:
        string literals and numeric literals.</p>
      </smintro>

<scrap headstyle="show">
  <head>Literals</head>
  <prod num="85 (XQuery)" id="doc-xquery-Literal"><lhs>Literal</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-NumericLiteral" xlink:type="simple">NumericLiteral</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-StringLiteral" xlink:type="simple">StringLiteral</nt></rhs></prod>
  <prod num="86 (XQuery)" id="doc-xquery-NumericLiteral"><lhs>NumericLiteral</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-IntegerLiteral" xlink:type="simple">IntegerLiteral</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DecimalLiteral" xlink:type="simple">DecimalLiteral</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DoubleLiteral" xlink:type="simple">DoubleLiteral</nt></rhs></prod>
  <prod num="141 (XQuery)" id="doc-xquery-IntegerLiteral"><lhs>IntegerLiteral</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Digits" xlink:type="simple">Digits</nt></rhs></prod>
  <prod num="142 (XQuery)" id="doc-xquery-DecimalLiteral"><lhs>DecimalLiteral</lhs><rhs>("."  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Digits" xlink:type="simple">Digits</nt>)  |  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Digits" xlink:type="simple">Digits</nt>  "."  [0-9]*)</rhs></prod>
  <prod num="143 (XQuery)" id="doc-xquery-DoubleLiteral"><lhs>DoubleLiteral</lhs><rhs>(("."  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Digits" xlink:type="simple">Digits</nt>)  |  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Digits" xlink:type="simple">Digits</nt>  ("."  [0-9]*)?))  [eE]  [+-]?  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Digits" xlink:type="simple">Digits</nt></rhs></prod>
  <prod num="144 (XQuery)" id="doc-xquery-StringLiteral"><lhs>StringLiteral</lhs><rhs>('"'  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PredefinedEntityRef" xlink:type="simple">PredefinedEntityRef</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-CharRef" xlink:type="simple">CharRef</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-EscapeQuot" xlink:type="simple">EscapeQuot</nt>  |  [^"&amp;])*  '"')  |  ("'"  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PredefinedEntityRef" xlink:type="simple">PredefinedEntityRef</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-CharRef" xlink:type="simple">CharRef</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-EscapeApos" xlink:type="simple">EscapeApos</nt>  |  [^'&amp;])*  "'")</rhs></prod>
  <prod num="140 (XQuery)" id="doc-xquery-URILiteral"><lhs>URILiteral</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-StringLiteral" xlink:type="simple">StringLiteral</nt></rhs></prod>
  <prod num="145 (XQuery)" id="doc-xquery-PredefinedEntityRef"><lhs>PredefinedEntityRef</lhs><rhs>"&amp;"  ("lt"  |  "gt"  |  "amp"  |  "quot"  |  "apos")  ";"</rhs></prod>
  <prod num="158 (XQuery)" id="doc-xquery-Digits"><lhs>Digits</lhs><rhs>[0-9]+</rhs></prod>
</scrap>

      <smcore>
        <p>The Core grammar productions for literals are:</p>

<scrap headstyle="show">
  <head>Literals</head>

  <prod num="56 (Core)" id="doc-core-Literal"><lhs>Literal</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-NumericLiteral" xlink:type="simple">NumericLiteral</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-StringLiteral" xlink:type="simple">StringLiteral</nt></rhs></prod>
  <prod num="57 (Core)" id="doc-core-NumericLiteral"><lhs>NumericLiteral</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-IntegerLiteral" xlink:type="simple">IntegerLiteral</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-DecimalLiteral" xlink:type="simple">DecimalLiteral</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-DoubleLiteral" xlink:type="simple">DoubleLiteral</nt></rhs></prod>
  <prod num="98 (Core)" id="doc-core-IntegerLiteral"><lhs>IntegerLiteral</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Digits" xlink:type="simple">Digits</nt></rhs></prod>
  <prod num="99 (Core)" id="doc-core-DecimalLiteral"><lhs>DecimalLiteral</lhs><rhs>("."  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Digits" xlink:type="simple">Digits</nt>)  |  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Digits" xlink:type="simple">Digits</nt>  "."  [0-9]*)</rhs></prod>
  <prod num="100 (Core)" id="doc-core-DoubleLiteral"><lhs>DoubleLiteral</lhs><rhs>(("."  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Digits" xlink:type="simple">Digits</nt>)  |  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Digits" xlink:type="simple">Digits</nt>  ("."  [0-9]*)?))  [eE]  [+-]?  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Digits" xlink:type="simple">Digits</nt></rhs></prod>
  <prod num="101 (Core)" id="doc-core-StringLiteral"><lhs>StringLiteral</lhs><rhs>('"'  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-EscapeQuot" xlink:type="simple">EscapeQuot</nt>  |  [^"])*  '"')  |  ("'"  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-EscapeApos" xlink:type="simple">EscapeApos</nt>  |  [^'])*  "'")</rhs></prod>
  <prod num="97 (Core)" id="doc-core-URILiteral"><lhs>URILiteral</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-StringLiteral" xlink:type="simple">StringLiteral</nt></rhs></prod>
  <prod num="112 (Core)" id="doc-core-Digits"><lhs>Digits</lhs><rhs>[0-9]+</rhs></prod>
</scrap>
      </smcore>

      <smnotation>
         <p>To define the dynamic semantics of literals, we introduce
         the following auxiliary judgments.</p>

        <p id="jd_has_atomic_value">The judgment</p>
        <display>
          <clause>
          <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
          <expression><emph>Literal</emph> <a href="#jd_has_atomic_value" class="judgment"><term>has atomic value</term></a> <a href="#doc-fs-AtomicValue"><emph>AtomicValue</emph></a></expression>
          </clause>
        </display>

          <p>holds if the literal expression <emph>Literal</emph> corresponds
          to the value <a href="#doc-fs-AtomicValue"><emph>AtomicValue</emph></a>. This judgment yields an
          atomic value, according to the rules described in <bibref ref="xquery"/>. Notably, this judgment deals with handling
          of literal overflows for numeric literals, and handling of
          character references, and predefined entity references for
          string literals.</p> </smnotation>

      <smnorm>
        <p>Literals are left unchanged through normalization.</p>

        <mapping>
         <xquery>
          <expression>
           <map><emph>IntegerLiteral</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>
            <emph>IntegerLiteral</emph>
          </expression>
         </core>
        </mapping>

        <mapping>
         <xquery>
          <expression>
           <map><emph>DecimalLiteral</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>
            <emph>DecimalLiteral</emph>
          </expression>
         </core>
        </mapping>

        <mapping>
         <xquery>
          <expression>
           <map><emph>DoubleLiteral</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>
            <emph>DoubleLiteral</emph>
          </expression>
         </core>
        </mapping>

        <mapping>
         <xquery>
          <expression>
           <map><emph>StringLiteral</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>
            <emph>StringLiteral</emph>
          </expression>
         </core>
        </mapping>
      </smnorm>

      <smtype>
        <p>The static type of a literal expression is its
        corresponding atomic type.</p>

	<infergr>
	  <infer>
	    <postjudge>
	      <multiclause>
		<clause><environment> <a href="#xq_stat_env_def" class="env">statEnv</a></environment>
			<expression><emph>IntegerLiteral</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> xs:integer</expression>
		</clause>
	      </multiclause>
	    </postjudge>
	  </infer>
	</infergr>

	<infergr>
	  <infer>
	    <postjudge>
	      <multiclause>
		<clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression><emph>DecimalLiteral</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> xs:decimal</expression>
		</clause>
	      </multiclause>
	    </postjudge>
	  </infer>
	</infergr>

        <infergr>
          <infer>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><emph>DoubleLiteral</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> xs:double</expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <infergr>
          <infer>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><emph>StringLiteral</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> xs:string</expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>
      </smtype>

      <smeval>
        <p>In the dynamic semantics, a literal is evaluated by
        constructing an atomic value in the data model, using the
        <a href="#jd_has_atomic_value" class="judgment"><term>has atomic value</term></a> judgment defined above.</p>

	<infergr>
	  <infer>
 	    <prejudge>
 	      <multiclause>
 	        <clause>
                <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 	        <expression><emph>Literal</emph> <a href="#jd_has_atomic_value" class="judgment"><term>has atomic value</term></a> <a href="#doc-fs-AtomicValue"><emph>AtomicValue</emph></a>
 	        </expression>
 	        </clause>
 	      </multiclause>
 	    </prejudge>
	    <postjudge>
	      <multiclause>
		<clause><environment> <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
			<expression><emph>Literal</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-AtomicValue"><emph>AtomicValue</emph></a></expression>
		</clause>
	      </multiclause>
	    </postjudge>
	  </infer>
	</infergr>
      </smeval>

  </div3>

  <div3 id="id-variables">
    <head>Variable References</head>

    <smintro>
      <p>A <term>variable</term> evaluates to the value to which the
      variable's QName is bound in the <term>dynamic
      context</term>.</p>
    </smintro>

    <scrap headstyle="show">
      <head>Variable References</head>
      <prod num="87 (XQuery)" id="doc-xquery-VarRef"><lhs>VarRef</lhs><rhs>"$"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-VarName" xlink:type="simple">VarName</nt></rhs></prod>
      <prod num="88 (XQuery)" id="doc-xquery-VarName"><lhs>VarName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt></rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar productions for variable references are:</p>
  
      <scrap headstyle="show">
        <head>Primary Expressions</head>
        <prod num="58 (Core)" id="doc-core-VarRef"><lhs>VarRef</lhs><rhs>"$"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-VarName" xlink:type="simple">VarName</nt></rhs></prod>
        <prod num="59 (Core)" id="doc-core-VarName"><lhs>VarName</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt></rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>Variable references are left unchanged through
      normalization.</p>

      <mapping>
       <xquery>
        <expression>
         <map><a href="#prod-core-VarRef"><emph>VarRef</emph></a></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
          <a href="#prod-core-VarRef"><emph>VarRef</emph></a>
        </expression>
       </core>
      </mapping>
    </smnorm>

    <smtype>
      <p>In the static semantics, the type of a variable is simply its
      type in the static environment <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a>:</p>

      <infergr>
    	<infer>
 	  <prejudge>
 	    <multiclause>
 	      <clause>
              <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 	      <expression><emph>VarName</emph> <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a> <emph>Variable</emph>
 	      </expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a>(<emph>Variable</emph>) = <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </prejudge>
 	  <postjudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 		      <expression>$<emph>VarName</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>

      <p>If the variable is not bound in the static environment, a
      static type error is raised.</p>
    </smtype>

    <smeval>
      <p>In the dynamic semantics, a locally declared variable is
      evaluated by "looking up" its value in <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a>:</p>
    
      <infergr>
    	<infer>
 	  <prejudge>
 	    <multiclause>
 	      <clause>
              <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 	      <expression><emph>VarName</emph> <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a> <emph>Variable</emph>
 	      </expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
 	      <clause>
 		<expression><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a>(<emph>Variable</emph>) = <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </prejudge>
 	  <postjudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 		      <expression>$<emph>VarName</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>

      <p>In the dynamic semantics, a reference to a variable imported
      from a module is evaluated by accessing the dynamic context of
      the module in which the variable is declared.</p>

      <p>The notation <emph>AnyURI</emph> <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript> is
      used to access a module context and is defined in <specref ref="id-module-declaration"/>.</p>

      <infergr>
    	<infer>
 	  <prejudge>
 	    <multiclause>
 	      <clause>
              <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 	      <expression><emph>VarName</emph> <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a> <emph>Variable</emph></expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
 	      <clause>
 		<expression><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a>(<emph>Variable</emph>) = <code>#IMPORTED</code>(<emph>AnyURI</emph>)</expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
 	      <clause>
 	      <expression><emph>AnyURI</emph> <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript></expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
 	      <clause>
 		<expression><a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_val_env" class="env">varValue</a>(<emph>Variable</emph>) = <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </prejudge>
 	  <postjudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 		      <expression>$<emph>VarName</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>
    </smeval>

  </div3>

  <div3 id="id-paren-expressions">
    <head>Parenthesized Expressions</head>

    <scrap headstyle="show"> 
      <head/>
      <prod num="89 (XQuery)" id="doc-xquery-ParenthesizedExpr"><lhs>ParenthesizedExpr</lhs><rhs>"("  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Expr" xlink:type="simple">Expr</nt>?  ")"</rhs></prod> 
    </scrap>

    <smcore>
      <p>The Core grammar production for parenthesized expressions is:</p>

      <scrap headstyle="show">
        <head/>
        <prod num="60 (Core)" id="doc-core-ParenthesizedExpr"><lhs>ParenthesizedExpr</lhs><rhs>"("  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Expr" xlink:type="simple">Expr</nt>?  ")"</rhs></prod> 
      </scrap>
    </smcore>

    <p>Empty parentheses <code>()</code> always have the empty
    type. Remember that it is a static type error for most expressions
    other than <code>()</code> to have the empty type (see <specref ref="id-expressions"/> for the complete rule.)</p>

    <smtype>
      <infergr>
    	<infer>
 	  <postjudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 		      <expression>() <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>empty</code></expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>

      <infergr>
    	<infer>
 	  <prejudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 	      <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </prejudge>
 	  <postjudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 		      <expression>( <emph>Expr</emph> ) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>
    </smtype>

    <smeval>
      <p>Empty parentheses <code>()</code> evaluate to the empty
      sequence.</p>

      <infergr>
    	<infer>
 	  <postjudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 		      <expression>() <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> ()</expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>

      <infergr>
    	<infer>
 	  <prejudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 	      <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </prejudge>
 	  <postjudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 		      <expression>( <emph>Expr</emph> ) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>
    </smeval>

  </div3>

         <div3 id="id-context-item-expression">
           <head>Context Item Expression</head>

           <scrap headstyle="show"> <head/><prod num="90 (XQuery)" id="doc-xquery-ContextItemExpr"><lhs>ContextItemExpr</lhs><rhs>"."</rhs></prod> </scrap>

           <smintro>
             <p>A <term>context item expression</term> evaluates to
             the context item, which may be either a node or an atomic
             value.</p>
           </smintro>

         <smnorm>
           <p>A context item expression is normalized to the built-in
           variable <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a>. Because it can only be bound through the
           external context or a path expression, there is no need for
           a specific static typing rule to enforce that its value is
           a singleton item.</p>

           <mapping>
            <xquery>
              <expression><map>.</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
            </xquery>
            <core>
              <expression><a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a></expression>
            </core>
           </mapping>
         </smnorm>

         </div3>

  <div3 id="id-function-calls">
    <head>Function Calls</head>

    <smintro>
      <p>A function call consists of a QName followed by a
      parenthesized list of zero or more expressions.  In [XPath/XQuery],
      the actual argument to a function is called an
      <term>argument</term> and the formal argument of a function is
      called a <term>parameter</term>.  We use the same terminology
      here. </p>
      
      <scrap headstyle="show">
        <head>Function Calls</head>
        <prod num="93 (XQuery)" id="doc-xquery-FunctionCall"><lhs>FunctionCall</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt>  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ExprSingle" xlink:type="simple">ExprSingle</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ExprSingle" xlink:type="simple">ExprSingle</nt>)*)?  ")"</rhs></prod>
      </scrap>

      <p>Because [XPath/XQuery] implicitly converts the values of function
      arguments, a normalization step is required.</p>
    </smintro>

    <smcore>
      <p>The Core grammar production for function calls is:</p>

      <scrap headstyle="show">
        <head>Function Calls</head>
        <prod num="63 (Core)" id="doc-core-FunctionCall"><lhs>FunctionCall</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt>  "("  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ExprSingle" xlink:type="simple">ExprSingle</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ExprSingle" xlink:type="simple">ExprSingle</nt>)*)?  ")"</rhs></prod>
      </scrap>
    </smcore>
 
    <smnotation>
      <p id="jd_map_function_argument">Normalization of function calls
      uses an auxiliary mapping <map/><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<a href="#doc-fs-Type"><emph>Type</emph></a>)</subscript></a>
      used to insert conversions of function arguments that depend
      only on the expected <a href="#doc-fs-Type"><emph>Type</emph></a> of the corresponding
      parameters. It is defined as follows:</p>

      <mapping>
       <xquery>
         <expression><map><emph>Expr</emph></map><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<a href="#doc-fs-Type"><emph>Type</emph></a>)</subscript></a></expression>
       </xquery>
       <core>
         <expression><map><map><map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></map><a href="#jd_map_function_argument" class="judgment"><subscript>AtomizeAtomic(<a href="#doc-fs-Type"><emph>Type</emph></a>)</subscript></a></map><a href="#jd_map_function_argument" class="judgment"><subscript>Convert(<a href="#doc-fs-Type"><emph>Type</emph></a>)</subscript></a></expression>
       </core>
      </mapping>

      <p>where</p>

        <ulist>

          <item>
            <p><map><emph>Expr</emph></map><a href="#jd_map_function_argument" class="judgment"><subscript>AtomizeAtomic(<a href="#doc-fs-Type"><emph>Type</emph></a>)</subscript></a> denotes</p>
            <table>
              <tbody>
                <tr>
                  <td rowspan="1" colspan="1">If
                  </td>

                  <td rowspan="1" colspan="1"><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#dt-xs_anyAtomicType"><code>xs:anyAtomicType</code></a>*</td>
                </tr>
                <tr>
                  <td rowspan="1" colspan="1">Then</td>
                  <td rowspan="1" colspan="1"><code>fn:data</code>(<emph>Expr</emph>)</td><td rowspan="1" colspan="1">  </td>
                </tr>
                <tr>
                  <td rowspan="1" colspan="1">Else</td>
                  <td rowspan="1" colspan="1"><emph>Expr</emph></td><td rowspan="1" colspan="1">  </td>
                </tr>
              </tbody>
            </table>

            <p>which specifies that if the function expects atomic
            parameters, then <code>fn:data</code> is called to obtain them.</p>
          </item>

          <item>
            <p><map><emph>Expr</emph></map><a href="#jd_map_function_argument" class="judgment"><subscript>Convert(<a href="#doc-fs-Type"><emph>Type</emph></a>)</subscript></a> denotes</p>
            <table>
              <tbody>
                <tr>
                  <td rowspan="1" colspan="1">If</td>
                  <td rowspan="1" colspan="1"><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#dt-xs_anyAtomicType"><code>xs:anyAtomicType</code></a>*</td>
                </tr>
                <tr>
                  <td rowspan="1" colspan="1">Then</td>
                  <td rowspan="1" colspan="1"><a href="#sec_convert_simple_operand"><emph>fs:</emph><code>convert-simple-operand</code></a>(<emph>Expr</emph>,<emph>PrototypicalValue</emph>)</td>
                </tr>
                <tr>
                  <td rowspan="1" colspan="1">Else</td>
                  <td rowspan="1" colspan="1"><emph>Expr</emph></td>
                </tr>
              </tbody>
            </table>

          <p>where <emph>PrototypicalValue</emph> is a built-in atomic
          value used to encode the expected atomic type (for instance
          the value <code>1.0</code> if the expected type is
          <code>xs:decimal</code>). A value is used here since [XPath/XQuery]
          expressions cannot operate directly on types. Which value is
          chosen does not have any impact on the actual semantics,
          only its actual atomic type matters.</p>
        </item>
      </ulist>

    </smnotation>

      <smnote>
        <p>The <a href="#sec_convert_simple_operand"><emph>fs:</emph><code>convert-simple-operand</code></a> function takes a
        <emph>PrototypicalValue</emph>, which is a value of the target
        type, to ensure that conversion to base types is possible even
        though types are not first class objects in [XPath/XQuery]. Also,
        note that in the case of built-in functions where the expected
        type is specified as numeric, the prototypical value is a
        value of type <code>xs:double</code>.</p>
      </smnote>

      <smnorm>
        <p>Each argument expression in a function call is normalized
        to its corresponding Core expression by applying
        <map/><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<a href="#doc-fs-Type"><emph>Type</emph></a>)</subscript></a> for each argument with
        the expected <emph>SequenceType</emph> for the argument inserted.</p>

         <infergr>
          <infer>
           <prejudge>
            <multiclause>
              <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><emph>QName</emph> <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
              </clause>
            </multiclause>
            <multiclause>
             <clause>
              <expression>
                <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_defn" class="env">typeDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = define type <emph>QName</emph><subscript>2</subscript> <a href="#doc-fs-AtomicTypeDerivation"><emph>AtomicTypeDerivation</emph></a>
              </expression>
             </clause>
            </multiclause>
            <multiclause>
              <clause>
                <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>,n) =
                  declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>) as <a href="#doc-fs-Type"><emph>Type</emph></a>
                </expression>
              </clause>
            </multiclause>
           </prejudge>
          <postjudge>
           <clause>
            <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
            <expression>
              <map><emph>QName</emph> (<emph>Expr</emph><subscript>1</subscript>, ..., <emph>Expr</emph><subscript>n</subscript>)</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
              =
              <emph>QName</emph>
              ( <map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>)</subscript></a>, ...,
                <map><emph>Expr</emph><subscript>n</subscript></map><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)</subscript></a> )
            </expression>
           </clause>
          </postjudge>
         </infer>
         </infergr>

        <mapping>
          <xquery>
            <expression>
              
            </expression>
          </xquery>
          <core>
            <expression>
              <emph>QName</emph>
              ( <map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>)</subscript></a>, ...,
                <map><emph>Expr</emph><subscript>n</subscript></map><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)</subscript></a> )
            </expression>
          </core>
        </mapping>

        <p>Note that this normalization rule depends on the function
        signatures, which is used to get the types of the function
        parameters (<emph>SequenceType</emph><subscript>1</subscript>,...,<emph>SequenceType</emph><subscript>n</subscript>). For
        user-defined functions, the function signature can be obtained
        from the XQuery prolog where the function is declared.  For
        built-in functions, the signature is given in the
        <bibref ref="xpath-functions"/> document. For overloaded built-in functions,
        several signatures may exists, however, because they all
        correspond to sequences of atomic values, they all result in
        the same normalization.</p>
      </smnorm>

      <smtype>
        <p>Different sets of static typing rules are used to type
        check function calls depending on which of the following
        categories the belong to: overloaded internal functions,
        built-in functions with a specific static typing rule, and
        other built-in and user-defined functions.</p>

        <p>The following two rules are common to all those categories,
        and are used to bootstrap type inference, by first looking-up
        the expanded QName for the function, then applying the
        appropriate set of static typing rules depending on the
        category in which the function is.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>QName</emph>
                  <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>() <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <emph>QName</emph>() <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>QName</emph>
                  <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment> 
                  <expression>
                    <emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause>
                  <expression>
                    ...
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment> 
                  <expression>
                    <emph>Expr</emph><subscript>n</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <emph>QName</emph> (<emph>Expr</emph><subscript>1</subscript>,...,<emph>Expr</emph><subscript>n</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <p>The following depends on the kind of function call.</p>

        <olist>
          <item>
            <p>If the expanded QName for the function corresponds to
            one of the overloaded internal <emph>fs:</emph> functions
            listed in <specref ref="sec_operators"/>, the static
            typing rules in <specref ref="sec_operators"/> are
            applied.</p>
          </item>
          <item>
            <p>If the expanded QName for the function corresponds to
            one of the built-in functions with a specialized static
            typing rule, listed in <specref ref="sec_special_functions"/>, the static typing rules in
            <specref ref="sec_special_functions"/> are applied.</p>
          </item>
          <item><p>Otherwise, the following general static typing
            rules are applied.</p></item>
        </olist>

        <p>The two following rules look up the function in the static
        environment and check that some signature for the function
        satisfies the following constraint: the type of each actual
        argument is a subtype of some type that can be promoted to the
        type of the corresponding function parameter.  In this case,
        the function call is well typed and the result type is the
        return type specified in the function's signature. </p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause>
                  <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>,0) =
                    declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>() as <a href="#doc-fs-Type"><emph>Type</emph></a>'
                  </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>() <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>'
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause>
                  <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>,n) =
                    declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>', ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>') as <a href="#doc-fs-Type"><emph>Type</emph></a>'
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause><environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                        <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_can_be_promoted_to" class="judgment"><term>can be promoted to</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>'</expression></clause>
              </multiclause>
              <multiclause>
                <clause><expression>...</expression></clause>
              </multiclause>
              <multiclause>
                <clause><environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                        <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript> <a href="#jd_can_be_promoted_to" class="judgment"><term>can be promoted to</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>'</expression></clause>
              </multiclause>

            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>) <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>'
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <p>The function body itself is not analyzed for each
        invocation: static typing of the function definition itself
        guarantees that the function body always returns a value of
        the declared return type.</p>

        <p>Notice that the static context contains at most one
        function declaration for each function. This is possible since
        the treatment of overloaded operators is done through a set of
        specific static typing rules which do not require access to
        the environment. See <specref ref="sec_operators"/>.</p>
      </smtype>

      <smnotation>
        <p id="jd_function_with_types">The following auxiliary
        judgment</p>

        <display>
         <clause>
          <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
          <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
          </expression>
         </clause>
        </display>

        <p>holds when applying the function with expanded QName
        <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> and no parameter yields the value <a href="#doc-fs-Value"><emph>Value</emph></a>.</p>

        <display>
         <clause>
          <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
          <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a> (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
          </expression>
         </clause>
        </display>

        <p>holds when applying the function with expanded QName
        <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>, and parameters of type (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)
        on the values (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>) yields the value
        <a href="#doc-fs-Value"><emph>Value</emph></a>.</p>

        <p>That judgment is defined below for each kind of function
        (user-defined, built-in, external, and imported
        functions).</p>
      </smnotation>

      <smeval>
        <p>The following rules apply to all the different kinds of
        functions using the previously defined judgment.</p>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><emph>QName</emph> <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = <emph>FunctionSig</emph>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <emph>FunctionSig</emph>
          =
          declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>() as <a href="#doc-fs-Type"><emph>Type</emph></a>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>'</expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
         <expression>
          <emph>QName</emph>() <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>'
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
          <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
          <emph>Expr</emph><subscript>n</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><emph>QName</emph> <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = <emph>FunctionSig</emph>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <emph>FunctionSig</emph>
          =
          declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>) as <a href="#doc-fs-Type"><emph>Type</emph></a>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a> (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>',...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>')
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>'</expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
         <expression>
          <emph>QName</emph> ( <emph>Expr</emph><subscript>1</subscript>, ...,  <emph>Expr</emph><subscript>n</subscript> ) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>'
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <p>First the function name is expanded, and the expanded name is
    used to retrieve the function signature from the static
    environment. Then, the rule evaluates each function argument
    expression, and the resulting values are promoted according to the
    expected type for the function. The result of evaluating the
    function is obtained through the auxiliary judgment previously
    defined, and the resulting value is promoted according to the
    expected return type.</p>

    <p>In case the function is a user defined function in a main
    module, the expression body is retrieved from the dynamic
    environment and used to compute the value of the function. The
    rule extends <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a> by binding each formal variable to its
    corresponding value, and evaluates the body of the function in the
    new environment. The resulting value is the value of the function
    call.</p>

    <p>The notation <emph>AnyURI</emph> <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript> is used
    to access a module context and is defined in <specref ref="id-module-declaration"/>.</p>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>()
          =
          (<emph>Expr</emph>)
         </expression>
        </clause>
       </multiclause>
       <multiclause>
 	<clause>
 	 <expression>#MAIN <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript></expression>
 	</clause>
       </multiclause>
       <multiclause>
        <clause>
         <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_val_env" class="env">varValue</a></environment>
         <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> </expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>))
          =
          (<emph>Expr</emph>, <emph>Variable</emph><subscript>1</subscript>, ... , <emph>Variable</emph><subscript>n</subscript>)
         </expression>
        </clause>
       </multiclause>
       <multiclause>
 	<clause>
 	 <expression>#MAIN <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript></expression>
 	</clause>
       </multiclause>
       <multiclause>
        <clause>
         <environment>
          <update>
           <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_val_env" class="env">varValue</a></environment>
           <expression>
            <emph>Variable</emph><subscript>1</subscript> =&gt; <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>;
            ...;
            <emph>Variable</emph><subscript>n</subscript> =&gt; <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript></expression>
          </update>
         </environment>
         <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> </expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a> (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <p>Note that the function body is evaluated in the dynamic
    environment containing the main module declarations.</p>

    <p>The rule for evaluating an function imported from a module is
    similar to that for evaluating a user-defined function in a main
    module, except that the function call is evaluated in the dynamic
    context of the module in which it is declared, and that the
    appropriate additional type matching must be performed.</p>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>())
          = <code>#IMPORTED</code>(<emph>AnyURI</emph>)
         </expression>
        </clause>
       </multiclause>
       <multiclause>
 	<clause>
 	 <expression><emph>AnyURI</emph> <a href="#jd_module_statEnv" class="judgment"><term>=&gt;</term><subscript>module_statEnv</subscript></a> <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript></expression>
 	</clause>
       </multiclause>
       <multiclause>
 	<clause>
 	 <expression><emph>AnyURI</emph> <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript></expression>
 	</clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a><subscript>1</subscript>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = <emph>FunctionSig</emph>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <emph>FunctionSig</emph>'
          =
          declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>() as <a href="#doc-fs-Type"><emph>Type</emph></a>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>()
          =
          (<emph>Expr</emph>)
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_val_env" class="env">varValue</a></environment>
         <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>'
         </expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>))
          = <code>#IMPORTED</code>(<emph>AnyURI</emph>)
         </expression>
        </clause>
       </multiclause>
       <multiclause>
 	<clause>
 	 <expression><emph>AnyURI</emph> <a href="#jd_module_statEnv" class="judgment"><term>=&gt;</term><subscript>module_statEnv</subscript></a> <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript></expression>
 	</clause>
       </multiclause>
       <multiclause>
 	<clause>
 	 <expression><emph>AnyURI</emph> <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript></expression>
 	</clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a><subscript>1</subscript>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = <emph>FunctionSig</emph>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <emph>FunctionSig</emph>'
          =
          declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>', ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>') as <a href="#doc-fs-Type"><emph>Type</emph></a>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>', ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>'))
          =
          (<emph>Expr</emph>, <emph>Variable</emph><subscript>1</subscript>, ... , <emph>Variable</emph><subscript>n</subscript>)
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <environment>
          <update>
           <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_val_env" class="env">varValue</a></environment>
           <expression>
            <emph>Variable</emph><subscript>1</subscript> =&gt; <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>;
            ...;
            <emph>Variable</emph><subscript>n</subscript> =&gt; <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript></expression>
          </update>
         </environment>
         <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>'
         </expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a> (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <p>If the function is a built-in function (resp. special formal
    semantics function), the value returned by the function is the one
    specified in <bibref ref="xpath-functions"/> (resp. <specref ref="sec_special_functions"/>).</p>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>())
          = <code>#BUILT-IN</code>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
           The built-in function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> (See <bibref ref="xpath-functions"/> or
           <specref ref="sec_special_functions"/>) yields the value
           <a href="#doc-fs-Value"><emph>Value</emph></a>
          </expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>))
          = <code>#BUILT-IN</code>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
           The built-in function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> (See <bibref ref="xpath-functions"/> or
           <specref ref="sec_special_functions"/>) applied to values
           (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>) yields the value <a href="#doc-fs-Value"><emph>Value</emph></a>
          </expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a> (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <p>If the function is an external function, the value returned by
    the function is implementation-defined.</p>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>()) = <code>#EXTERNAL</code>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
           The external function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> yields the value
           <a href="#doc-fs-Value"><emph>Value</emph></a>
          </expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>))
          = <code>#EXTERNAL</code>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
           The external function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> applied to values
           (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>) yields the value <a href="#doc-fs-Value"><emph>Value</emph></a>
          </expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
           <a href="#jd_function_with_types" class="judgment"><term>function</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
           <a href="#jd_function_with_types" class="judgment"><term>with types</term></a> (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>on values</term></a>
           (<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>,...,<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>)
           <a href="#jd_function_with_types" class="judgment"><term>yields</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>
   </smeval>

  </div3>

  </div2>

  <div2 id="id-path-expressions">
    <head>Path Expressions</head>

    <smintro>
      <p>Path expressions are used to locate nodes within a
      tree. There are two kinds of path expressions, absolute path
      expressions and relative path expressions. An absolute path
      expression is a rooted relative path expression. A relative path
      expression is composed of a sequence of steps.</p>
  
      <scrap headstyle="show">
        <head>Path Expressions</head>
        <prod num="68 (XQuery)" id="doc-xquery-PathExpr"><lhs>PathExpr</lhs><rhs>("/"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-RelativePathExpr" xlink:type="simple">RelativePathExpr</nt>?)<br/>|  ("//"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-RelativePathExpr" xlink:type="simple">RelativePathExpr</nt>)<br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-RelativePathExpr" xlink:type="simple">RelativePathExpr</nt></rhs></prod>
        <prod num="69 (XQuery)" id="doc-xquery-RelativePathExpr"><lhs>RelativePathExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-StepExpr" xlink:type="simple">StepExpr</nt>  (("/"  |  "//")  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-StepExpr" xlink:type="simple">StepExpr</nt>)*</rhs></prod>
      </scrap>

    </smintro>

    <smcore>
      <p>PathExpr and RelativePathExpr are fully normalized, therefore
      they have no corresponding productions in the Core. The grammar
      for path expressions in the Core starts with the StepExpr
      production.</p>
    </smcore>

    <smnorm>
      <p>Absolute path expressions are path expressions starting with
      the <code>/</code> or <code>//</code> symbols, indicating that
      the expression must be applied on the root node in the current
      context.  The root node in the current context is the greatest
      ancestor of the context node. The following two rules normalize
      absolute path expressions to relative ones. They use the
      <code>fn:root</code> function, which returns the greatest ancestor of its
      argument node. The treat expressions guarantee that the value
      bound to the context variable <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> is a document node.</p>

      <mapping>
       <xquery>
         <expression><map>/</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </xquery>
       <core>
         <expression><map>(<code>fn:root</code>(self::node()) treat as document-node())</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </core>
      </mapping>

      <mapping>
       <xquery>
         <expression><map>/ <emph>RelativePathExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </xquery>
       <core>
         <expression><map>((<code>fn:root</code>(self::node())) treat as document-node()) /
         <emph>RelativePathExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </core>
      </mapping>

      <mapping>
       <xquery>
         <expression><map>// <emph>RelativePathExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </xquery>
       <core>
         <expression><map>((<code>fn:root</code>(self::node())) treat as document-node()) /
         <code>descendant-or-self::node()</code> /
         <emph>RelativePathExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </core>
      </mapping>

      <mapping>
        <xquery>
          <expression><map><emph>RelativePathExpr</emph> // <emph>StepExpr</emph> </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
        </xquery>
        <core>
          <expression><map><emph>RelativePathExpr</emph> / descendant-or-self::node() / <emph>StepExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
        </core>
      </mapping>

      <p>A composite relative path expression (using <code>/</code>)
      is normalized into a <code>for</code> expression by
      concatenating the sequences obtained by mapping each node of the
      left-hand side in document order to the sequence it generates on
      the right-hand side.  The call to the <a href="#sec_distinct_docorder"><emph>fs:</emph><code>distinct-doc-order</code></a>
      function ensures that the result is in document order without
      duplicates.  The dynamic context is defined by binding the
      <code>$</code><emph>fs:</emph><code>dot</code>,
      <code>$</code><emph>fs:</emph><code>sequence</code>,
      <code>$</code><emph>fs:</emph><code>position</code> and
      <code>$</code><emph>fs:</emph><code>last</code> variables.</p>

      <p>Note that sorting by document order enforces the restriction
      that input and output sequences contains only nodes, and that
      the last step in a path expression may actually return atomic
      values.</p>

      <mapping>
       <xquery>
        <expression><map><emph>RelativePathExpr</emph> / <emph>StepExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
       </xquery>
       <core>
        <expression>
         <table><tbody>
           <tr><td rowspan="1" colspan="1"><a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a> (</td></tr>
           <tr><td rowspan="1" colspan="1"><a href="#sec_distinct_docorder_or_atomic_sequence"><emph>fs:</emph><code>distinct-doc-order-or-atomic-sequence</code></a> (</td></tr>
           <tr><td rowspan="1" colspan="1">  let <code>$</code><emph>fs:</emph><code>sequence</code> as node()* := <map><emph>RelativePathExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>   return</td></tr>  
           <tr><td rowspan="1" colspan="1">  let <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a> := fn:count(<code>$</code><emph>fs:</emph><code>sequence</code>)   return</td></tr>  
           <tr><td rowspan="1" colspan="1">  for <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> at <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> in <code>$</code><emph>fs:</emph><code>sequence</code> return</td></tr>  
           <tr><td rowspan="1" colspan="1">    <map><emph>StepExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></td></tr>
           <tr><td rowspan="1" colspan="1">))</td></tr>
         </tbody></table>
        </expression>
       </core>
      </mapping>

    </smnorm>

    <div3 id="id-axis-steps">
      <head>Steps</head>

      <p>Note that this section uses some auxiliary judgments which
      are defined in <specref ref="sec_auxiliary_xpath"/>.</p>

      <smintro>
        <scrap headstyle="show">
          <head>Steps</head>
          <prod num="70 (XQuery)" id="doc-xquery-StepExpr"><lhs>StepExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-FilterExpr" xlink:type="simple">FilterExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AxisStep" xlink:type="simple">AxisStep</nt></rhs></prod>
          <prod num="71 (XQuery)" id="doc-xquery-AxisStep"><lhs>AxisStep</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ReverseStep" xlink:type="simple">ReverseStep</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ForwardStep" xlink:type="simple">ForwardStep</nt>)  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PredicateList" xlink:type="simple">PredicateList</nt></rhs></prod>
          <prod num="72 (XQuery)" id="doc-xquery-ForwardStep"><lhs>ForwardStep</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ForwardAxis" xlink:type="simple">ForwardAxis</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-NodeTest" xlink:type="simple">NodeTest</nt>)  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AbbrevForwardStep" xlink:type="simple">AbbrevForwardStep</nt></rhs></prod>
          <prod num="75 (XQuery)" id="doc-xquery-ReverseStep"><lhs>ReverseStep</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ReverseAxis" xlink:type="simple">ReverseAxis</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-NodeTest" xlink:type="simple">NodeTest</nt>)  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AbbrevReverseStep" xlink:type="simple">AbbrevReverseStep</nt></rhs></prod>
          <prod num="82 (XQuery)" id="doc-xquery-PredicateList"><lhs>PredicateList</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Predicate" xlink:type="simple">Predicate</nt>*</rhs></prod>
        </scrap>
      </smintro>

      <smcore>
        <p>The Core grammar productions for XPath steps are:</p>
    
        <scrap headstyle="show">
          <head>Steps</head>
          <prod num="46 (Core)" id="doc-core-StepExpr"><lhs>StepExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-PrimaryExpr" xlink:type="simple">PrimaryExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AxisStep" xlink:type="simple">AxisStep</nt></rhs></prod>
          <prod num="47 (Core)" id="doc-core-AxisStep"><lhs>AxisStep</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ReverseStep" xlink:type="simple">ReverseStep</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ForwardStep" xlink:type="simple">ForwardStep</nt></rhs></prod>
          <prod num="48 (Core)" id="doc-core-ForwardStep"><lhs>ForwardStep</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ForwardAxis" xlink:type="simple">ForwardAxis</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-NodeTest" xlink:type="simple">NodeTest</nt></rhs></prod>
          <prod num="50 (Core)" id="doc-core-ReverseStep"><lhs>ReverseStep</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ReverseAxis" xlink:type="simple">ReverseAxis</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-NodeTest" xlink:type="simple">NodeTest</nt></rhs></prod>
        </scrap>
      </smcore>

      <smnote>
        <p>Step expressions can be followed by
        predicates. Normalization of predicates uses the following
        auxiliary mapping rule: <map/><a href="#jd_map_predicates" class="judgment"><subscript>Predicates</subscript></a>, which
        is specified in <specref ref="id-predicates"/>. Normalization
        for step expressions also uses the following auxiliary mapping
        rule: <map/><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a>, which is specified in <specref ref="sec_axes"/>.</p>
      </smnote>

      <smnorm>
        <p>Normalization of predicates need to distinguish between
        forward steps, reverse steps, and primary expressions.</p>
  
        <p>As explained in the [XPath/XQuery] document, applying a step in
        XPath changes the focus (or context). The change of focus is
        made explicit by the normalization rule below, which binds the
        variable <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> to the node currently being processed, and
        the variable <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> to the position (i.e., the position
        within the input sequence) of that node.</p>

        <p>There are two sets of normalization rules for Predicates.
        The first set of rules apply when the predicate is a numeric
        literal or the expression <code>last()</code>.  The second set
        of rules apply to all predicate expressions other than numeric
        literals and the expression <code>last()</code>.  In the first
        case, the normalization rules provides a more precise static
        type than if the general rules were applied.</p>

        <p>When the predicate expression is a numeric literal or the
        <code>fn:last</code> function, the following normalization rules
        apply.</p>
  
        <mapping>
          <xquery>
            <expression><map><emph>ForwardStep</emph> <emph>PredicateList</emph> [
            <emph>NumericLiteral</emph> ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
            <expression>
              <table><tbody>
                <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> :=
                <a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a>(<a href="#sec_distinct_docorder"><emph>fs:</emph><code>distinct-doc-order</code></a>( <map><emph>ForwardStep</emph>
                <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> )) return</td></tr>
<tr><td rowspan="1" colspan="1"><code>fn:subsequence</code>(<code>$</code><emph>fs:</emph><code>sequence</code>,<emph>NumericLiteral</emph>,1)</td></tr>
              </tbody></table>
            </expression>
          </core>
        </mapping>
  
        <mapping>
          <xquery>
            <expression><map><emph>ForwardStep</emph> <emph>PredicateList</emph> [
            <code>fn:last</code>() ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
            <expression>
              <table><tbody>
                <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> :=
                <a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a>(<a href="#sec_distinct_docorder"><emph>fs:</emph><code>distinct-doc-order</code></a>( <map><emph>ForwardStep</emph>
                <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> )) return</td></tr>
                <tr><td rowspan="1" colspan="1">let <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a> := <code>fn:count</code>(<code>$</code><emph>fs:</emph><code>sequence</code>) return</td></tr>
<tr><td rowspan="1" colspan="1"><code>fn:subsequence</code>(<code>$</code><emph>fs:</emph><code>sequence</code>,<a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a>,1)</td></tr>
              </tbody></table>
            </expression>
          </core>
        </mapping>
  
        <p>When predicates are applied on a reverse step, the position
        variable is bound in reverse document order. </p>
  
        <mapping>
          <xquery>
            <expression><map><emph>ReverseStep</emph> <emph>PredicateList</emph> [
            <emph>NumericLiteral</emph> ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
          <expression>
             <table><tbody>
                <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> :=
                <a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a>(<a href="#sec_distinct_docorder"><emph>fs:</emph><code>distinct-doc-order</code></a>( <map><emph>ReverseStep</emph> <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> )) return</td></tr>
                <tr><td rowspan="1" colspan="1">let <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a> := fn:count(<code>$</code><emph>fs:</emph><code>sequence</code>) return</td></tr>
                <tr><td rowspan="1" colspan="1">let <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> := <emph>fs:</emph><code>plus</code>(1, <emph>fs:</emph><code>minus</code>(<a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a>,<emph>NumericLiteral</emph>)) return</td></tr>
                <tr><td rowspan="1" colspan="1">  <code>fn:subsequence</code>(<code>$</code><emph>fs:</emph><code>sequence</code>,<a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a>,1)</td></tr>
             </tbody></table>
           </expression>
          </core>
        </mapping>

        <p>When the step is a reverse axis, then the last item in the
        context sequence is the first in document order.</p>

        <mapping>
          <xquery>
            <expression><map><emph>ReverseStep</emph> <emph>PredicateList</emph> [
            <code>fn:last</code>() ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
          <expression>
             <table><tbody>
                <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> :=
                <a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a>(<a href="#sec_distinct_docorder"><emph>fs:</emph><code>distinct-doc-order</code></a>( <map><emph>ReverseStep</emph> <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> )) return</td></tr>
                <tr><td rowspan="1" colspan="1">  <code>fn:subsequence</code>(<code>$</code><emph>fs:</emph><code>sequence</code>,1,1)</td></tr>
             </tbody></table>
           </expression>
          </core>
        </mapping>
  
        <p>The normalization rules above all use the function
        <code>fn:subsequence</code> to select a particular item.  The static
        typing rules for this function are defined in <specref ref="sec_fn_subsequence"/>.</p>
  
        <p>When predicates are applied on a forward step, the input
        sequence is first sorted in document order and duplicates are
        removed. The context is changed by binding the <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a>
        variable to each node in document order.</p>
  
        <mapping>
          <xquery>
            <expression><map><emph>ForwardStep</emph> <emph>PredicateList</emph> [
            <emph>Expr</emph> ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
            <expression>
              <table><tbody>
                <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> :=
                <a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a>(<a href="#sec_distinct_docorder"><emph>fs:</emph><code>distinct-doc-order</code></a>( <map><emph>ForwardStep</emph>
                <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> )) return</td></tr>
                <tr><td rowspan="1" colspan="1">let <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a> := fn:count(<code>$</code><emph>fs:</emph><code>sequence</code>) return</td></tr>
                <tr><td rowspan="1" colspan="1">for <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> at <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> in <code>$</code><emph>fs:</emph><code>sequence</code> return</td></tr>
                <tr><td rowspan="1" colspan="1">   if (<map><emph>Expr</emph></map><a href="#jd_map_predicates" class="judgment"><subscript>Predicates</subscript></a>) then <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> else ()</td></tr>
              </tbody></table>
            </expression>
          </core>
        </mapping>
  
        <p>When predicates are applied on a reverse step, the input
        sequence is first sorted in document order and duplicates are
        removed. The context is changed by binding the <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a>
        variable to each node in document order. </p>

        <mapping>
          <xquery>
            <expression><map><emph>ReverseStep</emph> <emph>PredicateList</emph> [
            <emph>Expr</emph> ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
          <expression>
             <table><tbody>
               <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> := <a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a>(<a href="#sec_distinct_docorder"><emph>fs:</emph><code>distinct-doc-order</code></a>(
               <map><emph>ReverseStep</emph> <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> )) return</td></tr>
               <tr><td rowspan="1" colspan="1">let <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a> := fn:count(<code>$</code><emph>fs:</emph><code>sequence</code>) return</td></tr>
               <tr><td rowspan="1" colspan="1">for <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> at $<emph>fs:</emph>new in <code>$</code><emph>fs:</emph><code>sequence</code> return</td></tr>
               <tr><td rowspan="1" colspan="1">let <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> := <emph>fs:</emph><code>plus</code>(1,<emph>fs:</emph><code>minus</code>(<a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a>,$<emph>fs:</emph>new)) return </td></tr>
               <tr><td rowspan="1" colspan="1">  if (<map><emph>Expr</emph></map><a href="#jd_map_predicates" class="judgment"><subscript>Predicates</subscript></a>)
                            then <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> else ()</td></tr>
             </tbody></table>
           </expression>
          </core>
        </mapping>
  

        <p>Finally, a stand-alone forward or reverse step is
        normalized by the auxiliary normalization rule for
        <emph>Axis</emph>.</p>
  
        <mapping>
          <xquery>
            <expression><map><emph>ForwardStep</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
            <expression><a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a>(<map><emph>ForwardStep</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a>)</expression>
          </core>
        </mapping>

        <mapping>
          <xquery>
            <expression><map><emph>ReverseStep</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
            <expression><a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a>(<map><emph>ReverseStep</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a>)</expression>
          </core>
        </mapping>
      </smnorm>

      <smtype>
        <p>The static semantics of an <emph>Axis</emph> <emph>NodeTest</emph> pair is
        obtained by retrieving the type of the context node, and
        applying the two filters (the <emph>Axis</emph>, and then the
        <emph>NodeTest</emph> with a <emph>PrincipalNodeKind</emph>) on the
        result.</p>

        <infergr>
         <infer>
          <prejudge>
           <multiclause>
            <clause>
             <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a>((<code>FS-URI</code>,<quote><code>dot</code></quote>)) = <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
            </clause>
           </multiclause>
           <multiclause>
            <clause>
             <environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
             <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <map>node()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a></expression>
            </clause>
           </multiclause>
           <multiclause>
            <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
             <expression>
              <a href="#jd_axis_type" class="judgment"><term>axis</term></a> <emph>Axis</emph> <a href="#jd_axis" class="judgment"><term>of</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_axis_type" class="judgment"><term>:</term></a>
              <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>
             </expression>
            </clause>
           </multiclause>
           <multiclause>
            <clause>
             <expression><emph>Axis</emph> <a href="#jd_principal" class="judgment"><term>has principal</term></a> <emph>PrincipalNodeKind</emph></expression>
            </clause>
           </multiclause>
           <multiclause>
            <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
             <expression>
              <a href="#jd_test_type" class="judgment"><term>test</term></a> <emph>NodeTest</emph> <a href="#jd_test_type" class="judgment"><term>with</term></a>
              <emph>PrincipalNodeKind</emph> <a href="#jd_test_type" class="judgment"><term>of</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>
              <a href="#jd_test_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>3</subscript>
             </expression>
            </clause>
           </multiclause>
          </prejudge>
          <postjudge>
           <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
            <expression>
             <emph>Axis</emph> <emph>NodeTest</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>3</subscript>
            </expression>
           </clause>
          </postjudge>
         </infer>
        </infergr>

        <smnote>
          <p>Note that the second judgment in the rule requires that
          the context item be a node, guaranteeing that a type error
          is raised when the context item is an atomic value.</p>
        </smnote>
      
      </smtype>
      
      <smeval>
        <p>The dynamic semantics of an <emph>Axis</emph> <emph>NodeTest</emph> pair is
        obtained by retrieving the context node, and applying the two
        filters (<emph>Axis</emph>, then <emph>NodeTest</emph>) on the result. The
        application of each filter is expressed through several
        auxiliary judgments (<a href="#jd_axis" class="judgment"><term>of</term></a>, <a href="#jd_principal" class="judgment"><term>has principal</term></a>, and
        <a href="#jd_test" class="judgment"><term>test</term></a>), as follows.</p>
      
        <infergr>
         <infer>
          <prejudge>
           <multiclause>
            <clause>
             <expression><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a>(<a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a>) = <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript></expression>
            </clause>
           </multiclause>
           <multiclause>
            <clause>
             <environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
             <expression><a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <map>node()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a></expression>
            </clause>
           </multiclause>
           <multiclause>
            <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
             <expression>
              <a href="#jd_axis" class="judgment"><term>axis</term></a> <emph>Axis</emph> <a href="#jd_axis" class="judgment"><term>of</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
              <a href="#jd_axis" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript>
             </expression>
            </clause>
           </multiclause>
           <multiclause>
            <clause>
             <expression><emph>Axis</emph> <a href="#jd_principal" class="judgment"><term>has principal</term></a> <emph>PrincipalNodeKind</emph></expression>
            </clause>
           </multiclause>
           <multiclause>
            <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
             <expression>
              <a href="#jd_test" class="judgment"><term>test</term></a> <emph>NodeTest</emph> <a href="#jd_test" class="judgment"><term>with</term></a> <emph>PrincipalNodeKind</emph>
              <a href="#jd_test" class="judgment"><term>of</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> <a href="#jd_test" class="judgment"><term>=&gt;</term></a>
              <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>3</subscript></expression>
            </clause>
           </multiclause>
          </prejudge>
          <postjudge>
           <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
            <expression>
             <emph>Axis</emph> <emph>NodeTest</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#sec_distinct_docorder"><emph>fs:</emph><code>distinct-doc-order</code></a>(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>3</subscript>)
            </expression>
           </clause>
          </postjudge>
         </infer>
        </infergr>
      
        <smnote>
          <p>Note that the second judgment in the rule guarantees that
          the context item is bound to a node.</p>
        </smnote>
      
      </smeval>
      
      <div4 id="sec_axes">
        <head>Axes</head>

        <smintro>
          <p>The XQuery grammar for forward and reverse axis is as
          follows.</p>

          <scrap headstyle="show">
            <head>Axes</head>
            <prod num="73 (XQuery)" id="doc-xquery-ForwardAxis"><lhs>ForwardAxis</lhs><rhs>("child"  "::")<br/>|  ("descendant"  "::")<br/>|  ("attribute"  "::")<br/>|  ("self"  "::")<br/>|  ("descendant-or-self"  "::")<br/>|  ("following-sibling"  "::")<br/>|  ("following"  "::")</rhs></prod>
            <prod num="76 (XQuery)" id="doc-xquery-ReverseAxis"><lhs>ReverseAxis</lhs><rhs>("parent"  "::")<br/>|  ("ancestor"  "::")<br/>|  ("preceding-sibling"  "::")<br/>|  ("preceding"  "::")<br/>|  ("ancestor-or-self"  "::")</rhs></prod>
          </scrap>

          <p>In the case of XPath, forward axis also contain the
          <code>namespace::</code> axis.</p>

          <scrap headstyle="show">
            <head>Axes</head>
            <prod num="30 (XPath)" id="noid_N1492C.doc-xpath-ForwardAxis"><lhs>ForwardAxis</lhs><rhs>("child"  "::")<br/>|  ("descendant"  "::")<br/>|  ("attribute"  "::")<br/>|  ("self"  "::")<br/>|  ("descendant-or-self"  "::")<br/>|  ("following-sibling"  "::")<br/>|  ("following"  "::")<br/>|  ("namespace"  "::")</rhs></prod>
          </scrap>

        </smintro>

        <smcore>
          <p>The Core grammar productions for XPath axis are:</p>
      
            <scrap headstyle="show">
            <head>Axes</head>
              <prod num="49 (Core)" id="doc-core-ForwardAxis"><lhs>ForwardAxis</lhs><rhs>("child"  "::")<br/>|  ("descendant"  "::")<br/>|  ("attribute"  "::")<br/>|  ("self"  "::")<br/>|  ("descendant-or-self"  "::")<br/>|  ("namespace"  "::")</rhs></prod>
              <prod num="51 (Core)" id="doc-core-ReverseAxis"><lhs>ReverseAxis</lhs><rhs>("parent"  "::")<br/>|  ("ancestor"  "::")<br/>|  ("ancestor-or-self"  "::")</rhs></prod>
            </scrap>
        </smcore>

        <smnotation>
        <p>We introduce the following auxiliary grammar production to
        describe all axis.</p>

      <scrap headstyle="show">
        <head/>
        <prod num="90 (Formal)" id="doc-fs-Axis"><lhs>Axis</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ForwardAxis" xlink:type="simple">ForwardAxis</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ReverseAxis" xlink:type="simple">ReverseAxis</nt></rhs></prod>
      </scrap>
        </smnotation>

        <smnotation>
          <p id="jd_map_axis">The normalization of axes uses the
          following auxiliary mapping rule:
          <map/><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a>.</p>
        </smnotation>

        <smnorm>
          <p>The normalization for all axes is specified as
          follows.</p>

          <p>The semantics of the following(-sibling) and
          preceding(-sibling) axes are expressed by mapping them to
          Core expressions. All other axes are part of the Core and
          therefore are left unchanged through normalization.</p>
           
          <mapping>
            <xquery>
              <expression><map><code>following-sibling::</code>
              <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
            </xquery>
            <core>
              <expression><map>let $e := . return $e/parent::node()/child:: <emph>NodeTest</emph> [.&gt;&gt;$e]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
            </core>
          </mapping>
    
          <mapping>
          <xquery>
            <expression><map><code>following::</code>
            <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
            <expression><map><code>ancestor-or-self::node()/following-sibling::node()/descendant-or-self::</code><emph>NodeTest</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </core>
          </mapping>
    
          <p>All other forward axes are part of the Core [XPath/XQuery]
          and handled by the normalization rules below:</p>
    
          <mapping>
          <xquery>
            <expression><map><code>child::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
            <expression><code>child::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
          
          <mapping>
          <xquery>
            <expression><map><code>attribute::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
            <expression><code>attribute::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
          
          <mapping>
          <xquery>
            <expression><map><code>self::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
            <expression><code>self::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
          
          <mapping>
          <xquery>
            <expression><map><code>descendant::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
            <expression><code>descendant::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
          
          <mapping>
          <xquery>
            <expression><map><code>descendant-or-self::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
            <expression><code>descendant-or-self::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
          
          <mapping>
          <xquery>
            <expression><map><code>namespace::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
            <expression><code>namespace::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
    
          <p>Reverse axes:</p>
          
          <mapping>
          <xquery>
            <expression><map><code>preceding-sibling::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
            <expression><map>let $e := . return $e/parent::node()/child:: <emph>NodeTest</emph> [.&lt;&lt;$e]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </core>
          </mapping>
          
          <mapping>
          <xquery>
          <expression><map><code>preceding::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
          <expression><map><code>ancestor-or-self::node()/preceding-sibling::node()/descendant-or-self::</code><emph>NodeTest</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </core>
          </mapping>
           
          <p>All other reverse axes are part of the Core [XPath/XQuery]
          and handled by the normalization rules below:</p>
           
          <mapping>
          <xquery>
          <expression><map><code>parent::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
          <expression><code>parent::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
          
          <mapping>
          <xquery>
          <expression><map><code>ancestor::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
          <expression><code>ancestor::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
          
          <mapping>
          <xquery>
          <expression><map><code>ancestor-or-self::</code> <emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
          </xquery>
          <core>
          <expression><code>ancestor-or-self::</code> <emph>NodeTest</emph></expression>
          </core>
          </mapping>
    
        </smnorm>

      </div4>

      <div4 id="node-tests">
        <head>Node Tests</head>
  
        <smintro>
          <p>A node test is a condition applied on the nodes selected
          by an axis step. Node tests are described by the following
          grammar productions.</p>
    
          <scrap headstyle="show">
            <head>Node Tests</head>
            <prod num="78 (XQuery)" id="doc-xquery-NodeTest"><lhs>NodeTest</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-KindTest" xlink:type="simple">KindTest</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-NameTest" xlink:type="simple">NameTest</nt></rhs></prod>
            <prod num="79 (XQuery)" id="doc-xquery-NameTest"><lhs>NameTest</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Wildcard" xlink:type="simple">Wildcard</nt></rhs></prod>
            <prod num="80 (XQuery)" id="doc-xquery-Wildcard"><lhs>Wildcard</lhs><rhs>"*"<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-NCName" xlink:type="simple">NCName</nt>  ":"  "*")<br/>|  ("*"  ":"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-NCName" xlink:type="simple">NCName</nt>)</rhs></prod>
          </scrap>
        </smintro>
  
        <smcore>
          <p>The Core grammar productions for node tests are:</p>

          <scrap headstyle="show">
            <head>Node Tests</head>
            <prod num="52 (Core)" id="doc-core-NodeTest"><lhs>NodeTest</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-KindTest" xlink:type="simple">KindTest</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-NameTest" xlink:type="simple">NameTest</nt></rhs></prod>
            <prod num="53 (Core)" id="doc-core-NameTest"><lhs>NameTest</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Wildcard" xlink:type="simple">Wildcard</nt></rhs></prod>
            <prod num="54 (Core)" id="doc-core-Wildcard"><lhs>Wildcard</lhs><rhs>"*"<br/>|  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-NCName" xlink:type="simple">NCName</nt>  ":"  "*")<br/>|  ("*"  ":"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-NCName" xlink:type="simple">NCName</nt>)</rhs></prod>
          </scrap>
        </smcore>

        <smnotation>
          <p>For convenience, we will use the grammar non-terminals
          <emph>Prefix</emph>, and <emph>LocalPart</emph>, both of which are
          <emph>NCName</emph>s, in some of the inference rules. They are
          defined by the following grammar productions.</p>

          <scrap headstyle="show">
            <head>Prefix and LocalPart</head>
            <prod num="18 (Formal)" id="doc-fs-Prefix"><lhs>Prefix</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-NCName" xlink:type="simple">NCName</nt></rhs></prod>
            <prod num="19 (Formal)" id="doc-fs-LocalPart"><lhs>LocalPart</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-NCName" xlink:type="simple">NCName</nt></rhs></prod>
          </scrap>

        </smnotation>

      </div4>

    </div3>

    <div3 id="id-predicates">
      <head>Predicates</head>
  
      <smintro>
        <p>A predicate consists of an expression, called a <term>predicate
        expression</term>, enclosed in square brackets.</p>
  
        <scrap headstyle="show">
          <head/>
          <prod num="83 (XQuery)" id="doc-xquery-Predicate"><lhs>Predicate</lhs><rhs>"["  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Expr" xlink:type="simple">Expr</nt>  "]"</rhs></prod>
        </scrap>
  
      </smintro>
  
      <smnotation>
        <p id="jd_map_predicates">Normalization of predicates uses the
        following auxiliary mapping rule:
        <map/><a href="#jd_map_predicates" class="judgment"><subscript>Predicates</subscript></a>.</p>
      </smnotation>

      <smnorm>
        <p>Predicates in path expressions are normalized with a
        special mapping rule:</p>
  
        <mapping>
          <xquery>
            <expression><map><emph>Expr</emph></map><a href="#jd_map_predicates" class="judgment"><subscript>Predicates</subscript></a></expression>
          </xquery>
          <core>
            <expression>
              <table><tbody>
              <tr><td rowspan="1" colspan="1">typeswitch (<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)</td></tr>
              <tr><td rowspan="1" colspan="1">  case $v as <emph>fs:</emph><code>numeric</code> return <emph>op:</emph>numeric-equal($v, <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a>)</td></tr>
              <tr><td rowspan="1" colspan="1">  default $v return <code>fn:boolean</code>($v)</td></tr> 
              </tbody>
              </table>
            </expression>
          </core>
        </mapping>
  
        <p>Note that the semantics of predicates whose input
        expression returns a numeric value also work if that value is
        not an integer. In those cases the
        <code>op:numeric-equal</code> returns false when compared to a
        position. For example, the expression <code>//a[3.4]</code>
        always returns the empty sequence.</p>
      </smnorm>
  
    </div3>

    <div3 id="unabbrev">
      <head>Unabbreviated Syntax</head>
  
      <p>The corresponding Section in the [XPath/XQuery] document just
      contains examples.</p>
  
    </div3>

    <div3 id="abbrev">
      <head>Abbreviated Syntax</head>
  
      <scrap headstyle="show">
        <head>Abbreviated Syntax</head>
        <prod num="74 (XQuery)" id="doc-xquery-AbbrevForwardStep"><lhs>AbbrevForwardStep</lhs><rhs>"@"?  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-NodeTest" xlink:type="simple">NodeTest</nt></rhs></prod> 
        <prod num="77 (XQuery)" id="doc-xquery-AbbrevReverseStep"><lhs>AbbrevReverseStep</lhs><rhs>".."</rhs></prod>
      </scrap>
  
      <smnorm>
        <p>Here are normalization rules for the abbreviated
        syntax.</p>

        <mapping>
        <xquery>
          <expression><map>..</map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
        </xquery>
        <core>
          <expression>parent::node()</expression>
        </core>
        </mapping>
        
        <mapping>
        <xquery>
          <expression><map>@<emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
        </xquery>
        <core>
          <expression>attribute::<emph>NodeTest</emph></expression>
        </core>
        </mapping>
        <mapping>
        <xquery>
          <expression><map><emph>NodeTest</emph></map><a href="#jd_map_axis" class="judgment"><subscript>Axis</subscript></a></expression>
        </xquery>
        <core>
          <expression>child::<emph>NodeTest</emph></expression>
        </core>
        </mapping>
      </smnorm>
    </div3>
  </div2>

  <div2 id="id-sequence-expressions">
    <head>Sequence Expressions</head>

    <smintro>
      <p>[XPath/XQuery] supports operators to construct and combine
      sequences.  A <term>sequence</term> is an ordered collection of
      zero or more items. An <term>item</term> is either an atomic
      value or a node.</p>
    </smintro>

    <div3 id="sec_constructing_sequences">
      <head>Constructing Sequences</head>

      <scrap headstyle="show">
        <head>Constructing Sequences</head>
        <prod num="31 (XQuery)" id="noid_N14C83.doc-xquery-Expr"><lhs>Expr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ExprSingle" xlink:type="simple">ExprSingle</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ExprSingle" xlink:type="simple">ExprSingle</nt>)*</rhs></prod>
        <prod num="49 (XQuery)" id="doc-xquery-RangeExpr"><lhs>RangeExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AdditiveExpr" xlink:type="simple">AdditiveExpr</nt> ( "to"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AdditiveExpr" xlink:type="simple">AdditiveExpr</nt> )?</rhs></prod>
      </scrap>

      <smcore>
        <p>The Core grammar production for sequence expressions is:</p>

        <scrap headstyle="show">
          <head>Core Sequence Expressions</head>
          <prod num="22 (Core)" id="noid_N14C98.doc-core-Expr"><lhs>Expr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ExprSingle" xlink:type="simple">ExprSingle</nt>  (","  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ExprSingle" xlink:type="simple">ExprSingle</nt>)*</rhs></prod>
        </scrap>
      </smcore>

      <smnorm>
        <p>A sequence expression is normalized into a sequence of
        normalized single expressions:</p>

        <mapping>
          <xquery>
            <expression>
              <map><emph>Expr</emph><subscript>1</subscript> , <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
            </expression>
          </xquery>
          <core>
            <expression>
              <map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>, <map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
            </expression>
          </core>
        </mapping>
      </smnorm>

      <smtype>
        <p><!--The static semantics of the sequence expression follows.-->
        The type of the sequence expression is the sequence over the
        types of the individual expressions.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
                </clause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression><emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                        <expression><emph>Expr</emph><subscript>1</subscript> , <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>
      </smtype>

      <smeval>  
        <p><!-- The dynamic semantics of the sequence expression follows.-->
        Each expression in the sequence is evaluated and the resulting
        values are concatenated into one sequence.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript></expression></clause>
                <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
                <expression><emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript></expression></clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
                <expression><emph>Expr</emph><subscript>1</subscript>, <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>, <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript></expression></clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

      </smeval>

      <smnorm>
        <p>The range operator is normalized to the <emph>fs:</emph><code>to</code>
        function.</p>

        <mapping>
          <xquery>
            <expression>
             <map><emph>Expr</emph><subscript>1</subscript> <code>to</code> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
            </expression>
          </xquery>
          <core>
            <expression>
              <emph>fs:</emph><code>to</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>),(<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))
            </expression>
          </core>
        </mapping>
      </smnorm>

      <smtype>
        <p>The static semantics of the <emph>fs:</emph><code>to</code> function is defined in
        <specref ref="sec_fs_to"/>.</p>
      </smtype>

      <smeval>
        <p>The dynamic semantics of the <emph>fs:</emph><code>to</code> function is defined in
        <specref ref="sec_fs_to"/>.</p>
      </smeval>

    </div3>

    <div3 id="sec_filter_exprs">
      <head>Filter Expressions</head>
      <smintro>
        <scrap headstyle="show">
          <head>Filter Expression</head>
          <prod num="81 (XQuery)" id="doc-xquery-FilterExpr"><lhs>FilterExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PrimaryExpr" xlink:type="simple">PrimaryExpr</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PredicateList" xlink:type="simple">PredicateList</nt></rhs></prod>
        </scrap>
      </smintro>

      <smcore>
        <p>There are no Core grammar productions for filter
        expressions as they are normalized to other Core
        expressions. </p>
      </smcore>

      <smnorm>
        <p>When a predicate with a numeric literal or the
        <code>last()</code> expression is applied on a primary
        expression, it is normalized using the <code>fn:subsequence</code>
        function. This results in a more precise static type for those
        cases.</p>
  
        <mapping>
          <xquery>
            <expression><map><emph>PrimaryExpr</emph> <emph>PredicateList</emph> [ <emph>NumericLiteral</emph> ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
            <expression>
              <table><tbody>
                <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> := <map><emph>PrimaryExpr</emph> <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> return</td></tr>
                <tr><td rowspan="1" colspan="1"><code>fn:subsequence</code>(<code>$</code><emph>fs:</emph><code>sequence</code>,<emph>NumericLiteral</emph>,1)</td></tr>
              </tbody></table>
            </expression>
          </core>
        </mapping>
        
        <mapping>
          <xquery>
            <expression><map><emph>PrimaryExpr</emph> <emph>PredicateList</emph> [ fn:last() ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
            <expression>
              <table><tbody>
                <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> := <map><emph>PrimaryExpr</emph> <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> return</td></tr>
                <tr><td rowspan="1" colspan="1"><code>fn:subsequence</code>(<code>$</code><emph>fs:</emph><code>sequence</code>,<a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a>,1)</td></tr>
              </tbody></table>
            </expression>
          </core>
        </mapping>
        
        <p>In the general case, when a predicate is applied on a
        primary expression, it is normalized to a FLWOR expression as
        follows. The input sequence is processed in sequence order and
        the context item is bound to each item in the input
        sequence.</p>

        <mapping>
          <xquery>
            <expression><map><emph>PrimaryExpr</emph> <emph>PredicateList</emph> [ <emph>Expr</emph> ]</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
          </xquery>
          <core>
            <expression>
              <table><tbody>
                <tr><td rowspan="1" colspan="1">let <code>$</code><emph>fs:</emph><code>sequence</code> := <map><emph>PrimaryExpr</emph> <emph>PredicateList</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> return</td></tr>
                <tr><td rowspan="1" colspan="1">let <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a> := fn:count(<code>$</code><emph>fs:</emph><code>sequence</code>) return</td></tr>
                <tr><td rowspan="1" colspan="1">for <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> at <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> in <code>$</code><emph>fs:</emph><code>sequence</code> return</td></tr>
                <tr><td rowspan="1" colspan="1">   if (<map><emph>Expr</emph></map><a href="#jd_map_predicates" class="judgment"><subscript>Predicates</subscript></a>) then <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a> else ()</td></tr>
              </tbody></table>
            </expression>
          </core>
        </mapping>
    </smnorm>

    <smtype>
      <p>There are no additional static typing rules for filter
      expressions.</p>
    </smtype>

    <smeval>
      <p>There are no additional dynamic evaluation rules for filter
      expressions.</p>
    </smeval>
    </div3>

    <div3 id="sec_combining_sequences">
      <head>Combining Node Sequences</head>

      <p>[XPath/XQuery] provides several operators for combining
      sequences of nodes.</p>

      <scrap headstyle="show">
        <head>Combining Sequences</head>
        <prod num="52 (XQuery)" id="doc-xquery-UnionExpr"><lhs>UnionExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-IntersectExceptExpr" xlink:type="simple">IntersectExceptExpr</nt> ( ("union"  |  "|")  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-IntersectExceptExpr" xlink:type="simple">IntersectExceptExpr</nt> )*</rhs></prod>
        <prod num="53 (XQuery)" id="doc-xquery-IntersectExceptExpr"><lhs>IntersectExceptExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-InstanceofExpr" xlink:type="simple">InstanceofExpr</nt> ( ("intersect"  |  "except")  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-InstanceofExpr" xlink:type="simple">InstanceofExpr</nt> )*</rhs></prod>
      </scrap>

    <smnotation>
      <p id="jd_map_sequenceop">The union, intersect, and except
      expressions are normalized into function calls to the
      appropriate functions. The mapping function
      <map/><a href="#jd_map_sequenceop" class="judgment"><subscript>SequenceOp</subscript></a> is defined by the following
      table:</p>

      <table border="1"><tbody>
      <tr><td rowspan="1" colspan="1">SequenceOp</td><td rowspan="1" colspan="1"><map><emph>SequenceOp</emph></map><a href="#jd_map_sequenceop" class="judgment"><subscript>SequenceOp</subscript></a></td></tr>
      <tr><td rowspan="1" colspan="1">"union"</td><td rowspan="1" colspan="1"><emph>op:</emph>union</td></tr>
      <tr><td rowspan="1" colspan="1">"|"</td><td rowspan="1" colspan="1"><emph>op:</emph>union</td></tr>
      <tr><td rowspan="1" colspan="1">"intersect"</td><td rowspan="1" colspan="1"><emph>op:</emph>intersect</td></tr>
      <tr><td rowspan="1" colspan="1">"except"</td><td rowspan="1" colspan="1"><emph>op:</emph>except</td></tr> </tbody></table>
    </smnotation>

    <smnorm>
      <p>Operators for combining node sequences are normalized as
      follows.</p>

      <mapping>
       <xquery>
        <expression>
        <map><emph>Expr</emph><subscript>1</subscript> <emph>SequenceOp</emph> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
        <a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a> (<map><emph>SequenceOp</emph></map><a href="#jd_map_sequenceop" class="judgment"><subscript>SequenceOp</subscript></a> (
        <map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>, <map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> ))
        </expression>
       </core>
      </mapping>

    </smnorm>

    <smtype>
      <p>The static semantics of the operators that combine sequences
      are defined in <specref ref="sec_op_union_intersect_except"/>.</p>
    </smtype>

    <smeval>
      <p>The dynamic semantics for function calls is given in <specref ref="id-function-calls"/>.</p>
    </smeval>

  </div3>

  </div2>

  <div2 id="sec_arithmetic">
    <head>Arithmetic Expressions</head>

    <p>[XPath/XQuery] provides arithmetic operators for addition,
    subtraction, multiplication, division, and modulus, in their usual
    binary and unary forms.</p>

    <scrap headstyle="show">
     <head>Arithmetic Expressions</head>
     <prod num="50 (XQuery)" id="doc-xquery-AdditiveExpr"><lhs>AdditiveExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-MultiplicativeExpr" xlink:type="simple">MultiplicativeExpr</nt> ( ("+"  |  "-")  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-MultiplicativeExpr" xlink:type="simple">MultiplicativeExpr</nt> )*</rhs></prod>
     <prod num="51 (XQuery)" id="doc-xquery-MultiplicativeExpr"><lhs>MultiplicativeExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-UnionExpr" xlink:type="simple">UnionExpr</nt> ( ("*"  |  "div"  |  "idiv"  |  "mod")  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-UnionExpr" xlink:type="simple">UnionExpr</nt> )*</rhs></prod>
     <prod num="58 (XQuery)" id="doc-xquery-UnaryExpr"><lhs>UnaryExpr</lhs><rhs>("-"  |  "+")* <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ValueExpr" xlink:type="simple">ValueExpr</nt></rhs></prod>
     <prod num="59 (XQuery)" id="doc-xquery-ValueExpr"><lhs>ValueExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ValidateExpr" xlink:type="simple">ValidateExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PathExpr" xlink:type="simple">PathExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ExtensionExpr" xlink:type="simple">ExtensionExpr</nt></rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar production for arithmetic expressions
      is:</p>

      <scrap headstyle="show">
        <head/>
        <prod num="40 (Core)" id="doc-core-ValueExpr"><lhs>ValueExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ValidateExpr" xlink:type="simple">ValidateExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-StepExpr" xlink:type="simple">StepExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ExtensionExpr" xlink:type="simple">ExtensionExpr</nt></rhs></prod>
      </scrap>
    </smcore>

    <smnotation>
      <p id="jd_map_arithop">The mapping function
      <map/><a href="#jd_map_arithop" class="judgment"><subscript>ArithOp</subscript></a> is defined by the following
      table:</p>

      <table border="1">
        <tbody>
          <tr><td rowspan="1" colspan="1"><emph>ArithOp</emph></td><td rowspan="1" colspan="1"><map><emph>ArithOp</emph></map><a href="#jd_map_arithop" class="judgment"><subscript>ArithOp</subscript></a></td></tr>
          <tr><td rowspan="1" colspan="1">"+"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>plus</code></td></tr>
          <tr><td rowspan="1" colspan="1">"-"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>minus</code></td></tr>
          <tr><td rowspan="1" colspan="1">"*"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>times</code></td></tr>
          <tr><td rowspan="1" colspan="1">"div"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>div</code></td></tr>
          <tr><td rowspan="1" colspan="1">"mod"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>mod</code></td></tr>
        </tbody>
      </table>

   </smnotation>

  <smcore>
    <p>There are no Core grammar productions for arithmetic
    expressions as they are normalized to other Core expressions. </p>
    </smcore>

  <smnorm>
     <p>The normalization rules for all the arithmetic operators
     except <code>idiv</code> first atomize each argument by applying
     <code>fn:data</code> and then apply the internal function
     <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a> to each argument.  If the first argument to
     this function has type <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>, then the first
     argument is cast to a double, otherwise it is returned unchanged.
     The overloaded internal function corresponding to the arithmetic
     operator is then applied to the two converted arguments.  The
     table above maps the operators to the corresponding internal
     function.  The mapping from the overloaded internal functions to
     the corresponding non-overloaded function is given in <specref ref="sec_operators"/>.</p>

   <mapping>
    <xquery>
     <expression>
      <map><emph>Expr</emph><subscript>1</subscript> <emph>ArithOp</emph>  <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <table summary=""><tbody>
        <tr><td rowspan="1" colspan="1"><map><emph>ArithOp</emph></map><a href="#jd_map_arithop" class="judgment"><subscript>ArithOp</subscript></a>
          (</td><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1.0E0),
         </td></tr>
        <tr><td rowspan="1" colspan="1"/><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1.0E0))
         </td></tr>
       </tbody></table>
     </expression>
    </core>
   </mapping>

   <p>The normalization rules for the <code>idiv</code> operator are similar,
   but instead of casting arguments with type <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a> to
   <code>xs:double</code>, they are cast to <code>xs:integer</code>.</p>

   <mapping>
    <xquery>
     <expression>
      <map><emph>Expr</emph><subscript>1</subscript> <code>idiv</code>  <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <table summary=""><tbody>
        <tr valign="baseline"><td rowspan="1" colspan="1"><emph>fs:</emph><code>idiv</code>
          (</td><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1),
         </td></tr>
        <tr><td rowspan="1" colspan="1"/><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1))
         </td></tr>
       </tbody></table>
     </expression>
    </core>
   </mapping>
  

   <p>The unary operators are mapped similarly.</p>

   <mapping>
    <xquery>
     <expression>
      <map><code>+</code> <emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
            <emph>fs:</emph><code>unary-plus</code>(<a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1.0E0))
     </expression>
    </core>
   </mapping>

   <mapping>
    <xquery>
     <expression>
      <map><code>-</code> <emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
        <emph>fs:</emph><code>unary-minus</code>(<a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1.0E0))
     </expression>
    </core>
   </mapping>

</smnorm>

<smtype>
  <p>The static semantics for function calls is given in <specref ref="id-function-calls"/>. The mapping from the overloaded internal
  functions to the corresponding non-overloaded function is given in
  <specref ref="sec_operators"/>.</p>
</smtype>

<smeval>
  <p>The dynamic semantics for function calls is given in <specref ref="id-function-calls"/>. The mapping from the overloaded internal
  functions to the corresponding non-overloaded function is given in
  <specref ref="sec_operators"/>.</p>
</smeval>

  </div2>

  <div2 id="sec_comparisons">
    <head>Comparison Expressions</head>

    <smintro>

      <p>Comparison expressions allow two values to be
      compared. [XPath/XQuery] provides three kinds of comparison
      expressions, called value comparisons, general comparisons, and
      node comparisons.</p>

      <scrap headstyle="show">
        <head>Comparison Expressions</head>
        <prod num="48 (XQuery)" id="doc-xquery-ComparisonExpr"><lhs>ComparisonExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-RangeExpr" xlink:type="simple">RangeExpr</nt> ( (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ValueComp" xlink:type="simple">ValueComp</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-GeneralComp" xlink:type="simple">GeneralComp</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-NodeComp" xlink:type="simple">NodeComp</nt>)  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-RangeExpr" xlink:type="simple">RangeExpr</nt> )?</rhs></prod>
        <prod num="61 (XQuery)" id="doc-xquery-ValueComp"><lhs>ValueComp</lhs><rhs>"eq"  |  "ne"  |  "lt"  |  "le"  |  "gt"  |  "ge"</rhs></prod>
        <prod num="60 (XQuery)" id="doc-xquery-GeneralComp"><lhs>GeneralComp</lhs><rhs>"="  |  "!="  |  "&lt;"  |  "&lt;="  |  "&gt;"  |  "&gt;="</rhs></prod>
        <prod num="62 (XQuery)" id="doc-xquery-NodeComp"><lhs>NodeComp</lhs><rhs>"is"  |  "&lt;&lt;"  |  "&gt;&gt;"</rhs></prod>
      </scrap>
    </smintro>

  <div3 id="sec_value_comparisons">
    <head>Value Comparisons</head>

    <smnotation>
      <p id="jd_map_valuecomp">The mapping function
      <map/><a href="#jd_map_valuecomp" class="judgment"><subscript>ValueComp</subscript></a> is defined by the following
      table:</p>

      <table border="1"><tbody>
      <tr><td rowspan="1" colspan="1">ValueComp</td><td rowspan="1" colspan="1"><map>ValueComp</map><a href="#jd_map_valuecomp" class="judgment"><subscript>ValueComp</subscript></a></td></tr>
      <tr><td rowspan="1" colspan="1">"<code>eq</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>eq</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>ne</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>ne</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>lt</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>lt</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>le</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>le</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>gt</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>gt</td></tr> 
      <tr><td rowspan="1" colspan="1">"<code>ge</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>ge</td></tr></tbody></table>
    </smnotation>

    <smcore>
      <p>There are no Core grammar productions for value comparisons
      as they are normalized to other Core expressions. </p>
    </smcore>

    <smnorm>
      <p>The normalization rules for the value comparison operators
      first atomize each argument by applying <code>fn:data</code> and then apply
      the internal function <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a> defined in <specref ref="sec_convert_operand"/>.  If the first argument to this
      function has type <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>, then the first argument is
      cast to a string, otherwise it is returned unchanged.  The
      overloaded internal function corresponding to the value
      comparison operator is then applied to the two converted
      arguments.  The table above maps the value operators to the
      corresponding internal function.  The mapping from the
      overloaded internal functions to the corresponding
      non-overloaded function is given in <specref ref="sec_operators"/>.</p>

      <mapping>
       <xquery>
        <expression>
         <map><emph>Expr</emph><subscript>1</subscript> <emph>ValueComp</emph>  <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
         <table summary=""><tbody>
           <tr><td rowspan="1" colspan="1"><map><emph>ValueComp</emph></map><a href="#jd_map_valuecomp" class="judgment"><subscript>ValueComp</subscript></a>
             (</td><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), "string"),
            </td></tr>
           <tr><td rowspan="1" colspan="1"/><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), "string") )
            </td></tr>
          </tbody></table>
        </expression>
       </core>
      </mapping>
    </smnorm>

    <smtype>
      <p>The static semantics for function calls is given in <specref ref="id-function-calls"/>.  The comparison functions all have
      return type <code>xs:boolean</code>, as specified in <bibref ref="xpath-functions"/>. </p>
    </smtype>

    <smeval>
      <p>The dynamic semantics for function calls is given in <specref ref="id-function-calls"/>.</p>
    </smeval>

  </div3>

  <div3 id="sec_general_comparisons">
    <head>General Comparisons</head>

    <smintro>
      <p>General comparisons are defined by adding existential
      semantics to value comparisons. The operands of a general
      comparison may be sequences of any length. The result of a
      general comparison is always <code>true</code> or
      <code>false</code>.</p>
    </smintro>

    <smnotation>
      <p id="jd_map_generalcomp">The function
      <map/><a href="#jd_map_generalcomp" class="judgment"><subscript>GeneralComp</subscript></a> is defined by the following
      table:</p>

      <table border="1"><tbody>
      <tr><td rowspan="1" colspan="1"><emph>GeneralComp</emph></td><td rowspan="1" colspan="1"><map><emph>GeneralComp</emph></map><a href="#jd_map_generalcomp" class="judgment"><subscript>GeneralComp</subscript></a></td></tr>
      <tr><td rowspan="1" colspan="1">"<code>=</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>eq</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>!=</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>ne</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>&lt;</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>lt</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>&lt;=</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>le</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>&gt;</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>gt</td></tr> 
      <tr><td rowspan="1" colspan="1">"<code>&gt;=</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>ge</td></tr></tbody></table>

    </smnotation>

    <smcore>
      <p>There are no Core grammar productions for general comparisons
      as they are normalized to existentially quantified Core
      expressions.</p>
    </smcore>

    <smnorm>
      <p>The normalization rule for a general comparison expression
      first atomizes each argument by applying <code>fn:data</code> and then
      applies the existentially quantified <emph>SomeExpr</emph> expression to
      each sequence.  The internal function <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a> is
      applied to each pair of atomic values.  If the first argument to
      this function has type <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>, then the first
      argument is cast to type of the second argument.  If the second
      argument has type <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>, the first argument is cast
      to a string.  The overloaded internal function corresponding to
      the general comparison operator is then applied to the two
      converted values.</p>

          <mapping>
            <xquery>
              <expression>
                <map><emph>Expr</emph><subscript>1</subscript> <emph>GeneralComp</emph> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
              </expression>
            </xquery>
            <core>
              <expression>
                <table summary="">
                  <tbody>
                    <tr><td rowspan="1" colspan="1">some $v1 in <code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) satisfies</td></tr>
                    <tr><td rowspan="1" colspan="1">some $v2 in <code>fn:data</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) satisfies</td></tr>
                    <tr><td rowspan="1" colspan="1">let $u1 := <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>($v1, $v2) return </td></tr>
                    <tr><td rowspan="1" colspan="1">let $u2 := <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>($v2, $v1) return </td></tr>
                    <tr><td rowspan="1" colspan="1"><map><emph>GeneralComp</emph></map><a href="#jd_map_generalcomp" class="judgment"><subscript>GeneralComp</subscript></a> ($u1, $u2)</td></tr>
                  </tbody>
                </table>
              </expression>
            </core>
          </mapping>
      </smnorm>

  </div3>

  <div3 id="sec_node-comparisons">
    <head>Node Comparisons</head>

<smcore>
  <p>There are no Core grammar productions for node comparisons as
  they are normalized to other Core expressions.</p>
</smcore>

<smnorm>
  <p>The normalization rules for node comparisons map each argument
  expression and then apply the internal function corresponding to the
  node comparison operator. The internal function are defined in
  <specref ref="sec_operators"/>.</p>

<mapping>
<xquery>
<expression>
<map><emph>Expr</emph><subscript>1</subscript> <code>is</code> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
</expression>
</xquery>
<core>
     <expression>
       <table summary=""><tbody>
       <tr><td rowspan="1" colspan="1">   <emph>fs:</emph><code>is-same-node</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>), (<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))</td></tr>
       </tbody></table>
     </expression>
</core>
</mapping>

<mapping>
<xquery>
<expression>
<map><emph>Expr</emph><subscript>1</subscript> &lt;&lt; <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
</expression>
</xquery>
<core>
<expression>
       <table summary=""><tbody>
       <tr><td rowspan="1" colspan="1"><emph>fs:</emph><code>node-before</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>), (<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))</td></tr>
       </tbody></table>
</expression>
</core>
</mapping>

<mapping>
<xquery>
<expression>
<map><emph>Expr</emph><subscript>1</subscript> &gt;&gt; <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
</expression>
</xquery>
<core>
<expression>
       <table summary=""><tbody>
       <tr><td rowspan="1" colspan="1"><emph>fs:</emph><code>node-after</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>), (<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))</td></tr>
       </tbody></table>
</expression>
</core>
</mapping>

</smnorm>

<smtype>
  <p>The static semantics for the internal functions are defined in
  <specref ref="sec_operators"/>. </p>
</smtype>

<smeval>
  <p>The dynamic semantics for internal function is defined in
  <specref ref="sec_operators"/>.</p>
</smeval>

  </div3>

  </div2>

  <div2 id="id-logical-expressions"> 
    <head>Logical Expressions</head>

    <smintro>
      <p>A <term>logical expression</term> is either an
      <term>and-expression</term> or an
      <term>or-expression</term>. The value of a logical expression is
      always one of the boolean values: <code>true</code> or
      <code>false</code>.</p>
    </smintro>

    <scrap headstyle="show">
      <head>Logical Expressions</head>
      <prod num="46 (XQuery)" id="doc-xquery-OrExpr"><lhs>OrExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AndExpr" xlink:type="simple">AndExpr</nt> ( "or"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AndExpr" xlink:type="simple">AndExpr</nt> )*</rhs></prod>
      <prod num="47 (XQuery)" id="doc-xquery-AndExpr"><lhs>AndExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ComparisonExpr" xlink:type="simple">ComparisonExpr</nt> ( "and"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ComparisonExpr" xlink:type="simple">ComparisonExpr</nt> )*</rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar productions for logical expressions are:</p>

      <scrap headstyle="show">
        <head>Core Logical Expressions</head>
        <prod num="36 (Core)" id="doc-core-OrExpr"><lhs>OrExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AndExpr" xlink:type="simple">AndExpr</nt> ( "or"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-AndExpr" xlink:type="simple">AndExpr</nt> )*</rhs></prod>
        <prod num="37 (Core)" id="doc-core-AndExpr"><lhs>AndExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CastableExpr" xlink:type="simple">CastableExpr</nt> ( "and"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CastableExpr" xlink:type="simple">CastableExpr</nt> )*</rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>The normalization rules for "<code>and</code>" and "<code>or</code>" first
      get the effective boolean value of each argument, then apply the
      appropriate Core operator.</p>

   <mapping>
    <xquery>
     <expression>
      <map><emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <code>fn:boolean</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) <code>and</code> <code>fn:boolean</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))
     </expression>
    </core>
   </mapping>

   <mapping>
    <xquery>
     <expression>
      <map><emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <code>fn:boolean</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) <code>or</code> <code>fn:boolean</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))
     </expression>
    </core>
   </mapping>
    </smnorm>

    <smtype>
      <p>The logical expressions require that each subexpression have
type <code>xs:boolean</code>.  The result type is also <code>xs:boolean</code>.</p>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>n</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>n</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

    </smtype>
    
    <smeval>
      <p>The dynamic semantics of logical expressions is
non-deterministic.   This non-determinism permits implementations to use
short-circuit evaluation strategies when evaluating logical expressions. In the expression, <emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript>,
if either expression raises an error or evaluates to false, the entire
expression may raise an error or evaluate to false. 
In the expression, <emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript>,
if either expression raises an error or evaluates to true, the entire
expression may raise an error or evaluate to true. </p>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>i</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> false</expression>
        </clause>
      </multiclause>
      <multiclause>
        <clause><expression>i <a href="#jd_isin" class="judgment"><term>in</term></a> { 1,2 }</expression></clause>
      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                false</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> true</expression>
        </clause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> true</expression>
        </clause>

      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                true</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>i</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> true</expression>
        </clause>
      </multiclause>
      <multiclause>
        <clause><expression>i <a href="#jd_isin" class="judgment"><term>in</term></a> { 1,2 }</expression></clause>
      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                true</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> false</expression>
        </clause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> false</expression>
        </clause>

      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                false</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

    </smeval>

  </div2>

  <div2 id="sec_constructors">
    <head>Constructors</head>

    <p>[XPath/XQuery] supports two forms of constructors. <term>Direct
    constructors</term> support literal XML syntax for elements,
    attributes, text nodes, processing-instructions and
    comments. <term>Computed constructors</term> can be used to
    construct elements, attributes, text nodes,
    processing-instructions, comments, and document nodes.  All direct
    constructors are normalized into computed constructors, i.e.,
    there are no direct-constructor expressions in the Core.</p>

    <scrap headstyle="show">
      <head>Constructors</head>
      <prod num="94 (XQuery)" id="doc-xquery-Constructor"><lhs>Constructor</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirectConstructor" xlink:type="simple">DirectConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ComputedConstructor" xlink:type="simple">ComputedConstructor</nt></rhs></prod>
      <prod num="95 (XQuery)" id="doc-xquery-DirectConstructor"><lhs>DirectConstructor</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirElemConstructor" xlink:type="simple">DirElemConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirCommentConstructor" xlink:type="simple">DirCommentConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirPIConstructor" xlink:type="simple">DirPIConstructor</nt></rhs></prod>
      <prod num="96 (XQuery)" id="doc-xquery-DirElemConstructor"><lhs>DirElemConstructor</lhs><rhs>"&lt;"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirAttributeList" xlink:type="simple">DirAttributeList</nt>  ("/&gt;"  |  ("&gt;"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirElemContent" xlink:type="simple">DirElemContent</nt>*  "&lt;/"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-S" xlink:type="simple">S</nt>?  "&gt;"))</rhs></prod>
      <prod num="101 (XQuery)" id="doc-xquery-DirElemContent"><lhs>DirElemContent</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirectConstructor" xlink:type="simple">DirectConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CDataSection" xlink:type="simple">CDataSection</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CommonContent" xlink:type="simple">CommonContent</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ElementContentChar" xlink:type="simple">ElementContentChar</nt></rhs></prod>
      <prod num="148 (XQuery)" id="doc-xquery-ElementContentChar"><lhs>ElementContentChar</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-Char" xlink:type="simple">Char</nt> - [{}&lt;&amp;]</rhs></prod>
      <prod num="102 (XQuery)" id="doc-xquery-CommonContent"><lhs>CommonContent</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PredefinedEntityRef" xlink:type="simple">PredefinedEntityRef</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-CharRef" xlink:type="simple">CharRef</nt>  |  "{{"  |  "}}"  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-EnclosedExpr" xlink:type="simple">EnclosedExpr</nt></rhs></prod>
      <prod num="107 (XQuery)" id="doc-xquery-CDataSection"><lhs>CDataSection</lhs><rhs>"&lt;![CDATA["  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CDataSectionContents" xlink:type="simple">CDataSectionContents</nt>  "]]&gt;"</rhs></prod>
      <prod num="108 (XQuery)" id="doc-xquery-CDataSectionContents"><lhs>CDataSectionContents</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-Char" xlink:type="simple">Char</nt>* - (Char* ']]&gt;' Char*))</rhs></prod>
      <prod num="97 (XQuery)" id="doc-xquery-DirAttributeList"><lhs>DirAttributeList</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-S" xlink:type="simple">S</nt>  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-S" xlink:type="simple">S</nt>?  "="  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-S" xlink:type="simple">S</nt>?  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirAttributeValue" xlink:type="simple">DirAttributeValue</nt>)?)*</rhs></prod>
      <prod num="98 (XQuery)" id="doc-xquery-DirAttributeValue"><lhs>DirAttributeValue</lhs><rhs>('"'  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-EscapeQuot" xlink:type="simple">EscapeQuot</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-QuotAttrValueContent" xlink:type="simple">QuotAttrValueContent</nt>)*  '"')<br/>|  ("'"  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-EscapeApos" xlink:type="simple">EscapeApos</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AposAttrValueContent" xlink:type="simple">AposAttrValueContent</nt>)*  "'")</rhs></prod>
      <prod num="99 (XQuery)" id="doc-xquery-QuotAttrValueContent"><lhs>QuotAttrValueContent</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-QuotAttrContentChar" xlink:type="simple">QuotAttrContentChar</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CommonContent" xlink:type="simple">CommonContent</nt></rhs></prod>
      <prod num="100 (XQuery)" id="doc-xquery-AposAttrValueContent"><lhs>AposAttrValueContent</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-AposAttrContentChar" xlink:type="simple">AposAttrContentChar</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CommonContent" xlink:type="simple">CommonContent</nt></rhs></prod>
      <prod num="149 (XQuery)" id="doc-xquery-QuotAttrContentChar"><lhs>QuotAttrContentChar</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-Char" xlink:type="simple">Char</nt> - ["{}&lt;&amp;]</rhs></prod>
      <prod num="150 (XQuery)" id="doc-xquery-AposAttrContentChar"><lhs>AposAttrContentChar</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-Char" xlink:type="simple">Char</nt> - ['{}&lt;&amp;]</rhs></prod>
      <prod num="146 (XQuery)" id="doc-xquery-EscapeQuot"><lhs>EscapeQuot</lhs><rhs>'""'</rhs></prod>
      <prod num="147 (XQuery)" id="doc-xquery-EscapeApos"><lhs>EscapeApos</lhs><rhs>"''"</rhs></prod>
      <prod num="29 (XQuery)" id="doc-xquery-EnclosedExpr"><lhs>EnclosedExpr</lhs><rhs>"{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Expr" xlink:type="simple">Expr</nt>  "}"</rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar productions for constructors are:</p>

<scrap headstyle="show">
  <head>Constructors</head>
  <prod num="64 (Core)" id="doc-core-Constructor"><lhs>Constructor</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ComputedConstructor" xlink:type="simple">ComputedConstructor</nt></rhs></prod>
  <prod num="65 (Core)" id="doc-core-ComputedConstructor"><lhs>ComputedConstructor</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CompDocConstructor" xlink:type="simple">CompDocConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CompElemConstructor" xlink:type="simple">CompElemConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CompAttrConstructor" xlink:type="simple">CompAttrConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CompTextConstructor" xlink:type="simple">CompTextConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CompCommentConstructor" xlink:type="simple">CompCommentConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-CompPIConstructor" xlink:type="simple">CompPIConstructor</nt></rhs></prod>
  <prod num="21 (Core)" id="doc-core-EnclosedExpr"><lhs>EnclosedExpr</lhs><rhs>"{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Expr" xlink:type="simple">Expr</nt>  "}"</rhs></prod>
</scrap>

      <p>There are no Core grammar productions for direct XML element
      or attribute constructors as they are normalized to computed
      constructors.</p>
    </smcore>

  <div3 id="id_element_constructor">
    <head>Direct Element Constructors</head>

    <smintro>
      <p>The static and dynamic semantics of the direct forms of
      element and attribute constructors are specified on the
      equivalent computed element and attribute constructors.</p>
    </smintro>

    <smnotation>
      <p id="jd_map_element_content">The auxiliary mapping rules
      <map/><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a>,
      <map/><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a>,
      <map/><a href="#jd_map_element_content" class="judgment"><subscript>PartitionIntoUnits</subscript></a>, and
      <map/><a href="#jd_map_element_content" class="judgment"><subscript>DirCharsUnits</subscript></a> are defined in this section and
      are used for the normalization of the content of direct element
      constructors.</p>
    </smnotation>

    <smnotation>
      <p>An element-content unit is either a maximal contiguous
      sequence of literal characters (including character references,
      escaped braces, and predefined entity references), an enclosed
      expression, a direct element constructor, an XML comment, or an
      XML processing instruction. We use the following auxiliary
      grammar productions to describe element-content units.</p>

      <scrap headstyle="show">
        <head/>
        <prod num="83 (Formal)" id="doc-fs-ElementContentUnit"><lhs>ElementContentUnit</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirectConstructor" xlink:type="simple">DirectConstructor</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-EnclosedExpr" xlink:type="simple">EnclosedExpr</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-DirCharsUnit" xlink:type="simple">DirCharsUnit</nt></rhs></prod>
        <prod num="84 (Formal)" id="doc-fs-DirCharsUnit"><lhs>DirCharsUnit</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CDataSection" xlink:type="simple">CDataSection</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-PredefinedEntityRef" xlink:type="simple">PredefinedEntityRef</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-CharRef" xlink:type="simple">CharRef</nt>  |  <rhs-group role="xquery">"{{"</rhs-group>  |  <rhs-group role="xquery">"}}"</rhs-group>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ElementContentChar" xlink:type="simple">ElementContentChar</nt>)+</rhs></prod>
      </scrap>

      <p>We use the auxiliary normalization rule <map>
      <emph>DirElemContent</emph>* </map><a href="#jd_map_element_content" class="judgment"><subscript>PartitionIntoUnits</subscript></a> to
      restructure the original element content, <emph>DirElemContent</emph>*,
      into the appropriate sequence of element-content units. This
      normalization rule is not specified formally.</p>

      <p>Here are three direct element constructors, each of which
      contains one element-content unit:</p>

<eg xml:space="preserve">
&lt;date&gt;{ xs:date("2003-03-18") }&lt;/date&gt;

&lt;name&gt;Dizzy Gillespie&lt;/name&gt;

&lt;comment&gt;&lt;!-- Just a comment --&gt;&lt;/comment&gt;
</eg>

     <p>The first contains one enclosed expression, the second
     contains one contiguous sequence of characters, and the third
     contains one XML comment.</p>

     <p>After boundary whitespace is stripped, the next example
     contains six element-content units: </p>

<eg xml:space="preserve">
&lt;address&gt;
  &lt;!-- Dizzy's address --&gt;
  { 123 }-0A &lt;street&gt;Roosevelt Ave.&lt;/street&gt; Flushing, NY { 11368 }
&lt;/address&gt;
</eg>

     <p>It contains an XML comment, followed by an enclosed expression
     that contains the integer 123, a contiguous sequence of
     characters ("-0A "), a direct XML element constructor, a
     contiguous sequence of characters (" Flushing, NY "), and an
     enclosed expression that contains the integer 11368. Evaluation
     of that constructor will result in the following element.</p>

<eg xml:space="preserve">
&lt;address&gt;&lt;!-- Dizzy's address --&gt;123-0A &lt;street&gt;Roosevelt Ave.&lt;/street&gt; Flushing, NY 11368&lt;/address&gt;
</eg>

    </smnotation>

    <smnorm>
      <p>We start by giving the rules for the two forms of direct XML
      element constructors.  Note that the direct attribute
      constructors are normalized twice: the
      <map/><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a> normalizes the
      namespace-declaration attributes and
      <map/><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a> normalizes all other attributes
      that are not namespace-declaration attributes. </p>

      <mapping>
      <xquery>
        <expression><map> &lt; <emph>QName</emph> <emph>DirAttributeList</emph> &gt;
          <emph>DirElemContent</emph>* &lt;/ <emph>QName</emph> S? &gt;
          </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
      </xquery>
      <core>
        <expression>element <emph>QName</emph> { <map>
          <emph>DirAttributeList</emph> </map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a> , <map>
          <map> <emph>DirElemContent</emph>* </map><a href="#jd_map_element_content" class="judgment"><subscript>PartitionIntoUnits</subscript></a>
          </map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a> } { <map>
          <emph>DirAttributeList</emph> </map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a> }
        </expression>
      </core>
      </mapping>

      <mapping>
      <xquery>
        <expression><map> &lt; <emph>QName</emph> <emph>DirAttributeList</emph> /&gt;
          </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
      </xquery>
      <core>
        <expression>element <emph>QName</emph> { <map>
        <emph>DirAttributeList</emph> </map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a> } { <map>
          <emph>DirAttributeList</emph> </map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a> }
        </expression>
      </core>
      </mapping>

      <p>We can now give the rules for normalizing a direct element
      constructor's content. We distinguish between direct element
      constructors that contain only one element-content unit and
      those that contain more than one element-content unit.</p>

      <p>Adjacent element-content units are convenient because they
      permit arbitrary interleaving of text and atomic data.  During
      evaluation, atomic values are converted to text nodes containing
      the string representations of the atomic values, and then
      adjacent text nodes are concatenated together.  In the example
      at the beginning of this section, the integer 123 is converted
      to a string and concatenated with "-0A" and the result is a
      single text node containing "123-0A".</p>

      <p>Below are two examples of normalization for element
      constructors.</p>

<eg xml:space="preserve">
&lt;date&gt;{ xs:date("2003-03-18") }&lt;/date&gt;
 =
element date { 
  fs:item-sequence-to-node-sequence(
    xs:date("2003-03-18")
  )
}

&lt;address&gt;
  &lt;!-- Dizzy's address --&gt;
  { 123 }-0A &lt;street&gt;Roosevelt Ave.&lt;/street&gt; Flushing, NY { 11368 }
&lt;/address&gt;
 =
element address {
  fs:item-sequence-to-node-sequence(
    comment { " Dizzy's address "},
    123, 
    text { "-0A "}, 
    element street {"Roosevelt Ave."},
    text { " Flushing, NY "  },
    11368
  )
}
</eg>

    <p>We normalize each unit individually and construct a sequence of
    the normalized results interleaved with empty text nodes.  The
    empty text nodes guarantee that the results of evaluating
    consecutive element-content units can be distinguished.  Then we
    apply the function <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>.  <xspecref spec="XQ" ref="id-element-constructor"/> specifies the rules for converting
    a sequence of atomic values and nodes into a sequence of nodes
    before element construction.  The Formal Semantics function
    <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> implements these conversion rules.</p>

      <mapping>
        <xquery>
          <expression><map><a href="#doc-fs-ElementContentUnit"><emph>ElementContentUnit</emph></a><subscript>1</subscript></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a></expression>
        </xquery>
        <core>
          <expression><a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>((<map> <a href="#doc-fs-ElementContentUnit"><emph>ElementContentUnit</emph></a><subscript>1</subscript></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a>))
          </expression>
        </core>
      </mapping>

      <mapping>
        <xquery>
          <expression><map><a href="#doc-fs-ElementContentUnit"><emph>ElementContentUnit</emph></a><subscript>1</subscript>, ...,  <a href="#doc-fs-ElementContentUnit"><emph>ElementContentUnit</emph></a><subscript>n</subscript></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a></expression>
        </xquery>
        <core>
          <expression><a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>((<map> <a href="#doc-fs-ElementContentUnit"><emph>ElementContentUnit</emph></a><subscript>1</subscript> </map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a> , text { "" }, ..., text { "" }, <map> <a href="#doc-fs-ElementContentUnit"><emph>ElementContentUnit</emph></a><subscript>n</subscript></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a>))
          </expression>
        </core>
      </mapping>

    <p>We must distinguish between the results of consecutive
    element-content units, because the rule for converting sequences
    of atomic values into strings applies to sequences within distinct
    enclosed expressions.  The empty text nodes are eliminated during
    evaluation of <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> when consecutive text
    nodes are coalesced into a single text node.  The text node
    guarantees that a whitespace character will not be inserted
    between atomic values computed by distinct enclosed expressions.
    For example, here is an expression, its normalization, and the
    resulting XML value:</p>

<eg xml:space="preserve">
&lt;example&gt;{ 1 }{ 2 }&lt;/example&gt;
 =
element example { fs:item-sequence-to-node-sequence ((1, text {""}, 2)) }
 ==&gt;
&lt;example&gt;12&lt;/example&gt;
</eg>

      <p>In the absence of the empty text node, the expression would
      evaluate to the following incorrect value:</p>

<eg xml:space="preserve">
&lt;example&gt;{ 1 }{ 2 }&lt;/example&gt;
 (incorrect normalization) =
element example { fs:item-sequence-to-node-sequence ((1, 2)) }
 (incorrect value) ==&gt;
&lt;example&gt;1 2&lt;/example&gt;
</eg>

      <p>Next, we give the normalization rules for each
      element-content unit. The normalization rule for a contiguous
      sequence of characters assumes that the significant whitespace
      characters in element constructors have been preserved, as
      described in <specref ref="sec_whitespace"/>.</p>

      <p>The following normalization rule takes the longest
      consecutive sequence of individual characters that include
      literal characters, escaped curly braces, character references,
      and predefined entity references and normalizes the character
      sequence as a text node containing the string of characters.</p>

      <mapping>
        <xquery>
       <expression><map><a href="#doc-fs-DirCharsUnit"><emph>DirCharsUnit</emph></a></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a></expression>
        </xquery>
        <core>
       <expression>text { <map> <a href="#doc-fs-DirCharsUnit"><emph>DirCharsUnit</emph></a>
       </map><a href="#jd_map_element_content" class="judgment"><subscript>DirCharsUnits</subscript></a> }</expression>
        </core>
      </mapping>

    <p>The application of <map/><a href="#jd_map_element_content" class="judgment"><subscript>DirCharsUnits</subscript></a> to
    <a href="#doc-fs-DirCharsUnit"><emph>DirCharsUnit</emph></a> is defined informally. It produces a string
    literal corresponding to the content of the <a href="#doc-fs-DirCharsUnit"><emph>DirCharsUnit</emph></a>, in
    boundary whitespace is processed and non-literal characters
    (CharRefs, PredefinedEntityRefs, CDataSections, and
    escaped-braces) are resolved according to the rules in <xspecref spec="XQ" ref="id-content"/>.</p>

    <p>XML processing instructions and comments in element content are
    normalized by applying the standard normalization rules for
    expressions, which appear in <specref ref="sec_other_constructors"/>.</p>

      <mapping>
      <xquery>
        <expression><map><emph>DirPIConstructor</emph></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a></expression>
      </xquery>
      <core>
        <expression><map><emph>DirPIConstructor</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
      </core>
      </mapping>

      <mapping>
      <xquery>
        <expression><map><emph>DirCommentConstructor</emph></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a></expression>
      </xquery>
      <core>
        <expression><map><emph>DirCommentConstructor</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
      </core>
      </mapping>

    <p>A direct element constructor is normalized using the
    normalization rule for expressions.</p>

      <mapping>
      <xquery>
        <expression><map><emph>DirElemConstructor</emph></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a></expression>
      </xquery>
      <core>
        <expression><map><emph>DirElemConstructor</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
      </core>
      </mapping>

    <p>An enclosed expression in element content is normalized by
    normalizing each individual expression in its expression sequence
    and then constructing a sequence of the normalized values:</p>

      <mapping>
        <xquery>
          <expression><map>
            { <emph>Expr</emph><subscript>1</subscript>, ..., <emph>Expr</emph><subscript>n</subscript> }
          </map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContentUnit</subscript></a></expression>
        </xquery>
        <core>
          <expression><map> <emph>Expr</emph><subscript>1</subscript> </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> , ..., <map> <emph>Expr</emph><subscript>n</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
        </core>
      </mapping>
      </smnorm>

      <smtype>
        <p>There are no additional static typing rules for direct XML
        element or attribute constructors.</p>
      </smtype>

      <smeval>
        <p>There are no additional dynamic evaluation rules for direct
        XML element or attribute constructors.</p>
      </smeval>

      <div4 id="sec_direct_attributes">
        <head>Attributes</head>

        <p>Like direct element constructors, direct attribute
        constructors are normalized to computed attribute
        constructors.</p>

      <smnotation>
        <p id="jd_map_attr_content">The auxiliary mapping rules
        <map/><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>,
        <map/><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a>, and
        <map/><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContentUnit</subscript></a>, are defined in
        this section and are used for the normalization of direct
        attribute constructors.</p>

        <p>We use the following grammar productions to represent
        <emph>AttributeContentUnit</emph>s, i.e., the expressions used to
        compute the content of an attribute.</p>

      <scrap headstyle="show">
      <head/>
      <prod num="87 (Formal)" id="doc-fs-AttributeContentUnits"><lhs>AttributeContentUnits</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-fs-AttributeContentUnit" xlink:type="simple">AttributeContentUnit</nt>*</rhs></prod>
      <prod num="88 (Formal)" id="doc-fs-AttributeContentUnit"><lhs>AttributeContentUnit</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-EscapeQuot" xlink:type="simple">EscapeQuot</nt>  |  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-QuotAttrValueContent" xlink:type="simple">QuotAttrValueContent</nt></rhs></prod>
      </scrap>
      </smnotation>

      <smnorm>
        <p id="sec_attribute_normalization">Direct attributes may
        contain namespace-declaration attributes.  The normalization
        rules in this section ignore namespace-declaration attributes
        -- they are handled by the normalization rules in <specref ref="sec_namespace_attrs"/>.</p>

        <p>A <emph>DirAttributeList</emph> is normalized by the following
        rule, which maps each of the individual attribute-value
        expressions in the attribute list and constructs a sequence of
        the normalized values.</p>

      <mapping>
      <xquery>
        <expression>
      <map><table><tbody>
      <tr><td rowspan="1" colspan="1">
          <emph>QName</emph><subscript>1</subscript> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a><subscript>1</subscript>
      </td></tr>
      <tr><td rowspan="1" colspan="1">
          ...
      </td></tr>
      <tr><td rowspan="1" colspan="1">
          <emph>QName</emph><subscript>n</subscript> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a><subscript>n</subscript>
      </td></tr>
      </tbody></table></map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>
        </expression>
        </xquery>
      <core>
      <expression>
      <table><tbody>
      <tr><td rowspan="1" colspan="1">
        (<map><emph>QName</emph><subscript>1</subscript> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a><subscript>1</subscript> </map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>
      </td></tr>
      <tr><td rowspan="1" colspan="1">
         ..., 
      </td></tr>
      <tr><td rowspan="1" colspan="1">
        <map><emph>QName</emph><subscript>n</subscript> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a><subscript>n</subscript></map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>)
      </td></tr>
      </tbody></table>
      </expression>
      </core>
      </mapping>

      <p id="jd_map_attribute">Namespace-declaration attributes, i.e.,
      those attributes whose prefix is <code>xmlns</code> are ignored
      by mapping them to the empty sequence.</p>

      <mapping>
      <xquery>
        <expression>
<table><tbody><tr><td rowspan="1" colspan="1">
      <map><emph>Prefix</emph>:<emph>LocalPart</emph> S? = S?
      <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a></map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>
</td></tr>
<tr><td rowspan="1" colspan="1">
      (<emph>Prefix</emph> = <code>xmlns</code>)
</td></tr>
</tbody></table>
        </expression>
        </xquery>
      <core>
      <expression>
        ()
      </expression>
      </core>
      </mapping>

      <p>All attributes that are not namespace-declaration attributes
      are mapped to computed attribute constructors.</p>

      <mapping>
      <xquery>
        <expression>
<table><tbody><tr><td rowspan="1" colspan="1">
      <map><emph>Prefix</emph>:<emph>LocalPart</emph> S? = S? "
      <emph>AttributeContentUnits</emph> "</map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>
</td></tr>
<tr><td rowspan="1" colspan="1">
      <a href="#jd_not" class="judgment"><term>not</term></a>(<emph>Prefix</emph> = <code>xmlns</code>)
</td></tr>
</tbody></table>
        </expression>
        </xquery>
      <core>
      <expression>
        attribute <emph>Prefix</emph>:<emph>LocalPart</emph> { <map><emph>AttributeContentUnits</emph></map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a>}
      </expression>
      </core>
      </mapping>

      <p>As with literal XML elements, we need to distinguish between
      direct attribute constructors that contain one attribute-content
      unit and those that contain multiple attribute-content units,
      because the rule for converting sequences of atomic values into
      strings is applied to sequences within distinct enclosed
      expressions.  If the direct attribute constructor contains
      exactly one attribute-content unit, we simply normalize that
      unit by applying the normalization rule for attribute content
      units:</p>

      <mapping>
      <xquery>
        <expression><map> <emph>AttributeContentUnit</emph><subscript>1</subscript>
          </map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a></expression>
      </xquery>
      <core>
        <expression>
          <map><emph>AttributeContentUnit</emph><subscript>1</subscript></map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContentUnit</subscript></a>
        </expression>
      </core>
      </mapping>

      <p>If the direct attribute constructor contains more than one
      attribute-content unit, we normalize each unit individually and
      construct a sequence of the normalized results interleaved with
      empty text nodes.  The empty text nodes guarantee that the
      results of evaluating consecutive attribute-content units can be
      distinguished.  Then we apply the function
      <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>, which applies the appropriate
      conversion rules to the normalized attribute content:</p>

      <mapping>
      <xquery>
        <expression><map> <emph>AttributeContentUnit</emph><subscript>1</subscript> ...
          <emph>AttributeContentUnit</emph><subscript>n</subscript>
          </map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a></expression>
      </xquery>
      <core>
        <expression>
          <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>((<map> <emph>AttributeContentUnit</emph><subscript>1</subscript>
          </map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContentUnit</subscript></a> , text { "" }, ..., text
          {""}, <map>
          <emph>AttributeContentUnit</emph><subscript>n</subscript></map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContentUnit</subscript></a>))
        </expression>
      </core>
      </mapping>

      <p>Literal characters, escaped curly braces, character
      references, and predefined entity references in attribute
      content are treated as in element content.  In addition, the
      normalization rule for characters in attributes assumes:</p>

      <olist>
        <item><p>that an escaped single or double quote is converted
        to an individual single or double quote.</p></item>
      </olist>

      <p>The following normalization rules take the longest
      consecutive sequence of individual characters that include
      literal characters, escaped curly braces, escaped quotes,
      character references, predefined entity references, and escaped
      single and double quotes and normalizes the character sequence
      as a string. </p>

      <mapping>
        <xquery>
          <expression><map>( <emph>Char</emph> | <emph>CharRef</emph> |
          <emph>EscapeQuot</emph> | <emph>EscapeApos</emph> | <emph>PredefinedEntityRef</emph>
          ) +</map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContentUnit</subscript></a></expression>
        </xquery>
        <core>
          <expression><code>fn:codepoints-to-string</code>(( <emph>Char</emph> | <emph>CharRef</emph> |
          <emph>EscapeQuot</emph> | <emph>EscapeApos</emph> | <emph>PredefinedEntityRef</emph>
          )+)</expression>
        </core>
      </mapping>

      <p>We normalize an enclosed expression by normalizing each
      individual expression in its expression sequence and then
      constructing a sequence of the normalized values:</p>

      <mapping>
      <xquery>
        <expression><map> { <emph>Expr</emph><subscript>1</subscript>, ..., <emph>Expr</emph><subscript>n</subscript> }
          </map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContentUnit</subscript></a></expression>
      </xquery>
      <core>
        <expression>(<map> <emph>Expr</emph><subscript>1</subscript> </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> , ..., <map>
        <emph>Expr</emph><subscript>n</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)
        </expression>
      </core>
      </mapping>
    </smnorm>
    </div4>

    <div4 id="sec_namespace_attrs">
      <head>Namespace Declaration Attributes</head>

      <smnotation>
        <p id="jd_map_namespace_attr">The auxiliary mapping rules
        <map/><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>, and
        <map/><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a> are defined in this
        section and are used for the normalization of namespace
        declaration attributes.</p>
      </smnotation>

      <smnorm>
        <p>Some direct attributes may be namespace-declaration
        attributes.  The normalization rules for namespace-declaration
        attributes ignore all non-namespace attributes -- they are
        handled by the normalization rules in <specref ref="sec_direct_attributes"/>.</p>

        <p>A <emph>DirAttributeList</emph> containing namespace-declaration
        attributes is normalized by the following rule, which maps
        each of the individual namespace-declaration attributes in the
        attribute list and constructs a sequence of the normalized
        namespace attribute values.</p>

      <mapping>
      <xquery>
        <expression>
      <map><table><tbody>
      <tr><td rowspan="1" colspan="1">
          <emph>QName</emph><subscript>1</subscript> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a><subscript>1</subscript>
      </td></tr>
      <tr><td rowspan="1" colspan="1">
          ...
      </td></tr>
      <tr><td rowspan="1" colspan="1">
          <emph>QName</emph><subscript>n</subscript> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a><subscript>n</subscript>
      </td></tr>
      </tbody></table></map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a>
        </expression>
        </xquery>
      <core>
      <expression>
      <table><tbody>
      <tr><td rowspan="1" colspan="1">
        (<map><emph>QName</emph><subscript>1</subscript> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a><subscript>1</subscript></map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>
      </td></tr>
      <tr><td rowspan="1" colspan="1">
         ..., 
      </td></tr>
      <tr><td rowspan="1" colspan="1">
        <map><emph>QName</emph><subscript>n</subscript> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a><subscript>n</subscript></map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>)
      </td></tr>
      </tbody></table>
      </expression>
      </core>
      </mapping>

      <p>Attributes whose prefix is not <code>xmlns</code> are ignored
      by mapping them to the empty sequence.</p>

      <mapping>
      <xquery>
        <expression>
<table><tbody><tr><td rowspan="1" colspan="1">
      <map><emph>Prefix</emph>:<emph>LocalPart</emph> S? = S? <a href="#doc-xquery-DirAttributeValue"><emph>DirAttributeValue</emph></a></map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>
</td></tr>
<tr><td rowspan="1" colspan="1">
      <a href="#jd_not" class="judgment"><term>not</term></a>(<emph>Prefix</emph> = <code>xmlns</code>)
</td></tr>
</tbody></table>
</expression>
        </xquery>
      <core>
      <expression>
        ()
      </expression>
      </core>
      </mapping>

      <p>Namespace-declaration attributes are normalized to local
      namespace declarations (<a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a>). The content of
      such attributes must be defined with a URI literal.</p>

      <mapping>
      <xquery>
        <expression>
<table><tbody><tr><td rowspan="1" colspan="1">
      <map><emph>Prefix</emph>:<emph>LocalPart</emph> S? = S? " <emph>URILiteral</emph> "</map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>
</td></tr>
<tr><td rowspan="1" colspan="1">
      (<emph>Prefix</emph> = <code>xmlns</code>)
</td></tr>
</tbody></table>
        </expression>
        </xquery>
      <core>
      <expression>
        namespace <emph>LocalPart</emph> { <emph>URILiteral</emph> }
      </expression>
      </core>
      </mapping>

    </smnorm>
    </div4>

    <div4 id="sec_content">
      <head>Content</head>
<p>The rules for normalizing element content are given above in
<specref ref="id_element_constructor"/>.</p>
    </div4>

    <div4 id="sec_whitespace">
      <head>Boundary Whitespace</head>

      <p><xspecref spec="XQ" ref="id-whitespace"/> describes how
      whitespace is processed in element constructors depending on the
      value of the <code>boundary-space</code> declaration in the
      query prolog. The Formal Semantics assumes that the rules for
      handling whitespace are applied by the (informally defined)
      auxiliary rule <map/><a href="#jd_map_element_content" class="judgment"><subscript>DirCharsUnits</subscript></a>.</p> </div4>

  </div3>
  <!-- ***************** PI and Comments       ****** -->
  <div3 id="sec_other_constructors">
    <head>Other Direct Constructors</head>

    <scrap headstyle="show">
      <head>Other Constructors</head>
      <prod num="105 (XQuery)" id="doc-xquery-DirPIConstructor"><lhs>DirPIConstructor</lhs><rhs>"&lt;?"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-PITarget" xlink:type="simple">PITarget</nt>  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-S" xlink:type="simple">S</nt>  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirPIContents" xlink:type="simple">DirPIContents</nt>)?  "?&gt;"</rhs></prod>
      <prod num="106 (XQuery)" id="doc-xquery-DirPIContents"><lhs>DirPIContents</lhs><rhs>(<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-Char" xlink:type="simple">Char</nt>* - (Char* '?&gt;' Char*))</rhs></prod>
      <prod num="103 (XQuery)" id="doc-xquery-DirCommentConstructor"><lhs>DirCommentConstructor</lhs><rhs>"&lt;!--"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-DirCommentContents" xlink:type="simple">DirCommentContents</nt>  "--&gt;"</rhs></prod>
      <prod num="104 (XQuery)" id="doc-xquery-DirCommentContents"><lhs>DirCommentContents</lhs><rhs>((<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-Char" xlink:type="simple">Char</nt> - '-')  |  ('-'  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-Char" xlink:type="simple">Char</nt> - '-')))*</rhs></prod>
    </scrap>

    <smnotation>
      <p id="jd_map_characters">The auxiliary mapping rule
      <map/><a href="#jd_map_characters" class="judgment"><subscript>Characters</subscript></a> is defined in this section and
      used for the normalization of direct PI and comment
      constructors.</p>

      <p><map><emph>Char</emph>*</map><a href="#jd_map_characters" class="judgment"><subscript>Characters</subscript></a> takes the character
      content of a PI or comment constructor and yields a
      corresponding <emph>StringLiteral</emph>.</p>
    </smnotation>

    <smnorm>
      <p>A literal XML processing instruction is normalized into a
      computed processing-instruction constructor; its character
      content is converted to a string using the auxiliary mapping
      rule <map/><a href="#jd_map_characters" class="judgment"><subscript>Characters</subscript></a>.</p>

   <mapping>
    <xquery>
     <expression>
      <map>&lt;? <emph>NCName</emph> <emph>DirPIContents</emph> ?&gt;</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <map>processing-instruction <emph>NCName</emph> { <map><emph>DirPIContents</emph></map><a href="#jd_map_characters" class="judgment"><subscript>Characters</subscript></a> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </core>
   </mapping>

    <p>A literal XML comment is normalized into a computed comment
    constructor; its character content is converted to a string using
    the auxiliary mapping rule <map/><a href="#jd_map_characters" class="judgment"><subscript>Characters</subscript></a>.</p>

    <mapping>
    <xquery>
    <expression>
     <map>&lt;!-- <emph>DirCommentContents</emph> --&gt;</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
    </expression>
    </xquery>
    <core>
    <expression>
     <map>comment { <map><emph>DirCommentContents</emph></map><a href="#jd_map_characters" class="judgment"><subscript>Characters</subscript></a> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
    </expression>
    </core>
    </mapping>

    </smnorm>

    <smtype>
      <p>There are no additional static typing rules for direct
      processing-instruction or comment constructors.</p>
    </smtype>

    <smeval>
      <p>There are no additional dynamic evaluation rules for direct
      processing-instruction or comment constructors.</p>
    </smeval>

  </div3>
   
  <div3 id="sec_computedConstructors">
    <head>Computed Constructors</head>

    <scrap headstyle="show">
      <head>Computed Constructors</head>
      <prod num="109 (XQuery)" id="doc-xquery-ComputedConstructor"><lhs>ComputedConstructor</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CompDocConstructor" xlink:type="simple">CompDocConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CompElemConstructor" xlink:type="simple">CompElemConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CompAttrConstructor" xlink:type="simple">CompAttrConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CompTextConstructor" xlink:type="simple">CompTextConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CompCommentConstructor" xlink:type="simple">CompCommentConstructor</nt><br/>|  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-CompPIConstructor" xlink:type="simple">CompPIConstructor</nt></rhs></prod>
    </scrap>

  <div4 id="sec_comp_elem_constructor">
    <head>Computed Element Constructors</head>

    <smintro><p>This section describes the semantics of computed
    element constructors. Remember that direct element constructors
    are normalized into computed element constructors. This document
    does not formally specify how namespaces are copied. The semantics
    of namespaces copying in element constructors can be found in
    <bibref ref="xquery"/>.</p></smintro>

    <scrap headstyle="show">
      <head/>
      <prod num="111 (XQuery)" id="doc-xquery-CompElemConstructor"><lhs>CompElemConstructor</lhs><rhs>"element"  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt>  |  ("{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Expr" xlink:type="simple">Expr</nt>  "}"))  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-ContentExpr" xlink:type="simple">ContentExpr</nt>?  "}"</rhs></prod>
      <prod num="112 (XQuery)" id="doc-xquery-ContentExpr"><lhs>ContentExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Expr" xlink:type="simple">Expr</nt></rhs></prod>
    </scrap>

<!--
    <smnotation>
      <p id="jd_split_namespaces">Local namespace declarations may
      occur explicitly in a computed element constructor or may be the
      result of normalizing namespace-declaration attributes contained
      in direct element constructors.  For local element declarations
      that occur explicitly in a query, the immediately enclosing
      expression of the local namespace declaration
      (&gr_NamespaceBinding;) must be a computed element constructor;
      otherwise, as specified in &language;, a static error is
      raised.</p>
    </smnotation>
-->

    <smcore>
      <p>The Core grammar productions for computed element
      constructors are:</p>

      <scrap headstyle="show"><head>Computed Element Constructors</head>
      <prod num="67 (Core)" id="doc-core-CompElemConstructor"><lhs>CompElemConstructor</lhs><rhs>"element"  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt>  |  ("{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Expr" xlink:type="simple">Expr</nt>  "}"))  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-ContentExpr" xlink:type="simple">ContentExpr</nt>  "}"  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-NamespaceBinding" xlink:type="simple">NamespaceBinding</nt>*  "}"</rhs></prod>
      <prod num="69 (Core)" id="doc-core-ContentExpr"><lhs>ContentExpr</lhs><rhs><nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Expr" xlink:type="simple">Expr</nt></rhs></prod>
      <prod num="68 (Core)" id="doc-core-NamespaceBinding"><lhs>NamespaceBinding</lhs><rhs>"namespace"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-NCName" xlink:type="simple">NCName</nt>  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-URILiteral" xlink:type="simple">URILiteral</nt>  "}"</rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>If the content expression is missing, the computed element
      constructor is normalized as if its content expression was the
      empty sequence.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>element <emph>QName</emph> { }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
         <map>element <emph>QName</emph> { () }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </core>
      </mapping>

      <p>Computed element constructors are normalized using the
      <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> function over their content
      expression.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>element <emph>QName</emph> { <emph>Expr</emph> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
         element <emph>QName</emph> { <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
        </expression>
       </core>
      </mapping>

      <p>When the name of the element is also computed, the
      normalization rule applies atomization to the name
      expression.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>element { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
          element { <code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }{ <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
        </expression>
       </core>
      </mapping>
    </smnorm>

    <smnotation>
      <p id="jd_add_namespace_bindings">The following auxiliary
      judgment adds a sequence of namespace bindings to the static
      context.</p>

        <display>
          <clause>
          <expression><a href="#jd_add_namespace_bindings" class="judgment"><term>add namespace bindings</term></a>
          <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> <a href="#jd_add_namespace_bindings" class="judgment"><term>to</term></a>
          <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> <a href="#jd_add_namespace_bindings" class="judgment"><term>yields</term></a>
          <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></expression>
          </clause>
        </display>

      <p>This judgment is defined as follows.</p>

        <infergr>
          <infer>
            <prejudge>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#jd_add_namespace_bindings" class="judgment"><term>add namespace bindings</term></a> <a href="#jd_add_namespace_bindings" class="judgment"><term>to</term></a>
          <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>0</subscript> <a href="#jd_add_namespace_bindings" class="judgment"><term>yields</term></a>
          <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>0</subscript>
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause>
                  <expression><a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>1</subscript> = namespace <emph>LocalPart</emph><subscript>1</subscript> { <emph>URILiteral</emph><subscript>1</subscript> }</expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause>
                  <expression>
                    ...
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause>
                  <expression><a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>n</subscript> = namespace <emph>LocalPart</emph><subscript>n</subscript> { <emph>URILiteral</emph><subscript>n</subscript> }</expression>
                </clause>
              </multiclause>
 	  <multiclause>
 	    <clause>
            <environment><a href="#xq_default_dyn_env" class="env">dynEnvDefault</a></environment>
 	    <expression><emph>URILiteral</emph><subscript>1</subscript> <a href="#jd_has_atomic_value" class="judgment"><term>has atomic value</term></a> <emph>AnyURI</emph>
 	    </expression>
 	    </clause>
 	  </multiclause>
          <multiclause>
            <clause>
              <expression>
                ...
              </expression>
            </clause>
          </multiclause>
 	  <multiclause>
 	    <clause>
            <environment><a href="#xq_default_dyn_env" class="env">dynEnvDefault</a></environment>
 	    <expression><emph>URILiteral</emph><subscript>1</subscript> <a href="#jd_has_atomic_value" class="judgment"><term>has atomic value</term></a> <emph>AnyURI</emph>
 	    </expression>
 	    </clause>
 	  </multiclause>
          <multiclause>
          <clause>
            <expression><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript>.<a href="#xq_ns_env" class="env">namespace</a> =
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>0</subscript>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>LocalPart</emph><subscript>1</subscript> =&gt; (passive, <emph>AnyURI</emph><subscript>1</subscript>)</expression>
              </update></expression>
          </clause>
          </multiclause>
              <multiclause>
                <clause>
                  <expression>
                    ...
                  </expression>
                </clause>
              </multiclause>
          <multiclause>
          <clause>
            <expression><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript>.<a href="#xq_ns_env" class="env">namespace</a> =
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n-1</subscript>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>LocalPart</emph><subscript>n</subscript> =&gt; (passive, <emph>AnyURI</emph><subscript>n</subscript>)</expression>
              </update></expression>
          </clause>
          </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression>
                    <a href="#jd_add_namespace_bindings" class="judgment"><term>add namespace bindings</term></a>
          <a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>1</subscript> ... <a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>n</subscript> <a href="#jd_add_namespace_bindings" class="judgment"><term>to</term></a>
          <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>0</subscript> <a href="#jd_add_namespace_bindings" class="judgment"><term>yields</term></a>
          <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript>
                  </expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>
    </smnotation>

    <smtype>
      <p>The normalization rules of direct element and attribute
      constructors leave us with only the computed forms of
      constructors. The static semantics for constructors is defined
      on all the computed forms. The computed element constructor
      itself has two forms: one in which the element name is a literal
      <emph>QName</emph>, and the other in which the element name is a
      computed expression.</p>

      <p>A computed element constructor creates a new element with
      either the <xtermref spec="XQ" ref="dt-type-annotation"/>
      <a href="#dt-xs_untyped"><code>xs:untyped</code></a> (in strip construction mode), or with the <xtermref spec="XQ" ref="dt-type-annotation"/> <code>xs:anyType</code> (in preserve
      construction mode). The content expression must return a
      sequence of nodes with all the attribute nodes before any
      element, processing-instructions or comment nodes. Note that the
      type allows text nodes interleaved with attribute nodes in the
      beginning. Although this results in a looser type checking, this
      accounts for the possible presence of empty text nodes resulting
      from normalization of direct element constructors (See <specref ref="id_element_constructor"/>).</p>

      <infergr>
	<infer>
	  <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = preserve</expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
               <clause>
               <expression><a href="#jd_add_namespace_bindings" class="judgment"><term>add namespace bindings</term></a>
               <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> <a href="#jd_add_namespace_bindings" class="judgment"><term>to</term></a>
               <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> <a href="#jd_add_namespace_bindings" class="judgment"><term>yields</term></a>
               <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></expression>
               </clause>
 	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
              <expression><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
              (attribute*, (element | comment | processing-instruction)*) &amp; text* </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript></environment>
	            <expression> element <emph>QName</emph> {
	            <emph>Expr</emph> } { <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> element <emph>QName</emph> of type <code>xs:anyType</code>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
      </infergr>

      <infergr>
	<infer>
	  <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = strip</expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
               <clause>
               <expression><a href="#jd_add_namespace_bindings" class="judgment"><term>add namespace bindings</term></a>
               <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> <a href="#jd_add_namespace_bindings" class="judgment"><term>to</term></a>
               <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> <a href="#jd_add_namespace_bindings" class="judgment"><term>yields</term></a>
               <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></expression>
               </clause>
 	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
              (attribute*, (element | comment | processing-instruction)*) &amp; text* </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript></environment>
	            <expression> element <emph>QName</emph> {
	            <emph>Expr</emph> } { <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> element <emph>QName</emph> of type <a href="#dt-xs_untyped"><code>xs:untyped</code></a>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
      </infergr>

      <p>In case the element name is computed as well, the name
      expression must be of type <code>xs:QName</code>, <code>xs:string</code>, or
      <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>.</p>

      <infergr>
	<infer>
	  <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = preserve</expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
               <clause>
               <expression><a href="#jd_add_namespace_bindings" class="judgment"><term>add namespace bindings</term></a>
               <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> <a href="#jd_add_namespace_bindings" class="judgment"><term>to</term></a>
               <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> <a href="#jd_add_namespace_bindings" class="judgment"><term>yields</term></a>
               <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></expression>
               </clause>
 	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      (<code>xs:QName</code> | <code>xs:string</code> | <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>) </expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
              (attribute*, (element | comment | processing-instruction)*) &amp; text* </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript></environment>
	            <expression> element { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
	            { <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> element of type <code>xs:anyType</code>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
      </infergr>

      <infergr>
	<infer>
	  <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = strip</expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
               <clause>
               <expression><a href="#jd_add_namespace_bindings" class="judgment"><term>add namespace bindings</term></a>
               <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> <a href="#jd_add_namespace_bindings" class="judgment"><term>to</term></a>
               <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> <a href="#jd_add_namespace_bindings" class="judgment"><term>yields</term></a>
               <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></expression>
               </clause>
 	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      (<code>xs:QName</code> | <code>xs:string</code> | <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>) </expression>
	      </clause>
	    </multiclause>
	    <multiclause>
              <clause>
	      <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
	      <expression> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>2</subscript></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
              (attribute*, (element | comment | processing-instruction)*) &amp; text* </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript></environment>
	            <expression> element { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
	            { <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> element of type <a href="#dt-xs_untyped"><code>xs:untyped</code></a>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
      </infergr>

     </smtype>

     <smeval>
       <p>The following rules take a computed element constructor
       expression and construct an element node.  The dynamic
       semantics for computed element constructors is the most complex
       of all expressions in XQuery.  Here is how to read the rule
       below.</p>

       <p>First, the element's content expression is partitioned into
       the local namespace declarations and all other expressions, and
       the local namespace declarations are evaluated, yielding a
       sequence of namespace bindings.  The static environment is
       extended to include the new namespace bindings, which are all
       <a href="#term_active">active</a>.  In <xspecref spec="XQ" ref="id-namespaces"/>, it is implementation-defined whether
       undeclaration of namespace prefixes (by setting the namespace
       prefix to the zero-length string) in an element constructor is
       supported.  In the dynamic semantics below, we assume all local
       namespace declarations declare a binding of a prefix to a
       URI.</p>

       <p>Second, the expression is evaluated, and the resulting value
       <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> must match zero-or-more attributes followed by
       zero-or-more element, text, processing-instruction or comment
       nodes.</p>

       <p>Third, the namespace bindings are concatenated with the list
       of active namespaces in the namespace environment <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a>
       and the namespaces corresponding to the element's name and all
       attributes names.  The resulting sequence is the sequence of
       namespace bindings for the element.</p>

       <infergr>
	 <infer>
	   <prejudge>
	     <multiclause>
	       <clause>
		 <expression><emph>Expr</emph> = <emph>Expr</emph><subscript>0</subscript> </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
		 <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript>; <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		 <expression> <emph>Expr</emph><subscript>0</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
		 <environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                 <expression> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> (attribute*, (element | text | processing-instruction | comment)*)</expression>
	       </clause>
	     </multiclause>
	   </prejudge>
	   <postjudge>
	     <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a>; <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
	       <expression>
		     element <emph>QName</emph> { <emph>Expr</emph> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
		     element <emph>QName</emph> of type <code>xs:anyType</code> {
		     <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> } { }
	       </expression>
	     </clause>
	   </postjudge>
	 </infer>
       </infergr>

       <infergr>
	 <infer>
	   <prejudge>
	     <multiclause>
	       <clause>
		 <expression> <emph>Expr</emph> = <a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>n</subscript>, <emph>Expr</emph><subscript>0</subscript> </expression>
	       </clause>
	     </multiclause>

            <multiclause> 
            <clause>          
            <expression><a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>1</subscript> = namespace <emph>NCName</emph><subscript>1</subscript> { <emph>AnyURI</emph><subscript>1</subscript> }</expression>
            </clause>
            </multiclause>
	     <multiclause>
               <clause><expression>...</expression></clause>
             </multiclause>
            <multiclause>
            <clause>          
            <expression><a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>n</subscript> = namespace  <emph>NCName</emph><subscript>n</subscript> { <emph>AnyURI</emph><subscript>n</subscript> }</expression>
            </clause>
            </multiclause>
            <multiclause>
            <clause>
            <expression>
              <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> = 
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>NCName</emph><subscript>1</subscript> =&gt; (active, <emph>AnyURI</emph><subscript>1</subscript>)</expression>
              </update>
            </expression>
            </clause>
            </multiclause>
            <multiclause>
            <clause><expression>...</expression></clause>
            </multiclause>
            <multiclause>
            <clause>
            <expression>
              <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript> = 
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n-1</subscript>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>NCName</emph><subscript>n</subscript> =&gt; (active, <emph>AnyURI</emph><subscript>n</subscript>)</expression>
              </update>
            </expression>
            </clause>
            </multiclause>

	     <multiclause>
	       <clause>
		 <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript>; <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		 <expression><emph>Expr</emph><subscript>0</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
		 <environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                 <expression> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> (attribute*, (element | text | processing-instruction | comment)*)</expression>
	       </clause>
	     </multiclause>

	     <multiclause>
	       <clause>
		 <expression><a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> = 
		 <a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>1</subscript>,
		 ..., <a href="#doc-fs-NamespaceBinding"><emph>NamespaceBinding</emph></a><subscript>n</subscript>,
		 <a href="#fs_activeNS">fs:<code>active_ns</code></a>(<a href="#xq_stat_env_def" class="env">statEnv</a>),
		 <a href="#fs_ns_from_items">fs:<code>get_static_ns_from_items</code></a>(<a href="#xq_stat_env_def" class="env">statEnv</a>, <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript>)</expression>
	       </clause>
	     </multiclause>

	   </prejudge>
	   <postjudge>
	     <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a>; <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
	       <expression>
		     element <emph>QName</emph> { <emph>Expr</emph> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
		     element <emph>QName</emph> of type <code>xs:anyType</code> {
		     <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> } { <a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> }
	       </expression>
	     </clause>
	   </postjudge>
	 </infer>
       </infergr>

       <p>The dynamic evaluation of an element constructor with a
       computed name is similar.  There is one additional rule that
       checks that the value of the element's name expression matches
       <code>xs:QName</code>.</p>

       <infergr>
	 <infer>
	   <prejudge>
	     <multiclause>
	       <clause>
		 <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment><expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript>
		 </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
                 <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		 <expression><a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <code>xs:QName</code>
		 </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
		 <expression><emph>QName</emph> = <code>fn:prefix-from-QName</code>(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript>)<code>:</code><code>fn:local-name-from-QName</code>(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript>)</expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	     <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
	       <expression>
		     element <emph>QName</emph> { <emph>Expr</emph><subscript>2</subscript> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
	       </expression>
	     </clause>
	     </multiclause>
	   </prejudge>
	   <postjudge>
	     <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
	       <expression>
		     element { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }  <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
	       </expression>
	     </clause>
	   </postjudge>
	 </infer>
       </infergr>

    </smeval>

    </div4>

    <div4 id="sec_attribute_constructor">
      <head>Computed Attribute Constructors</head>

      <scrap headstyle="show">
        <head/>
        <prod num="113 (XQuery)" id="doc-xquery-CompAttrConstructor"><lhs>CompAttrConstructor</lhs><rhs>"attribute"  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-xquery-QName" xlink:type="simple">QName</nt>  |  ("{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Expr" xlink:type="simple">Expr</nt>  "}"))  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-xquery-Expr" xlink:type="simple">Expr</nt>?  "}"</rhs></prod>
      </scrap>

      <smcore>
        <p>The Core grammar production for computed attribute
        constructors is:</p>

      <scrap headstyle="show"><head>Computed Attribute Constructors</head>
      <prod num="70 (Core)" id="doc-core-CompAttrConstructor"><lhs>CompAttrConstructor</lhs><rhs>"attribute"  (<nt xmlns:xlink="http://www.w3.org/1999/xlink" def="prod-core-QName" xlink:type="simple">QName</nt>  |  ("{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Expr" xlink:type="simple">Expr</nt>  "}"))  "{"  <nt xmlns:xlink="http://www.w3.org/1999/xlink" def="doc-core-Expr" xlink:type="simple">Expr</nt>  "}"</rhs></prod>
      </scrap>
      </smcore>

      <smnorm>
        <p>Computed attribute constructors are normalized by mapping
        their name and content expression in a similar way as computed
        element constructors. The normalization rule uses the
        <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a> function.</p>

        <mapping>
         <xquery>
          <expression>
           <map>attribute <emph>QName</emph> { }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>
           <map>attribute <emph>QName</emph> { () }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </core>
        </mapping>

        <mapping>
         <xquery>
          <expression>
           <map>attribute <emph>QName</emph> { <emph>Expr</emph> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>attribute <emph>QName</emph> { <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
          </expression>
         </core>
        </mapping>

        <mapping>
         <xquery>
          <expression>
           <map>attribute { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>
            attribute { <code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) } { <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
          </expression>
         </core>
        </mapping>
    </smnorm>

    <smtype>
      <p>The normalization rules for direct attribute constructors
      leave us with only the computed form of the attribute
      constructors.  Like in a computed element constructor, a
      computed attribute constructor has two forms: one in which the
      attribute name is a literal <emph>QName</emph>, and the other in which
      the attribute name is a computed expression.</p>

      <p>In the case of attribute constructors, the <xtermref spec="XQ" ref="dt-type-annotation"/> is always
      <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a>.</p>

     <infergr>
	<infer>
	  <prejudge>
	    <multiclause>
              <clause>
	        <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	        <expression> <emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause>
                <environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                <expression><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#dt-xs_untypedAtomic"><code>xs:untypedAtomic</code></a></expression>
              </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause>
              <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	      <expression> attribute