<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 item