<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE spec SYSTEM "../xquery-semantics.dtd">
<spec xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax" w3c-doctype="wd">
  <!-- W3C header, disclaimers and abstracts -->
       <header>
  <title>XQuery 1.0 and XPath 2.0 Formal Semantics</title>
  <w3c-designation>WD-xquery-semantics-20050915</w3c-designation>
  <w3c-doctype>W3C Working Draft</w3c-doctype>

  <pubdate>
   <day>15</day>
   <month>September</month>
   <year>2005</year>
  </pubdate>

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

  <altlocs>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2005/WD-xquery-semantics-20050915/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-20050603.html" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Revisions to LC Draft</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/2005/WD-xquery-semantics-20050603/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/2005/WD-xquery-semantics-20050603/</loc>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2005/WD-xquery-semantics-20050404/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/2005/WD-xquery-semantics-20050404/</loc>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2005/WD-xquery-semantics-20050211/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">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>

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

  <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 a public W3C Working Draft for review by W3C Members
    and other interested parties. Publication as a Working Draft does
    not imply endorsement by the W3C Membership. This is a draft
    document and may be updated, replaced or obsoleted by other
    documents at any time. It is inappropriate to cite this document
    as other than work in progress.</p>

    <p>XQuery 1.0, XPath 2.0, and their formal semantics has been
    defined jointly by the <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 <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> (both
    part of the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/Activity.html" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML
    Activity</loc>).</p>

    <p>This draft includes corrections and changes based on public
    comments on the Last Call Working Draft dated 03 June 2005.
These decisions
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/buglist.cgi?query_format=advanced&amp;short_desc_type=allwordssubstr&amp;short_desc=&amp;product=XPath+%2F+XQuery+%2F+XSLT&amp;component=Formal+Semantics&amp;version=Last+Call+drafts&amp;long_desc_type=allwordssubstr&amp;long_desc=&amp;bug_file_loc_type=allwordssubstr&amp;bug_file_loc=&amp;status_whiteboard_type=allwordssubstr&amp;status_whiteboard=&amp;keywords_type=allwords&amp;keywords=&amp;bug_status=NEW&amp;bug_status=ASSIGNED&amp;bug_status=REOPENED&amp;bug_status=RESOLVED&amp;bug_status=CLOSED&amp;emailtype1=substring&amp;email1=&amp;emailtype2=substring&amp;email2=&amp;bugidtype=include&amp;bug_id=&amp;votes=&amp;chfieldfrom=&amp;chfieldto=Now&amp;chfieldvalue=&amp;cmdtype=doit&amp;order=Reuse+same+sort+as+last+time&amp;field0-0-0=noop&amp;type0-0-0=noop&amp;value0-0-0=" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">are recorded</loc>
in the
<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">Bugzilla database</loc> (http://www.w3.org/Bugs/Public/).
 A list
    of changes since the Last Call Working Draft of 03 June 2005 can
    be found in <specref ref="id-fs-revisions-log"/>.</p>

    <p>A number of technical and editorial issues are still being
    processed by the Working Groups. Some of the main technical
    changes that are still not implemented in this working draft
    include improvements to the formal notations (Bugs <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1605" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1605]</loc>,<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1614" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1614]</loc>,
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1618" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1618]</loc>,
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1730" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1730]</loc>,
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1790" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1790]</loc>),
    fixes to bugs in the semantics of function calls (Bugs <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1582" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1582]</loc>,<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1583" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1583]</loc>,
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1820" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1820]</loc>),
    function and variable declarations (Bugs <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1743" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1743]</loc>,<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1964" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1964]</loc>,
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1965" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1965]</loc>),
    and fixes to the semantics of constructors (Bugs <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1628" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1628]</loc>,<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1629" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1629]</loc>,<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=1641" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">[1641]</loc>).</p>

<p>This draft is being provided to permit public review of the changes
that have been made as a result of the Last Call comments.
Comments on the
changes should be made against the pertinent Last Call comment
(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 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>
(archived 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>). Please start
the subject line with “[FS]” so comments can be classified correctly.</p>

<p>The XML Query and XSL Working Groups expect to progress this document to
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2004/02/Process-20040205/tr.html#RecsCR" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Candidate
Recommendation</loc> status in
the very near future.</p>

    <p>The patent policy for this document is 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>.  Patent disclosures
    relevant to this specification may be found on the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2002/08/xmlquery-IPR-statements" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Query
    Working Group's patent disclosure page</loc> and the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Style/XSL/Disclosures" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XSL Working Group's
    patent disclosure page</loc>. An individual who has actual
    knowledge of a patent which the individual believes contains
    Essential Claim(s) with respect to this specification should
    disclose the information in accordance with <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>

  <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 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>
    <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>
      <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_N10462.doc-xquery-FLWORExpr"><lhs>FLWORExpr</lhs><rhs>(<nt def="doc-xquery-ForClause">ForClause</nt>  |  <nt def="doc-xquery-LetClause">LetClause</nt>)+  <nt def="doc-xquery-WhereClause">WhereClause</nt>?  <nt def="doc-xquery-OrderByClause">OrderByClause</nt>?  "return"  <nt def="doc-xquery-ExprSingle">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_N10479.doc-xpath-ForExpr"><lhs>ForExpr</lhs><rhs><nt def="doc-xpath-SimpleForClause">SimpleForClause</nt>  "return"  <nt def="doc-xpath-ExprSingle">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="32 (Core)" id="noid_N10494.doc-core-FLWORExpr"><lhs>FLWORExpr</lhs><rhs>(<nt def="doc-core-ForClause">ForClause</nt>  |  <nt def="doc-core-LetClause">LetClause</nt>)  "return"  <nt def="doc-core-ExprSingle">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="40 (Formal)" id="noid_N104B5.doc-fs-Definition"><lhs>Definition</lhs><rhs>("define"  "element"  <nt def="doc-fs-ElementName">ElementName</nt>  <nt def="doc-fs-Substitution">Substitution</nt>?  <nt def="doc-fs-Nillable">Nillable</nt>?  <nt def="doc-fs-TypeReference">TypeReference</nt>)<br/>|  ("define"  "attribute"  <nt def="doc-fs-AttributeName">AttributeName</nt>  <nt def="doc-fs-TypeReference">TypeReference</nt>)<br/>|  ("define"  "type"  <nt def="doc-fs-TypeName">TypeName</nt>  <nt def="doc-fs-TypeDerivation">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 inference rules (see the next
      section). As a convenience, non-terminals used in inference
      rules link to the appropriate grammar production.</p>
    </div3>

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

    <div3>
      <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>Painting</emph> <term>is beautiful</term></expression>
          </clause>
        </display>

        <p>holds if the object <emph>Painting</emph> is beautiful.</p>
      </smnotation>

      <smnotation>
        <p>Here are three judgments that are used extensively in this
        document.</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 when 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, and
        written in bold fonts. For instance, the judgment</p>

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

        <p>holds <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. In general, symbols in a judgment are chosen to
      reflect its meaning. For example, '<term>is beautiful</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 written with italicized words. The name of a
      pattern is significant: each pattern name corresponds to an
      <quote>object</quote> (a value, a type, an expression, etc.)
      that can be substituted legally for the pattern. By convention,
      all patterns in the Formal Semantics correspond to grammar
      non-terminals, and are used to represent entities that can be
      constructed through application of the corresponding grammar
      production. For example, <emph>Expr</emph> represents any [XPath/XQuery]
      expression, and <a href="#doc-fs-Value"><emph>Value</emph></a> represents any value in the
      [XPath/XQuery] data model.</p>

      <p>When applying the judgment, each pattern must be
      <emph>instantiated</emph> 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 the
      value '3' (on the right-hand side of the <term>=&gt;</term>
      symbol).</p>

      <p>Patterns 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. Each distinct pattern must be instantiated to a
      single <quote>object</quote> (value, type, expression, etc.). If
      the same pattern occurs twice in a judgment description then it
      should be instantiated with the same <quote>object</quote>. For
      example, '3 <term>=&gt;</term> 3' is an instance of the
      judgment '<emph>Expr</emph><subscript>1</subscript> <term>=&gt;</term> <emph>Expr</emph><subscript>1</subscript>' but '$x+0
      <term>=&gt;</term> 3' is not since the two expressions '$x+0'
      and '3' cannot be both instance of the pattern <emph>Expr</emph><subscript>1</subscript>.  The
      judgment'$x+0 <term>=&gt;</term> 3' is an instance of the
      judgment '<emph>Expr</emph><subscript>1</subscript> <term>=&gt;</term> <emph>Expr</emph><subscript>2</subscript>'.</p>

      <p>Patterns may have a name that is not exactly the name of a
      grammar production 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 id="jd_not">In some cases, inference rules may need to use
      the fact that a certain judgment <emph>does not</emph> hold. We
      may write
      <expression><a href="#jd_not" class="judgment"><term>not</term></a>(<emph>Judgment</emph>)</expression> the
      judgment which holds iff <emph>Judgment</emph> does not
      hold.</p>

      <p id="jd_isin">In some cases, an <quote>object</quote> may take
      a value within a finite set of pre-determined values. We may
      write those set of possible value 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>which holds, if the object <emph>Color</emph> has either the
      value <emph>blue</emph> or the value <emph>green</emph>.</p>
    </div3>

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

    <div3>
      <head>Notations for inference rules</head>

      <p>Inference rules are used to specify whether a 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:</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. The
      interpretation of an inference rule is: if all the premise
      judgments above the line hold, then the conclusion judgment
      below the line also hold.</p>

      <p>Here is a simple example of inference rule, which uses 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 of the
      judgment '<emph>Expr</emph> <term>=&gt;</term> <a href="#doc-fs-Value"><emph>Value</emph></a>':
      <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:</p>

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

      <p>This inference rule expresses the following property of the
      judgment '<emph>Expr</emph> <term>=&gt;</term> <a href="#doc-fs-Value"><emph>Value</emph></a>': evaluating
      the literal expression '3' always yields the value '3'.</p>

      <p>The two above rules are expressed in terms of specific
      variables and values, but usually rules are more abstract.  That
      is, the judgments they relate contain patterns.  Here is a rule
      that says that for any variable <emph>Variable</emph> that yields the
      integer value <emph>Integer</emph>, adding '0' yields the same integer
      value:</p>

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

      <p>As in a judgment, 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
      one can talk about <quote>the value of <emph>Variable</emph></quote>
      instead of the value bound to the first (second, etc) occurrence
      of <emph>Variable</emph>.</p>
    </div3>

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

    <div3>
      <head>Notations for environments</head>

      <p>Logical inference rules use environments to record
      information computed during static type analysis or dynamic
      evaluation so that this information can be used by other logical
      inference rules. For example, the type signature of a
      user-defined function in a [expression/query] prolog can be recorded
      in an environment and used by subsequent rules.  Similarly, the
      value assigned to a variable within a "let" expression can be
      captured in an environment and accessed later on during
      evaluation when that variable is accessed.</p>

      <p id="xq_environment">An environment 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 or update
      the environment.</p>

      <p>If <quote>env</quote> is an environment, then
      <quote>env(<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 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>environment groups</emph> that group
      related environments.  If <quote>group</quote> is an environment
      group with the environment component <quote>env</quote>, then
      that environment is denoted <quote>group.env</quote> and the
      value that <emph>symbol</emph> is mapped to is denoted
      <quote><expression>group.env(<emph>symbol</emph>)</expression></quote>.</p>

      <p>The two main environment groups used in the Formal Semantics
      are: a dynamic environment group (<a href="#xq_dyn_env_def" class="env">dynEnv</a>), which models to
      the [XPath/XQuery]'s dynamic context, and a static environment group
      (<a href="#xq_stat_env_def" class="env">statEnv</a>), which models the [XPath/XQuery]'s 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
      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>Environment groups 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 group
      <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>Environment groups can be <emph>updated</emph>, using the
      following notation:</p>

      <ulist>
        <item>
          <p>
            <quote><expression><update><environment>group.env</environment>
            <expression><emph>symbol</emph> =&gt;
            <emph>object</emph></expression></update></expression></quote>
            denotes the new environment group that is identical to
            <emph>group</emph> except that the <emph>env</emph>
            environment 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>
            The following shorthand is also allowed:
            <quote><expression><update>
                  <environment>group.env</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>
                (group + env(
                <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>
<!-- Removed for consistency - Jerome
        <item>
          <p>If the <quote>object</quote> is a type then the following
          notation relates a symbol to a type:
          <quote><expression><update><environment>group.env</environment>
          <expression><emph>symbol</emph> :
          <emph>object</emph></expression></update></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 group 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 environment is used for the evaluation of the
      expression <code>$x + 2</code>. Then it is updated a second time
      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>

    <div3>
      <head>Putting it together</head>

      <p>Putting the above notations together, here is an example of
      an inference rule that occurs 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 expression below the line <quote><emph>Expr</emph><subscript>1</subscript> ,
      <emph>Expr</emph><subscript>2</subscript></quote> must have 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>. The above 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 group
      <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>Each inference rule describes a fragment of the semantics for
      a given expression. Here is how those rules are
      combined. Consider the following expression.</p>

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

      <p>We have just seen the static typing rule for the let clause
      and sequence construction. To handle this expression completely,
      we need inference rules for integer literals and variable
      access. Those two rules are as follows.</p>

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

      <infergr>
    	<infer>
 	  <prejudge>
 	    <multiclause>
 	      <clause>
              <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 	      <expression><emph>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>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_env" class="env">varType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></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>VarName</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>

      <p>With this set of rules, we can compute the type of the
      expression above in a bottom-up fashion, i.e., starting with the
      sub-expressions. The resulting type inference proceeds as
      follows.</p>

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

  statEnv0 |- 1 : xs:integer

  statEnv1 = ($x -&gt; 1)

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

  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>
    <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>
      <item><p><code>xdt:</code> for [XPath/XQuery] built-in
      types.</p></item>
    </ulist>

    <p>All 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 a URI.</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-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>, or
      <a href="#dt-xdt_anyAtomicType"><code>xdt: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-xdt_untypedAtomic"><code>xdt: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 def="doc-fs-Item">Item</nt><br/>|  (<nt def="doc-fs-Value">Value</nt>  ","  <nt def="doc-fs-Value">Value</nt>)<br/>|  ("("  ")")</rhs></prod>
        <prod num="21 (Formal)" id="doc-fs-Item"><lhs>Item</lhs><rhs><nt def="doc-fs-NodeValue">NodeValue</nt><br/>|  <nt def="doc-fs-AtomicValue">AtomicValue</nt></rhs></prod>
        <prod num="22 (Formal)" id="doc-fs-AtomicValue"><lhs>AtomicValue</lhs><rhs><nt def="doc-fs-AtomicValueContent">AtomicValueContent</nt>  <nt def="doc-fs-TypeAnnotation">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 def="doc-fs-TypeName">TypeName</nt></rhs></prod>
        <prod num="9 (Formal)" id="doc-fs-ElementValue"><lhs>ElementValue</lhs><rhs>"element"  <nt def="doc-fs-ElementName">ElementName</nt>  "nilled"?  <nt def="doc-fs-TypeAnnotation">TypeAnnotation</nt>?  "{"  <nt def="doc-fs-Value">Value</nt>  "}"  ("{"  <nt def="doc-fs-NamespaceBindings">NamespaceBindings</nt>  "}")?</rhs></prod>
        <prod num="10 (Formal)" id="doc-fs-AttributeValue"><lhs>AttributeValue</lhs><rhs>"attribute"  <nt def="doc-fs-AttributeName">AttributeName</nt>  <nt def="doc-fs-TypeAnnotation">TypeAnnotation</nt>?  "{"  <nt def="doc-fs-SimpleValue">SimpleValue</nt>  "}"</rhs></prod>
        <prod num="8 (Formal)" id="doc-fs-SimpleValue"><lhs>SimpleValue</lhs><rhs><nt def="doc-fs-AtomicValue">AtomicValue</nt><br/>|  (<nt def="doc-fs-SimpleValue">SimpleValue</nt>  ","  <nt def="doc-fs-SimpleValue">SimpleValue</nt>)<br/>|  ("("  ")")</rhs></prod>
        <prod num="11 (Formal)" id="doc-fs-DocumentValue"><lhs>DocumentValue</lhs><rhs>"document"  "{"  <nt def="doc-fs-Value">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 def="prod-fs-QName">QName</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 def="doc-fs-ElementValue">ElementValue</nt><br/>|  <nt def="doc-fs-AttributeValue">AttributeValue</nt><br/>|  <nt def="doc-fs-DocumentValue">DocumentValue</nt><br/>|  <nt def="doc-fs-TextValue">TextValue</nt><br/>|  <nt def="doc-fs-CommentValue">CommentValue</nt><br/>|  <nt def="doc-fs-ProcessingInstructionValue">ProcessingInstructionValue</nt></rhs></prod>
        <prod num="3 (Formal)" id="doc-fs-ElementName"><lhs>ElementName</lhs><rhs><nt def="prod-fs-QName">QName</nt></rhs></prod>
        <prod num="6 (Formal)" id="doc-fs-AttributeName"><lhs>AttributeName</lhs><rhs><nt def="prod-fs-QName">QName</nt></rhs></prod>
        <prod num="23 (Formal)" id="doc-fs-TypeName"><lhs>TypeName</lhs><rhs><nt def="prod-fs-QName">QName</nt></rhs></prod>
        <prod num="15 (Formal)" id="doc-fs-NamespaceBindings"><lhs>NamespaceBindings</lhs><rhs><nt def="doc-fs-NamespaceBinding">NamespaceBinding</nt>  (","  <nt def="doc-fs-NamespaceBinding">NamespaceBinding</nt>)*</rhs></prod>
        <prod num="17 (Formal)" id="doc-fs-NamespaceBinding"><lhs>NamespaceBinding</lhs><rhs>"namespace"  <nt def="prod-fs-NCName">NCName</nt>  "{"  String  "}"</rhs></prod>
      </scrap>

      <smnotation>
        <p>In that grammar, <quote>String</quote> indicates the value
        space of <code>xs:string</code>, <quote>Decimal</quote> indicates the
        value space of <code>xs:decimal</code>, etc.</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-xdt_untyped"><code>xdt:untyped</code></a>,
      untyped attributes have the <xtermref spec="XQ" ref="dt-type-annotation"/> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>, and untyped
      atomic values have the <xtermref spec="XQ" ref="dt-type-annotation"/> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>.</p>

      <p>An element has an optional <quote>nilled</quote> marker. This
      marker can only be present 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>

      <p>In examples, we omit the namespace bindings when they are
      empty.  For example, the following two values are the same (note
      that the <code>xs</code> and <code>xdt</code> prefixes are
      built-in):</p>

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

      <p>The same rule about constructing sequences apply to the
      values described by that grammar. Notably sequences are
      automatically flattened. For example, the sequence <code>(10,
      (1, 2), (), (3, 4))</code> is equivalent to the sequence
      <code>(10, 1, 2, 3, 4)</code>. Those rules are described in more
      details in <bibref ref="xpath-datamodel"/>.</p>

      <p>When the context is clear, we may omit the <xtermref spec="XQ" ref="dt-type-annotation"/> on literal values. For
      instance:</p>

<eg xml:space="preserve">
  "Hello World!"   instead of   "Hello World!" of type xs:string
  10               instead of   10 of type xs:integer
</eg>
    
    </div3>

    <div3>
      <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 xdt:untyped {
    text { "The cat weighs " },
    element weight of type xdt:untyped {
      attribute units of type xdt:untypedAtomic {
        "lbs" of type xdt: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 xdt:untyped {
    attribute xsi:type of type xdt:untypedAtomic {
      "xs:integer" of type xdt: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 xdt: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 xdt: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>A nillable element with <code>xsi:type</code> set to
      true:</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 xdt:untyped {
    attribute xsi:nil of type xdt:untypedAtomic { "true" of type xdt: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 xdt: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>
      <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="26 (Formal)" id="doc-fs-FormalItemType"><lhs>FormalItemType</lhs><rhs><nt def="doc-fs-AtomicTypeName">AtomicTypeName</nt>  |  <nt def="doc-fs-NodeType">NodeType</nt></rhs></prod>
        <prod num="29 (Formal)" id="doc-fs-AtomicTypeName"><lhs>AtomicTypeName</lhs><rhs><nt def="prod-fs-QName">QName</nt></rhs></prod>
        <prod num="27 (Formal)" id="doc-fs-NodeType"><lhs>NodeType</lhs><rhs><nt def="doc-fs-DocumentType">DocumentType</nt><br/>|  <nt def="doc-fs-AttributeType">AttributeType</nt><br/>|  <nt def="doc-fs-ElementContentType">ElementContentType</nt></rhs></prod>
        <prod num="28 (Formal)" id="doc-fs-ElementContentType"><lhs>ElementContentType</lhs><rhs><nt def="doc-fs-ElementType">ElementType</nt><br/>|  "comment"<br/>|  "processing-instruction"<br/>|  "text"</rhs></prod>
        <prod num="30 (Formal)" id="doc-fs-ElementType"><lhs>ElementType</lhs><rhs>"element"  <nt def="doc-fs-ElementName">ElementName</nt>?  <nt def="doc-fs-TypeSpecifier">TypeSpecifier</nt>?</rhs></prod>
        <prod num="31 (Formal)" id="doc-fs-TypeSpecifier"><lhs>TypeSpecifier</lhs><rhs><nt def="doc-fs-Nillable">Nillable</nt>?  <nt def="doc-fs-TypeReference">TypeReference</nt></rhs></prod>
        <prod num="32 (Formal)" id="doc-fs-AttributeType"><lhs>AttributeType</lhs><rhs>"attribute"  <nt def="doc-fs-AttributeName">AttributeName</nt>?  <nt def="doc-fs-TypeReference">TypeReference</nt>?</rhs></prod>
        <prod num="33 (Formal)" id="doc-fs-Nillable"><lhs>Nillable</lhs><rhs>"nillable"</rhs></prod>
        <prod num="37 (Formal)" id="doc-fs-TypeReference"><lhs>TypeReference</lhs><rhs>"of"  "type"  <nt def="doc-fs-TypeName">TypeName</nt></rhs></prod>
        <prod num="49 (Formal)" id="doc-fs-DocumentType"><lhs>DocumentType</lhs><rhs>"document"  ("{"  <nt def="doc-fs-Type">Type</nt>  "}")?</rhs></prod>
      </scrap>

      <p>An element or attribute type has an optional name 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. The word "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 union types (e.g., <code>element | attribute | text
        | comment | processing-instruction</code>) and therefore do
        not appear here. 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
</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 def="doc-fs-FormalItemType">FormalItemType</nt><br/>|  (<nt def="doc-fs-Type">Type</nt>  <nt def="doc-fs-Occurrence">Occurrence</nt>)<br/>|  (<nt def="doc-fs-Type">Type</nt>  "&amp;"  <nt def="doc-fs-Type">Type</nt>)<br/>|  (<nt def="doc-fs-Type">Type</nt>  ","  <nt def="doc-fs-Type">Type</nt>)<br/>|  (<nt def="doc-fs-Type">Type</nt>  "|"  <nt def="doc-fs-Type">Type</nt>)<br/>|  "empty"<br/>|  "none"<br/>|  ("("  <nt def="doc-fs-Type">Type</nt>  ")")</rhs></prod>
        <prod num="25 (Formal)" id="doc-fs-Occurrence"><lhs>Occurrence</lhs><rhs>"*"  |  "+"  |  "?"</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>
      <code>&amp;</code> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> matches any sequence that is an
      interleaving of a sequence that matches <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> and a
      sequence that matches <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript>. The interleaved product
      captures the semantics of <term>all groups</term> in XML Schema,
      but it more general has 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>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, the first one matching Type1 and the second
      matching Type2. Where 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>.</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><th rowspan="1" colspan="1">Associativity</th></tr>
      <tr><td rowspan="1" colspan="1">1</td><td rowspan="1" colspan="1">| (choice)</td><td rowspan="1" colspan="1">left-to-right</td></tr>
      <tr><td rowspan="1" colspan="1">2</td><td rowspan="1" colspan="1">&amp; (interleaving)</td><td rowspan="1" colspan="1">right-to-left</td></tr>
      <tr><td rowspan="1" colspan="1">3</td><td rowspan="1" colspan="1">, (sequence)</td><td rowspan="1" colspan="1">left-to-right</td></tr>
      <tr><td rowspan="1" colspan="1">4</td><td rowspan="1" colspan="1">*, +, ? (occurrence)</td><td rowspan="1" colspan="1">left-to-right</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>      

    </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="41 (Formal)" id="doc-fs-Definitions"><lhs>Definitions</lhs><rhs><nt def="doc-fs-Definition">Definition</nt>*</rhs></prod>
      <prod num="40 (Formal)" id="doc-fs-Definition"><lhs>Definition</lhs><rhs>("define"  "element"  <nt def="doc-fs-ElementName">ElementName</nt>  <nt def="doc-fs-Substitution">Substitution</nt>?  <nt def="doc-fs-Nillable">Nillable</nt>?  <nt def="doc-fs-TypeReference">TypeReference</nt>)<br/>|  ("define"  "attribute"  <nt def="doc-fs-AttributeName">AttributeName</nt>  <nt def="doc-fs-TypeReference">TypeReference</nt>)<br/>|  ("define"  "type"  <nt def="doc-fs-TypeName">TypeName</nt>  <nt def="doc-fs-TypeDerivation">TypeDerivation</nt>)</rhs></prod>
      <prod num="42 (Formal)" id="doc-fs-Substitution"><lhs>Substitution</lhs><rhs>"substitutes"  "for"  <nt def="doc-fs-ElementName">ElementName</nt></rhs></prod>
      <prod num="34 (Formal)" id="doc-fs-TypeDerivation"><lhs>TypeDerivation</lhs><rhs><nt def="doc-fs-ComplexTypeDerivation">ComplexTypeDerivation</nt>  |  <nt def="doc-fs-AtomicTypeDerivation">AtomicTypeDerivation</nt></rhs></prod>
      <prod num="35 (Formal)" id="doc-fs-ComplexTypeDerivation"><lhs>ComplexTypeDerivation</lhs><rhs><nt def="doc-fs-Derivation">Derivation</nt>?  <nt def="doc-fs-Mixed">Mixed</nt>?  "{"  <nt def="doc-fs-Type">Type</nt>?  "}"</rhs></prod>
      <prod num="36 (Formal)" id="doc-fs-AtomicTypeDerivation"><lhs>AtomicTypeDerivation</lhs><rhs>"restricts"  <nt def="doc-fs-AtomicTypeName">AtomicTypeName</nt></rhs></prod>
      <prod num="38 (Formal)" id="doc-fs-Derivation"><lhs>Derivation</lhs><rhs>("restricts"  <nt def="doc-fs-TypeName">TypeName</nt>)<br/>|  ("extends"  <nt def="doc-fs-TypeName">TypeName</nt>)</rhs></prod>
      <prod num="39 (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 wether it is derived by extension or restriction, its
       base type, and its content model, with an optional flag
       indicating if it has mixed content. 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>

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

       <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 respresented as follow</p>

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

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

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

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

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

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

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

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

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

         <p>An element declaration</p>

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

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

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

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

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

       </smexamples>

     </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"/>.
       <!-- The complete mapping from XML Schema into the
       &language; type system is given in <specref
       ref="sec_importing_schema"/>. --></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>
    <head>Functions and operators</head>

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

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

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

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

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

</div1>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a> environment stores the
                static type signatures of functions. Because
                [XPath/XQuery] allows multiple functions with the same
                name differing only in the number and signature of the
                parameters, this environment maps an expanded
                <emph>QName</emph> and an arity to a function signatures of
                the form <quote><code>define function</code>
                <emph>QName</emph> (<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>)
                <code>return</code> <a href="#doc-fs-Type"><emph>Type</emph></a></quote>.</td></tr>
              </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

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

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

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

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

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

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

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

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

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

      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_boundary_space" valign="top" rowspan="1" colspan="1"><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_boundary