<?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_space" class="env">boundarySpace</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_boundary_space" class="env">boundarySpace</a> environment
                corresponds to the <term>boundary-space policy</term>
                in the [XPath/XQuery] static context.</td></tr>

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

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

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

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

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

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

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

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

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

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

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

      <infergr>
        <infer>
         <prejudge>
          <multiclause>
          <clause>
            <environment>
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>NCName</emph> =&gt; (passive, <emph>URI</emph>)</expression>
              </update>
            </environment>
            <expression><emph>Expr</emph>*</expression>
          </clause>
          </multiclause>
         </prejudge>
         <postjudge>
          <clause>
           <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
           <expression><code>declare namespace</code> <emph>NCName</emph> = <emph>URI</emph> <emph>Expr</emph>*</expression>
          </clause>
         </postjudge>
        </infer>
      </infergr>
  
      <p>This rule reads as follows: <quote>the phrase on the bottom
      (a namespace declaration in the query prolog followed by a
      sequence of expressions) is well-typed (accepted by the static
      type inference rules) within an environment <a href="#xq_stat_env_def" class="env">statEnv</a>
      <emph>if</emph> the sequence of expressions above the line is
      well-typed in the environment obtained from <a href="#xq_stat_env_def" class="env">statEnv</a> by
      adding the namespace declaration</quote>.</p>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      <p id="xq_dyn_env_def">The environment group <term id="xq_dyn_env">dynEnv</term> denotes the group of environments
      built and used during dynamic evaluation.</p>

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

      <p>The following environments are part of evaluation 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_val_env" valign="top" rowspan="1" colspan="1"><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a></td>
          <td rowspan="1" colspan="1">
           <table><tbody>
            <tr><td rowspan="1" colspan="1">The <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a> environment corresponds to the
            <term>variable values</term>, the <term>context
            item</term>, the <term>context position</term> and the
            <term>context size</term> in the [XPath/XQuery] evaluation
            context.</td></tr>

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

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

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

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

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

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

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

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

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

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

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

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

      <p id="fs_builtin_vars">The following Formal Semantics variables
      represent the <term>context item</term>, <term>context
      position</term>, and <term>context size</term> properties of the
      dynamic context:</p>
  
      <table summary="" border="1">
       <tbody>
        <tr><td rowspan="1" colspan="1">Built-in Variable  </td><td rowspan="1" colspan="1">Represents:</td></tr>
        <tr><td rowspan="1" colspan="1"><a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>dot</code></a></td><td rowspan="1" colspan="1">context item</td></tr>
        <tr><td rowspan="1" colspan="1"><a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a></td><td rowspan="1" colspan="1">context position </td></tr>
        <tr><td rowspan="1" colspan="1"><a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a></td><td rowspan="1" colspan="1">context size</td></tr>
       </tbody>
      </table>
  
      <p>Variables with the "fs" prefix are reserved for use in the
      definition of the Formal Semantics. Since there is no namespace
      URI associated to the "fs" prefix, users cannot refer to those
      variables directly using a variable expression.</p>

      <p>Values of <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>position</code></a> and <a href="#fs_builtin_vars"><code>$</code><emph>fs:</emph><code>last</code></a> can be obtained by
      invoking the <code>fn:position</code> and <code>fn:last</code> functions,
      respectively.</p>

    </div3>
  </div2>

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

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

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

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

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

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

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

      <p>Query processing consists of two phases: a static analysis
      phase and a dynamic evaluation phase. Static analysis is further
      divided into four sub-phases.  Each phase consumes the result of
      the previous phase and generates output for the next phase. For
      each processing phase, we point to the relevant notations
      introduced later in the document.</p>

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

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

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

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

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

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

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

          <p>After normalization, the full semantics is obtained by
          giving a semantics to the normalized Core [expression/query]. This
          is done during the last two phases.</p>
        </item>
        <item>
          <p id="processing_static"><term>Static type analysis.</term>
          (Step SQ6 in Figure 1).  Static type analysis is optional.
          If this phase is not supported, then normalization is
          followed directly by dynamic evaluation.  </p> <p>Static
          type analysis checks whether each [expression/query] is type safe,
          and if so, determines its static type. Static type analysis
          is defined only for Core [expression/query]. Static type analysis
          works by bottom-up application of type inference rules over
          expressions, taking the type of literals and of input
          documents into account.</p> <p>If the [expression/query] is not
          type-safe, static type analysis yields a <emph>type
          error</emph>. For instance, a comparison between an integer
          value and a string value might be detected as an type error
          during the static type analysis. If static type analysis
          succeeds, it yields an abstract syntax tree where each
          sub-expression is associated with its static type.</p>

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

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

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

      <olist>
        <item>
          <p id="dyn_processing_context"><term>Dynamic Context
          Processing.</term> (Steps DQ2 and DQ3 in Figure 1).The
          dynamic semantics of [expression/query] depends on the dynamic
          input context. The dynamic input context needs to be
          generated before the [expression/query] can be evaluated. The
          dynamic input context may be defined by the processing
          environment and by statements in the Query Prolog (See
          <specref ref="id-query-prolog"/>). In XPath, the dynamic
          input context is defined by the processing environment. The
          static input context is denoted by <a href="#xq_dyn_env_def" class="env">dynEnv</a>.</p>
        </item>
        <item>
          <p id="processing_dynamic"><term>Dynamic Evaluation.</term>
          (Steps DQ4 and DQ5 in Figure 1). This phase computes the
          value of an [expression/query].  The semantics of evaluation is
          defined only for Core [expression/query] terms. The formal
          description of evaluation works by bottom-up application of
          evaluation rules over expressions, starting with evaluation
          of literals and variables. (Note that in practice some
          implementations may prefer top-down evaluation strategies.)
          Evaluation may result in a value OR a dynamic error, which
          may be a non-type error or a type error.  If static typing
          of an expression does not raise a type error, then dynamic
          evaluation of the same expression will not raise a type
          error (and thus dynamic type checking can be avoided when
          static typing is enabled). Dynamic evaluation may still
          raise a non-type error.</p>

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

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

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

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

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

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

    </div3>

    <div3 id="sec_normalization">
      <head>Normalization judgment</head>

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

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

        <mapping>
          <xquery>
            <expression>
              <map><expression>Object</expression></map><subscript>Subscript</subscript>
            </expression>
          </xquery>
          <core>
            <expression>
              <emph>Mapped Object</emph>
            </expression>
          </core>
        </mapping>
        
        <p>The original <quote>object</quote> is written above the
        <term>==</term> sign. The rewritten <quote>object</quote> is written
        beneath the <term>==</term> sign. The subscript is used to indicate
        what kind of <quote>object</quote> is mapped, and sometimes to
        pass some information between mapping rules.</p>
        
        <p>Since normalization is always applied in the presence of a 
          static context, the above rule is a shorthand for:</p>

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

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

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

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

        <p>which indicates that the expression <emph>Expr</emph> is normalized
        to the expression <emph>CoreExpr</emph> in the [XPath/XQuery] Core
        (with the implied <a href="#xq_stat_env_def" class="env">statEnv</a>).</p>
      </smnotation>

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

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

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

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

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

    </div3>

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

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

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

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

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

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

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

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

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

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

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

        <p>The <quote>left half</quote> (the part before the
        <term>:</term>) of the expression <emph>below</emph> the line
        corresponds to some [expression/query], for which a type is
        computed. If the [expression/query] has been parsed into an internal
        abstract syntax tree, this usually corresponds to some node in
        that tree.  The expression usually has
        <emph>pattern</emph>s in it (here <emph>Expr</emph><subscript>1</subscript>, <emph>Expr</emph><subscript>2</subscript>,
        and <emph>Expr</emph><subscript>3</subscript>) that need to be matched against the children
        of the node in the abstract syntax tree. The expressions
        <emph>above</emph> the line indicate things that need to be
        computed to use this rule; in this case, the types of the
        condition expression and the two branches of the if-then-else
        expression.  Once those types are computed (by further
        applying static inference rules recursively to the expressions
        on each side), then the type of the expression below the line
        can be computed.  This example illustrates a general feature
        of the [XPath/XQuery] type system: the type of an expression
        depends only on the type of its sub-expressions.  The overall
        static type inference algorithm is recursive, following the
        abstract syntax of the [expression/query].  At each point in the
        recursion, an appropriate matching inference rule is sought;
        if at any point there is no applicable rule, then static type
        inference has failed and the [expression/query] is not type
        correct.</p>

     </smnote>

    </div3>

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

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

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

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

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

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

      </smnotation>

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

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

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

        <p>yields the integer value 8. This can be inferred as
        follows: the input literals '3' and '5' denote the values 3
        and 5, respectively, so the variable $v has the value 3. Since
        the sum of 3 and 5 is 8, the complete expression has the value
        8.</p>
      </smexample>

      <smnote>
        <p>As with static type inference, logical inference rules are
        used to determine the value of each expression, given the
        dynamic environment and the values of its sub-expressions.</p>

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

      </smnote>

    </div3>

  </div2>

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

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

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

  </div2>

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

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

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

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


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

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

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

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

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

    </div3>
  </div2>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    </div3>

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

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

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

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

        <smcore>
          <p>The Core grammar productions for sequence types are:</p>
      
  <scrap headstyle="show">
   <head/>
   <prod num="83 (Core)" id="doc-core-SequenceType"><lhs>SequenceType</lhs><rhs>(<nt def="doc-core-ItemType">ItemType</nt>  <nt def="doc-core-OccurrenceIndicator">OccurrenceIndicator</nt>?)<br/>|  ("empty-sequence"    "("    ")")</rhs></prod>
   <prod num="85 (Core)" id="doc-core-ItemType"><lhs>ItemType</lhs><rhs><nt def="doc-core-AtomicType">AtomicType</nt>  |  <nt def="doc-core-KindTest">KindTest</nt>  |  ("item"    "("    ")")</rhs></prod>
   <prod num="84 (Core)" id="doc-core-OccurrenceIndicator"><lhs>OccurrenceIndicator</lhs><rhs>"?"  |  "*"  |  "+"</rhs></prod>
   <prod num="86 (Core)" id="doc-core-AtomicType"><lhs>AtomicType</lhs><rhs><nt def="prod-core-QName">QName</nt></rhs></prod>
   <prod num="87 (Core)" id="doc-core-KindTest"><lhs>KindTest</lhs><rhs><nt def="doc-core-DocumentTest">DocumentTest</nt><br/>|  <nt def="doc-core-ElementTest">ElementTest</nt><br/>|  <nt def="doc-core-AttributeTest">AttributeTest</nt><br/>|  <nt def="doc-core-SchemaElementTest">SchemaElementTest</nt><br/>|  <nt def="doc-core-SchemaAttributeTest">SchemaAttributeTest</nt><br/>|  <nt def="doc-core-PITest">PITest</nt><br/>|  <nt def="doc-core-CommentTest">CommentTest</nt><br/>|  <nt def="doc-core-TextTest">TextTest</nt><br/>|  <nt def="doc-core-AnyKindTest">AnyKindTest</nt></rhs></prod>
   <prod num="89 (Core)" id="doc-core-DocumentTest"><lhs>DocumentTest</lhs><rhs>"document-node"    "("  (<nt def="doc-core-ElementTest">ElementTest</nt>  |  <nt def="doc-core-SchemaElementTest">SchemaElementTest</nt>)?  ")"</rhs></prod>
   <prod num="97 (Core)" id="doc-core-ElementTest"><lhs>ElementTest</lhs><rhs>"element"    "("  (<nt def="doc-core-ElementNameOrWildcard">ElementNameOrWildcard</nt>  (","  <nt def="doc-core-TypeName">TypeName</nt>  "?"?)?)?  ")"</rhs></prod>
   <prod num="99 (Core)" id="doc-core-SchemaElementTest"><lhs>SchemaElementTest</lhs><rhs>"schema-element"    "("  <nt def="doc-core-ElementDeclaration">ElementDeclaration</nt>  ")"</rhs></prod>
   <prod num="100 (Core)" id="doc-core-ElementDeclaration"><lhs>ElementDeclaration</lhs><rhs><nt def="doc-core-ElementName">ElementName</nt></rhs></prod>
   <prod num="93 (Core)" id="doc-core-AttributeTest"><lhs>AttributeTest</lhs><rhs>"attribute"    "("  (<nt def="doc-core-AttribNameOrWildcard">AttribNameOrWildcard</nt>  (","  <nt def="doc-core-TypeName">TypeName</nt>)?)?  ")"</rhs></prod>
   <prod num="95 (Core)" id="doc-core-SchemaAttributeTest"><lhs>SchemaAttributeTest</lhs><rhs>"schema-attribute"    "("  <nt def="doc-core-AttributeDeclaration">AttributeDeclaration</nt>  ")"</rhs></prod>
   <prod num="96 (Core)" id="doc-core-AttributeDeclaration"><lhs>AttributeDeclaration</lhs><rhs><nt def="doc-core-AttributeName">AttributeName</nt></rhs></prod>
   <prod num="98 (Core)" id="doc-core-ElementNameOrWildcard"><lhs>ElementNameOrWildcard</lhs><rhs><nt def="doc-core-ElementName">ElementName</nt>  |  "*"</rhs></prod>
   <prod num="102 (Core)" id="doc-core-ElementName"><lhs>ElementName</lhs><rhs><nt def="prod-core-QName">QName</nt></rhs></prod>
   <prod num="94 (Core)" id="doc-core-AttribNameOrWildcard"><lhs>AttribNameOrWildcard</lhs><rhs><nt def="doc-core-AttributeName">AttributeName</nt>  |  "*"</rhs></prod>
   <prod num="101 (Core)" id="doc-core-AttributeName"><lhs>AttributeName</lhs><rhs><nt def="prod-core-QName">QName</nt></rhs></prod>
   <prod num="103 (Core)" id="doc-core-TypeName"><lhs>TypeName</lhs><rhs><nt def="prod-core-QName">QName</nt></rhs></prod>
   <prod num="92 (Core)" id="doc-core-PITest"><lhs>PITest</lhs><rhs>"processing-instruction"    "("  (<nt def="prod-core-NCName">NCName</nt>  |  <nt def="doc-core-StringLiteral">StringLiteral</nt>)?  ")"</rhs></prod>
   <prod num="91 (Core)" id="doc-core-CommentTest"><lhs>CommentTest</lhs><rhs>"comment"    "("  ")"</rhs></prod>
   <prod num="90 (Core)" id="doc-core-TextTest"><lhs>TextTest</lhs><rhs>"text"    "("  ")"</rhs></prod>
   <prod num="88 (Core)" id="doc-core-AnyKindTest"><lhs>AnyKindTest</lhs><rhs>"node"    "("  ")"</rhs></prod>
  </scrap>
        </smcore>

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

        <p>However, the [XPath/XQuery] type system not being part of the
        [XPath/XQuery] syntax, the SequenceType syntax is still part of
        the [XPath/XQuery] Core. Normalization from SequenceTypes to types
        is not applied during the normalization phase but whenever a
        dynamic or static rule requires it. Normalization of
        SequenceTypes is the only example of normalization that does
        not yield an expression in the [XPath/XQuery] Core and that occurs
        on-demand in dynamic or static rules.</p>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            <mapping>
             <xquery>
              <expression>
               <map>node()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                (element | attribute | text | document | comment | processing-instruction)
              </expression>
             </core>
            </mapping>

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

            <mapping>
             <xquery>
              <expression>
               <map>item()</map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
              </expression>
             </xquery>
             <core>
              <expression>
                (element | attribute | text | document | comment |
                processing-instruction | <a href="#dt-xdt_anyAtomicType"><code>xdt:anyAtomicType</code></a> )
              </expression>
             </core>
            </mapping>

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

  <div2 id="comments">
    <head>Comments</head>
  
    <scrap headstyle="show">
      <head/>
      <prod num="152 (XQuery)" id="doc-xquery-Comment"><lhs>Comment</lhs><rhs>"(:"  (<nt def="doc-xquery-CommentContents">CommentContents</nt>  |  <nt def="doc-xquery-Comment">Comment</nt>)*  ":)"</rhs></prod>
      <prod num="160 (XQuery)" id="doc-xquery-CommentContents"><lhs>CommentContents</lhs><rhs>(<nt def="prod-xquery-Char">Char</nt>+ - (Char* ('(:' | ':)') Char*))</rhs></prod>
    </scrap>

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

</div1>

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

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

  <scrap headstyle="show"><head/>
    <prod num="31 (XQuery)" id="doc-xquery-Expr"><lhs>Expr</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt>  (","  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>)*</rhs></prod>
    <prod num="32 (XQuery)" id="doc-xquery-ExprSingle"><lhs>ExprSingle</lhs><rhs><nt def="doc-xquery-FLWORExpr">FLWORExpr</nt><br/>|  <nt def="doc-xquery-QuantifiedExpr">QuantifiedExpr</nt><br/>|  <nt def="doc-xquery-TypeswitchExpr">TypeswitchExpr</nt><br/>|  <nt def="doc-xquery-IfExpr">IfExpr</nt><br/>|  <nt def="doc-xquery-OrExpr">OrExpr</nt></rhs></prod>
    <prod num="1 (XPath)" id="doc-xpath-XPath"><lhs>XPath</lhs><rhs><nt def="doc-xpath-Expr">Expr</nt></rhs></prod>
  </scrap>

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

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

    <scrap headstyle="show"><head/>
      <prod num="30 (Core)" id="doc-core-Expr"><lhs>Expr</lhs><rhs><nt def="doc-core-ExprSingle">ExprSingle</nt>  (","  <nt def="doc-core-ExprSingle">ExprSingle</nt>)*</rhs></prod>
      <prod num="31 (Core)" id="doc-core-ExprSingle"><lhs>ExprSingle</lhs><rhs><nt def="doc-core-FLWORExpr">FLWORExpr</nt><br/>|  <nt def="doc-core-TypeswitchExpr">TypeswitchExpr</nt><br/>|  <nt def="doc-core-IfExpr">IfExpr</nt><br/>|  <nt def="doc-core-OrExpr">OrExpr</nt></rhs></prod>
    </scrap>
  </smcore>

  <smtype>
    <p>It is a static type error for any expression to have the empty
    type, except for the following expressions and functions:

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

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

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

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

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

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

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

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

    <scrap headstyle="show">
      <head>Primary Expressions</head>
      <prod num="84 (XQuery)" id="doc-xquery-PrimaryExpr"><lhs>PrimaryExpr</lhs><rhs><nt def="doc-xquery-Literal">Literal</nt>  |  <nt def="doc-xquery-VarRef">VarRef</nt>  |  <nt def="doc-xquery-ParenthesizedExpr">ParenthesizedExpr</nt>  |  <nt def="doc-xquery-ContextItemExpr">ContextItemExpr</nt>  |  <nt def="doc-xquery-FunctionCall">FunctionCall</nt>  |  <nt def="doc-xquery-Constructor">Constructor</nt>  |  <nt def="doc-xquery-OrderedExpr">OrderedExpr</nt>  |  <nt def="doc-xquery-UnorderedExpr">UnorderedExpr</nt></rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar production for primary expressions is:</p>
  
      <scrap headstyle="show">
        <head>Primary Expressions</head>
        <prod num="63 (Core)" id="doc-core-PrimaryExpr"><lhs>PrimaryExpr</lhs><rhs><nt def="doc-core-Literal">Literal</nt>  |  <nt def="doc-core-VarRef">VarRef</nt>  |  <nt def="doc-core-ParenthesizedExpr">ParenthesizedExpr</nt>  |  <nt def="doc-core-FunctionCall">FunctionCall</nt>  |  <nt def="doc-core-Constructor">Constructor</nt></rhs></prod>
      </scrap>
    </smcore>

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

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

<scrap headstyle="show">
  <head>Literals</head>
  <prod num="85 (XQuery)" id="doc-xquery-Literal"><lhs>Literal</lhs><rhs><nt def="doc-xquery-NumericLiteral">NumericLiteral</nt>  |  <nt def="doc-xquery-StringLiteral">StringLiteral</nt></rhs></prod>
  <prod num="86 (XQuery)" id="doc-xquery-NumericLiteral"><lhs>NumericLiteral</lhs><rhs><nt def="doc-xquery-IntegerLiteral">IntegerLiteral</nt>  |  <nt def="doc-xquery-DecimalLiteral">DecimalLiteral</nt>  |  <nt def="doc-xquery-DoubleLiteral">DoubleLiteral</nt></rhs></prod>
  <prod num="142 (XQuery)" id="doc-xquery-IntegerLiteral"><lhs>IntegerLiteral</lhs><rhs><nt def="doc-xquery-Digits">Digits</nt></rhs></prod>
  <prod num="143 (XQuery)" id="doc-xquery-DecimalLiteral"><lhs>DecimalLiteral</lhs><rhs>("."  <nt def="doc-xquery-Digits">Digits</nt>)  |  (<nt def="doc-xquery-Digits">Digits</nt>  "."  [0-9]*)</rhs></prod>
  <prod num="144 (XQuery)" id="doc-xquery-DoubleLiteral"><lhs>DoubleLiteral</lhs><rhs>(("."  <nt def="doc-xquery-Digits">Digits</nt>)  |  (<nt def="doc-xquery-Digits">Digits</nt>  ("."  [0-9]*)?))  [eE]  [+-]?  <nt def="doc-xquery-Digits">Digits</nt></rhs></prod>
  <prod num="145 (XQuery)" id="doc-xquery-StringLiteral"><lhs>StringLiteral</lhs><rhs>('"'  (<nt def="doc-xquery-PredefinedEntityRef">PredefinedEntityRef</nt>  |  <nt def="prod-xquery-CharRef">CharRef</nt>  |  <nt def="doc-xquery-EscapeQuot">EscapeQuot</nt>  |  [^"&amp;])*  '"')  |  ("'"  (<nt def="doc-xquery-PredefinedEntityRef">PredefinedEntityRef</nt>  |  <nt def="prod-xquery-CharRef">CharRef</nt>  |  <nt def="doc-xquery-EscapeApos">EscapeApos</nt>  |  [^'&amp;])*  "'")</rhs></prod>
  <prod num="146 (XQuery)" id="doc-xquery-PredefinedEntityRef"><lhs>PredefinedEntityRef</lhs><rhs>"&amp;"  ("lt"  |  "gt"  |  "amp"  |  "quot"  |  "apos")  ";"</rhs></prod>
  <prod num="159 (XQuery)" id="doc-xquery-Digits"><lhs>Digits</lhs><rhs>[0-9]+</rhs></prod>
</scrap>

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

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

  <prod num="64 (Core)" id="doc-core-Literal"><lhs>Literal</lhs><rhs><nt def="doc-core-NumericLiteral">NumericLiteral</nt>  |  <nt def="doc-core-StringLiteral">StringLiteral</nt></rhs></prod>
  <prod num="65 (Core)" id="doc-core-NumericLiteral"><lhs>NumericLiteral</lhs><rhs><nt def="doc-core-IntegerLiteral">IntegerLiteral</nt>  |  <nt def="doc-core-DecimalLiteral">DecimalLiteral</nt>  |  <nt def="doc-core-DoubleLiteral">DoubleLiteral</nt></rhs></prod>
  <prod num="106 (Core)" id="doc-core-IntegerLiteral"><lhs>IntegerLiteral</lhs><rhs><nt def="doc-core-Digits">Digits</nt></rhs></prod>
  <prod num="107 (Core)" id="doc-core-DecimalLiteral"><lhs>DecimalLiteral</lhs><rhs>("."  <nt def="doc-core-Digits">Digits</nt>)  |  (<nt def="doc-core-Digits">Digits</nt>  "."  [0-9]*)</rhs></prod>
  <prod num="108 (Core)" id="doc-core-DoubleLiteral"><lhs>DoubleLiteral</lhs><rhs>(("."  <nt def="doc-core-Digits">Digits</nt>)  |  (<nt def="doc-core-Digits">Digits</nt>  ("."  [0-9]*)?))  [eE]  [+-]?  <nt def="doc-core-Digits">Digits</nt></rhs></prod>
  <prod num="109 (Core)" id="doc-core-StringLiteral"><lhs>StringLiteral</lhs><rhs>('"'  (<nt def="doc-core-EscapeQuot">EscapeQuot</nt>  |  [^"])*  '"')  |  ("'"  (<nt def="doc-core-EscapeApos">EscapeApos</nt>  |  [^'])*  "'")</rhs></prod>
  <prod num="120 (Core)" id="doc-core-Digits"><lhs>Digits</lhs><rhs>[0-9]+</rhs></prod>
</scrap>
      </smcore>

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

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

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

      <smnorm>
        <p>All literals are Core expressions, therefore no
        normalization rules are required for literals.  Predefined
        entity references and character references in strings are
        resolved to characters as part of parsing and therefore they
        do not occur in the Core grammar.</p>
      </smnorm>

      <smtype>
        <p>In the static semantics, the type of an integer literal is
        simply xs:integer:</p>

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

      <smeval>
        <p>In the dynamic semantics, an integer literal is evaluated
        by constructing an atomic value in the data model, which
        consists of the literal value and its type:</p>

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

      <p>The formal definitions of decimal, double, and string
      literals are analogous to those for integer.</p>

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

      <smeval>  
	<infergr>
	  <infer>
 	    <prejudge>
 	      <multiclause>
 	        <clause>
                <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 	        <expression><emph>DecimalLiteral</emph> <a href="#jd_has_atomic_value" class="judgment"><term>has atomic value</term></a> <emph>Decimal</emph>
 	        </expression>
 	        </clause>
 	      </multiclause>
 	    </prejudge>
	    <postjudge>
	      <multiclause>
		<clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression><emph>DecimalLiteral</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <emph>Decimal</emph></expression>
		</clause>
	      </multiclause>
	    </postjudge>
	  </infer>
	</infergr>
      </smeval>
 
<smtype>
<infergr>
  <infer>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
        <expression><emph>DoubleLiteral</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> xs:double</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>
</smtype>

<smeval>
<infergr>
  <infer>
 	    <prejudge>
 	      <multiclause>
 	        <clause>
                <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 	        <expression><emph>DoubleLiteral</emph> <a href="#jd_has_atomic_value" class="judgment"><term>has atomic value</term></a> <emph>Double</emph>
 	        </expression>
 	        </clause>
 	      </multiclause>
 	    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment>  <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
        <expression><emph>DoubleLiteral</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <emph>Double</emph></expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>
</smeval>
 
<smtype>
<infergr>
  <infer>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
        <expression><emph>StringLiteral</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> xs:string</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>
</smtype>

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

  </div3>

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

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

    <scrap headstyle="show">
      <head>Variable References</head>
      <prod num="87 (XQuery)" id="doc-xquery-VarRef"><lhs>VarRef</lhs><rhs>"$"  <nt def="doc-xquery-VarName">VarName</nt></rhs></prod>
      <prod num="88 (XQuery)" id="doc-xquery-VarName"><lhs>VarName</lhs><rhs><nt def="prod-xquery-QName">QName</nt></rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar productions for variable references are:</p>
  
      <scrap headstyle="show">
        <head>Primary Expressions</head>
        <prod num="66 (Core)" id="doc-core-VarRef"><lhs>VarRef</lhs><rhs>"$"  <nt def="doc-core-VarName">VarName</nt></rhs></prod>
        <prod num="67 (Core)" id="doc-core-VarName"><lhs>VarName</lhs><rhs><nt def="prod-core-QName">QName</nt></rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>A variable is a Core expression, therefore no normalization
      rule is required for a variable.</p>
    </smnorm>

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

      <infergr>
    	<infer>
 	  <prejudge>
 	    <multiclause>
 	      <clause>
              <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
 	      <expression><emph>VarName</emph> <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>
 	      </expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_type_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>If the variable is not bound in the static environment, a
      static type error is raised.</p>
    </smtype>

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

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

      <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>
 		<expression><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = <code>#IMPORTED</code>(<emph>URI</emph>)</expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
 	      <clause>
 	      <expression><emph>URI</emph> <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript></expression>
 	      </clause>
 	    </multiclause>
 	    <multiclause>
 	      <clause>
 		<expression><a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_val_env" class="env">varValue</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </prejudge>
 	  <postjudge>
 	    <multiclause>
 	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 		      <expression>$ <emph>VarName</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
 	      </clause>
 	    </multiclause>
 	  </postjudge>
    	</infer>
      </infergr>
    </smeval>
  
  </div3>

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

    <scrap headstyle="show"> 
      <head/>
      <prod num="89 (XQuery)" id="doc-xquery-ParenthesizedExpr"><lhs>ParenthesizedExpr</lhs><rhs>"("  <nt def="doc-xquery-Expr">Expr</nt>?  ")"</rhs></prod> 
    </scrap>

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

      <scrap headstyle="show">
        <head/>
        <prod num="68 (Core)" id="doc-core-ParenthesizedExpr"><lhs>ParenthesizedExpr</lhs><rhs>"("  <nt def="doc-core-Expr">Expr</nt>?  ")"</rhs></prod> 
      </scrap>
    </smcore>

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

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

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

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

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

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

  </div3>

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

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

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

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

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

         </div3>

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

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

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

      <smnotation>
        <p id="jd_map_function_argument">Normalization of function
        calls uses an auxiliary mapping
        <map/><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<emph>SequenceType</emph>)</subscript></a> used to insert
        conversions of function arguments that depend only on the
        expected SequenceType of the corresponding parameters. It is
        defined as follows:</p>

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

      <p>where</p>

        <ulist>

          <item>
            <p><map><emph>Expr</emph></map><a href="#jd_map_function_argument" class="judgment"><subscript>AtomizeAtomic(<emph>SequenceType</emph>)</subscript></a> denotes</p>
            <table>
              <tbody>
                <tr>
                  <td rowspan="1" colspan="1"><code>fn:data</code>(<emph>Expr</emph>)</td><td rowspan="1" colspan="1">  </td>
                  <td rowspan="1" colspan="1">If
                  <map><emph>SequenceType</emph></map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
                  <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#dt-xdt_anyAtomicType"><code>xdt:anyAtomicType</code></a>*</td>

                </tr>
                <tr>
                  <td rowspan="1" colspan="1"><emph>Expr</emph></td><td rowspan="1" colspan="1">  </td>
                  <td rowspan="1" colspan="1">Otherwise</td>
                </tr>
              </tbody>
            </table>

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

          <item>
            <p><map><emph>Expr</emph></map><a href="#jd_map_function_argument" class="judgment"><subscript>Convert(<emph>SequenceType</emph>)</subscript></a> denotes</p>
            <table>
              <tbody>
                <tr>
                  <td rowspan="1" colspan="1"><a href="#sec_convert_simple_operand"><emph>fs:</emph><code>convert-simple-operand</code></a>(<emph>Expr</emph>,<emph>PrototypicalValue</emph>)</td>
                  <td rowspan="1" colspan="1">If
                  <map><emph>SequenceType</emph></map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
                  <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#dt-xdt_anyAtomicType"><code>xdt:anyAtomicType</code></a>*</td>
                </tr>
                <tr>
                  <td rowspan="1" colspan="1"><emph>Expr</emph></td>
                  <td rowspan="1" colspan="1">Otherwise</td>
                </tr>
              </tbody>
            </table>

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

      </smnotation>

      <smnote>
        <p>The <a href="#sec_convert_simple_operand"><emph>fs:</emph><code>convert-simple-operand</code></a> function takes a 
        <emph>PrototypicalValue</emph>, which is a value of the target
        type, to ensure that conversion to base types is possible
        even though types are not first class objects in
        [XPath/XQuery].</p>
      </smnote>


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

      <scrap headstyle="show">
        <head>Function Calls</head>
        <prod num="71 (Core)" id="doc-core-FunctionCall"><lhs>FunctionCall</lhs><rhs><nt def="prod-core-QName">QName</nt>    "("  (<nt def="doc-core-ExprSingle">ExprSingle</nt>  (","  <nt def="doc-core-ExprSingle">ExprSingle</nt>)*)?  ")"</rhs></prod>
      </scrap>
    </smcore>
 
      <smnorm>
        <p>Each argument expression in a function call is normalized
        to its corresponding Core expression by applying
        <map/><a href="#jd_map_function_argument" class="judgment"><subscript>FunctionArgument(<emph>SequenceType</emph>)</subscript></a> for each argument with
        the expected <emph>SequenceType</emph> for the argument inserted.</p>

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

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

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

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

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

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

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

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

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

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

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

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

      <smeval>

        <p>Based on a function's name and parameter types, the
        function body is retrieved from the dynamic environment.</p>

        <p>If the function is a locally-declared, user-defined
        function then it is evaluated as follows. First, the rule
        evaluates each actual function argument expression.  Next, a
        match is searched for among the function's possible
        declaration signatures, retrieved from <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>.  If
        the function is not present in the environment, or there is no
        matching declaration signature, a static type error is raised.
        Otherwise, the function body and formal variables are obtained
        from <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a> for the matching declaration signature.
        The rule then extends <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a> by binding each formal
        variable to its corresponding value (converted by the
        normalization as required for the expected type), and
        evaluates the body of the function in the new environment.
        The resulting value is the value of the function call.</p>

    <infergr>
     <infer>
      <prejudge>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><emph>QName</emph> <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = 
          { <emph>FunctionDecl</emph><subscript>1</subscript>, ...,
            <emph>FunctionDecl</emph><subscript>m</subscript> }
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <emph>FunctionDecl</emph><subscript>i</subscript>
          =
          declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>) as <a href="#doc-fs-Type"><emph>Type</emph></a>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
          <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
          <emph>Expr</emph><subscript>n</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>
         </expression>
        </clause>
       </multiclause>

       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>'
         </expression>
        </clause>
       </multiclause>

       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>))
          =
          (<emph>Expr</emph>, <emph>Variable</emph><subscript>1</subscript>, ... , <emph>Variable</emph><subscript>n</subscript>)
         </expression>
        </clause>
       </multiclause>

       <multiclause>
        <clause>
         <environment>
          <update>
           <environment><a href="#xq_default_dyn_env" class="env">dynEnvDefault</a>.<a href="#xq_val_env" class="env">varValue</a></environment>
           <expression>
            <emph>Variable</emph><subscript>1</subscript> =&gt; <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>';
            ...;
            <emph>Variable</emph><subscript>n</subscript> =&gt; <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>'</expression>
          </update>
         </environment>
         <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>'</expression>
        </clause>

       </multiclause>

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

    <p>Note that the function body is evaluated in the default
    (top-level) environment extended with just the parameter bindings.
    Note also that input values and output values are matched against
    the types declared for the function. If static analysis was
    performed, all these checks are guaranteed to be true and may be
    omitted.</p>

    <p>The rule for evaluating an imported function is similar to that
    for evaluating a locally declared function, except that the
    function call is evaluated in the dynamic context of the module in
    which it is declared.</p>

    <infergr>
     <infer>
      <prejudge>

       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
          <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
          <emph>Expr</emph><subscript>n</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>
         </expression>
        </clause>
       </multiclause>

       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><emph>QName</emph> <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
        </clause>
       </multiclause>
      
       <multiclause>
        <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = 
          { <emph>FunctionDecl</emph><subscript>1</subscript>, ...,
            <emph>FunctionDecl</emph><subscript>m</subscript> }
         </expression>
        </clause>
       </multiclause>
      
       <multiclause>
        <clause>
         <expression>
          <emph>FunctionDecl</emph><subscript>i</subscript>
          =
          declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>) as <a href="#doc-fs-Type"><emph>Type</emph></a>
         </expression>
        </clause>
       </multiclause>
       
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>))
          =
          <code>#IMPORTED</code>(<emph>URI</emph>)
         </expression>
        </clause>
       </multiclause>

       <multiclause>
 	<clause>
 	 <expression><emph>URI</emph> <a href="#jd_module_statEnv" class="judgment"><term>=&gt;</term><subscript>module_statEnv</subscript></a> <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript></expression>
 	</clause>
       </multiclause>
       <multiclause>
 	<clause>
 	 <expression><emph>URI</emph> <a href="#jd_module_dynEnv" class="judgment"><term>=&gt;</term><subscript>module_dynEnv</subscript></a> <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript></expression>
 	</clause>
       </multiclause>

       <multiclause>
        <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a><subscript>1</subscript>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) = 
          { <emph>FunctionDecl</emph><subscript>1</subscript>', ...,
            <emph>FunctionDecl</emph><subscript>m</subscript>' }
         </expression>
        </clause>
       </multiclause>
      
       <multiclause>
        <clause>
         <expression>
          <emph>FunctionDecl</emph><subscript>i</subscript>'
          =
          declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>', ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>') as <a href="#doc-fs-Type"><emph>Type</emph></a>'
         </expression>
        </clause>
       </multiclause>
       
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a><subscript>1</subscript>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>))
          =
          (<emph>Expr</emph>, <emph>Variable</emph><subscript>1</subscript>, ... , <emph>Variable</emph><subscript>n</subscript>)
         </expression>
        </clause>
       </multiclause>

       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>'
         </expression>
        </clause>
       </multiclause>

       <multiclause>
        <clause>
         <environment>
          <update>
           <environment><a href="#xq_default_dyn_env" class="env">dynEnvDefault</a>.<a href="#xq_val_env" class="env">varValue</a></environment>
           <expression>
            <emph>Variable</emph><subscript>1</subscript> =&gt; <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>';
            ...;
            <emph>Variable</emph><subscript>n</subscript> =&gt; <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>'</expression>
          </update>
         </environment>
         <expression><emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>' </expression>
        </clause>
       </multiclause>

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

    <p>If the function is a built-in or external function then the rule is
    somewhat simpler:</p>

    <infergr>
     <infer>
      <prejudge>

       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><emph>QName</emph> <a href="#jd_func_qname_expands_to" class="judgment"><term>of func expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
        </clause>
       </multiclause>
      
       <multiclause>
        <clause>
         <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_functy_env" class="env">funcType</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>) =
          { <emph>FunctionDecl</emph><subscript>1</subscript>, ...,
            <emph>FunctionDecl</emph><subscript>m</subscript> }
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <emph>FunctionDecl</emph><subscript>i</subscript>
          =
          declare function <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>) as <a href="#doc-fs-Type"><emph>Type</emph></a>
         </expression>
        </clause>
       </multiclause>
       
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
          <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
         <expression>
          <emph>Expr</emph><subscript>n</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><expression>...</expression></clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression>
          <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>'
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
          <a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_func_env" class="env">funcDefn</a>(<a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>n</subscript>))
          <a href="#jd_isin" class="judgment"><term>in</term></a>
          { <code>#BUILT-IN</code>, <code>#EXTERNAL</code> }
         </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause>
         <expression>
           <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a>(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>', ..., <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>')  <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
          </expression>
        </clause>
       </multiclause>
       <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
         <expression><a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_promotes_to" class="judgment"><term>against</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_promotes_to" class="judgment"><term>promotes to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>'</expression>
        </clause>
       </multiclause>
      </prejudge>
      <postjudge>
       <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
         <expression>
          <emph>QName</emph> ( <emph>Expr</emph><subscript>1</subscript>, ...,  <emph>Expr</emph><subscript>n</subscript> ) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>'
         </expression>
        </clause>
       </multiclause>
      </postjudge>
     </infer>
    </infergr>

    <p>Calls to built-in or external functions use the following
    auxiliary judgment to evaluate the built-in or external
    function:</p>

    <infergr>
      <infer>
        <prejudge>
          <clause>
            <expression>
            "The built-in or external function F (from <bibref ref="xpath-datamodel"/>,
            <bibref ref="xpath-functions"/>, <specref ref="sec_special_functions"/>, or as defined in <a href="#xq_default_dyn_env" class="env">dynEnvDefault</a>.funcDefn) applied to the given
            parameter values yields the specified result value"
            </expression>
          </clause>
        </prejudge>
        <postjudge>
          <multiclause>
            <clause>
              <expression>F(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>, ..., <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript>) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a></expression>
            </clause>
          </multiclause>
        </postjudge>
      </infer>
    </infergr>

   </smeval>

  </div3>

  </div2>

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

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

    </smintro>

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

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

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

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

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

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

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

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

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

    </smnorm>

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

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

      <smintro>
        <scrap headstyle="show">
          <head>Steps</head>
          <prod num="70 (XQuery)" id="doc-xquery-StepExpr"><lhs>StepExpr</lhs><rhs><nt def="doc-xquery-AxisStep">AxisStep</nt>  |  <nt def="doc-xquery-FilterExpr">FilterExpr</nt></rhs></prod>
          <prod num="71 (XQuery)" id="doc-xquery-AxisStep"><lhs>AxisStep</lhs><rhs>(<nt def="doc-xquery-ForwardStep">ForwardStep</nt>  |  <nt def="doc-xquery-ReverseStep">ReverseStep</nt>)  <nt def="doc-xquery-PredicateList">PredicateList</nt></rhs></prod>
          <prod num="72 (XQuery)" id="doc-xquery-ForwardStep"><lhs>ForwardStep</lhs><rhs>(<nt def="doc-xquery-ForwardAxis">ForwardAxis</nt>  <nt def="doc-xquery-NodeTest">NodeTest</nt>)  |  <nt def="doc-xquery-AbbrevForwardStep">AbbrevForwardStep</nt></rhs></prod>
          <prod num="75 (XQuery)" id="doc-xquery-ReverseStep"><lhs>ReverseStep</lhs><rhs>(<nt def="doc-xquery-ReverseAxis">ReverseAxis</nt>  <nt def="doc-xquery-NodeTest">NodeTest</nt>)  |  <nt def="doc-xquery-AbbrevReverseStep">AbbrevReverseStep</nt></rhs></prod>
          <prod num="82 (XQuery)" id="doc-xquery-PredicateList"><lhs>PredicateList</lhs><rhs><nt def="doc-xquery-Predicate">Predicate</nt>*</rhs></prod>
        </scrap>
      </smintro>

      <smcore>
        <p>The Core grammar productions for XPath steps are:</p>
    
        <scrap headstyle="show">
          <head>Steps</head>
          <prod num="54 (Core)" id="doc-core-StepExpr"><lhs>StepExpr</lhs><rhs><nt def="doc-core-AxisStep">AxisStep</nt>  |  <nt def="doc-core-PrimaryExpr">PrimaryExpr</nt></rhs></prod>
          <prod num="55 (Core)" id="doc-core-AxisStep"><lhs>AxisStep</lhs><rhs><nt def="doc-core-ForwardStep">ForwardStep</nt>  |  <nt def="doc-core-ReverseStep">ReverseStep</nt></rhs></prod>
          <prod num="56 (Core)" id="doc-core-ForwardStep"><lhs>ForwardStep</lhs><rhs><nt def="doc-core-ForwardAxis">ForwardAxis</nt>  <nt def="doc-core-NodeTest">NodeTest</nt></rhs></prod>
          <prod num="58 (Core)" id="doc-core-ReverseStep"><lhs>ReverseStep</lhs><rhs><nt def="doc-core-ReverseAxis">ReverseAxis</nt>  <nt def="doc-core-NodeTest">NodeTest</nt></rhs></prod>
        </scrap>
      </smcore>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        </smintro>

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

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

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

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

      </div4>

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

          <scrap headstyle="show">
            <head>Node Tests</head>
            <prod num="60 (Core)" id="doc-core-NodeTest"><lhs>NodeTest</lhs><rhs><nt def="doc-core-KindTest">KindTest</nt>  |  <nt def="doc-core-NameTest">NameTest</nt></rhs></prod>
            <prod num="61 (Core)" id="doc-core-NameTest"><lhs>NameTest</lhs><rhs><nt def="prod-core-QName">QName</nt>  |  <nt def="doc-core-Wildcard">Wildcard</nt></rhs></prod>
            <prod num="62 (Core)" id="doc-core-Wildcard"><lhs>Wildcard</lhs><rhs>"*"<br/>|  (<nt def="prod-core-NCName">NCName</nt>  ":"  "*")<br/>|  ("*"  ":"  <nt def="prod-core-NCName">NCName</nt>)</rhs></prod>
          </scrap>
        </smcore>

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

          <scrap headstyle="show">
            <head>Prefix and LocalPart</head>
            <prod num="18 (Formal)" id="doc-fs-Prefix"><lhs>Prefix</lhs><rhs><nt def="prod-fs-NCName">NCName</nt></rhs></prod>
            <prod num="19 (Formal)" id="doc-fs-LocalPart"><lhs>LocalPart</lhs><rhs><nt def="prod-fs-NCName">NCName</nt></rhs></prod>
          </scrap>

        </smnotation>

      </div4>

    </div3>

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

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

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

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

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

      </smnorm>

    </div3>

  </div2>

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

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

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

      <scrap headstyle="show">
        <head>Constructing Sequences</head>
        <prod num="31 (XQuery)" id="noid_N14461.doc-xquery-Expr"><lhs>Expr</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt>  (","  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>)*</rhs></prod>
        <prod num="49 (XQuery)" id="doc-xquery-RangeExpr"><lhs>RangeExpr</lhs><rhs><nt def="doc-xquery-AdditiveExpr">AdditiveExpr</nt> ( "to"  <nt def="doc-xquery-AdditiveExpr">AdditiveExpr</nt> )?</rhs></prod>
      </scrap>

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

        <scrap headstyle="show">
          <head>Core Sequence Expressions</head>
          <prod num="30 (Core)" id="noid_N14476.doc-core-Expr"><lhs>Expr</lhs><rhs><nt def="doc-core-ExprSingle">ExprSingle</nt>  (","  <nt def="doc-core-ExprSingle">ExprSingle</nt>)*</rhs></prod>
        </scrap>
      </smcore>

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

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

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

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

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

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

      </smeval>

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

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

      <smtype>
        <p>The static semantics of the <emph>fs:</emph><code>to</code> function is defined in
        <bibref ref="xpath-functions"/>.</p>
      </smtype>

      <smeval>
        <p>The dynamic semantics of the <emph>op:</emph>to operator is
        defined in <bibref ref="xpath-functions"/>.</p>
      </smeval>

    </div3>

    <div3 id="sec_filter_exprs">
      <head>Filter Expressions</head>
      <smintro>
        <scrap headstyle="show">
          <head>Filter Expression</head>
          <prod num="81 (XQuery)" id="doc-xquery-FilterExpr"><lhs>FilterExpr</lhs><rhs><nt def="doc-xquery-PrimaryExpr">PrimaryExpr</nt>  <nt def="doc-xquery-PredicateList">PredicateList</nt></rhs></prod>
        </scrap>
      </smintro>

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

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

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

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

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

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

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

      <scrap headstyle="show">
        <head>Combining Sequences</head>
        <prod num="52 (XQuery)" id="doc-xquery-UnionExpr"><lhs>UnionExpr</lhs><rhs><nt def="doc-xquery-IntersectExceptExpr">IntersectExceptExpr</nt> ( ("union"  |  "|")  <nt def="doc-xquery-IntersectExceptExpr">IntersectExceptExpr</nt> )*</rhs></prod>
        <prod num="53 (XQuery)" id="doc-xquery-IntersectExceptExpr"><lhs>IntersectExceptExpr</lhs><rhs><nt def="doc-xquery-InstanceofExpr">InstanceofExpr</nt> ( ("intersect"  |  "except")  <nt def="doc-xquery-InstanceofExpr">InstanceofExpr</nt> )*</rhs></prod>
      </scrap>

    <smnotation>
      <p id="jd_map_sequenceop">The union, intersect, and except
      expressions are normalized into function calls to the
      appropriate functions. The mapping function
      <map/><a href="#jd_map_sequenceop" class="judgment"><subscript>SequenceOp</subscript></a> is defined by the following
      table:</p>

      <table border="1"><tbody>
      <tr><td rowspan="1" colspan="1">SequenceOp</td><td rowspan="1" colspan="1"><map><emph>SequenceOp</emph></map><a href="#jd_map_sequenceop" class="judgment"><subscript>SequenceOp</subscript></a></td></tr>
      <tr><td rowspan="1" colspan="1">"union"</td><td rowspan="1" colspan="1"><emph>op:</emph>union</td></tr>
      <tr><td rowspan="1" colspan="1">"|"</td><td rowspan="1" colspan="1"><emph>op:</emph>union</td></tr>
      <tr><td rowspan="1" colspan="1">"intersect"</td><td rowspan="1" colspan="1"><emph>op:</emph>intersect</td></tr>
      <tr><td rowspan="1" colspan="1">"except"</td><td rowspan="1" colspan="1"><emph>op:</emph>except</td></tr> </tbody></table>
    </smnotation>

    <smnorm>

      <mapping>
       <xquery>
        <expression>
        <map><emph>Expr</emph><subscript>1</subscript> <emph>SequenceOp</emph> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
        <a href="#sec_apply_ordering_mode"><emph>fs:</emph><code>apply-ordering-mode</code></a> (<map><emph>SequenceOp</emph></map><a href="#jd_map_sequenceop" class="judgment"><subscript>SequenceOp</subscript></a> (
        <map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>, <map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> ))
        </expression>
       </core>
      </mapping>

    </smnorm>

    <smtype>
      <p>The static semantics of the operators that combine sequences
      are defined in <specref ref="sec_op_union_intersect_except"/>.</p>
    </smtype>

    <smeval>
      <p>The dynamic semantics for function calls is given in <specref ref="id-function-calls"/>.</p>
    </smeval>

  </div3>

  </div2>

  <div2 id="sec_arithmetic">
    <head>Arithmetic Expressions</head>

    <p>[XPath/XQuery] provides arithmetic operators for addition,
    subtraction, multiplication, division, and modulus, in their usual
    binary and unary forms.</p>

    <scrap headstyle="show">
     <head>Arithmetic Expressions</head>
     <prod num="50 (XQuery)" id="doc-xquery-AdditiveExpr"><lhs>AdditiveExpr</lhs><rhs><nt def="doc-xquery-MultiplicativeExpr">MultiplicativeExpr</nt> ( ("+"  |  "-")  <nt def="doc-xquery-MultiplicativeExpr">MultiplicativeExpr</nt> )*</rhs></prod>
     <prod num="51 (XQuery)" id="doc-xquery-MultiplicativeExpr"><lhs>MultiplicativeExpr</lhs><rhs><nt def="doc-xquery-UnionExpr">UnionExpr</nt> ( ("*"  |  "div"  |  "idiv"  |  "mod")  <nt def="doc-xquery-UnionExpr">UnionExpr</nt> )*</rhs></prod>
     <prod num="58 (XQuery)" id="doc-xquery-UnaryExpr"><lhs>UnaryExpr</lhs><rhs>("-"  |  "+")* <nt def="doc-xquery-ValueExpr">ValueExpr</nt></rhs></prod>
     <prod num="59 (XQuery)" id="doc-xquery-ValueExpr"><lhs>ValueExpr</lhs><rhs><nt def="doc-xquery-ValidateExpr">ValidateExpr</nt>  |  <nt def="doc-xquery-PathExpr">PathExpr</nt>  |  <nt def="doc-xquery-ExtensionExpr">ExtensionExpr</nt></rhs></prod>
    </scrap>

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

      <scrap headstyle="show">
        <head/>
        <prod num="48 (Core)" id="doc-core-ValueExpr"><lhs>ValueExpr</lhs><rhs><nt def="doc-core-ValidateExpr">ValidateExpr</nt>  |  <nt def="doc-core-StepExpr">StepExpr</nt>  |  <nt def="doc-core-ExtensionExpr">ExtensionExpr</nt></rhs></prod>
      </scrap>
    </smcore>

    <smnotation>
      <p id="jd_map_arithop">The mapping functions
      <map/><a href="#jd_map_arithop" class="judgment"><subscript>ArithOp</subscript></a> and <a href="#jd_map_arithop" class="judgment"><subscript>UnaryArithOp</subscript></a> are
      defined by the following tables:</p>

      <table border="1">
        <tbody>
          <tr><td rowspan="1" colspan="1"><emph>ArithOp</emph></td><td rowspan="1" colspan="1"><map><emph>ArithOp</emph></map><a href="#jd_map_arithop" class="judgment"><subscript>ArithOp</subscript></a></td></tr>
          <tr><td rowspan="1" colspan="1">"+"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>plus</code></td></tr>
          <tr><td rowspan="1" colspan="1">"-"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>minus</code></td></tr>
          <tr><td rowspan="1" colspan="1">"*"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>times</code></td></tr>
          <tr><td rowspan="1" colspan="1">"div"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>div</code></td></tr>
          <tr><td rowspan="1" colspan="1">"mod"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>mod</code></td></tr>
        </tbody>
      </table>

      <table border="1">
        <tbody>
          <tr><td rowspan="1" colspan="1"><emph>UnaryArithOp</emph></td><td rowspan="1" colspan="1"><map><emph>UnaryArithOp</emph></map><a href="#jd_map_arithop" class="judgment"><subscript>UnaryArithOp</subscript></a></td></tr>
          <tr><td rowspan="1" colspan="1">"+"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>unary-plus</code></td></tr>
          <tr><td rowspan="1" colspan="1">"-"</td><td rowspan="1" colspan="1"><emph>fs:</emph><code>unary-minus</code></td></tr>
        </tbody>
      </table>

   </smnotation>

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

  <smnorm>

   <p>The normalization rules for all the arithmetic operators except
   <code>idiv</code> first atomize each argument by applying <code>fn:data</code> and
   then apply the internal function <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a> to each
   argument.  If the first argument to this function has type
   <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>, then the first argument is cast to a double,
   otherwise it is returned unchanged.  The overloaded internal
   function corresponding to the arithmetic operator is then applied
   to the two converted arguments.  The table above maps the operators
   to the corresponding internal function.  The mapping from the
   overloaded internal functions to the corresponding monomorphic
   function is given in <specref ref="sec_operators"/>.</p>

   <mapping>
    <xquery>
     <expression>
      <map><emph>Expr</emph><subscript>1</subscript> <emph>ArithOp</emph>  <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <table summary=""><tbody>
        <tr><td rowspan="1" colspan="1"><map><emph>ArithOp</emph></map><a href="#jd_map_arithop" class="judgment"><subscript>ArithOp</subscript></a>
          (</td><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1.0E0),
         </td></tr>
        <tr><td rowspan="1" colspan="1"/><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1.0E0))
         </td></tr>
       </tbody></table>
     </expression>
    </core>
   </mapping>

   <p>The normalization rules for the <code>idiv</code> operator are similar,
   but instead of casting arguments with type <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> to
   <code>xs:double</code>, they are cast to <code>xs:integer</code>.</p>

   <mapping>
    <xquery>
     <expression>
      <map><emph>Expr</emph><subscript>1</subscript> <code>idiv</code>  <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <table summary=""><tbody>
        <tr valign="baseline"><td rowspan="1" colspan="1"><emph>fs:</emph><code>idiv</code>
          (</td><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1),
         </td></tr>
        <tr><td rowspan="1" colspan="1"/><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1))
         </td></tr>
       </tbody></table>
     </expression>
    </core>
   </mapping>
  

   <p>The unary operators are mapped similarly.</p>

   <mapping>
    <xquery>
     <expression>
      <map><code>+</code> <emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
            <emph>fs:</emph><code>unary-plus</code>(<a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1.0E0))
     </expression>
    </core>
   </mapping>

   <mapping>
    <xquery>
     <expression>
      <map><code>-</code> <emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
        <emph>fs:</emph><code>unary-minus</code>(0, <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), 1.0E0))
     </expression>
    </core>
   </mapping>

</smnorm>

<smtype>
  <p>The static semantics for function calls is given in
  <specref ref="id-function-calls"/>.</p>
</smtype>

<smeval>
  <p>The dynamic semantics for
  function calls is given in <specref ref="id-function-calls"/>.</p>
</smeval>

  </div2>

  <div2>
    <head>Comparison Expressions</head>

    <smintro>

      <p>Comparison expressions allow two values to be
      compared. [XPath/XQuery] provides three kinds of comparison
      expressions, called value comparisons, general comparisons, and
      node comparisons.</p>

      <scrap headstyle="show">
        <head>Comparison Expressions</head>
        <prod num="48 (XQuery)" id="doc-xquery-ComparisonExpr"><lhs>ComparisonExpr</lhs><rhs><nt def="doc-xquery-RangeExpr">RangeExpr</nt> ( (<nt def="doc-xquery-ValueComp">ValueComp</nt><br/>|  <nt def="doc-xquery-GeneralComp">GeneralComp</nt><br/>|  <nt def="doc-xquery-NodeComp">NodeComp</nt>)  <nt def="doc-xquery-RangeExpr">RangeExpr</nt> )?</rhs></prod>
        <prod num="61 (XQuery)" id="doc-xquery-ValueComp"><lhs>ValueComp</lhs><rhs>"eq"  |  "ne"  |  "lt"  |  "le"  |  "gt"  |  "ge"</rhs></prod>
        <prod num="60 (XQuery)" id="doc-xquery-GeneralComp"><lhs>GeneralComp</lhs><rhs>"="  |  "!="  |  "&lt;"  |  "&lt;="  |  "&gt;"  |  "&gt;="</rhs></prod>
        <prod num="62 (XQuery)" id="doc-xquery-NodeComp"><lhs>NodeComp</lhs><rhs>"is"  |  "&lt;&lt;"  |  "&gt;&gt;"</rhs></prod>
      </scrap>
    </smintro>

  <div3 id="sec_value_comparisons">
    <head>Value Comparisons</head>

    <smnotation>
      <p id="jd_map_valueop">The mapping function
      <map/><a href="#jd_map_valueop" class="judgment"><subscript>ValueOp</subscript></a> is defined by the following
      table:</p>

      <table border="1"><tbody>
      <tr><td rowspan="1" colspan="1">ValueOp</td><td rowspan="1" colspan="1"><map>ValueOp</map><a href="#jd_map_valueop" class="judgment"><subscript>ValueOp</subscript></a></td></tr>
      <tr><td rowspan="1" colspan="1">"<code>eq</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>eq</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>ne</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>ne</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>lt</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>lt</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>le</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>le</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>gt</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>gt</td></tr> 
      <tr><td rowspan="1" colspan="1">"<code>ge</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>ge</td></tr></tbody></table>
    </smnotation>

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

    <smnorm>
      <p>The normalization rules for the value comparison operators
      first atomize each argument by applying <code>fn:data</code> and then apply
      the internal function <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a> defined in <specref ref="sec_convert_operand"/>.  If the first argument to this
      function has type <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>, then the first argument
      is cast to a string, otherwise it is returned unchanged.  The
      overloaded internal function corresponding to the value
      comparison operator is then applied to the two converted
      arguments.  The table above maps the value operators to the
      corresponding internal function.  The mapping from the
      overloaded internal functions to the corresponding monomorphic
      function is given in <specref ref="sec_operators"/>.</p>

   <mapping>
    <xquery>
     <expression>
      <map><emph>Expr</emph><subscript>1</subscript> <emph>ValueOp</emph>  <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <table summary=""><tbody>
        <tr><td rowspan="1" colspan="1"><map><emph>ValueOp</emph></map><a href="#jd_map_valueop" class="judgment"><subscript>ValueOp</subscript></a>
          (</td><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), "string"),
         </td></tr>
        <tr><td rowspan="1" colspan="1"/><td rowspan="1" colspan="1"><a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>(<code>fn:data</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)), "string") )
         </td></tr>
       </tbody></table>
     </expression>
    </core>
   </mapping>
</smnorm>

<smtype>
  <p>The static semantics for function calls is given in
  <specref ref="id-function-calls"/>.  The comparison functions all have
  return type <code>xs:boolean</code>, as specified in <bibref ref="xpath-functions"/>. </p>
</smtype>

<smeval>
  <p>The dynamic semantics for
  function calls is given in <specref ref="id-function-calls"/>.</p>
</smeval>

  </div3>

  <div3 id="sec_general_comparisons">
    <head>General Comparisons</head>

<smintro>
<p>
General comparisons are defined by adding existential semantics to
value comparisons. The operands of a general comparison may be
sequences of any length. The result of a general comparison is always
<code>true</code> or <code>false</code>.</p></smintro>

<smnotation>
<p>
For convenience, GeneralOp denotes the operators "<code>=</code>", "<code>!=</code>",
"<code>&lt;</code>", "<code>&lt;=</code>", 
"<code>&gt;</code>", or "<code>&gt;=</code>".
</p>
<p id="jd_map_generalop">The function <map/><a href="#jd_map_generalop" class="judgment"><subscript>GeneralOp</subscript></a> is defined
by the following table:</p>

      <table border="1"><tbody>
      <tr><td rowspan="1" colspan="1"><emph>GeneralOp</emph></td><td rowspan="1" colspan="1"><map><emph>GeneralOp</emph></map><a href="#jd_map_generalop" class="judgment"><subscript>GeneralOp</subscript></a></td></tr>
      <tr><td rowspan="1" colspan="1">"<code>=</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>eq</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>!=</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>ne</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>&lt;</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>lt</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>&lt;=</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>le</td></tr>
      <tr><td rowspan="1" colspan="1">"<code>&gt;</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>gt</td></tr> 
      <tr><td rowspan="1" colspan="1">"<code>&gt;=</code>"</td><td rowspan="1" colspan="1"><emph>fs:</emph>ge</td></tr></tbody></table>

</smnotation>

      <smcore>
        <p>There are no Core grammar productions for general
        comparisons as they are normalized to existentially quantified
        Core expressions.</p>
      </smcore>

      <smnorm>
        <p>The normalization rule for a general comparison expression
          first atomizes each argument by applying <code>fn:data</code> and then
          applies the existentially quantified some expression to
          each sequence.  The internal function
          <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a> is applied to each pair of atomic
          values.  If the first argument to this function has type
          <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>, then the first argument is cast to type
          of the second argument.  If the second argument has type
          <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>, the first argument is cast to a string.
          The overloaded internal function corresponding to the general
          comparison operator is then applied to the two converted
          values. 
        </p>

          <mapping>
            <xquery>
              <expression>
                <map><emph>Expr</emph><subscript>1</subscript> <emph>GeneralOp</emph> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
              </expression>
            </xquery>
            <core>
              <expression>
                <table summary="">
                  <tbody>
                    <tr><td rowspan="1" colspan="1">some $v1 in <code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) satisfies</td></tr>
                    <tr><td rowspan="1" colspan="1">some $v2 in <code>fn:data</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) satisfies</td></tr>
                    <tr><td rowspan="1" colspan="1">let $u1 := <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>($v1, $v2) return </td></tr>
                    <tr><td rowspan="1" colspan="1">let $u2 := <a href="#sec_convert_operand"><emph>fs:</emph><code>convert-operand</code></a>($v2, $v1) return </td></tr>
                    <tr><td rowspan="1" colspan="1"><map><emph>GeneralOp</emph></map><a href="#jd_map_generalop" class="judgment"><subscript>GeneralOp</subscript></a> ($u1, $u2)</td></tr>
                  </tbody>
                </table>
              </expression>
            </core>
          </mapping>
      </smnorm>

<!--
<smtype>
<p>
The static semantics of the existentially quantified "&xq_some;" expression is given in
<specref ref="sec_quantified_expressions"/>.
It always returns &xs_boolean;.  
</p>
</smtype>

<smeval>
<p>
The dynamic semantics of the existentially quantified "&xq_some;" expression is given in
<specref ref="sec_quantified_expressions"/>.
</p>
</smeval>

-->

  </div3>

  <div3>
    <head>Node Comparisons</head>

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

<smnorm>
  <p>The normalization rules for node comparisons map each argument
  expression and then apply the internal function corresponding to the
  node comparison operator.  
The internal function are defined in <specref ref="sec_operators"/>.
</p>

<mapping>
<xquery>
<expression>
<map><emph>Expr</emph><subscript>1</subscript> <code>is</code> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
</expression>
</xquery>
<core>
     <expression>
       <table summary=""><tbody>
       <tr><td rowspan="1" colspan="1">   <emph>fs:</emph><code>is-same-node</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>), (<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))</td></tr>
       </tbody></table>
     </expression>
</core>
</mapping>

<mapping>
<xquery>
<expression>
<map><emph>Expr</emph><subscript>1</subscript> &lt;&lt; <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
</expression>
</xquery>
<core>
<expression>
       <table summary=""><tbody>
       <tr><td rowspan="1" colspan="1"><emph>fs:</emph><code>node-before</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>), (<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))</td></tr>
       </tbody></table>
</expression>
</core>
</mapping>

<mapping>
<xquery>
<expression>
<map><emph>Expr</emph><subscript>1</subscript> &gt;&gt; <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
</expression>
</xquery>
<core>
<expression>
       <table summary=""><tbody>
       <tr><td rowspan="1" colspan="1"><emph>fs:</emph><code>node-after</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>), (<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))</td></tr>
       </tbody></table>
</expression>
</core>
</mapping>

</smnorm>

<smtype>
  <p>The static semantics for the internal functions are defined in <specref ref="sec_operators"/>. </p>
</smtype>

<smeval>
  <p>The dynamic semantics for internal function is defined in <specref ref="sec_operators"/>.</p>
</smeval>

  </div3>

  </div2>

  <div2 id="id-logical-expressions"> 
    <head>Logical Expressions</head>

    <smintro>
      <p>A <term>logical expression</term> is either an
      <term>and-expression</term> or an
      <term>or-expression</term>. The value of a logical expression is
      always one of the boolean values: <code>true</code> or
      <code>false</code>.</p>
    </smintro>

    <scrap headstyle="show">
      <head>Logical Expressions</head>
      <prod num="46 (XQuery)" id="doc-xquery-OrExpr"><lhs>OrExpr</lhs><rhs><nt def="doc-xquery-AndExpr">AndExpr</nt> ( "or"  <nt def="doc-xquery-AndExpr">AndExpr</nt> )*</rhs></prod>
      <prod num="47 (XQuery)" id="doc-xquery-AndExpr"><lhs>AndExpr</lhs><rhs><nt def="doc-xquery-ComparisonExpr">ComparisonExpr</nt> ( "and"  <nt def="doc-xquery-ComparisonExpr">ComparisonExpr</nt> )*</rhs></prod>
    </scrap>

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

      <scrap headstyle="show">
        <head>Core Logical Expressions</head>
        <prod num="44 (Core)" id="doc-core-OrExpr"><lhs>OrExpr</lhs><rhs><nt def="doc-core-AndExpr">AndExpr</nt> ( "or"  <nt def="doc-core-AndExpr">AndExpr</nt> )*</rhs></prod>
        <prod num="45 (Core)" id="doc-core-AndExpr"><lhs>AndExpr</lhs><rhs><nt def="doc-core-CastableExpr">CastableExpr</nt> ( "and"  <nt def="doc-core-CastableExpr">CastableExpr</nt> )*</rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>The normalization rules for "<code>and</code>" and "<code>or</code>" first
      get the effective boolean value of each argument, then apply the
      appropriate Core operator.</p>

   <mapping>
    <xquery>
     <expression>
      <map><emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <code>fn:boolean</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) <code>and</code> <code>fn:boolean</code>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))
     </expression>
    </core>
   </mapping>

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

    <smtype>
      <p>The logical expressions require that each subexpression have
type <code>xs:boolean</code>.  The result type is also <code>xs:boolean</code>.</p>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>n</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>n</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:boolean</code></expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

    </smtype>
    
    <smeval>
      <p>The dynamic semantics of logical expressions is
non-deterministic.   This non-determinism permits implementations to use
short-circuit evaluation strategies when evaluating logical expressions. In the expression, <emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript>,
if either expression raises an error or evaluates to false, the entire
expression may raise an error or evaluate to false. 
In the expression, <emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript>,
if either expression raises an error or evaluates to true, the entire
expression may raise an error or evaluate to true. </p>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>i</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> false</expression>
        </clause>
        <clause><expression> 1 &lt;= i &lt;= 2</expression></clause>
      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                false</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

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

      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>and</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                true</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

<infergr>
  <infer>
    <prejudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
          <expression><emph>Expr</emph><subscript>i</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> true</expression>
        </clause>
        <clause><expression> 1 &lt;= i &lt;= 2</expression></clause>
      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                true</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

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

      </multiclause>
    </prejudge>
    <postjudge>
      <multiclause>
        <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a> </environment>
                <expression><emph>Expr</emph><subscript>1</subscript> <code>or</code> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
                false</expression>
        </clause>
      </multiclause>
    </postjudge>
  </infer>
</infergr>

    </smeval>

  </div2>

  <div2 id="sec_constructors">
    <head>Constructors</head>

    <p>[XPath/XQuery] supports two forms of constructors. <term>Direct
    constructors</term> support literal XML syntax for elements,
    attributes, text nodes, processing-instructions and
    comments. <term>Computed constructors</term> can be used to
    construct elements, attributes, text nodes,
    processing-instructions, comments, and document nodes.  All direct
    constructors are normalized into computed constructors, i.e.,
    there are no direct-constructor expressions in the Core.</p>

  <div3 id="id_element_constructor">
    <head>Direct Element Constructors</head>

    <smintro>
      <p>The static and dynamic semantics of the direct forms of
      element and attribute constructors are specified on the
      equivalent computed element and attribute constructors.</p>

    <scrap headstyle="show">
      <head>Constructors</head>
      <prod num="94 (XQuery)" id="doc-xquery-Constructor"><lhs>Constructor</lhs><rhs><nt def="doc-xquery-DirectConstructor">DirectConstructor</nt><br/>|  <nt def="doc-xquery-ComputedConstructor">ComputedConstructor</nt></rhs></prod>
      <prod num="95 (XQuery)" id="doc-xquery-DirectConstructor"><lhs>DirectConstructor</lhs><rhs><nt def="doc-xquery-DirElemConstructor">DirElemConstructor</nt><br/>|  <nt def="doc-xquery-DirCommentConstructor">DirCommentConstructor</nt><br/>|  <nt def="doc-xquery-DirPIConstructor">DirPIConstructor</nt></rhs></prod>
      <prod num="96 (XQuery)" id="doc-xquery-DirElemConstructor"><lhs>DirElemConstructor</lhs><rhs>"&lt;"  <nt def="prod-xquery-QName">QName</nt>  <nt def="doc-xquery-DirAttributeList">DirAttributeList</nt>  ("/&gt;"  |  ("&gt;"  <nt def="doc-xquery-DirElemContent">DirElemContent</nt>*  "&lt;/"  <nt def="prod-xquery-QName">QName</nt>  <nt def="prod-xquery-S">S</nt>?  "&gt;"))</rhs></prod>
      <prod num="101 (XQuery)" id="doc-xquery-DirElemContent"><lhs>DirElemContent</lhs><rhs><nt def="doc-xquery-DirectConstructor">DirectConstructor</nt><br/>|  <nt def="doc-xquery-ElementContentChar">ElementContentChar</nt><br/>|  <nt def="doc-xquery-CDataSection">CDataSection</nt><br/>|  <nt def="doc-xquery-CommonContent">CommonContent</nt></rhs></prod>
      <prod num="149 (XQuery)" id="doc-xquery-ElementContentChar"><lhs>ElementContentChar</lhs><rhs><nt def="prod-xquery-Char">Char</nt> - [{}&lt;&amp;]</rhs></prod>
      <prod num="102 (XQuery)" id="doc-xquery-CommonContent"><lhs>CommonContent</lhs><rhs><nt def="doc-xquery-PredefinedEntityRef">PredefinedEntityRef</nt>  |  <nt def="prod-xquery-CharRef">CharRef</nt>  |  "{{"  |  "}}"  |  <nt def="doc-xquery-EnclosedExpr">EnclosedExpr</nt></rhs></prod>
      <prod num="107 (XQuery)" id="doc-xquery-CDataSection"><lhs>CDataSection</lhs><rhs>"&lt;![CDATA["  <nt def="doc-xquery-CDataSectionContents">CDataSectionContents</nt>  "]]&gt;"</rhs></prod>
      <prod num="108 (XQuery)" id="doc-xquery-CDataSectionContents"><lhs>CDataSectionContents</lhs><rhs>(<nt def="prod-xquery-Char">Char</nt>* - (Char* ']]&gt;' Char*))</rhs></prod>
      <prod num="97 (XQuery)" id="doc-xquery-DirAttributeList"><lhs>DirAttributeList</lhs><rhs>(<nt def="prod-xquery-S">S</nt>  (<nt def="prod-xquery-QName">QName</nt>  <nt def="prod-xquery-S">S</nt>?  "="  <nt def="prod-xquery-S">S</nt>?  <nt def="doc-xquery-DirAttributeValue">DirAttributeValue</nt>)?)*</rhs></prod>
      <prod num="98 (XQuery)" id="doc-xquery-DirAttributeValue"><lhs>DirAttributeValue</lhs><rhs>('"'  (<nt def="doc-xquery-EscapeQuot">EscapeQuot</nt>  |  <nt def="doc-xquery-QuotAttrValueContent">QuotAttrValueContent</nt>)*  '"')<br/>|  ("'"  (<nt def="doc-xquery-EscapeApos">EscapeApos</nt>  |  <nt def="doc-xquery-AposAttrValueContent">AposAttrValueContent</nt>)*  "'")</rhs></prod>
      <prod num="99 (XQuery)" id="doc-xquery-QuotAttrValueContent"><lhs>QuotAttrValueContent</lhs><rhs><nt def="doc-xquery-QuotAttrContentChar">QuotAttrContentChar</nt><br/>|  <nt def="doc-xquery-CommonContent">CommonContent</nt></rhs></prod>
      <prod num="100 (XQuery)" id="doc-xquery-AposAttrValueContent"><lhs>AposAttrValueContent</lhs><rhs><nt def="doc-xquery-AposAttrContentChar">AposAttrContentChar</nt><br/>|  <nt def="doc-xquery-CommonContent">CommonContent</nt></rhs></prod>
      <prod num="150 (XQuery)" id="doc-xquery-QuotAttrContentChar"><lhs>QuotAttrContentChar</lhs><rhs><nt def="prod-xquery-Char">Char</nt> - ["{}&lt;&amp;]</rhs></prod>
      <prod num="151 (XQuery)" id="doc-xquery-AposAttrContentChar"><lhs>AposAttrContentChar</lhs><rhs><nt def="prod-xquery-Char">Char</nt> - ['{}&lt;&amp;]</rhs></prod>
      <prod num="147 (XQuery)" id="doc-xquery-EscapeQuot"><lhs>EscapeQuot</lhs><rhs>'""'</rhs></prod>
      <prod num="148 (XQuery)" id="doc-xquery-EscapeApos"><lhs>EscapeApos</lhs><rhs>"''"</rhs></prod>
      <prod num="29 (XQuery)" id="doc-xquery-EnclosedExpr"><lhs>EnclosedExpr</lhs><rhs>"{"  <nt def="doc-xquery-Expr">Expr</nt>  "}"</rhs></prod>
    </scrap>

    </smintro>

    <smnotation>
      <p id="jd_map_element_content">The auxiliary mapping rules
      <map/><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a>, and
      <map/><a href="#jd_map_element_content_unit" class="judgment"><subscript>ElementContent-unit</subscript></a> are defined in this
      section and are used for the normalization of the content of
      direct element constructors.</p>
    </smnotation>
  
    <smcore>
      <p>The Core grammar productions for constructors are:</p>

<scrap headstyle="show">
  <head>Constructors</head>
  <prod num="72 (Core)" id="doc-core-Constructor"><lhs>Constructor</lhs><rhs><nt def="doc-core-ComputedConstructor">ComputedConstructor</nt></rhs></prod>
  <prod num="73 (Core)" id="doc-core-ComputedConstructor"><lhs>ComputedConstructor</lhs><rhs><nt def="doc-core-CompDocConstructor">CompDocConstructor</nt><br/>|  <nt def="doc-core-CompElemConstructor">CompElemConstructor</nt><br/>|  <nt def="doc-core-CompAttrConstructor">CompAttrConstructor</nt><br/>|  <nt def="doc-core-CompTextConstructor">CompTextConstructor</nt><br/>|  <nt def="doc-core-CompCommentConstructor">CompCommentConstructor</nt><br/>|  <nt def="doc-core-CompPIConstructor">CompPIConstructor</nt></rhs></prod>
  <prod num="28 (Core)" id="doc-core-EnclosedExpr"><lhs>EnclosedExpr</lhs><rhs>"{"  <nt def="doc-core-Expr">Expr</nt>  "}"</rhs></prod>
</scrap>

      <p>There are no Core grammar productions for direct XML element
      or attribute constructors as they are normalized to computed
      constructors.</p>
    </smcore>

    <smnorm>
      <p>We start with the rules for normalizing a direct element
      constructors' content. Literal XML character data (CDATA) is
      assumed to be processed directly at parsing level so it does not
      require any formal treatment. We distinguish between direct
      element constructors that contain only one element-content unit
      and those that contain more than one element-content unit.  An
      element-content unit is a contiguous sequence of literal
      characters (character references, escaped braces, and predefined
      entity references), one enclosed expression, one direct element
      constructor, one XML comment, or one XML processing instruction.
      Here are three direct element constructors that each contain one
      element-content unit:</p>

<eg xml:space="preserve">
&lt;date&gt;{ xs:date("2003-03-18") }&lt;/date&gt;

&lt;name&gt;Dizzy Gillespe&lt;/name&gt;

&lt;comment&gt;&lt;!-- Just a comment --&gt;&lt;/comment&gt;
</eg>

     <p>The first contains one enclosed expression, the second
     contains one contiguous sequence of characters, and the third
     contains one XML comment.</p>

     <p>After boundary-space is stripped, the next example contains
     six element-content units: </p> <eg xml:space="preserve">
&lt;address&gt;
  &lt;!-- Dizzy's address --&gt;
  { 123 }-0A &lt;street&gt;Roosevelt Ave.&lt;/street&gt; Flushing, NY { 11368 }
&lt;/address&gt;
</eg>

     <p>It contains one XML comment, followed by one enclosed
     expression that contains the integer 123, one contiguous sequence
     of characters ("-0A "), one direct XML element constructor, one
     contiguous sequence of characters (" Flushing, NY"), and one
     enclosed expression that contains the integer 11368. Evaluation
     of that constructor will result in the following element.</p>

<eg xml:space="preserve">
&lt;address&gt;&lt;!-- Dizzy's address --&gt;123-0A &lt;street&gt;Roosevelt Ave.&lt;/street&gt; Flushing, NY 11368&lt;/address&gt;
</eg>

    <p>Adjacent element-content units are convenient because they
    permit arbitrary interleaving of text and atomic data.  During
    evaluation, atomic values are converted to text nodes containing
    the string representations of the atomic values, and then adjacent
    text nodes are concatenated together.  In the example above, the
    integer 123 is converted to a string and concatenated with "-0A"
    and the result is a single text node containing "123-0A".</p>

    <p>In general, we do not want to convert all atomic values to text
    nodes, especially when performing static-type analysis, because we
    lose useful type information.  For example, if we normalize the
    first example above as follows, we lose the important information
    that the user constructed a date value, not just a text node
    containing an arbitrary string: </p>

<eg xml:space="preserve">
&lt;date&gt;{ xs:date("2003-03-18") }&lt;/date&gt;
 (normalization that loses type information) == 
element date { text { "2003-03-18" } } 
</eg>

    <p>To preserve useful type information, we distinguish between
    direct element constructors that contain one element-content unit
    and those that contain more than one (because multiple
    element-content units commonly denote concatenatation of atomic
    data and text). Below are two examples of normalization for
    element constructors.</p>

<eg xml:space="preserve">
&lt;date&gt;{ xs:date("2003-03-18") }&lt;/date&gt;
 ==
element date { xs:date("2003-03-18") } 

&lt;address&gt;
  &lt;!-- Dizzy's address --&gt;
  { 123 }-0A &lt;street&gt;Roosevelt Ave.&lt;/street&gt; Flushing, NY { 11368 }
&lt;/address&gt;
 ==
element address {
  fs:item-sequence-to-node-sequence(
    comment { " Dizzy's address "},
    123, 
    text { "-0A "}, 
    element street {"Roosevelt Ave."},
    text { " Flushing, NY "  },
    11368
  )
}
</eg>

    <p id="jd_map_element_content_unit">Given the distinction between
    direct element constructors that we made above, we give two
    normalization rules for a direct element constructor's content.
    If the direct element constructor contains exactly one
    element-content unit, we simply normalize that unit by applying
    the normalization rule for the element content:</p>

      <mapping>
        <xquery>
          <expression><map>
            <emph>ElementContent</emph><subscript>1</subscript>
          </map><a href="#jd_map_element_content_unit" class="judgment"><subscript>ElementContent-unit</subscript></a></expression>
        </xquery>
        <core>
          <expression><map> <emph>ElementContent</emph><subscript>1</subscript> </map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a> 
          </expression>
        </core>
      </mapping>

    <p>If the direct element constructor contains more than one
    element-content unit, we normalize each unit individually and
    construct a sequence of the normalized results interleaved with
    empty text nodes.  The empty text nodes guarantee that the results
    of evaluating consecutive element-content units can be
    distinguished.  Then we apply the function
    <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>.  <xspecref spec="XQ" ref="id-element-constructor"/> specifies the rules for converting
    a sequence of atomic values and nodes into a sequence of nodes
    before element construction.  The Formal Semantics function
    <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> implements these conversion rules.</p>

      <mapping>
        <xquery>
          <expression><map><emph>ElementContent</emph><subscript>1</subscript> ...,  <emph>ElementContent</emph><subscript>n</subscript></map><a href="#jd_map_element_content_unit" class="judgment"><subscript>ElementContent-unit</subscript></a>, n &gt; 1</expression>
        </xquery>
        <core>
          <expression><a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>(<map> <emph>ElementContent</emph><subscript>1</subscript> </map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a> , text { "" }, ..., text { "" }, <map> <emph>ElementContent</emph><subscript>n</subscript></map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a>)
          </expression>
        </core>
      </mapping>

    <p>We need to distinguish between multiple element-content units,
    because the rule for converting sequences of atomic values into
    strings apply to sequences within distinct enclosed expressions.
    The empty text nodes are eliminated during evaluation of
    <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> when consecutive text nodes are
    coalesced into a single text node.  The text node guarantees that
    a whitespace character will not be inserted between atomic values
    computed by distinct enclosed expressions.  For example, here is
    an expression, its normalization, and the resulting XML value:</p>

<eg xml:space="preserve">
&lt;example&gt;{ 1 }{ 2 }&lt;/example&gt;
 ==
element example { fs:item-sequence-to-node-sequence ((1, text {""}, 2)) }
 ==&gt;
&lt;example&gt;12&lt;/example&gt;
</eg>

      <p>In the absence of the empty text node, the expression would
      evaluate to the following incorrect value:</p>

<eg xml:space="preserve">
&lt;example&gt;{ 1 }{ 2 }&lt;/example&gt;
 (incorrect normalization) ==
element example { fs:item-sequence-to-node-sequence ((1, 2)) }
 (incorrect value) ==&gt;
&lt;example&gt;1 2&lt;/example&gt;
</eg>

      <p>Now that we have explained the normalization rules for direct
      element content, we give the rules for the two forms of direct
      XML element constructors.  Note that the direct attribute
      constructors are normalized twice: the
      <map/><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a> normalizes the
      namespace-declaration attributes and
      <map/><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a> normalizes all other attributes
      that are not namespace-declaration attributes. </p>

      <mapping>
      <xquery>
        <expression><map> &lt; <emph>QName</emph> <emph>AttributeList</emph> &gt;
          <emph>ElementContent</emph>* &lt;/ <emph>QName</emph> S? &gt;
          </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
      </xquery>
      <core>
        <expression>element <map><emph>QName</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>{ <map>
          <emph>AttributeList</emph> </map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a> , <map>
          <emph>AttributeList</emph> </map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a> , <map>
          <emph>ElementContent</emph>* </map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a> }
        </expression>
      </core>
      </mapping>

      <mapping>
      <xquery>
        <expression><map> &lt; <emph>QName</emph> <emph>AttributeList</emph> /&gt;
          </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></expression>
      </xquery>
      <core>
        <expression>element <map><emph>QName</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> { <map>
          <emph>AttributeList</emph> </map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a> , <map>
        <emph>AttributeList</emph> </map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a> }
        </expression>
      </core>
      </mapping>

      <p>Next, we give the normalization rules for each
      element-content unit.  The normalization rule for a contiguous
      sequence of characters assumes:</p>

      <olist>
      <item><p>
             that the significant whitespace characters in 
             element constructors have been preserved, as described in
             <specref ref="sec_whitespace"/>; 
      </p></item>
      <item><p>that character
            references have been resolved to individual characters and
             predefined entity references have been resolved to sequences of 
            characters, and 
      </p></item>
      <item><p>that the rule is applied to the longest
            contiguous sequence of characters.</p></item>
      </olist>

      <p>The following normalization rule takes the longest
      consecutive sequence of individual characters that include
      literal characters, escaped curly braces, character references,
      and predefined entity references and normalizes the character
      sequence as a text node containing the string of characters.</p>

      <mapping>
        <xquery>
          <expression><map>(<emph>Char</emph> | "{{" | "}}" | <emph>CharRef</emph> |
          <emph>PredefinedEntityRef</emph>)+</map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a></expression>
        </xquery>
        <core>
          <expression>text {
          <code>fn:codepoints-to-string</code>((<emph>Char</emph> | "{{" | "}}" | <emph>CharRef</emph> |
          <emph>PredefinedEntityRef</emph>)+) } </expression>
        </core>
      </mapping>

    <p>XML processing instructions and comments in element content are
    normalized by applying the standard normalization rules for
    expressions, which appear in <specref ref="sec_other_constructors"/>.</p>

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

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

    <p>An enclosed expression in element content is normalized by
    normalizing each individual expression in its expression sequence
    and then constructing a sequence of the normalized values:</p>

      <mapping>
        <xquery>
          <expression><map>
            { <emph>Expr</emph><subscript>1</subscript>, ..., <emph>Expr</emph><subscript>n</subscript> }
          </map><a href="#jd_map_element_content" class="judgment"><subscript>ElementContent</subscript></a></expression>
        </xquery>
        <core>
          <expression><map> <emph>Expr</emph><subscript>1</subscript> </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> , ..., <map> <emph>Expr</emph><subscript>n</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
        </core>
      </mapping>
      </smnorm>

      <smtype>
        <p>There are no additional static type rules for direct XML
        element or attribute constructors.</p>
      </smtype>

      <smeval>
        <p>There are no additional dynamic evaluation rules for direct
        XML element or attribute constructors.</p>
      </smeval>

      <div4 id="sec_direct_attributes">
        <head>Attributes</head>

      <p>Like literal XML element constructors, literal XML attribute
      constructors are normalized to computed attribute
      constructors.</p>

      <smnotation>
        <p>The auxiliary mapping rules <map/><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>,
        <map/><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a>, and
        <map/><a href="#jd_map_attr_content_unit" class="judgment"><subscript>AttributeContent-unit</subscript></a>, are defined in
        this section and are used for the normalization of the content
        of direct attribute constructors.</p>
      </smnotation>

      <smnorm>
        <p id="sec_attribute_normalization">Direct attributes may
        contain namespace-declaration attributes.  The normalization
        rules for attributes ignore namespace-declaration attributes
        -- they are handled by the normalization rules in <specref ref="sec_namespace_attrs"/>.</p>

        <p>An <emph>AttributeList</emph> is normalized by the following rule,
        which maps each of the individual attribute-value expressions
        in the attribute list and constructs a sequence of the
        normalized values.</p>

      <mapping>
      <xquery>
        <expression>
      <map><table><tbody>
      <tr><td rowspan="1" colspan="1">
          <emph>QName</emph><subscript>1</subscript> S? = S? '"' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a><subscript>0</subscript> '"'
      </td></tr>
      <tr><td rowspan="1" colspan="1">
          <emph>QName</emph><subscript>n</subscript> S? = S? '""' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a><subscript>n</subscript> '"'
      </td></tr>
      </tbody></table></map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>
        </expression>
        </xquery>
      <core>
      <expression>
      <table><tbody>
      <tr><td rowspan="1" colspan="1">
        (<map><emph>QName</emph><subscript>1</subscript> S? = S? '"' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a><subscript>0</subscript> '"'</map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>
      </td></tr>
      <tr><td rowspan="1" colspan="1">
         ..., 
      </td></tr>
      <tr><td rowspan="1" colspan="1">
        <map><emph>QName</emph><subscript>n</subscript> S? = S? '"' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a><subscript>n</subscript> '"'</map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>)
      </td></tr>
      </tbody></table>
      </expression>
      </core>
      </mapping>

      <p id="jd_map_attribute">Namespace-declaration attributes, i.e.,
      those attributes whose prefix is <code>xmlns</code> are ignored
      by mapping them to the empty sequence.</p>

      <mapping>
      <xquery>
        <expression>
<table><tbody><tr><td rowspan="1" colspan="1">
      <map><emph>Prefix</emph>:<emph>LocalPart</emph> S? = S? '"'
      <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a> '"'</map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>
</td></tr>
<tr><td rowspan="1" colspan="1">
      (<emph>Prefix</emph> = <code>xmlns</code>)
</td></tr>
</tbody></table>
        </expression>
        </xquery>
      <core>
      <expression>
        ()
      </expression>
      </core>
      </mapping>

<p>All attributes that are not namespace-declaration attributes are
mapped to computed attribute constructors. 

</p>
      <mapping>
      <xquery>
        <expression>
<table><tbody><tr><td rowspan="1" colspan="1">
      <map><emph>Prefix</emph>:<emph>LocalPart</emph> S? = S? '"'
      <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a> '"'</map><a href="#jd_map_attribute" class="judgment"><subscript>Attribute</subscript></a>
</td></tr>
<tr><td rowspan="1" colspan="1">
      <a href="#jd_not" class="judgment"><term>not</term></a>(<emph>Prefix</emph> = <code>xmlns</code>)
</td></tr>
</tbody></table>
        </expression>
        </xquery>
      <core>
      <expression>
        attribute <map><emph>Prefix</emph>:<emph>LocalPart</emph> </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> { <map><a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a></map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a>}
      </expression>
      </core>
      </mapping>

      <p id="jd_map_attr_content_unit">As with literal XML elements, we need to distinguish between
      direct attribute constructors that contain one attribute-content
      unit and those that contain multiple attribute-content units,
      because the rule for converting sequences of atomic values into
      strings are applied to sequences within distinct enclosed
      expressions.  If the direct attribute constructor contains
      exactly one attribute-content unit, we simply normalize that
      unit by applying the normalization rule for the attribute
      content:
      </p>

      <mapping>
      <xquery>
        <expression><map> <emph>AttributeValueContent</emph><subscript>1</subscript>
          </map><a href="#jd_map_attr_content_unit" class="judgment"><subscript>AttributeContent-unit</subscript></a></expression>
      </xquery>
      <core>
        <expression>
          <map><emph>AttributeValueContent</emph><subscript>1</subscript></map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a>
        </expression>
      </core>
      </mapping>

      <p id="jd_map_attr_content">If the direct attribute constructor
      contains more than one attribute-content unit, we normalize each
      unit individually and construct a sequence of the normalized
      results interleaved with empty text nodes.  The empty text nodes
      guarantee that the results of evaluating consecutive
      attribute-content units can be distinguished.  Then we apply the
      function <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>, which applies the
      appropriate conversion rules to the normalized attribute
      content:</p>

      <mapping>
      <xquery>
        <expression><map> <emph>AttributeValueContent</emph><subscript>1</subscript> ...,
          <emph>AttributeValueContent</emph><subscript>n</subscript>
          </map><a href="#jd_map_attr_content_unit" class="judgment"><subscript>AttributeContent-unit</subscript></a>, n &gt; 1</expression>
      </xquery>
      <core>
        <expression>
          <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>((<map> <emph>AttributeValueContent</emph><subscript>1</subscript>
          </map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a> , text { "" }, ..., text
          {""}, <map>
          <emph>AttributeValueContent</emph><subscript>n</subscript></map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a>))
        </expression>
      </core>
      </mapping>

      <p>Literal characters, escaped curly braces, character
      references, and predefined entity references in attribute
      content are treated as in element content.  In addition, the
      normalization rule for characters in attributes assumes:</p>

      <olist>
        <item><p>that an escaped single or double quote is converted
        to an individual single or double quote.</p></item>
      </olist>

      <p>The following normalization rules take the longest
      consecutive sequence of individual characters that include
      literal characters, escaped curly braces, escaped quotes,
      character references, predefined entity references, and escaped
      single and double quotes and normalizes the character sequence
      as a string. </p>

      <mapping>
        <xquery>
          <expression><map>( <emph>Char</emph> | <emph>CharRef</emph> |
          <emph>EscapeQuot</emph> | <emph>EscapeApos</emph> | <emph>PredefinedEntityRef</emph>
          ) +</map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a></expression>
        </xquery>
        <core>
          <expression><code>fn:codepoints-to-string</code>(( <emph>Char</emph> | <emph>CharRef</emph> |
          <emph>EscapeQuot</emph> | <emph>EscapeApos</emph> | <emph>PredefinedEntityRef</emph>
          )+)</expression>
        </core>
      </mapping>

      <p>We normalize an enclosed expression in attribute content by
      normalizing each individual expression in its expression
      sequence and then construct a sequence of the normalized
      values:</p>

      <mapping>
      <xquery>
        <expression><map> { <emph>Expr</emph><subscript>0</subscript>, ..., <emph>Expr</emph><subscript>n</subscript> }
          </map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a></expression>
      </xquery>
      <core>
        <expression>(<map> <emph>Expr</emph><subscript>0</subscript> </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> , ..., <map>
        <emph>Expr</emph><subscript>n</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)
        </expression>
      </core>
      </mapping>
    </smnorm>
    </div4>

    <div4 id="sec_namespace_attrs">
      <head>Namespace Declaration Attributes</head>

<smnotation><p id="jd_map_namespace_attr">
The auxiliary mapping rules  <map/><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>, and
      <map/><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a> are defined in this section
and 
      are used for the
      normalization of namespace declaration attributes.
</p></smnotation>

      <smnorm>
      <p>Direct attributes may contain namespace-declaration
      attributes.  The normalization rules for namespace-declaration attributes ignore
      all non-namespace attributes -- they are handled by the
      normalization rules in <specref ref="sec_direct_attributes"/>. 
      </p>

      <p>An <emph>AttributeList</emph> containing namespace-declaration
      attributes is normalized by the following rule,
      which maps each of the individual namespace-declaration attributes in
      the attribute list and constructs a sequence of the normalized
      namespace attribute values.</p>

      <mapping>
      <xquery>
        <expression>
      <map><table><tbody>
      <tr><td rowspan="1" colspan="1">
          <emph>QName</emph><subscript>1</subscript> S? = S? '"' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a><subscript>0</subscript> '"'
      </td></tr>
      <tr><td rowspan="1" colspan="1">
          <emph>QName</emph><subscript>n</subscript> S? = S? '""' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a><subscript>n</subscript> '"'
      </td></tr>
      </tbody></table></map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttrs</subscript></a>
        </expression>
        </xquery>
      <core>
      <expression>
      <table><tbody>
      <tr><td rowspan="1" colspan="1">
        (<map><emph>QName</emph><subscript>1</subscript> S? = S? '"' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a><subscript>0</subscript> '"'</map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>
      </td></tr>
      <tr><td rowspan="1" colspan="1">
         ..., 
      </td></tr>
      <tr><td rowspan="1" colspan="1">
        <map><emph>QName</emph><subscript>n</subscript> S? = S? '"' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a><subscript>n</subscript> '"'</map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>)
      </td></tr>
      </tbody></table>
      </expression>
      </core>
      </mapping>

      <p>Attributes whose prefix is not <code>xmlns</code> are ignored
      by mapping them to the empty sequence.</p>

      <mapping>
      <xquery>
        <expression>
<table><tbody><tr><td rowspan="1" colspan="1">
      <map><emph>Prefix</emph>:<emph>LocalPart</emph> S? = S? '"' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a> '"'</map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>
</td></tr>
<tr><td rowspan="1" colspan="1">
      not (<emph>Prefix</emph> = <code>xmlns</code>)
</td></tr>
</tbody></table>
</expression>
        </xquery>
      <core>
      <expression>
        ()
      </expression>
      </core>
      </mapping>

<p>Namespace-declaration attributes are normalized to local
namespace declarations (<emph>CompElemNamespace</emph>). 
</p>
      <mapping>
      <xquery>
        <expression>
<table><tbody><tr><td rowspan="1" colspan="1">
      <map><emph>Prefix</emph>:<emph>LocalPart</emph> S? = S? '"' <a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a> '"'</map><a href="#jd_map_namespace_attr" class="judgment"><subscript>NamespaceAttr</subscript></a>
</td></tr>
<tr><td rowspan="1" colspan="1">
      (<emph>Prefix</emph> = <code>xmlns</code>)
</td></tr>
</tbody></table>
        </expression>
        </xquery>
      <core>
      <expression>
        namespace <emph>LocalPart</emph> { <map><a href="#doc-fs-AttributeValue"><emph>AttributeValue</emph></a></map><a href="#jd_map_attr_content" class="judgment"><subscript>AttributeContent</subscript></a>}
      </expression>
      </core>
      </mapping>

    </smnorm>
    </div4>

    <div4>
      <head>Content</head>
<p>The rules for normalizing element content are given above in
<specref ref="id_element_constructor"/>.</p>
    </div4>

    <div4 id="sec_whitespace">
      <head>Whitespace in Element Content</head>

      <p><xspecref spec="XQ" ref="id-whitespace"/> describes
      how whitespace in element and attribute constructors is
      processed depending on the value of the <code>xmlspace</code>
      declaration in the query prolog.  the Formal Semantics assumes
      that the rules for handling whitespace are applied prior to
      normalization rules, for example, during parsing of a query.
      Therefore, there are no formal rules for handling
      whitespace.</p>
    </div4>

  </div3>
  <!-- ***************** PI and Comments       ****** -->
  <div3 id="sec_other_constructors">
    <head>Other Direct Constructors</head>

    <scrap headstyle="show">
      <head>Other Constructors</head>
      <prod num="105 (XQuery)" id="doc-xquery-DirPIConstructor"><lhs>DirPIConstructor</lhs><rhs>"&lt;?"  <nt def="prod-xquery-PITarget">PITarget</nt>  (<nt def="prod-xquery-S">S</nt>  <nt def="doc-xquery-DirPIContents">DirPIContents</nt>)?  "?&gt;"</rhs></prod>
      <prod num="106 (XQuery)" id="doc-xquery-DirPIContents"><lhs>DirPIContents</lhs><rhs>(<nt def="prod-xquery-Char">Char</nt>* - (Char* '?&gt;' Char*))</rhs></prod>
      <prod num="103 (XQuery)" id="doc-xquery-DirCommentConstructor"><lhs>DirCommentConstructor</lhs><rhs>"&lt;!--"  <nt def="doc-xquery-DirCommentContents">DirCommentContents</nt>  "--&gt;"</rhs></prod>
      <prod num="104 (XQuery)" id="doc-xquery-DirCommentContents"><lhs>DirCommentContents</lhs><rhs>((<nt def="prod-xquery-Char">Char</nt> - '-')  |  ('-'  (<nt def="prod-xquery-Char">Char</nt> - '-')))*</rhs></prod>
    </scrap>
   <smnorm>

   <p>A literal XML processing instruction is normalized into a
   computed processing-instruction constructor; its character content
   is converted to a string as in attribute content.</p>

   <mapping>
    <xquery>
     <expression>
      <map>&lt;? <emph>NCName</emph> <emph>Char</emph>* ?&gt;"</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </xquery>
    <core>
     <expression>
      <map>processing-instruction <emph>NCName</emph> { <emph>Char</emph>* }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
     </expression>
    </core>
   </mapping>

    <p>A literal XML comment is normalized into a computed comment
    constructor; its character content is converted to a string as in
    attribute content.</p>

    <mapping>
    <xquery>
    <expression>
     <map>&lt;!-- <emph>Char</emph>* --&gt;</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
    </expression>
    </xquery>
    <core>
    <expression>
     <map>comment { <emph>Char</emph>* }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
    </expression>
    </core>
    </mapping>
    </smnorm>

    <smtype>
      <p>There are no additional static type rules for direct
      processing-instruction or comment constructors.</p>
    </smtype>

    <smeval>
      <p>There are no additional dynamic evaluation rules for direct
      processing-instruction or comment constructors.</p>
    </smeval>

  </div3>
   
  <div3>
    <head>Computed Constructors</head>

    <scrap headstyle="show">
      <head>Computed Constructors</head>
      <prod num="109 (XQuery)" id="doc-xquery-ComputedConstructor"><lhs>ComputedConstructor</lhs><rhs><nt def="doc-xquery-CompDocConstructor">CompDocConstructor</nt><br/>|  <nt def="doc-xquery-CompElemConstructor">CompElemConstructor</nt><br/>|  <nt def="doc-xquery-CompAttrConstructor">CompAttrConstructor</nt><br/>|  <nt def="doc-xquery-CompTextConstructor">CompTextConstructor</nt><br/>|  <nt def="doc-xquery-CompCommentConstructor">CompCommentConstructor</nt><br/>|  <nt def="doc-xquery-CompPIConstructor">CompPIConstructor</nt></rhs></prod>
    </scrap>

  <div4 id="sec_comp_elem_constructor">
    <head>Computed Element Constructors</head>

    <smintro><p>This section describes the semantics of computed
    element constructors. Remember that direct element constructors
    are normalized into computed element constructors. This document
    does not formally specify how namespaces are copied. The semantics
    of namespaces copying in element constructors can be found in
    <bibref ref="xquery"/>.</p></smintro>

    <scrap headstyle="show">
      <head/>
      <prod num="111 (XQuery)" id="doc-xquery-CompElemConstructor"><lhs>CompElemConstructor</lhs><rhs>(("element"  <nt def="prod-xquery-QName">QName</nt>    "{")  |  ("element"    "{"  <nt def="doc-xquery-Expr">Expr</nt>  "}"  "{"))  <nt def="doc-xquery-ContentExpr">ContentExpr</nt>?  "}"</rhs></prod>
      <prod num="112 (XQuery)" id="doc-xquery-ContentExpr"><lhs>ContentExpr</lhs><rhs><nt def="doc-xquery-Expr">Expr</nt></rhs></prod>
    </scrap>

    <smnotation>
      <p id="jd_split_namespaces">Local namespace declarations may
      occur explicitly in a computed element constructor or may be the
      result of normalizing namespace-declaration attributes contained
      in direct element constructors.  For local element declarations
      that occur explicitly in a query, the immediately enclosing
      expression of the local namespace declaration
      (<emph>CompElemNamespace</emph>) must be a computed element constructor;
      otherwise, as specified in [XPath/XQuery], a static error is
      raised.</p>
    </smnotation>

    <smcore>
      <p>The Core grammar productions for computed element
      constructors are:</p>

      <scrap headstyle="show"><head>Computed Element Constructors</head>
      <prod num="75 (Core)" id="doc-core-CompElemConstructor"><lhs>CompElemConstructor</lhs><rhs>(("element"  <nt def="prod-core-QName">QName</nt>    "{")  |  ("element"    "{"  <nt def="doc-core-Expr">Expr</nt>  "}"  "{"))  <nt def="doc-core-ContentExpr">ContentExpr</nt>  "}"</rhs></prod>
      <prod num="76 (Core)" id="doc-core-ContentExpr"><lhs>ContentExpr</lhs><rhs><nt def="doc-core-Expr">Expr</nt></rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>If the content expression is missing, the computed element
      constructor is normalized as if its content expression was the
      empty sequence.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>element <emph>QName</emph> { }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
         <map>element <emph>QName</emph> { () }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </core>
      </mapping>

      <p>Computed element constructors using the
      <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> function over their content
      expression.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>element <emph>QName</emph> { <emph>Expr</emph> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
         element <emph>QName</emph> { <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
        </expression>
       </core>
      </mapping>

      <p>When the name of the element is also computed, the
      normalization rule applies atomization to the name
      expression.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>element { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
          element { <code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }{ <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
        </expression>
       </core>
      </mapping>
    </smnorm>

    <smtype>
      <p>The normalization rules of direct element and attribute
      constructors leave us with only the computed forms of
      constructors. The static semantic for constructors is defined on
      all the computed forms. The computed element constructor itself
      has two forms: one in which the element name is a literal
      <emph>QName</emph>, and the other in which the element name is a
      computed expression.</p>

      <p>A computed element constructor creates a new element with
      either the <xtermref spec="XQ" ref="dt-type-annotation"/>
      <a href="#dt-xdt_untyped"><code>xdt:untyped</code></a> (in strip construction mode), or with the
      <xtermref spec="XQ" ref="dt-type-annotation"/> <code>xs:anyType</code> (in
      preserve construction mode). The content expression must return
      a sequence of nodes with attribute nodes at the beginning.</p>

      <infergr>
	<infer>
	  <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = preserve</expression>
 	      </clause>
 	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      attribute *, (element | text | comment | processing-instruction) * </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	            <expression> element <emph>QName</emph> { <emph>Expr</emph> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> element <emph>QName</emph> of type <code>xs:anyType</code>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
      </infergr>

      <infergr>
	<infer>
	  <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = strip</expression>
 	      </clause>
 	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      attribute *, (element | text | comment | processing-instruction) * </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	            <expression> element <emph>QName</emph> { <emph>Expr</emph> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> element <emph>QName</emph> of type <a href="#dt-xdt_untyped"><code>xdt:untyped</code></a>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
      </infergr>

      <p>In case the element name is computed as well, the name
      expression must be of type <code>xs:QName</code>, <code>xs:string</code>, or
      <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>.</p>

      <infergr>
	<infer>
	  <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = preserve</expression>
 	      </clause>
 	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      (<code>xs:QName</code> | <code>xs:string</code> | <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>) </expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      attribute *, (element | text | comment | processing-instruction) * </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	            <expression> element { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> element of type <code>xs:anyType</code>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
      </infergr>

      <infergr>
	<infer>
	  <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = strip</expression>
 	      </clause>
 	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      (<code>xs:QName</code> | <code>xs:string</code> | <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>) </expression>
	      </clause>
	    </multiclause>
	    <multiclause>
              <clause>
	      <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	      <expression> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      attribute *, (element | text | comment | processing-instruction) * </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	            <expression> element { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> element of type <a href="#dt-xdt_untyped"><code>xdt:untyped</code></a>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
      </infergr>

     </smtype>

     <smeval>
       <p>The following rules take a computed element constructor
       expression and construct an element node.  The dynamic
       semantics for computed element constructors is the most complex
       of all expressions in XQuery.  Here is how to read the rule
       below.</p>

       <p>First, the element's content expression is partitioned into
       the local namespace declarations and all other expressions, and
       the local namespace declarations are evaluated, yielding a
       sequence of namespace bindings.  The static environment is
       extended to include the new namespace bindings, which are all
       <a href="#term_active">active</a>.  In <xspecref spec="XQ" ref="id-namespaces"/>, it is implementation-defined whether
       undeclaration of namespace prefixes (by setting the namespace
       prefix to the zero-length string) in an element constructor is
       supported.  In the dynamic semantics below, we assume all local
       namespace declarations declare a binding of a prefix to a
       URI.</p>

      <p>Second, the function <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> is applied to
      the element's content expression (excluding local namespace
      declarations); this function call is evaluated in the new static
      and dynamic environment.  Recall from <specref ref="id_element_constructor"/> that during normalization, we do
      not convert the content of direct element constructors that
      contain one element-content unit.  This guarantees that useful
      type information is preserved for static analysis.  Since the
      conversion function <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> was not applied to
      all element constructors during normalization, we have to apply
      it at evaluation time.  (Obviously, it is possible to elide the
      application of <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> injected during
      normalization and the application injected during evaluation.)
      The resulting value <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> must match zero-or-more
      attributes followed by zero-or-more element, text,
      processing-instruction or comment nodes.</p>

      <p>Third, The namespace bindings are concatenated with the list
      of active namespaces in the namespace environment <a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a>
      and the namespaces corresponding to the element's name and all
      attributes names.  The resulting sequence is the sequence of
      namespace bindings for the element.</p>

       <infergr>
	 <infer>
	   <prejudge>
	     <multiclause>
	       <clause>
		 <expression> <emph>Expr</emph> = <emph>CompElemNamespace</emph><subscript>1</subscript>, ..., <emph>CompElemNamespace</emph><subscript>n</subscript>, (<emph>Expr</emph><subscript>0</subscript>) </expression>
	       </clause>
	     </multiclause>

            <multiclause> 
            <clause>          
            <expression><emph>CompElemNamespace</emph><subscript>1</subscript> = namespace <emph>NCName</emph><subscript>1</subscript> { <emph>URI</emph><subscript>1</subscript> }</expression>
            </clause>
            </multiclause>
	     <multiclause>
               <clause><expression>...</expression></clause>
             </multiclause>
            <multiclause>
            <clause>          
            <expression><emph>CompElemNamespace</emph><subscript>n</subscript> = namespace  <emph>NCName</emph><subscript>n</subscript> { <emph>URI</emph><subscript>n</subscript> }</expression>
            </clause>
            </multiclause>
            <multiclause>
            <clause>
            <expression>
              <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> = 
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>NCName</emph><subscript>1</subscript> =&gt; (active, <emph>URI</emph><subscript>1</subscript>)</expression>
              </update>
            </expression>
            </clause>
            </multiclause>
            <multiclause>
            <clause><expression>...</expression></clause>
            </multiclause>
            <multiclause>
            <clause>
            <expression>
              <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript> = 
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n-1</subscript>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>NCName</emph><subscript>n</subscript> =&gt; (active, <emph>URI</emph><subscript>n</subscript>)</expression>
              </update>
            </expression>
            </clause>
            </multiclause>

	     <multiclause>
	       <clause>
		 <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript>; <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		 <expression> <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>(<emph>Expr</emph><subscript>0</subscript>) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
		 <environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                 <expression> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> (attribute*, (element | text | processing-instruction | comment)*)</expression>
	       </clause>
	     </multiclause>

	     <multiclause>
	       <clause>
		 <expression><a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> = 
		 <emph>CompElemNamespace</emph><subscript>1</subscript>,
		 ..., <emph>CompElemNamespace</emph><subscript>n</subscript>,
		 <a href="#fs_activeNS">fs:<code>active_ns</code></a>(<a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a>),
		 <a href="#fs_ns_from_items">fs:<code>get_static_ns_from_items</code></a>(<a href="#xq_stat_env_def" class="env">statEnv</a>, <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript>)</expression>
	       </clause>
	     </multiclause>

	   </prejudge>
	   <postjudge>
	     <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a>; <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
	       <expression>
		     element <emph>QName</emph> { <emph>Expr</emph> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript>
	       </expression>
	     </clause>
	   </postjudge>
	 </infer>
       </infergr>

       <p>The dynamic evaluation of an element constructor with a
       computed name is similar.  There is one additional rule that
       checks that the value of the element's name expression matches
       <code>xs:QName</code>.</p>

       <infergr>
	 <infer>
	   <prejudge>
	     <multiclause>
	       <clause>
		 <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment><expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript>
		 </expression>
	       </clause>
	       <clause>
                 <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		 <expression><a href="#doc-fs-Value"><emph>Value</emph></a><subscript>0</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <code>xs:QName</code>
		 </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
		 <expression> <emph>Expr</emph><subscript>2</subscript> = <emph>CompElemNamespace</emph><subscript>1</subscript>, ..., <emph>CompElemNamespace</emph><subscript>n</subscript>, (<emph>Expr</emph><subscript>3</subscript>) </expression>
	       </clause>
	     </multiclause>

            <multiclause> 
            <clause>          
            <expression><emph>CompElemNamespace</emph><subscript>1</subscript> = namespace
            <emph>NCName</emph><subscript>1</subscript> { <emph>URI</emph><subscript>1</subscript> }</expression>
            </clause>
            </multiclause>
            <multiclause>
            <clause><expression>...</expression></clause>
            </multiclause>

            <multiclause>
            <clause>          
            <expression><emph>CompElemNamespace</emph><subscript>n</subscript> = namespace  <emph>NCName</emph><subscript>n</subscript> { <emph>URI</emph><subscript>n</subscript> }</expression>
            </clause>
            </multiclause>

            <multiclause>
            <clause>
            <expression>
              <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> = 
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>NCName</emph> =&gt; (active, <emph>URI</emph><subscript>1</subscript>)</expression>
              </update>
            </expression>
            </clause>
            </multiclause>
            <multiclause>
            <clause><expression>...</expression></clause>
            </multiclause>
            <multiclause>
            <clause>
            <expression>
              <a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript> = 
              <update>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n-1</subscript>.<a href="#xq_ns_env" class="env">namespace</a></environment>
                <expression><emph>NCName</emph> =&gt; (active, <emph>URI</emph><subscript>n</subscript>)</expression>
              </update>
            </expression>
            </clause>
            </multiclause>

	     <multiclause>
	       <clause>
		 <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript>, <a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		 <expression> <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>(<emph>Expr</emph><subscript>3</subscript>) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
                 <environment><a href="#xq_stat_env_def" class="env">statEnv</a><subscript>n</subscript></environment>
		 <expression> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> (attribute*, (element | text | processing-instruction | comment)*)</expression>
	       </clause>
	     </multiclause>

	     <multiclause>
	       <clause>
		 <expression><a href="#doc-fs-NamespaceBindings"><emph>NamespaceBindings</emph></a> = 
		 <emph>CompElemNamespace</emph><subscript>1</subscript>,
		 ..., <emph>CompElemNamespace</emph><subscript>n</subscript>,
		 <a href="#fs_activeNS">fs:<code>active_ns</code></a>(<a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_ns_env" class="env">namespace</a>),
		 <a href="#fs_ns_from_items">fs:<code>get_static_ns_from_items</code></a>(<a href="#xq_stat_env_def" class="env">statEnv</a>, <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>)</expression>
	       </clause>
	     </multiclause>

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

    </smeval>

    </div4>

    <div4 id="sec_attribute_constructor">
      <head>Computed Attribute Constructors</head>

      <scrap headstyle="show">
        <head/>
        <prod num="113 (XQuery)" id="doc-xquery-CompAttrConstructor"><lhs>CompAttrConstructor</lhs><rhs>(("attribute"  <nt def="prod-xquery-QName">QName</nt>    "{")  |  ("attribute"    "{"  <nt def="doc-xquery-Expr">Expr</nt>  "}"  "{"))  <nt def="doc-xquery-Expr">Expr</nt>?  "}"</rhs></prod>
      </scrap>

      <smcore>
        <p>The Core grammar production for computed attribute
        constructors is:</p>

      <scrap headstyle="show"><head>Computed Attribute Constructors</head>
      <prod num="77 (Core)" id="doc-core-CompAttrConstructor"><lhs>CompAttrConstructor</lhs><rhs>(("attribute"  <nt def="prod-core-QName">QName</nt>    "{")  |  ("attribute"    "{"  <nt def="doc-core-Expr">Expr</nt>  "}"  "{"))  <nt def="doc-core-Expr">Expr</nt>  "}"</rhs></prod>
      </scrap>
      </smcore>

      <smnorm>
        <p>Computed attribute constructors are normalized by mapping
        their name and content expression in a similar way as computed
        element constructors. The normalization rule uses the
        <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a> function.</p>

        <mapping>
         <xquery>
          <expression>
           <map>attribute <emph>QName</emph> { }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>
           <map>attribute <emph>QName</emph> { () }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </core>
        </mapping>

        <mapping>
         <xquery>
          <expression>
           <map>attribute <emph>QName</emph> { <emph>Expr</emph> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>attribute <emph>QName</emph> { <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
          </expression>
         </core>
        </mapping>

        <mapping>
         <xquery>
          <expression>
           <map>attribute { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
          </expression>
         </xquery>
         <core>
          <expression>
            attribute { <code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) } { <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
          </expression>
         </core>
        </mapping>
    </smnorm>

    <smtype>
      <p>The normalization rules for direct attribute constructors
      leave us with only the computed form of the attribute
      constructors.  Like in a computed element constructor, a
      computed attribute constructor has two forms: one in which the
      attribute name is a literal <emph>QName</emph>, and the other in which
      the attribute name is a computed expression.</p>

      <p>In the case of attribute constructors, the <xtermref spec="XQ" ref="dt-type-annotation"/> is always
      <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>.</p>

     <infergr>
	<infer>
	  <prejudge>
	    <multiclause>
              <clause>
	        <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	        <expression> <emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause>
                <environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                <expression><a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a></expression>
              </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause>
              <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	      <expression> attribute <emph>QName</emph> { <emph>Expr</emph> } <a href="#jd_has_type" class="judgment"><term>:</term></a> attribute <emph>QName</emph> of type <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
       </infergr>

      <infergr>
	<infer>
	  <prejudge>
	    <multiclause>
              <clause>
	      <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	      <expression> <emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                      (<code>xs:QName</code> | <code>xs:string</code> | <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>) </expression>
	      </clause>
	    </multiclause>
	    <multiclause>
              <clause>
	      <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	      <expression> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
	      </clause>
	    </multiclause>
	    <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                      <expression><a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> </expression>
	      </clause>
	    </multiclause>
	  </prejudge>
	  <postjudge>
	    <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
	            <expression> attribute { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
	            <a href="#jd_has_type" class="judgment"><term>:</term></a> attribute of type <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>
	      </expression>
	    </clause>
	  </postjudge>
	</infer>
     </infergr>

     </smtype>

     <smeval>
       <p>The following rules take a computed attribute constructor
       expression and construct an attribute node.  The rules are
       similar to those rules for element constructors.  First, the
       attribute's name is expanded into a qualified name.  Second,
       the function <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a> is applied to the
       content expression and this function call is evaluated in the
       dynamic environment.  Recall from <specref ref="sec_attribute_constructor"/> that during normalization, we
       do not convert the content of direct attribute constructors
       that contain one attribute-content unit.  This guarantees that
       useful type information is preserved for static analysis.
       Since the conversion function <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>
       was not applied to all attribute constructors during
       normalization, we have to apply it at evaluation time.  (As
       before, it is possible to elide the application of
       <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a> injected during normalization
       and the application injected during evaluation.)</p>

       <infergr>
	 <infer>
	   <prejudge>
	     <multiclause>
          <clause>
          <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
          <expression><emph>QName</emph> <a href="#jd_attr_qname_expands_to" class="judgment"><term>of attr expands to</term></a> <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a></expression>
</clause>
	     </multiclause>
	     <multiclause>
	       <clause>
		 <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		 <expression><a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>(<emph>Expr</emph>) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> </expression>
	       </clause>
	     </multiclause>
	   </prejudge>
	   <postjudge>
	     <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
	       <expression>
		     attribute <emph>QName</emph> { <emph>Expr</emph> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
		     attribute <a href="#id-expanded-qnames"><emph>expanded-QName</emph></a> of type <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> { <a href="#doc-fs-Value"><emph>Value</emph></a> }
	       </expression>
	     </clause>
	   </postjudge>
	 </infer>
       </infergr>

       <infergr>
	 <infer>
	   <prejudge>
	     <multiclause>
	       <clause>
		 <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		 <expression> <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
		 </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
                 <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		 <expression><a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <code>xs:QName</code>
		 </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
                 <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		 <expression><emph>QName</emph><subscript>1</subscript> = <code>xs:QName</code>(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>)
		 </expression>
	       </clause>
	     </multiclause>
	     <multiclause>
	       <clause>
		 <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		 <expression> <a href="#sec_item_seq_to_untypedAtomic"><emph>fs:</emph><code>item-sequence-to-untypedAtomic</code></a>(<emph>Expr</emph><subscript>2</subscript>) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript></expression>
	       </clause>
	     </multiclause>
	   </prejudge>
	   <postjudge>
	     <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
	       <expression>
		 attribute { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a>
		 attribute <emph>QName</emph><subscript>1</subscript> of type <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> { <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> }
	       </expression>
	     </clause>
	   </postjudge>
	 </infer>
       </infergr>
    </smeval>

    </div4>

  <div4>
    <head>Document Node Constructors</head>
 
    <scrap headstyle="show">
      <head/>
      <prod num="110 (XQuery)" id="doc-xquery-CompDocConstructor"><lhs>CompDocConstructor</lhs><rhs>"document"    "{"  <nt def="doc-xquery-Expr">Expr</nt>  "}"</rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar production for a computed document
      constructor is:</p>

      <scrap headstyle="show">
        <head>Core computed document construtor</head>
        <prod num="74 (Core)" id="doc-core-CompDocConstructor"><lhs>CompDocConstructor</lhs><rhs>"document"    "{"  <nt def="doc-core-Expr">Expr</nt>  "}"</rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>A document node constructor contains an expression, which
      must evaluate to a sequence of element, text, comment, or
      processing-instruction nodes.  <xspecref spec="XQ" ref="id-documentConstructors"/> specifies the rules for
      converting a sequence of atomic values and nodes into a sequence
      of nodes before document construction.  The built-in function
      <specref ref="sec_items_to_nodes"/> implements this
      conversion.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>document { <emph>Expr</emph> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
          document { <a href="#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
        </expression>
       </core>
      </mapping>
    </smnorm>

    <smtype>
      <p>The static semantics checks that the type of the argument
      expression is a sequence of element, text,
      processing-instruction, and comment nodes. The type of the
      entire expression is the most general <code>document</code>
      type, because the document constructor erases all <xtermref spec="XQ" ref="dt-type-annotation"/> on its content nodes.</p>

	<infergr>
	  <infer>
	    <prejudge>
             <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		  <emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a>
		</expression>
	      </clause>
             </multiclause>
             <multiclause>
              <clause>
		<environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                <expression>
		  <a href="#doc-fs-Type"><emph>Type</emph></a> <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> (element | text | processing-instruction | comment)* 
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		  document { <emph>Expr</emph> } <a href="#jd_has_type" class="judgment"><term>:</term></a> document { <a href="#doc-fs-Type"><emph>Type</emph></a> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
      </smtype>

    <smeval>
      <p>The dynamic semantics checks that the argument expression
      evaluates to a value that is a sequence of element, text,
      processing-instruction, or comment nodes. The entire expression
      evaluates to a new document node value. If the construction mode
      is set to <code>strip</code>, the <xtermref spec="XQ" ref="dt-type-annotation"/> for all the nodes in content of a
      document node are erased.</p>

	<infergr>
	  <infer>
	    <prejudge>
 	    <multiclause>
 	      <clause>
 	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = preserve</expression>
 	      </clause>
 	    </multiclause>
             <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> 
		</expression>
	      </clause>
             </multiclause>
             <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_matches" class="judgment"><term>matches</term></a> (element | text | processing-instruction | comment)* 
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      document { <emph>Expr</emph> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> document { <a href="#doc-fs-Value"><emph>Value</emph></a> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>

	<infergr>
	  <infer>
	    <prejudge>
	    <multiclause>
	      <clause>
	      <expression><a href="#xq_stat_env_def" class="env">statEnv</a>.<a href="#xq_construction_mode_env" class="env">constructionMode</a> = strip</expression>
	      </clause>
	    </multiclause>
             <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
		</expression>
	      </clause>
             </multiclause>
             <multiclause>
	      <clause>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_erases_to" class="judgment"><term>erases to</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> 
		</expression>
	      </clause>
             </multiclause>
             <multiclause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> (element | text | processing-instruction | comment)* 
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      document { <emph>Expr</emph> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> document { <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
    </smeval>

  </div4>

  <div4>
    <head>Text Node Constructors</head>

    <scrap headstyle="show">
      <head/>
      <prod num="114 (XQuery)" id="doc-xquery-CompTextConstructor"><lhs>CompTextConstructor</lhs><rhs>"text"    "{"  <nt def="doc-xquery-Expr">Expr</nt>  "}"</rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar production for a computed text constructor
      is:</p>

      <scrap headstyle="show">
       <head/>
       <prod num="78 (Core)" id="doc-core-CompTextConstructor"><lhs>CompTextConstructor</lhs><rhs>"text"    "{"  <nt def="doc-core-Expr">Expr</nt>  "}"</rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>A text node constructor contains an expression, which must
      evaluate to an <code>xs:string</code> value. <xspecref spec="XQ" ref="id-textConstructors"/> specifies the rules for converting a
      sequence of atomic values into a string prior to construction of
      a text node.  Each node is replaced by its string value. For
      each adjacent sequence of one or more atomic values returned by
      an enclosed expression, a untyped atomic value is constructed,
      containing the canonical lexical representation of all the
      atomic values, with a single blank character inserted between
      adjacent values.  As formal specification of these conversion
      rules is not instructive, <specref ref="sec_item_seq_to_untypedAtomic"/> implements this
      conversion.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>text { <emph>Expr</emph> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
         text {
         (<a href="#sec_item_seq_to_untypedAtomic_text"><emph>fs:</emph><code>item-sequence-to-untypedAtomic-text</code></a>(<code>fn:data</code>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))))
         cast as <code>xs:string</code>? }
        </expression>
       </core>
      </mapping>
    </smnorm>

    <smtype>
      <p>The static semantics checks that the argument expression has
      type <code>xs:string</code> or <code>empty</code>. The type of the entire
      expression is an optional text node type, as the text node
      constructor returns the empty sequence if its argument is the
      empty sequence.</p>

	<infergr>
	  <infer>
	    <prejudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:string</code>?
		</expression>
	      </clause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      text { <emph>Expr</emph> } <a href="#jd_has_type" class="judgment"><term>:</term></a> text?
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
    </smtype>

    <smeval>
      <p>If the argument expression returns the empty sequence, the
      text node constructor returns the empty sequence.</p>

	<infergr>
	  <infer>
	    <prejudge>
             <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
                  <emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <code>()</code>
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		  text { <emph>Expr</emph> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <code>()</code>
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>

      <p>If the argument expression returns a value of type
      <code>xs:string</code>, the text node constructor returns a text node with
      that string as content.</p>

	<infergr>
	  <infer>
	    <prejudge>
             <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
                  <emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> 
		</expression>
	      </clause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		  <a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_matches" class="judgment"><term>matches</term></a> <code>xs:string</code> 
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		  text { <emph>Expr</emph> } <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> text { <a href="#doc-fs-Value"><emph>Value</emph></a> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
    </smeval>

  </div4>

  <div4>
    <head>Computed Processing Instruction Constructors</head>

    <scrap headstyle="show">
      <head/>
      <prod num="116 (XQuery)" id="doc-xquery-CompPIConstructor"><lhs>CompPIConstructor</lhs><rhs>(("processing-instruction"  <nt def="prod-xquery-NCName">NCName</nt>    "{")  |  ("processing-instruction"    "{"  <nt def="doc-xquery-Expr">Expr</nt>  "}"  "{"))  <nt def="doc-xquery-Expr">Expr</nt>?  "}"</rhs></prod>
    </scrap>

    <smcore>
      <p>The Core grammar production for computed
      processing-instruction constructors is:</p>

      <scrap headstyle="show"><head/>
        <prod num="80 (Core)" id="doc-core-CompPIConstructor"><lhs>CompPIConstructor</lhs><rhs>(("processing-instruction"  <nt def="prod-core-NCName">NCName</nt>    "{")  |  ("processing-instruction"    "{"  <nt def="doc-core-Expr">Expr</nt>  "}"  "{"))  <nt def="doc-core-Expr">Expr</nt>?  "}"</rhs></prod>
      </scrap>
    </smcore>

    <smnorm>
      <p>Computed processing-instruction constructors are normalized
      by mapping their name and content expression in the same way
      that computed element and attribute constructors are
      normalized.</p>

      <mapping>
       <xquery>
 	<expression>
         <map>processing-instruction  <emph>NCName</emph> { }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
 	<expression>
         <map>processing-instruction  <emph>NCName</emph> { () }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </core>
      </mapping>

      <mapping>
       <xquery>
 	<expression>
         <map>processing-instruction  <emph>NCName</emph> { <emph>Expr</emph> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>processing-instruction <emph>NCName</emph> { <a href="#sec_item_seq_to_untypedAtomic_PI"><emph>fs:</emph><code>item-sequence-to-untypedAtomic-PI</code></a>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) } 
        </expression>
       </core>
      </mapping>

      <mapping>
       <xquery>
 	<expression>
         <map>processing-instruction  { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
 	<expression>
         processing-instruction  { <code>fn:data</code>((<map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) } { <a href="#sec_item_seq_to_untypedAtomic_PI"><emph>fs:</emph><code>item-sequence-to-untypedAtomic-PI</code></a>((<map><emph>Expr</emph><subscript>2</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)) }
        </expression>
       </core>
      </mapping>
    </smnorm>

    <smtype>
      <p>The static typing rules for processing-instruction
      constructors are straightforward.</p>
	<infergr>
	  <infer>
	    <prejudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> 
		</expression>
	      </clause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      processing-instruction  <emph>NCName</emph> { <emph>Expr</emph> }
		      <a href="#jd_has_type" class="judgment"><term>:</term></a> processing-instruction
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
	<infergr>
	  <infer>
	    <prejudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
	      <emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> (<code>xs:NCName</code> | <code>xs:string</code> | <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>)
		</expression>
	      </clause>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
	      <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> 
		</expression>
	      </clause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      processing-instruction  { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
		      <a href="#jd_has_type" class="judgment"><term>:</term></a> processing-instruction
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
    </smtype>

    <smeval>
      <p>The dynamic evaluation rules for computed processing
      instructions are straightforward.</p>

	<infergr>
	  <infer>
	    <prejudge>
             <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
	      <emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a>
		</expression>
	      </clause>
       	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
	      <a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
	      processing-instruction  <emph>NCName</emph> { <emph>Expr</emph> }
	      <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> processing-instruction <emph>NCName</emph> { <a href="#doc-fs-Value"><emph>Value</emph></a> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>

	<infergr>
	  <infer>
	    <prejudge>
             <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
       	      <clause>
                <environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <code>xs:NCName</code> 
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
       	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <code>xs:NCName</code>(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <emph>NCName</emph><subscript>1</subscript>
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript>
		</expression>
	      </clause>
       	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> 
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      processing-instruction  { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
		      <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> processing-instruction <emph>NCName</emph><subscript>1</subscript> { <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>

	<infergr>
	  <infer>
	    <prejudge>
             <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
       	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <code>xs:string</code> 
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
       	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <code>xs:NCName</code>(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>); <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <emph>NCName</emph><subscript>1</subscript>
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript>
		</expression>
	      </clause>
       	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> 
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      processing-instruction  { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
		      <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> processing-instruction <emph>NCName</emph><subscript>1</subscript> { <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>

	<infergr>
	  <infer>
	    <prejudge>
             <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
       	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a>
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
       	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <code>xs:NCName</code>(<a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript>) <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <emph>NCName</emph><subscript>1</subscript>
		</expression>
	      </clause>
              </multiclause>
              <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript>
		</expression>
	      </clause>
       	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> <a href="#jd_matches" class="judgment"><term>matches</term></a> <a href="#dt-xdt_untypedAtomic"><code>xdt:untypedAtomic</code></a> 
		</expression>
	      </clause>
             </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      processing-instruction  { <emph>Expr</emph><subscript>1</subscript> } { <emph>Expr</emph><subscript>2</subscript> }
		      <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> processing-instruction <emph>NCName</emph><subscript>1</subscript> { <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>2</subscript> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
    </smeval>

  </div4>
  <div4>
    <head>Computed Comment Constructors</head>

<scrap headstyle="show">
  <head/>
  <prod num="115 (XQuery)" id="doc-xquery-CompCommentConstructor"><lhs>CompCommentConstructor</lhs><rhs>"comment"    "{"  <nt def="doc-xquery-Expr">Expr</nt>  "}"</rhs></prod>
</scrap>

    <smcore>
      <p>The Core grammar production for computed comment constructors
      is:</p>

<scrap headstyle="show"><head/>
<prod num="79 (Core)" id="doc-core-CompCommentConstructor"><lhs>CompCommentConstructor</lhs><rhs>"comment"    "{"  <nt def="doc-core-Expr">Expr</nt>  "}"</rhs></prod>
</scrap>
    </smcore>

    <smnorm>
      <p>Computed comment constructors are normalized by mapping their
      content expression.
      </p>

      <mapping>
       <xquery>
 	<expression>
         <map>comment { <emph>Expr</emph> }</map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>comment { (<a href="#sec_item_seq_to_untypedAtomic_comment"><emph>fs:</emph><code>item-sequence-to-untypedAtomic-comment</code></a>((<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>))) cast as <code>xs:string</code> } 
        </expression>
       </core>
      </mapping>
      </smnorm>

      <smtype>
        <p> The static typing rule for computed comment constructors
        is straightforward.</p>

	<infergr>
	  <infer>
	    <prejudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <emph>Expr</emph> <a href="#jd_has_type" class="judgment"><term>:</term></a> <code>xs:string</code> 
		</expression>
	      </clause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      comment { <emph>Expr</emph> }
		      <a href="#jd_has_type" class="judgment"><term>:</term></a> comment
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
</smtype>

<smeval>
<p>
The dynamic evaluation rule for computed comment constructors is
straightforward. 
</p>
	<infergr>
	  <infer>
	    <prejudge>
            <multiclause>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      <emph>Expr</emph> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a> 
		</expression>
	      </clause>
       	      <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
		<expression>
		      <a href="#doc-fs-Value"><emph>Value</emph></a> <a href="#jd_matches" class="judgment"><term>matches</term></a> <code>xs:string</code> 
		</expression>
	      </clause>
            </multiclause>
	    </prejudge>
	    <postjudge>
	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
		<expression>
		      comment { <emph>Expr</emph> }
		      <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> comment { <a href="#doc-fs-Value"><emph>Value</emph></a> }
		</expression>
	      </clause>
	    </postjudge>
	  </infer>
	</infergr>
</smeval>

  </div4>

  </div3>

    <div3 id="id-ns-nodes-on-elements">

      <head>In-scope Namespaces of a Constructed Element</head>

      <p>The effect of in-scope namespaces on constructed elements is
      specified in <specref ref="id_element_constructor"/> and
      <specref ref="sec_comp_elem_constructor"/>. </p>
    </div3>

</div2>
  <div2>
    <head>[For/FLWOR] Expressions</head> 


    <smintro>
      <p>[XPath/XQuery] provides [For/FLWOR] expressions for iteration, for
      binding variables to intermediate results, and filtering bound
      variables according to a predicate.</p>

      <p>A <emph>FLWORExpr</emph> in XQuery 1.0 consists of a sequence of
      <emph>ForClause</emph>s and <emph>LetClause</emph>s, followed by an optional
      <emph>WhereClause</emph>, followed by the , as described by the
      following grammar productions. Each variable binding is preceded
      by an optional type declaration which specify the type expected
      for the variable.</p>

      <p>The dynamic semantics of the ordering mode in FLWOR
      expressions is not specified formally. The dynamic semantics is
      not specified formally as it would require the introduction of
      tuples, which are not supported in the [XPath/XQuery] data
      model.</p>

      <scrap headstyle="show">
    	<head>[For/FLWOR] Expressions</head>
    	<prod num="33 (XQuery)" id="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>
    	<prod num="34 (XQuery)" id="doc-xquery-ForClause"><lhs>ForClause</lhs><rhs>"for"    "$"  <nt def="doc-xquery-VarName">VarName</nt>  <nt def="doc-xquery-TypeDeclaration">TypeDeclaration</nt>?  <nt def="doc-xquery-PositionalVar">PositionalVar</nt>?  "in"  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>  (","  "$"  <nt def="doc-xquery-VarName">VarName</nt>  <nt def="doc-xquery-TypeDeclaration">TypeDeclaration</nt>?  <nt def="doc-xquery-PositionalVar">PositionalVar</nt>?  "in"  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>)*</rhs></prod>
    	<prod num="36 (XQuery)" id="doc-xquery-LetClause"><lhs>LetClause</lhs><rhs>"let"    "$"  <nt def="doc-xquery-VarName">VarName</nt>  <nt def="doc-xquery-TypeDeclaration">TypeDeclaration</nt>?  ":="  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>  (","  "$"  <nt def="doc-xquery-VarName">VarName</nt>  <nt def="doc-xquery-TypeDeclaration">TypeDeclaration</nt>?  ":="  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>)*</rhs></prod>
        <prod num="118 (XQuery)" id="doc-xquery-TypeDeclaration"><lhs>TypeDeclaration</lhs><rhs>"as"  <nt def="doc-xquery-SequenceType">SequenceType</nt></rhs></prod>
        <prod num="35 (XQuery)" id="doc-xquery-PositionalVar"><lhs>PositionalVar</lhs><rhs>"at"  "$"  <nt def="doc-xquery-VarName">VarName</nt></rhs></prod>
    	<prod num="37 (XQuery)" id="doc-xquery-WhereClause"><lhs>WhereClause</lhs><rhs>"where"  <nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod>
        <prod num="38 (XQuery)" id="doc-xquery-OrderByClause"><lhs>OrderByClause</lhs><rhs>("order"  "by"  |  "stable"  "order"  "by")  <nt def="doc-xquery-OrderSpecList">OrderSpecList</nt></rhs></prod>
        <prod num="39 (XQuery)" id="doc-xquery-OrderSpecList"><lhs>OrderSpecList</lhs><rhs><nt def="doc-xquery-OrderSpec">OrderSpec</nt>  (","  <nt def="doc-xquery-OrderSpec">OrderSpec</nt>)*</rhs></prod>
        <prod num="40 (XQuery)" id="doc-xquery-OrderSpec"><lhs>OrderSpec</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt>  <nt def="doc-xquery-OrderModifier">OrderModifier</nt></rhs></prod>
        <prod num="41 (XQuery)" id="doc-xquery-OrderModifier"><lhs>OrderModifier</lhs><rhs>("ascending"  |  "descending")?  (("empty"  "greatest")  |  ("empty"  "least"))?  ("collation"  <nt def="prod-xquery-URILiteral">URILiteral</nt>)?</rhs></prod>
    	<prod num="4 (XPath)" id="doc-xpath-ForExpr"><lhs>ForExpr</lhs><rhs><nt def="doc-xpath-SimpleForClause">SimpleForClause</nt>  "return"  <nt def="doc-xpath-ExprSingle">ExprSingle</nt></rhs></prod>
        <prod num="5 (XPath)" id="doc-xpath-SimpleForClause"><lhs>SimpleForClause</lhs><rhs>"for"    "$"  <nt def="doc-xpath-VarName">VarName</nt>  "in"  <nt def="doc-xpath-ExprSingle">ExprSingle</nt>  (","  "$"  <nt def="doc-xpath-VarName">VarName</nt>  "in"  <nt def="doc-xpath-ExprSingle">ExprSingle</nt>)*</rhs></prod>
      </scrap>
    </smintro>

      <smcore>
        <p>The Core grammar productions for FLWOR expressions are:</p>
      	<scrap headstyle="show">
     	  <head>For Expressions</head>
      	  <prod num="32 (Core)" id="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>
      	  <prod num="33 (Core)" id="doc-core-ForClause"><lhs>ForClause</lhs><rhs>"for"    "$"  <nt def="doc-core-VarName">VarName</nt>  <nt def="doc-core-TypeDeclaration">TypeDeclaration</nt>?  <nt def="doc-core-PositionalVar">PositionalVar</nt>?  "in"  <nt def="doc-core-ExprSingle">ExprSingle</nt></rhs></prod>
      	  <prod num="35 (Core)" id="doc-core-LetClause"><lhs>LetClause</lhs><rhs>"let"    "$"  <nt def="doc-core-VarName">VarName</nt>  <nt def="doc-core-TypeDeclaration">TypeDeclaration</nt>?  ":="  <nt def="doc-core-ExprSingle">ExprSingle</nt></rhs></prod>
      	  <prod num="34 (Core)" id="doc-core-PositionalVar"><lhs>PositionalVar</lhs><rhs>"at"  "$"  <nt def="doc-core-VarName">VarName</nt></rhs></prod>
          <prod num="82 (Core)" id="doc-core-TypeDeclaration"><lhs>TypeDeclaration</lhs><rhs>"as"  <nt def="doc-core-SequenceType">SequenceType</nt></rhs></prod>
          <prod num="36 (Core)" id="doc-core-OrderByClause"><lhs>OrderByClause</lhs><rhs>("order"  "by"  |  "stable"  "order"  "by")  <nt def="doc-core-OrderSpecList">OrderSpecList</nt></rhs></prod>
          <prod num="37 (Core)" id="doc-core-OrderSpecList"><lhs>OrderSpecList</lhs><rhs><nt def="doc-core-OrderSpec">OrderSpec</nt>  (","  <nt def="doc-core-OrderSpec">OrderSpec</nt>)*</rhs></prod>
          <prod num="38 (Core)" id="doc-core-OrderSpec"><lhs>OrderSpec</lhs><rhs><nt def="doc-core-ExprSingle">ExprSingle</nt>  <nt def="doc-core-OrderModifier">OrderModifier</nt></rhs></prod>
          <prod num="39 (Core)" id="doc-core-OrderModifier"><lhs>OrderModifier</lhs><rhs>("ascending"  |  "descending")?  (("empty"  "greatest")  |  ("empty"  "least"))?  ("collation"  <nt def="prod-core-URILiteral">URILiteral</nt>)?</rhs></prod>
      	</scrap>
      </smcore>

    <div3>
      <head>FLWOR expressions</head>

      <smnotation>
    	<p id="jd_map_flwor">Individual [For/FLWOR] clauses are
    	normalized by means of the auxiliary normalization rules:</p>

       	<display>
 	  <clause>
 	    <expression><map><emph>FLWORClause</emph></map><a href="#jd_map_flwor" class="judgment"><subscript>FLWOR</subscript></a>(<emph>Expr</emph>)</expression>
 	  </clause>
       	</display>

        <p>Where <emph>FLWORClause</emph> can be any either a <emph>ForClause</emph>,
        a <emph>LetClause</emph>, a <emph>WhereClause</emph>, or an
        <emph>OrderByClause</emph>.  The <emph>OrderByClause</emph> is discussed in
        <specref ref="id_orderby_clause"/>.</p>

        <p>Normalized FLWOR expressions restrict a For and Let clause
        to bind only one variable.  Otherwise, the Core FLWOR
        expression is the same as the XQuery FLWOR expression.</p>
      </smnotation> 

      <smnotation>
        <p>The auxiliary rule <map/><a href="#jd_map_flwor" class="judgment"><subscript>FLWOR</subscript></a>(<emph>Expr</emph>)
        normalizes a For, Let, or Where clause in a <emph>FLWORExpr</emph>
        expression.  Note that the rule takes the remainder of the
        FLWOR expression (other For, Let, or Where clauses and the
        Return clause) as a parameter in <emph>Expr</emph>.</p>
      </smnotation>
  
      <smnorm>
        <p>The [For/FLWOR] expressions include the <emph>FLWORExpr</emph> of
    	XQuery and the <emph>ForExpr</emph> of XPath.  The normalization rule
    	for <emph>ForExpr</emph> is simple: It simply unrolls a <emph>ForExpr</emph>
    	that binds multiple variables into nested <emph>ForExpr</emph>s, each
    	of which bind one variable.</p>

    	<mapping>
    	 <xquery>
    	  <expression>
    	   <map>for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>0</subscript> in <emph>Expr</emph><subscript>0</subscript>, ...,  <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>n</subscript>
    	    in <emph>Expr</emph><subscript>n</subscript> return <emph>Expr</emph> 
    	    </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
    	  </expression>
    	 </xquery>
    	 <core>
          <expression>
          <table><tbody><tr><td rowspan="1" colspan="1">
    	  for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>0</subscript> in  <map><emph>Expr</emph><subscript>0</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> return</td></tr>
          <tr><td rowspan="1" colspan="1">...</td></tr> 
<tr><td rowspan="1" colspan="1">
    	  for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>n</subscript> in  <map><emph>Expr</emph><subscript>n</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
    	  return</td></tr>
           <tr><td rowspan="1" colspan="1"><map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a></td></tr>
          </tbody></table>
          </expression>
    	 </core>
    	</mapping>

    	<p>Full <emph>FLWORExpr</emph> expressions are normalized to nested
    	Core expressions using two sets of normalization rules. Note
    	that some of the rules also accept ungrammatical
    	<emph>FLWORExpr</emph>s such as "where <emph>Expr</emph><subscript>1</subscript> return
    	<emph>Expr</emph><subscript>2</subscript>". This does not matter, as normalization is always
    	applied on parsed [XPath/XQuery] expressions, and ungrammatical
    	<emph>FLWORExpr</emph>s would be rejected by the parser
    	beforehand.</p>
   
        <p>The first set of rules is applied on a full [For/FLWOR]
        expression, splitting it at the clause level, then applying
        further normalization on each separate clause.</p>

    	<mapping>
    	 <xquery>
    	  <expression>
    	   <map>
    	    (<emph>ForClause</emph> | <emph>LetClause</emph> | <emph>WhereClause</emph> | <emph>OrderByClause</emph>) <emph>FLWORExpr</emph> 
    	   </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
    	  </expression>
    	 </xquery>
    	 <core>
          <expression>
    	  <map>(<emph>ForClause</emph> | <emph>LetClause</emph> |
    	  <emph>WhereClause</emph> | <emph>OrderByClause</emph>)</map><a href="#jd_map_flwor" class="judgment"><subscript>FLWOR</subscript></a>(<map><emph>FLWORExpr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)
          </expression>
    	 </core>
    	</mapping>
  
    	<mapping>
    	 <xquery>
    	  <expression>
    	   <map>
    	    (<emph>ForClause</emph> | <emph>LetClause</emph> | <emph>WhereClause</emph> | <emph>OrderByClause</emph>) return <emph>Expr</emph> 
    	   </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
    	  </expression>
    	 </xquery>
    	 <core>
    	  <expression><map>(<emph>ForClause</emph> | <emph>LetClause</emph> |
    	  <emph>WhereClause</emph> | <emph>OrderByClause</emph>)</map><a href="#jd_map_flwor" class="judgment"><subscript>FLWOR</subscript></a>(<map><emph>Expr</emph></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)</expression>
    	 </core>
    	</mapping>
  
    	<p>Then each [For/FLWOR] clause is normalized separately. A
    	<emph>ForClause</emph> may bind more than one variable, whereas a For
    	expression in the [XPath/XQuery] Core binds and iterates over only
    	one variable. Therefore, a <emph>ForClause</emph> is normalized to
    	nested for expressions:</p>

	<mapping>
	 <xquery>
	  <expression>
	   <map>
<table>
<tbody>
<tr>
<td rowspan="1" colspan="1">
	    for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> <emph>TypeDeclaration</emph><subscript>1</subscript>?
	    <emph>PositionalVar</emph><subscript>1</subscript>? in <emph>Expr</emph><subscript>1</subscript>, </td></tr>
<tr><td rowspan="1" colspan="1"> ···,</td></tr>
<tr><td rowspan="1" colspan="1"><a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>n</subscript> <emph>TypeDeclaration</emph><subscript>n</subscript>? <emph>PositionalVar</emph><subscript>n</subscript>? in
	    <emph>Expr</emph><subscript>n</subscript></td></tr>
</tbody>
</table>
	   </map>
           <a href="#jd_map_flwor" class="judgment"><subscript>FLWOR</subscript></a>(<emph>Expr</emph>)
          </expression>
         </xquery>
	 <core>
	  <expression>
	   <table>
	    <tbody>
	     <tr>
              <td rowspan="1" colspan="1"> for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> <emph>TypeDeclaration</emph><subscript>1</subscript>? <emph>PositionalVar</emph><subscript>1</subscript>? in <map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> return</td>
             </tr>
             <tr>
              <td rowspan="1" colspan="1">  ···</td>
             </tr>
	     <tr>
              <td rowspan="1" colspan="1">     for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>n</subscript> <emph>TypeDeclaration</emph><subscript>n</subscript>? <emph>PositionalVar</emph><subscript>n</subscript>? 
              in <map> <emph>Expr</emph><subscript>n</subscript> </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> return <emph>Expr</emph></td>
             </tr>
            </tbody>
	   </table>
	  </expression>
	 </core>
	</mapping>

<!--        <p>Each individual For clause, is then normalized to always
        have a type declaration.</p> -->

        <p>Note that the additional <emph>Expr</emph> parameter of the
        auxiliary normalization rule is used as the final return
        expression.</p>

        <p>Likewise, a <emph>LetClause</emph> clause is normalized to nested
        let expressions, each of which binds one variable:</p>

	<mapping>
	 <xquery>
	  <expression>
	   <map>
<table>
<tbody>
<tr><td rowspan="1" colspan="1">let <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> <emph>TypeDeclaration</emph><subscript>1</subscript>? := <emph>Expr</emph><subscript>1</subscript>,</td></tr>
<tr><td rowspan="1" colspan="1">···,</td></tr>
<tr><td rowspan="1" colspan="1"><a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>n</subscript> <emph>TypeDeclaration</emph><subscript>n</subscript>? := <emph>Expr</emph><subscript>n</subscript></td></tr>
</tbody>
</table></map><a href="#jd_map_flwor" class="judgment"><subscript>FLWOR</subscript></a>(<emph>Expr</emph>)
  	  </expression>
	 </xquery>
	 <core>
	  <expression>
	   <table>
	    <tbody>
	     <tr>
              <td rowspan="1" colspan="1">let <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> <emph>TypeDeclaration</emph><subscript>1</subscript>? := <map><emph>Expr</emph><subscript>1</subscript> </map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> return</td>
             </tr>
  	     <tr>
              <td rowspan="1" colspan="1">  ···</td>
             </tr>
	     <tr>
              <td rowspan="1" colspan="1">    let <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>n</subscript> <emph>TypeDeclaration</emph><subscript>n</subscript>? := <map><emph>Expr</emph><subscript>n</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> return <emph>Expr</emph></td>
             </tr>
            </tbody>
           </table>
	  </expression>
	 </core>
	</mapping>

        <p>A <emph>WhereClause</emph> is normalized to an <emph>IfExpr</emph>, with
        the else-branch returning the empty sequence:</p>

	<mapping>
	 <xquery>
	  <expression>
	   <map> where <emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_flwor" class="judgment"><subscript>FLWOR</subscript></a>(<emph>Expr</emph>)
	  </expression>
	 </xquery>
	 <core>
	  <expression>
	   if ( <map><emph>Expr</emph><subscript>1</subscript></map><a href="#jd_map_expr" class="judgment"><subscript>Expr</subscript></a> ) then <emph>Expr</emph> else ()
	  </expression>
	 </core>
	</mapping>

      </smnorm>

      <smexample>

        <p>The following simple example illustrates, how a
        <emph>FLWORExpr</emph> is normalized. The <code>for</code> expression
        in the example below is used to iterate over two collections,
        binding variables <code>$i</code> and <code>$j</code> to items
        in these collections. It uses a <code>let</code> clause to
        binds the local variable <code>$k</code> to the sum of both
        numbers, and a <code>where</code> clause to select only those
        numbers that have a sum equal to or greater than the integer
        <code>5</code>.</p>

<eg xml:space="preserve">  for $i as xs:integer in (1, 2),
      $j in (3, 4)
  let $k := $i + $j
  where $k &gt;= 5
  return
    &lt;tuple&gt;
       &lt;i&gt; { $i } &lt;/i&gt;
       &lt;j&gt; { $j } &lt;/j&gt;
    &lt;/tuple&gt;
</eg>

        <p>By the first set of rules, this is normalized to (except
        for the operators and element constructor which are not
        treated here):</p>

<eg xml:space="preserve">  for $i as xs:integer in (1, 2) return
    for $j in (3, 4) return
      let $k := $i + $j return
        if ($k &gt;= 5) then 
          &lt;tuple&gt;
            &lt;i&gt; { $i } &lt;/i&gt;
            &lt;j&gt; { $j } &lt;/j&gt;
          &lt;/tuple&gt;
        else
          ()
</eg>

        <p>For each binding of <code>$i</code> to an item in the
        sequence <code>(1 , 2)</code> the inner <code>for</code>
        expression iterates over the sequence <code>(3 , 4)</code> to
        produce tuples ordered by the ordering of the outer sequence
        and then by the ordering of the inner sequence. This Core
        expression eventually results in the following document
        fragment:</p>

<eg xml:space="preserve">
  (&lt;tuple&gt;
      &lt;i&gt;1&lt;/i&gt;
      &lt;j&gt;4&lt;/j&gt;
   &lt;/tuple&gt;,
   &lt;tuple&gt;
      &lt;i&gt;2&lt;/i&gt;
      &lt;j&gt;3&lt;/j&gt;
   &lt;/tuple&gt;,
   &lt;tuple&gt;
      &lt;i&gt;2&lt;/i&gt;
      &lt;j&gt;4&lt;/j&gt;
   &lt;/tuple&gt;)
</eg>

      </smexample>

    </div3>

    <div3 id="id-for-expression">
      <head>For expression</head>

      <smtype>

        <p>A single <code>for</code> expression is typed as follows:
        First <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> of the iteration expression <emph>Expr</emph><subscript>1</subscript> is
        inferred. Then the prime type of <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>, 
        <a href="#jd_prime" class="judgment">prime</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>),  is computed. This is a union over
        all item types in <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> (See <specref ref="sec_factor"/>). With the variable component of the static
        environment <a href="#xq_stat_env_def" class="env">statEnv</a> extended with <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> as type
        <a href="#jd_prime" class="judgment">prime</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>), the type <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> of
        <emph>Expr</emph><subscript>2</subscript> is inferred. Because the <code>for</code>
        expression iterates over the result of <emph>Expr</emph><subscript>1</subscript>, the final
        type of the iteration is <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> multiplied with the
        possible number of items in <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> (one, <code>?</code>,
        <code>*</code>, or <code>+</code>).  This number is determined
        by the auxiliary type-function
        <a href="#jd_quantifier" class="judgment">quantifier</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>).</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
                </clause>
              </multiclause>
              <multiclause>
                <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="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> =&gt; <a href="#jd_prime" class="judgment">prime</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>)</expression>
                    </update>
                  </environment>
                  <expression> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression>for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> in <emph>Expr</emph><subscript>1</subscript> return
                    <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_quantifier" class="judgment">·</a> <a href="#jd_quantifier" class="judgment">quantifier</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>)
                </expression>
              </clause>
            </postjudge>
          </infer>
        </infergr>

        <p>When a positional variable <emph>Variable</emph><subscript>pos</subscript> is present, the
        static environment is also extended with the positional
        variable typed as an <code>xs:integer</code>.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
                </clause>
              </multiclause>
              <multiclause>
                <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="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> =&gt; <a href="#jd_prime" class="judgment">prime</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>);</expression>
                      <expression><a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>pos</subscript> =&gt; <code>xs:integer</code></expression>
                    </update>
                  </environment>
                  <expression> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression>for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> at <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>pos</subscript> in <emph>Expr</emph><subscript>1</subscript> return
                    <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_quantifier" class="judgment">·</a> <a href="#jd_quantifier" class="judgment">quantifier</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>)
                </expression>
              </clause>
            </postjudge>
          </infer>
        </infergr>

        <p>When a type declaration is present, the static
        semantics also checks that the type of the input expression is
        a subtype of the declared type and extends the static
        environment by typing <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> with type <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>0</subscript>. This semantics is specified by
        the following typing rule.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
                </clause>
              </multiclause>
              <multiclause>
               <clause>
                <expression>
                   <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>0</subscript> = <map> <emph>SequenceType</emph> </map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
                </expression>
               </clause>
              </multiclause>
              <multiclause>
               <clause>
                 <environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                 <expression><a href="#jd_prime" class="judgment">prime</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>) <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>0</subscript> </expression></clause>
              </multiclause>
              <multiclause>
                <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="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> =&gt; <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>0</subscript></expression>
                    </update>
                  </environment>
                  <expression> <emph>Expr</emph><subscript>2</subscript> =&gt; <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>for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> as <emph>SequenceType</emph> in <emph>Expr</emph><subscript>1</subscript> return
                    <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_quantifier" class="judgment">·</a> <a href="#jd_quantifier" class="judgment">quantifier</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>)
                </expression>
              </clause>
            </postjudge>
          </infer>
        </infergr>

<p>The last rule contains a For expression that contains a type declaration and a positional
 variable.
When the positional variable is present, the static environment is
also extended with the positional variable typed as an integer.
</p>
        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                  <expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
                </clause>
              </multiclause>
              <multiclause>
               <clause>
                <expression>
                   <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>0</subscript> = <map> <emph>SequenceType</emph> </map><a href="#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a>
                </expression>
               </clause>
              </multiclause>
              <multiclause>
               <clause><environment> <a href="#xq_stat_env_def" class="env">statEnv</a> </environment>
                <expression><a href="#jd_prime" class="judgment">prime</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>) <a href="#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>0</subscript> </expression></clause>
              </multiclause>
              <multiclause>
                <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="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> =&gt; <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>0</subscript>; </expression>
                      <expression><a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>pos</subscript> =&gt; <code>xs:integer</code></expression>
                    </update>
                  </environment>
                  <expression> <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> </expression>
                </clause>
              </multiclause>
            </prejudge>
            <postjudge>
              <clause><environment><a href="#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression>for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> as <emph>SequenceType</emph> at <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>pos</subscript> in <emph>Expr</emph><subscript>1</subscript> return
                    <emph>Expr</emph><subscript>2</subscript> <a href="#jd_has_type" class="judgment"><term>:</term></a> <a href="#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="#jd_quantifier" class="judgment">·</a> <a href="#jd_quantifier" class="judgment">quantifier</a>(<a href="#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript>)
                </expression>
              </clause>
            </postjudge>
          </infer>
        </infergr>

      </smtype>

      <smexample>

        <p>For example, if <code>$example</code> is bound to the
        sequence <code>10.0, 1.0E1, 10</code> of type
        <code>xs:decimal, xs:float, xs:integer</code>, then the query</p>

<eg xml:space="preserve">  for $s in $example
  return $s * 2</eg>

        <p>is typed as follows:</p>

<eg xml:space="preserve">  (1) prime(xs:decimal, xs:float, xs:integer) =
      xs:decimal | xs:float | xs:integer
  (2) quantifier(xs:decimal, xs:float, xs:integer) = +
  (3) $s : xs:decimal | xs:float | xs:integer
  (4) $s * 2 : 
      xs:decimal | xs:float | xs:integer
  (5) result-type :
      ( xs:decimal | xs:float | xs:integer ) +
</eg>

        <p>This result-type is not the most specific type possible. It
        does not take into account the order of elements in the input
        type, and it ignores the individual and overall number
        of elements in the input type. The most specific type possible
        is: <code>element out {element one {}}, element out {element
        two {}}, element out {element three {}}</code>.  However,
        inferring such a specific type for arbitrary input types and
        arbitrary return clauses requires significantly more
        complex type inference rules. In addition, if put into the
        context of an element, the specific type violates the
        "unique particle attribution" restriction of XML schema, which requires
        that an element must have a unique content model within a
        particular context.</p>

      </smexample>

      <smeval>

        <p>The evaluation of a <code>for</code> expression
        distinguishes two cases: If the iteration expression
        <emph>Expr</emph><subscript>1</subscript> evaluates to the empty sequence, then the entire
        expression evaluates to the empty sequence:</p>

         <infergr>
 	  <infer>
 	    <prejudge>
 	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 		<expression><emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> ()</expression>
 	      </clause>
 	    </prejudge>
 	    <postjudge>
 	      <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
 		<expression>for <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> <emph>TypeDeclaration</emph>? in <emph>Expr</emph><subscript>1</subscript> return <emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> ()
 		</expression>
 	      </clause>
 	    </postjudge>
 	  </infer>
      	</infergr>

        <p>Otherwise, the iteration expression <emph>Expr</emph><subscript>1</subscript>, is
        evaluated to produce the sequence <a href="#doc-fs-Item"><emph>Item</emph></a><subscript>1</subscript>, ...,
        <a href="#doc-fs-Item"><emph>Item</emph></a><subscript>n</subscript>.  For each item <a href="#doc-fs-Item"><emph>Item</emph></a><subscript>i</subscript> in this
        sequence, the body of the <code>for</code> expression
        <emph>Expr</emph><subscript>2</subscript> is evaluated in the environment <a href="#xq_dyn_env_def" class="env">dynEnv</a>
        extended with <a href="#prod-core-VarRef"><emph>VarRef</emph></a><subscript>1</subscript> bound to <a href="#doc-fs-Item"><emph>Item</emph></a><subscript>i</subscript>. This
        produces values <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>i</subscript>, ..., <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>n</subscript> which are
        concatenated to produce the result sequence. </p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="#xq_dyn_env_def" class="env">dynEnv</a></environment>
                  <expression> 
                      <emph>Expr</emph><subscript>1</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Item"><emph>Item</emph></a><subscript>1</subscript> ,..., <a href="#doc-fs-Item"><emph>Item</emph></a><subscript>n</subscript>
                  </expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause>
                  <environment><a href="#xq_stat_env_def" class="env">statEnv</a>
                  </environment>
                  <expression><a href="#prod-core-VarRef"><emph>VarRef</emph></a> <a href="#jd_var_qname_expands_to" class="judgment"><term>of var expands to</term></a>
                  <emph>Variable</emph></expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause>
                  <environment>
                    <update>
                      <environment><a href="#xq_dyn_env_def" class="env">dynEnv</a>.<a href="#xq_val_env" class="env">varValue</a></environment>
                      <expression><emph>Variable</emph> =&gt; <a href="#doc-fs-Item"><emph>Item</emph></a><subscript>1</subscript></expression>
                    </update>
                  </environment>
                  <expression><emph>Expr</emph><subscript>2</subscript> <a href="#jd_yields" class="judgment"><term>=&gt;</term></a> <a href="#doc-fs-Value"><emph>Value</emph></a><subscript>1</subscript></expression>
                </clause>
              </multiclause>
              <multiclause>
                <clause>
                  <expression>···</expression>
                </clause>
              <