<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xmlspec-ie-dm.xsl"?>
<!DOCTYPE spec PUBLIC "-//W3C//DTD Specification V2.2//EN"
                      "http://www.w3.org/2002/xmlspec/dtd/2.2/xmlspec.dtd" [

  <!ENTITY dm-example.xml SYSTEM "dm-example.xml.cdata">
  <!ENTITY dm-example.xsd SYSTEM "dm-example.xsd.cdata">
  <!ENTITY dm-example.tbl SYSTEM "dm-example.tbl.xml">
<!--
  <!ENTITY revisiondesc SYSTEM "data-model.rev.xml">
-->

  <!ENTITY date.year "2002">
  <!ENTITY date.month "November">
  <!ENTITY date.MM "11">
  <!ENTITY date.day "15">
  <!ENTITY date.DD "15">
  <!ENTITY doc.date "&date.year;&date.MM;&date.DD;">
  <!ENTITY doc.prefix "WD-query-datamodel">
  <!ENTITY url.group "http://www.w3.org/XML/Group/">
  <!ENTITY url.group.ql "&url.group;xmlquery/">
  <!ENTITY url.publoc "&url.group;&date.year;/&date.MM;/&doc.prefix;-&doc.date;.html">
  <!ENTITY url.internal "http://www.w3.org/Style/XSL/Group/xpath2-tf/&doc.prefix;-&doc.date;.html">
  <!ENTITY url.external "http://www.w3.org/TR/&date.year;/&doc.prefix;-&doc.date;/">
  <!ENTITY url.this "&url.external;">
  <!ENTITY aacute "&#225;">

  <!ENTITY % local.proto.att "
	returnEmptyOk	(yes|no)	'no'
	returnSeq	(yes|no)	'no'
	class		(op|func|dm|schema|special)	'dm'
">

  <!ENTITY % local.arg.att "
	name	CDATA		#IMPLIED
	emptyOk	(yes|no)	'no'
	seq	(yes|no)	'no'
">

  <!ENTITY % argtypes "CDATA">

  <!ATTLIST issue
            date     CDATA             #IMPLIED
            raisedby CDATA             #IMPLIED>
  <!ATTLIST resolution
            date     CDATA             #IMPLIED>
]>
<spec w3c-doctype="wd">

<header>
  <title>XQuery 1.0 and XPath 2.0 Data Model</title>
  <version/>
  <w3c-designation>&doc.prefix;-&doc.date;</w3c-designation>
  <w3c-doctype>W3C Working Draft</w3c-doctype>
  <pubdate>
    <day>&date.day;</day>
    <month>&date.month;</month>
    <year>&date.year;</year>
  </pubdate>
  <publoc>
     <loc href="&url.this;">&url.this;</loc>
  </publoc>
  <altlocs>
    <loc href="&url.this;data-model.xml">XML</loc>
  </altlocs>
  <latestloc>
    <loc href="http://www.w3.org/TR/query-datamodel/">http://www.w3.org/TR/query-datamodel/</loc>
  </latestloc>
  <prevlocs role="private">
    <loc href="http://www.w3.org/TR/2002/WD-query-datamodel-20020816/">http://www.w3.org/TR/2002/WD-query-datamodel-20020816/</loc>
    <loc href="http://www.w3.org/TR/2002/WD-query-datamodel-20020430/">http://www.w3.org/TR/2002/WD-query-datamodel-20020430/</loc>
    <loc href="http://www.w3.org/TR/2001/WD-query-datamodel-20011220/">http://www.w3.org/TR/2001/WD-query-datamodel-20011220/</loc>
    <loc href="http://www.w3.org/TR/2001/WD-query-datamodel-20010607/">http://www.w3.org/TR/2001/WD-query-datamodel-20010607/</loc>
    <loc href="http://www.w3.org/TR/2001/WD-query-datamodel-20010215/">http://www.w3.org/TR/2001/WD-query-datamodel-20010215/</loc>
  </prevlocs>
  <authlist>
    <author>
      <name>Mary Fern&aacute;ndez (XML Query WG)</name>
      <affiliation>AT&amp;T Labs</affiliation>
      <email href="mailto:mff@research.att.com">mff@research.att.com</email>
    </author>
    <author>
      <name>Ashok Malhotra (XML Query and XSL WGs)</name>
      <affiliation>Microsoft</affiliation>
      <email href="mailto:ashokma@microsoft.com">ashokma@microsoft.com</email>
    </author>
    <author>
      <name>Jonathan Marsh (XSL WG)</name>
      <affiliation>Microsoft</affiliation>
      <email href="mailto:jmarsh@microsoft.com">jmarsh@microsoft.com</email>
    </author>
    <author>
      <name>Marton Nagy (XML Query WG)</name>
      <affiliation>Science Applications International Corporation (SAIC)</affiliation>
      <email href="mailto:marton.nagy@saic.com">marton.nagy@saic.com</email>
    </author>
    <author>
      <name>Norman Walsh (XSL WG)</name>
      <affiliation>Sun Microsystems</affiliation>
      <email href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</email>
    </author>
  </authlist>

  <status>
    <p>This section describes the status of this document at the time of its
    publication. Other documents may supersede this document. The latest status
    of this document series is maintained at the W3C.</p>

    <p>This is a Public Working Draft for review by W3C Members and
    other interested parties. It is a draft document and may be updated,
    replaced or made obsolete by other documents at any time. It is
    inappropriate to use W3C Working Drafts as reference material or to
    cite them as other than "work in progress". This is work in progress
    and does not imply endorsement by the W3C membership.</p>

    <p>The XQuery 1.0 and XPath 2.0 Data Model has been defined jointly by
	 the <loc href="http://www.w3.org/XML/Query">XML Query Working Group</loc>
	 (part of the <loc href="http://www.w3.org/XML/Activity.html">XML Activity</loc>) and
	 the <loc href="http://www.w3.org/Style/XSL/">XSL Working Group</loc>
	 (part of the <loc href="http://www.w3.org/Style/">Style Activity</loc>).
	 </p>

    <p>Comments on this document should be sent to the W3C mailing list <loc href="mailto:public-qt-comments@w3.org">public-qt-comments@w3.org</loc>.
    (archived at <loc href="http://lists.w3.org/Archives/Public/public-qt-comments/">http://lists.w3.org/Archives/Public/public-qt-comments/</loc>).
    </p>

    <p>A list of current W3C Recommendations and other technical documents
    can be found at <loc href="http://www.w3.org/TR/">http://www.w3.org/TR/</loc>.</p>

    <p>
    Patent disclosures relevant to this specification may be found on the
    XML Query Working Group's patent disclosure page at
    <loc href="http://www.w3.org/2002/08/xmlquery-IPR-statements">http://www.w3.org/2002/08/xmlquery-IPR-statements</loc>
    and the XSL Working Group's patent disclosure page at
    <loc href="http://www.w3.org/Style/XSL/Disclosures.html">http://www.w3.org/Style/XSL/Disclosures.html</loc>.
    </p>
  </status>

  <abstract>
    <p>This document defines the W3C XQuery 1.0 and XPath 2.0 Data Model, which is the data model of at least <bibref ref="XSLT2"/>,
    and <bibref ref="XQuery"/>,
    and any other specifications that reference it.  This data model is based on the data models of <bibref ref="XPath"/> and
    <bibref ref="XQDM00"/> and replaces <bibref ref="XQDM00"/>.
    This document is the result of joint work by the
    <bibref ref="XSLWG"/> and the <bibref ref="XQWG"/>.</p>
  </abstract>

  <langusage>
    <language id="en">English</language>
  </langusage>

  <revisiondesc>
    <p>See the CVS changelog.</p>
  </revisiondesc>
</header>

<body>

<div1>
  <head>Introduction</head>

  <p>This document defines the XQuery 1.0 and XPath 2.0 Data Model,
  which is the data model of <bibref ref="XPath2"/>, <bibref ref="XSLT2"/> and
  <bibref ref="XQuery"/></p>

  <p>The XQuery 1.0 and XPath 2.0 Data Model (henceforth "data model")
  serves two purposes.
  First, it defines precisely the information contained in the input to an
  XSLT or XQuery processor.  Second, it defines all permissible values of
  expressions in the XSLT, XQuery, and XPath languages.  A
  language is <emph>closed</emph> with respect to a data model if the value
  of every expression in a language is guaranteed to be in the data model.
  XSLT 2.0, XQuery 1.0, and XPath 2.0 are all closed with respect to
  the data model.</p>

  <p>The data model is based on the <bibref ref="xml-infoset"/>
  (henceforth "Infoset"), but it requires the following new features to
  meet the <bibref ref="XPathReq"/> and <bibref ref="XQueryReq"/>:</p>

  <ulist>
    <item>
      <p>Support for XML Schema types. The XML Schema recommendations
      define features, such as structures (<bibref ref="xmlschema-1"/>)
      and simple data types (<bibref ref="xmlschema-2"/>), that extend
      the XML Information Set with precise type information.</p>
    </item>
    <item>
      <p>Representation of collections of documents and of
      complex values. (<bibref ref="XQueryReq"/>)</p>
    </item>
  </ulist>

  <p>As with the Infoset, the XQuery 1.0 and XPath 2.0 Data Model
  specifies what
  information in the documents is accessible, but it does not specify
  the programming-language interfaces or bindings used to represent or
  access the data.</p>

  <p>Every value handled by the data model is a <emph>sequence</emph> of
  zero or more <emph>item</emph>s. An <emph>item</emph> is either a
  <emph>node</emph> or an <emph>atomic value</emph>.

  A node is defined in <specref ref="Node"/> and is one of seven node kinds.
  An atomic value encapsulates an XML Schema atomic type
  and a corresponding value of that type. They are defined in
  <specref ref="AtomicValue"/>.
  A sequence is an ordered collection of nodes, atomic values, or any
  mixture of nodes and atomic values.  A sequence cannot be a member of
  a sequence.  A single item appearing on its own is modeled as a sequence
  containing one item.  Sequences are defined in <specref ref="sequences"/>.
  </p>

  <note>
    <p>In XPath 1.0, the data model only
    defines nodes.  The primitive data types (number, boolean, string,
    node-set) are part of the expression language, not
    the data model.</p>
  </note>

  <p>The data model can represent various
  values including not only the input and the output of a stylesheet or query, but all
  values of expressions used during the intermediate calculations.
  Examples include the input document or document repository (represented
  as a document node or a sequence of document nodes), the result of a
  path expression (represented as a sequence of nodes), the result of an
  arithmetic or a logical expression (represented as an atomic value),
  a sequence expression resulting in a sequence of items, etc.
  Examples of values that cannot be expressed directly by the data model
  include schema components and atomic values whose type
  is not an XML Schema atomic type.
  </p>

  <p>In this document, we provide a precise definition of how values in the
  XQuery 1.0 and XPath 2.0 Data Model are constructed and accessed, and how
  they relate to values in the Infoset.  We note wherever the XQuery 1.0 and
  XPath 2.0 Data Model differs from that of XPath 1.0.</p>

</div1>

<div1>
  <head>Notation and Pseudo-code Syntax</head>

  <p>In addition to prose, we define two sets of functions to explain the
data model: accessors
and constructors. The accessors and constructors defined by the data
model are shown with the prefix <emph>dm</emph>. The prefix is always shown in
italics to emphasize that these functions are abstract; they exist to
explain the interface between the data model and specfications that
rely on the data model: they are not and cannot be made accessible
directly from the host language.</p>

<p>See <specref ref="Issue-0033"/>.</p>

<p>The signature of accessors and constructors is shown using the same
style as <bibref ref="XFO"/>. For example:</p>

<example role="signature">
  <proto class="dm" name="typed-value" return-type="AtomicValue" returnSeq="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>In the psuedo-code syntax, the term <emph>Node</emph> denotes
the category of node values, <emph>AtomicValue</emph> denotes the
category of atomic values, and <emph>Item</emph> refers to the
category of either node values or atomic values.</p>

<p>Some accessors and constructors can accept or return sequences.
The following notation is used to denote sequence values:</p>

<ulist>
<item><p><emph>V*</emph> denotes a sequence of zero or more items of type
<emph>V</emph>.
</p></item>
<item><p><emph>V?</emph> denotes a sequence of exactly zero or one items of type
<emph>V</emph>.
</p></item>
<item><p><emph>V+</emph> denotes a sequence of one or more items of type
<emph>V</emph>.
</p></item>
</ulist>

<p>In a sequence, <emph>V</emph> may be a <emph>Node</emph> or
<emph>AtomicValue</emph>, or the union (choice) of several categories of
<emph>Items</emph>.</p>

<p>There are some functions in the data model that are <emph>partial
functions</emph>. We use the occurrence indicators <emph>?</emph> or
<emph>*</emph> when specifying the return type of such functions.
For example, a node may have one parent node or no parent.
If the node argument has a parent, the
<function>parent</function> accessor returns a singleton sequence.  If the node
argument does not have a parent, it returns the empty sequence.
The signature of <function>parent</function> specifies that it returns
an empty sequence or a sequence containing one node:</p>

<example role="signature">
  <proto class="dm" name="parent" return-type="Node" returnEmptyOk="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

  <note><p>The XPath 1.0 data model defines accessors, but does not
  define constructors.</p></note>

<!-- ======================================================================

  <p>The term <emph>signature</emph> of a function specifies the
  type of its zero or more inputs and the type of its one
  output. The following signature denotes a function <emph>f</emph> that
  takes values in the categories <emph>V</emph><sub>1</sub>, ...,
  <emph>V</emph><sub>m</sub> and returns an output value in the category
  <emph>V</emph><sub>n</sub>.</p>

  <p role="aside">function f(<emph>V</emph><sub>1</sub> $v<sub>1</sub>, ..., <emph>V</emph><sub>m</sub> $v<sub>m</sub>) returns <emph>V</emph><sub>n</sub></p>

  <p>A member of a particular category is a permissible argument to any
  function that accepts the category, for example, a
  <emph>ProcessingInstructionNode</emph> is a permissible argument to a
  function expecting a <emph>Node</emph>.</p>
====================================================================== -->

<p>This document relies on the <bibref ref="xml-infoset"/>. Information items
and properties are indicated by the styles <emph role="info-item">information
item</emph> and <emph role="infoset-property">property</emph>, respectively.</p>

<!-- ======================================================================
  <p>This document also provides pseudo-code syntax describing the mapping
  from the Infoset to the data model.  To facilitate this we introduce accessors
  to the required infoset properties through <emph>InfoItem</emph> objects.
  These infoset accessors are for rhetorical purposes only and are not intended to be exposed outside this
  specification.  We name the accessors of the Infoset using the convention
  infoset-&lt;<emph>item-name</emph>&gt;-&lt;<emph>property-name</emph>&gt;.
  Similarly, accessor functions that return PSV Infoset properties use the
  naming convention psvi-&lt;<emph>item-name</emph>&gt;-&lt;<emph>property-name</emph>&gt;.
  For example, <code>infoset-element-attributes</code> is the accessor that
  returns an element information item's <emph role="infoset-property">attributes</emph>
  property:</p>

  <p role="definition">function infoset-element-attributes(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#AttributeItem">AttributeItem</loc>*</p>

  <p>An <emph>InfoItem</emph> is one of eleven kinds of item: document
  item, element item, attribute item, processing instruction item,
  unexpanded entity item, character item, comment item, document type declaration item,
  unparsed entity item, notation item, and namespace item.</p>

  <p>The <emph>infoitem-kind</emph> accessor
  returns a string value representing the information item's kind, either
  <code>"document"</code>, <code>"element"</code>, <code>"attribute"</code>,
  <code>"character"</code>, <code>"namespace"</code>,
  <code>"processing-instruction"</code>, <code>"comment"</code>,
  <code>"document-type-declaration"</code>,
  <code>"notation"</code>, or <code>"unparsed-entity"</code>.</p>

  <p role="definition" id="InfoItem">function infoitem-kind(InfoItem $ii) returns <loc href="#dt-atomic-value">xs:string</loc></p>

  <p>Throughout this document, the namespace prefix <code>xs</code> indicates
  the <bibref ref="xmlschema-1"/> namespace name
  <code>http://www.w3.org/2001/XMLSchema</code>.
  The namespace prefixes <code>fn</code> and <code>op</code> indicate
  the namespace of the functions and operators defined in
  Section 1.5 "Namespace Prefix" of <bibref ref="XFO"/>.</p>

  <p>The following is a list of some functions and operators that are used
  in this document but precisely defined in <bibref ref="XFO"/>:</p>

  <ulist>
    <item><p id="op_concatenate">op:concatenate</p></item>
    <item><p id="op_item-at">op:item-at</p></item>
    <item><p id="op_value-equal">op:value-equal</p></item>
    <item><p id="xf_anyURI">xf:anyURI</p></item>
    <item><p id="xf_concat">xf:concat</p></item>
    <item><p id="xf_count">xf:count</p></item>
    <item><p id="xf_decimal">xf:decimal</p></item>
    <item><p id="xf_empty">xf:empty</p></item>
    <item><p id="xf_get-local-name">xf:get-local-name</p></item>
    <item><p id="xf_get-namespace-uri">xf:get-namespace-uri</p></item>
    <item><p id="xf_NCName">xf:NCName</p></item>
    <item><p id="xf_QName">xf:QName</p></item>
    <item><p id="xf_QName-from-uri">xf:QName-from-uri</p></item>
    <item><p id="xf_QName-from-string">xf:QName-from-string</p></item>
    <item><p id="xf_string">xf:string</p></item>
    <item><p id="xf_subsequence">xf:subsequence</p></item>
  </ulist>
====================================================================== -->

  <p>This document frequently uses the term <emph>expanded-QName</emph>.
  <termdef id="dt-expanded-qname" term="expanded-QName">An <term>expanded-QName</term>
  is a pair of values consisting of a namespace URI and
  a local name. They belong to the value space of the XML Schema type
  xs:QName. When this document refers to xs:QName we always
  mean the value space, i.e. a namespace URI, local name pair (and not
  the lexical space referring to constructs of the form prefix:local-name).</termdef>
  </p>

</div1>

<div1>
  <head>Concepts</head>
  <div2>
    <head>Node Identity</head>

    <p>Because XML documents are tree-structured, we define the
    data model using conventional terminology for trees.  The data model is
    a node-labeled, tree-shaped graph, but also includes a
    concept of node identity.  The identity of a node is established
    when a node-constructor is applied to create the node: each
    application of a node constructor creates a new node that is
    identical to itself, and not identical to any other node
    (see <specref ref="Node"/>).</p>

<!-- ======================================================================
    <p>The <function>node-equal</function> function takes two nodes as arguments.
    It returns true if the arguments are identical and false otherwise.
    </p>

<example role="signature">
  <proto class="dm" name="node-equal" return-type="xs:boolean" returnEmptyOk="no">
    <arg name="n1" type="Node"/>
    <arg name="n2" type="Node"/>
  </proto>
</example>
====================================================================== -->

    <p>This concept should not be confused with the concept of a
    unique ID, which is a unique name assigned to an
    element by the author to represent references using ID/IDREF
    correlation.</p>
  </div2>

  <div2>
    <head>Document Order</head>

<p><termdef id="dt-document-order" term="document order">A
<term>document order</term> is defined on all the nodes in a document.
Document order is a total ordering, although the relative order of
some nodes is implementation-dependent. Informally, document order is
the order returned by an in-order, depth-first, left-to-right
traversal of the data model.</termdef> There is precisely one document
order and it satisfies the following constraints.</p>

<ulist>
<item><p>The document node is the first node.</p>
</item>
<item><p>The relative order of siblings is determined by their order in
the XML representation. A node N1 occurs before a node N2 in
document order if and only if the start of N1 occurs before the start of N2 in
the XML document.</p>
</item>
<item><p>Element nodes occur before their children; children occur before
following-siblings.</p>
</item>
<item><p>Namespace nodes immediately follow the element node with which
they are associated. The relative order of namespace nodes is stable but
implementation-dependent.</p>
</item>
<item><p>Attribute nodes immediately follow the namespace nodes of the element
with which they are associated. The relative order of attribute nodes is stable but
implementation-dependent.</p>
</item>
</ulist>

<p>Reverse document order is the reverse of document order.</p>

<!-- ======================================================================
    <p>A <emph>document order</emph> is defined on all the nodes in a
    document.  The document node is the first node.  Element nodes,
    comment nodes, and processing instruction nodes occur in the
    order of their representation in the XML (after expansion of
    entities).  Element nodes occur before their children.  The
    namespace nodes of an element immediately follow the element node.
    The relative order of namespace nodes is implementation-dependent.
    The attribute nodes of an element immediately follow the
    namespace nodes of the element. The relative order of attribute
    nodes is implementation-dependent. Reverse document order is the
    reverse of document order.</p>
====================================================================== -->

    <p>The relative order of nodes in distinct documents is
    implementation-dependent but stable.  In other words, given
    two distinct documents A and B, if a node in document A is
    before a node in document B, then every node in document A
    is before every node in document B.</p>

    <note><p>The relative order of free-floating nodes (those not in
    a document) is not defined.  See <specref ref="Issue-0050"/>.</p></note>

<!-- ======================================================================
    <p>The <function>node-before</function> function takes two nodes as arguments.
    It returns true if the first argument is before (but not identical
    to) the second one in document order and false otherwise
    </p>

<example role="signature">
  <proto class="dm" name="node-before" return-type="xs:boolean" returnEmptyOk="no">
    <arg name="n1" type="Node"/>
    <arg name="n2" type="Node"/>
  </proto>
</example>
====================================================================== -->

  </div2>

  <div2 id="psv">
    <head>XML Schemas and the XML Information Set</head>

    <p>The data model is defined in terms of the
    <bibref ref="xml-infoset"/> after XML Schema validity assessment.
    XML Schema validity assessment is the process of assessing an XML
    element information item with respect to an XML Schema and
    augmenting it and some or all of its descendants with properties
    that provide information about validity and type assignment.
    <termdef id="dt-psvi" term="PSVI">The result of schema validity assessment
    is an augmented
    Infoset, known as the Post Schema-Validation Infoset, or
    <term>PSVI</term>.</termdef>
    </p>

    <p>The data model supports well-formed XML documents conforming to
<bibref ref="xml-names"/>. XML documents that are not well-formed are
not XML, by definition. XML documents that do not conform to <bibref
ref="xml-names"/> are not supported (they are not supported by
<bibref ref="xml-infoset"/>).</p>

    <p>In other words, the data model supports the following classes
    of XML documents:</p>

    <ulist>
      <item>
        <p>Well-formed documents conforming to <bibref ref="xml-names"/>,</p>
      </item>
      <item>
        <p>DTD-valid documents conforming to <bibref ref="xml-names"/>, and</p>
      </item>
      <item>
        <p>W3C XML Schema-validated documents.</p>
      </item>
    </ulist>

    <p>The data model supports
    some kinds of values that are not supported by <bibref ref="xml-infoset"/>.
    Examples of these are well-formed document fragments, sequences of
    fragments or sequences of documents. The data model also
    supports values that are not nodes. Examples of these are atomic
    values, sequences of atomic values, or sequences mixing nodes and
    atomic values. These are necessary to be able to represent the results
    of intermediate expressions in the data model during expression
    processing.
    </p>

    <p>Schema-validated documents include documents in which some elements
    or attributes have been validated by "lax" or "skip" validation
    (<bibref ref="xmlschema-2"/>).</p>

    <p>An "incompletely validated document" is an XML document that has a
    corresponding schema but whose schema-validity assessment has resulted
    in one or more element or attribute information items being assigned
    values other than 'valid' for the <emph role="infoset-property">validity</emph>
    property in the PSVI.</p>

    <p>The data model supports incompletely validated documents.</p>

    <note><p>This implies accommodation for the case where
    both a DTD and a schema are applied.  This will probably require some
    reconciliation of the [attribute type] property with type
    information from the PSVI.  See issues <specref ref="Issue-0004"/>,
    <specref ref="Issue-0081"/>.
    </p></note>

    <p>In addition to specifying the transformation from the Post Schema
    Validation Infoset (PSVI) to the data model, this document also specifies
    the transformation from the data model back to the XML Information Set.
    This is a useful notion that can be used for defining serialization and
    validation.
    Serialization can be viewed as a two step process, first transforming
    to the XML Infoset and then to an XML document.
    Validation is described conceptually as a process of mapping the data
    model to the XML Infoset followed by XML Schema validation producing
    a PSVI which is then loaded into the data model.</p>

<!-- ======================================================================
    The mapping from the data model to the Infoset is specified
    via the function
    </p>

<example role="signature">
  <proto class="dm" name="node-to-infoitem" return-type="InfoItem" returnEmptyOk="no">
    <arg name="n" type="Node"/>
  </proto>
</example>

    <p>
    The function takes a data model Node as an argument and returns
    an Information Item. We define this for each data model node type
    in the appropriate section below. The returned InfoItem will be
    specified by having its type identified and its properties described.
    The definition of <function>node-to-infoitem()</function> will be recursive.
    Furthermore we will find it convenient to use a second function
    <function>nodes-to-infoitems()</function> which takes a sequence of data
    model Nodes and returns a sequence of InfoItems by applying
    <function>node-to-infoitem()</function> on each member of the sequence of
    Nodes in turn and creating a sequence of the resulting InfoItems.
    </p>
    <p role="definition">
define function dm:nodes-to-infoitems(<loc href="#Node">Node</loc>* $seq)
                returns <loc href="#InfoItem">InfoItem</loc>*
{
  for $node in $seq return dm:node-to-infoitem($node)
}
</p>
====================================================================== -->

<p>See <specref ref="Issue-0085"/>.</p>

  </div2>

<div2 id="types">
<head>Types</head>

<p>The data model supports a representation of named types as
<termref def="dt-expanded-qname">expanded-QNames</termref>.
Named types include both the built-in types defined by
<bibref ref="xmlschema-2"/> and user-types declared in a schema
and imported by a stylesheet or query.
Since named types in XML Schema are global, an expanded-QName
uniquely identifies such a type.
The namespace name of the expanded-QName is the target namespace
of the schema and its local name is the name of the type.
The data model does not uniquely identify anonymous types and
represents them by <code>xs:anyType</code> or <code>xs:anySimpleType</code>.
</p>

<p>The data model associates type information with element nodes,
attribute nodes and atomic values. If this type information is
something other than <code>xs:anyType</code> or
<code>xs:anySimpleType</code>, the item is guaranteed to be a valid
instance of that type as defined by XML Schema.
</p>

<p>The data model defines an accessor <function>type</function> that returns
an expanded-QName corresponding to the type of the element node,
attribute node or atomic value.
It returns <code>xs:anyType</code> or <code>xs:anySimpleType</code> if it is
locally declared, if no type information exists, or if it failed W3C XML Schema
validity assessment.</p>

<p>When no type information exists for an element or an attribute node
we frequently use the terminology "element with unknown type"
or "attribute with unknown simple type".
</p>

<p>The data model does not represent element or attribute declaration
schema components, but it supports various type-related operations.
The semantics of such operations, e.g. checking if a particular
instance of an element node has a given type is defined in
<bibref ref="XQFS"/>.
</p>

</div2>

<div2 id="typed-value">
<head>Typed Value and String Value</head>

<p>The content of a text, attribute, or element node can be interpreted
in two ways: as a string value or as a typed value.
For these types of nodes, the typed value can be extracted by the
<function>typed-value</function> accessor, and the string value can be
extracted by the <function>string-value</function> accessor.</p>

<p>The string value of a node is a single <code>xs:string</code>
derived from the content of the node as described in the definitions
of the accessor functions for each kind of node.</p>

<p>The typed value of a node is a sequence of atomic values derived
from its string value and its type in a way that is consistent with
schema validation, as described in the definitions of the accessor
functions for each kind of node.
</p>

<p><specref ref="Issue-0089"/></p>

</div2>

<div2 id="PSVI2Types">
<head>Mapping PSV Infoset additions to Types</head>

<p>This section specifies how the type of an element or attribute node
is computed from the <termref def="dt-psvi">PSVI</termref> properties that
specify validity and type
assessment for the node's corresponding information item.</p>

<p>See <specref ref="Issue-0077"/>.</p>

<p>A PSVI element or attribute information item has a
<emph role="infoset-property">validity</emph> property.
The <emph role="infoset-property">validity</emph> property may be
"<emph>valid</emph>", "<emph>invalid</emph>", or "<emph>notKnown</emph>"
and reflects the outcome of schema-validity assessment.
The only information that can be inferred from an
invalid or not known validity value is that the information
item is well-formed, therefore, we must associate some general
type information with the element or attribute node if it is not known
to be valid.
</p>

<p>The precise definition of the type of an element or attribute
information item depends on the properties of the PSVI.
XML Schema only guarantees the existence of either the
<emph role="infoset-property">type definition</emph> property,
or the
<emph role="infoset-property">type definition namespace</emph>,
<emph role="infoset-property">type definition name</emph> and
<emph role="infoset-property">type definition anonymous</emph>
properties.
If the type definition refers to a union type, there
are further properties defined, that refer to the type definition
which actually validated the item's normalized value.
These properties are either the
<emph role="infoset-property">member type definition</emph>,
or the
<emph role="infoset-property">member type definition namespace</emph>,
<emph role="infoset-property">member type definition name</emph> and
<emph role="infoset-property">member type definition anonymous</emph>
properties.
If these are available, the type of an element or attribute will
refer to the member type that actually validated the schema
normalized value.
</p>

<p>The <emph>type</emph> of an element information item is
represented by an <termref def="dt-expanded-qname">expanded-QName</termref>
whose namespace and local name correspond
to the first applicable items in the following list:
</p>

<ulist>
<item><p>If the <emph role="infoset-property">validity</emph> property exists
and is "<emph>"valid</emph>":</p>

<ulist>
<item><p>If <emph role="infoset-property">member type definition</emph>
exists and its {name} property is present:</p>
  <ulist>
    <item><p>The {target namespace} and {name} properties of the
<emph role="infoset-property">member type definition</emph> property.</p></item>
   </ulist>
</item>

<item><p>If the <emph role="infoset-property">type definition</emph> property
exists and its {name} property is present:</p>
  <ulist>
    <item><p>The {target namespace} and {name} properties of the
<emph role="infoset-property">type definition</emph> property.</p></item>
   </ulist>
</item>

<item><p>If <emph role="infoset-property">member type definition anonymous</emph>
exists and is <emph>false</emph>:
the <emph role="infoset-property">member type definition namespace</emph>
and the <emph role="infoset-property">member type definition name</emph>.
</p></item>

<item><p>If <emph role="infoset-property">type definition anonymous</emph>
exists and is <emph>false</emph>:
the <emph role="infoset-property">type definition namespace</emph>
and the <emph role="infoset-property">type definition name</emph>
</p></item>
</ulist>
</item>

<item><p>Otherwise, <code>xs:anyType</code> for elements or
 <code>xs:anySimpleType</code> for attributes.
</p></item>
</ulist>

<!--
<ulist>
<item><p><code>xs:anyType</code> for elements, or <code>xs:anySimpleType</code>
for attributes, if the <emph role="infoset-property">validity</emph>
property is not <emph>"valid"</emph>, or
</p></item>
<item><p>the {target namespace} and {name} properties of the
<emph role="infoset-property">member type definition</emph>
schema component if it exists, or
</p></item>
<item><p>the {target namespace} and {name} properties of the
<emph role="infoset-property">type definition</emph>
schema component if it exists, or
</p></item>
<item><p>the <emph role="infoset-property">member type definition namespace</emph>
and the <emph role="infoset-property">member type definition name</emph>
if <emph role="infoset-property">member type definition anonymous</emph>
exists and is false, or
</p></item>
<item><p>the <emph role="infoset-property">type definition namespace</emph>
and the <emph role="infoset-property">type definition name</emph>
if <emph role="infoset-property">type definition anonymous</emph>
exists and is false, or
</p></item>
<item><p><code>xs:anyType</code> for elements, or <code>xs:anySimpleType</code>
for attributes. See <specref ref="Issue-0082"/>.
</p></item>
</ulist>
-->

<ednote><edtext>The above definition is currently under discussion.
It is very likely that a change will be made in a future draft to
reflect a more precise definition covering derived types and the
possible usage of generated type identifiers when no type names
available. See <specref ref="Issue-0076"/>.
</edtext></ednote>

<!-- ======================================================================
<p>We will find it convenient to define the following function
that will be used later in various sections.
The <emph>infoitem-to-type</emph> function takes an element or
an attribute information item and returns an xs:QName that
corresponds to the type of its argument. </p>

<example role="signature">
  <proto class="dm" name="infoitem-to-type" return-type="xs:QName" returnEmptyOk="no">
    <arg name="ii" type="ElementInfoItem|AttributeInfoItem"/>
  </proto>
</example>

    <p id="infoitem-to-type" role="definition">
function infoitem-to-type((<loc href="#ElementItem">ElementItem</loc>|<loc href="#AttributeItem">AttributeItem</loc>) $ii)
         returns <loc href="#dt-atomic-value">xs:QName</loc>

define function infoitem-to-type(ElementItem $ii)
       returns <loc href="#dt-atomic-value">xs:QName</loc>
{
  if (psvi-element-validity($ii) ne "valid") then
    return <loc href="#xf_QName-from-string">xf:QName-from-string</loc>("xs:anyType")
  else if (psvi-element-member-type-definition($ii) ne ())
       then
    let $typedef:= psvi-element-member-type-definition($ii)
    let $name:= psvi-typedefinition-name($typedef)
    let $ns:=  psvi-typedefinition-namespace($typedef)
    return <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>($ns,$name)
  else if (psvi-element-type-definition($ii) ne ()) then
    let $typedef:= psvi-element-type-definition($ii)
    let $name:= psvi-typedefinition-name($typedef)
    let $ns:=  psvi-typedefinition-namespace($typedef)
    return <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>($ns,$name)
  else if (psvi-element-member-type-definition-anonymous($ii)
           eq false) then
    let $name:= psvi-element-member-type-definition-name($ii)
    let $ns:=
        psvi-element-member-type-definition-namespace($ii)
    return <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>($ns,$name)
  else if (psvi-element-type-definition-anonymous($ii)
           eq false) then
    let $name:= psvi-element-type-definition-name($ii)
    let $ns:= psvi-element-type-definition-namespace($ii)
    return <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>($ns,$name)
  else
    return <loc href="#xf_QName-from-string">xf:QName-from-string</loc>("xs:anyType")
}

define function infoitem-to-type(AttributeItem $ii)
       returns <loc href="#dt-atomic-value">xs:QName</loc>
{
  if (psvi-attribute-validity($ii) ne "valid") then
    return <loc href="#xf_QName-from-string">xf:QName-from-string</loc>("xs:anySimpleType")
  else if (psvi-attribute-member-type-definition($ii) ne ())
       then
    let $typedef:= psvi-attribute-member-type-definition($ii)
    let $name:= psvi-typedefinition-name($typedef)
    let $ns:=  psvi-typedefinition-namespace($typedef)
    return <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>($ns,$name)
  else if (psvi-attribute-type-definition(a) ne ()) then
    let $typedef:= psvi-attribute-type-definition($ii)
    let $name:= psvi-typedefinition-name($typedef)
    let $ns:=  psvi-typedefinition-namespace($typedef)
    return <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>($ns,$name)
  else if (psvi-attribute-member-type-definition-anonymous($ii)
           eq false) then
    let $name:= psvi-attribute-member-type-definition-name($ii)
    let $ns:=
        psvi-attribute-member-type-definition-namespace($ii)
    return <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>($ns,$name)
  else if (psvi-attribute-type-definition-anonymous($ii)
           eq false) then
    let $name:= psvi-attribute-type-definition-name($ii)
    let $ns:= psvi-attribute-type-definition-namespace($ii)
    return <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>($ns,$name)
  else
    return <loc href="#xf_QName-from-string">xf:QName-from-string</loc>("xs:anySimpleType")
} </p>
====================================================================== -->

  </div2>

<div2 id="flags">
  <head>Comments, Processing Instructions, and Whitespace</head>

  <p>Although the data model is able to represent comments, processing
instructions, and insignificant whitespace, preservation of this information
may be unnecessary and onerous for some applications.</p>

  <p>An instance of the data model can be constructed from an Infoset,
a PSVI, or from some other data source entirely. Different
applications may or may not choose to construct nodes in the data
model to represent comments, processing instructions, and
insignificant white space. These decisions are considered outside the
scope of the data model. Consequently the data model makes no attempt
to control or identify the sort of processing in this regard that an
application uses to construct a data model instance.</p>
</div2>

</div1>

<div1 id="Node">
  <head>Nodes</head>

  <p>The category of <emph>Node</emph> values contains seven distinct
  kinds of nodes: <loc href="#DocumentNode">document</loc>,
  <loc href="#ElementNode">element</loc>, <loc href="#AttributeNode">attribute</loc>,
  <loc href="#TextNode">text</loc>, <loc href="#NamespaceNode">namespace</loc>,
  <loc href="#ProcessingInstructionNode">processing instruction</loc>, and
  <loc href="#CommentNode">comment</loc>.  The seven kinds of nodes are
  defined in the following subsections.</p>

  <p>Each kind of node has its own constructor.   The effect of a node
  constructor is to create a new node with a unique identity, distinct
  from all other nodes.</p>

  <p>A tree contains a root plus all nodes that are reachable
  directly or indirectly from the root via the <function>children</function>,
  <function>attributes</function>, and <function>namespace</function> accessors.  Every
  node belongs to exactly one tree, and every tree has exactly one root
  node.  A tree whose root node is a document node is referred to as a
  <emph>document</emph>.  A tree whose root node is some other kind of
  node is referred to as a <emph>fragment</emph>.</p>

<div2 id="accessors">
<head>Accessors</head>

  <p>A set of accessors is defined on all seven kinds of Nodes.  Some
  accessors return a constant empty sequence on certain node kinds.</p>

<p>See <specref ref="Issue-0091"/>.</p>

<p>In order for applications to be able to operate on instances of the
data model, the model must expose properties of the items it contains.
The data model does this by defining a family of accessor functions.
These are not functions in the literal sense, they are not available
for users or applications to call directly, rather they are
descriptions of the interface that an implementation of the data model
must expose to applications. Functions and operators available to end-users
are described in <bibref ref="XFO"/>.</p>

<p>The following table summarizes the accessor functions and the types
of values that they return if called on a node of each type.</p>

<table border="1" cellspacing="0" summary="Accessors by node type">
  <thead align="center">
    <tr>
      <td>&#160;</td>
      <td>Document Node</td>
      <td>Element Node</td>
      <td>Attribute Node</td>
      <td>Namespace Node</td>
      <td>P.I. Node</td>
      <td>Comment Node</td>
      <td>Text Node</td>
    </tr>
  </thead>
  <tbody align="center">
    <tr>
      <td align="left"><function>base-uri</function></td>
      <td colspan="7" align="center">xs:anyURI?</td>
    </tr>
    <tr>
      <td align="left"><function>node-kind</function></td>
      <td colspan="7" align="center">xs:string</td>
    </tr>
    <tr>
      <td align="left"><function>node-name</function></td>
      <td role="D">()</td>
      <td role="E">xs:QName</td>
      <td role="A">xs:QName</td>
      <td role="N">xs:QName?</td>
      <td role="P">()</td>
      <td role="C">()</td>
      <td role="T">()</td>
    </tr>
    <tr>
      <td align="left"><function>parent</function></td>
      <td role="D">()</td>
      <td role="E">Node?</td>
      <td role="A">Node?</td>
      <td role="N">Node?</td>
      <td role="P">Node?</td>
      <td role="C">Node?</td>
      <td role="T">Node?</td>
    </tr>
    <tr>
      <td align="left"><function>string-value</function></td>
      <td colspan="7" align="center">xs:string</td>
    </tr>
    <tr>
      <td align="left"><function>typed-value</function></td>
      <td role="D">()</td>
      <td role="E">Value?</td>
      <td role="A">Value?</td>
      <td role="N">()</td>
      <td role="P">()</td>
      <td role="C">()</td>
      <td role="T">Value?</td>
    </tr>
    <tr>
      <td align="left"><function>type</function></td>
      <td role="D">()</td>
      <td role="E">xs:QName?</td>
      <td role="A">xs:QName?</td>
      <td role="N">()</td>
      <td role="P">()</td>
      <td role="C">()</td>
      <td role="T">()</td>
    </tr>
    <tr>
      <td align="left"><function>children</function></td>
      <td role="D">Node+</td>
      <td role="E">Node*</td>
      <td role="A">()</td>
      <td role="N">()</td>
      <td role="P">()</td>
      <td role="C">()</td>
      <td role="T">()</td>
    </tr>
    <tr>
      <td align="left"><function>attributes</function></td>
      <td role="D">()</td>
      <td role="E">Node*</td>
      <td role="A">()</td>
      <td role="N">()</td>
      <td role="P">()</td>
      <td role="C">()</td>
      <td role="T">()</td>
    </tr>
    <tr>
      <td align="left"><function>namespaces</function></td>
      <td role="D">()</td>
      <td role="E">Node*</td>
      <td role="A">()</td>
      <td role="N">()</td>
      <td role="P">()</td>
      <td role="C">()</td>
      <td role="T">()</td>
    </tr>
<!--
    <tr>
      <td align="left"><function>unique-ID</function></td>
      <td role="D">()</td>
      <td role="E">xs:ID?</td>
      <td role="A">()</td>
      <td role="N">()</td>
      <td role="P">()</td>
      <td role="C">()</td>
      <td role="T">()</td>
    </tr>
-->
  </tbody>
</table>

<div3 id="dm-base-uri">
<head><function>base-uri</function> Accessor</head>

<example role="signature">
  <proto class="dm" name="base-uri" return-type="xs:anyURI" returnEmptyOk="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>base-uri</function> accessor returns a sequence containing
zero or one uri references.</p>

<p>Document, element, and processing-instruction nodes have a base-uri
property. If that property is non-empty, its value is returned.</p>

<p>If the accessor is called on a node that does not have a base-uri
property, or whose base-uri property is empty, the base-uri of that
node's parent is returned. If the node has no parent, an error is
raised. See <specref ref="Issue-0087"/></p>

</div3>

<div3 id="dm-node-kind">
<head><code>node-kind</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="node-kind" return-type="xs:string" returnEmptyOk="no">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>node-kind</function> accessor returns a string value identifying the
kind of node on which the accessor was called. One of the following values
is returned:</p>

<ulist>
<item><p>"<code>document</code>" for document nodes.</p></item>
<item><p>"<code>element</code>" for element nodes.</p></item>
<item><p>"<code>attribute</code>" for attribute nodes.</p></item>
<item><p>"<code>text</code>" for text nodes.</p></item>
<item><p>"<code>namespace</code>" for namespace nodes.</p></item>
<item><p>"<code>processing-instruction</code>" for processing instruction nodes.</p></item>
<item><p>"<code>comment</code>" for comment nodes.</p></item>
</ulist>

</div3>

<div3 id="dm-node-name">
<head><code>node-name</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="node-name" return-type="xs:QName" returnEmptyOk="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>node-name</function> accessor returns a sequence of zero or one
xs:QNames.</p>

<ulist>
<item><p>For element and attribute nodes, <function>node-name</function> returns the
qualified name of the element or attribute.</p></item>
<item><p>For processing-instructions nodes, <function>node-name</function> returns an
xs:QName with a the processing instruction target name in the local-name and
no namespace URI.</p></item>
<item><p>For namespace nodes, <function>node-name</function> returns an xs:QName with
the <emph>prefix</emph> of the namespace declaration in the local-name and
no namespace URI. If the namespace declaration declares the default namespace,
which has no prefix, an empty sequence is returned.</p>
<p>Some implementations may
not preserve information about the prefixes declared. In these cases,
the <function>node-name</function> accessor returns the empty
sequence when applied to processing-instruction nodes.</p>
</item>
</ulist>
</div3>

<div3 id="dm-parent">
<head><code>parent</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="parent" return-type="Node" returnEmptyOk="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>parent</function> accessor returns a sequence containing
zero or one nodes.</p>

<p>For nodes that have a parent, <function>parent</function> returns the
parent node. For all other nodes, it returns the empty sequence.</p>

<p>If the return value is not the empty sequence, it will always be
either an element node or a document node.</p>
</div3>

<div3 id="dm-string-value">
<head><code>string-value</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="string-value" return-type="xs:string" returnEmptyOk="no">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>string-value</function> accessor returns a string representation
of the node.</p>

<p>For some kinds of nodes, this is part of the node; for other kinds
of nodes, it is computed from the <function>string-value</function> of its
descendant nodes.</p>

<p>The <function>string-value</function> accessor can be used to recover the
lexical representation of an atomic value. The details of converting
an atomic value to its string representation are described in the
<quote>Casting Functions</quote> section of <bibref ref="XFO"/>.
In particular if the atomic value's type is primitive,
<function>string-value</function> returns the atomic value's canonical
lexical representation for that primitive type as specified in
<bibref ref="xmlschema-2"/>. If the atomic value's type is derived, the
lexical representation depends on whether a value is supplied for the
type's pattern facet: If no such value is supplied
<function>string-value</function> returns the atomic value's canonical
lexical representation for the primitive base type.
Otherwise <function>string-value</function> returns a lexical
representation that matches the value specified for the pattern facet.
(This case includes <code>xs:integer</code>s.) See <specref ref="Issue-0072"/>.</p>

<note>
  <p>Using the canonical lexical representation for atomic values
   as described above may not always be compatible with XPath 1.0.</p>
</note>

</div3>

<div3 id="dm-typed-value">
<head><code>typed-value</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="typed-value" return-type="AtomicValue" returnSeq="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>typed-value</function> accessor returns the typed-value
of the node, which is a sequence of zero or more atomic values.
The typed-value is closely related to the node's string-value and its
type. For instance when the node's string-value is "3.14" and its type
is <code>xs:decimal</code>, the typed-value is a sequence containing the
atomic value 3.14 of type decimal.
In fact, when the type is an atomic type, typed-value is always the
atomic-value constructed from the string-value and the type.</p>

<p>In the general case, <function>typed-value</function> constructs a
sequence of atomic values. These values are derived from the
string-value of the element and its type, in such a way as to be
consistent with validation.</p>

<p>See <specref ref="Issue-0080"/>.</p>

<p>If the node (or node kind) has no typed value, the empty sequence
is returned.</p>

</div3>

<div3 id="dm-type">
<head><code>type</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="type" return-type="xs:QName" returnEmptyOk="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>type</function> accessor returns a sequence containing zero or one
xs:QName.</p>

<p>For element nodes, <function>type</function> returns the globally
declared QName of the type of the node or xs:anyType if it
is locally declared or no type information exists.</p>

<p>For attribute nodes, <function>type</function> returns the globally
declared QName of the type of the node or xs:anySimpleType if it
is locally declared or no type information exists.</p>

<p>For other node kinds, it always returns the empty sequence. </p>

</div3>

<div3 id="dm-children">
<head><code>children</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="children" return-type="Node" returnSeq="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>children</function> accessor returns a sequence containing
zero or more nodes.</p>

<p>For document and element nodes, it returns the nodes that are the
children of that node. It returns the empty sequence for document and
element nodes that have no children.</p>

<p>For all other nodes, it always returns the empty sequence.</p>

<p>A document node or an element node is the parent of each of
its child nodes.  Nodes never share children: if two nodes have
distinct identities, then no child of one node will be a child of
the other node.</p>
</div3>

<div3 id="dm-attributes">
<head><code>attributes</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="attributes" return-type="AttributeNode" returnSeq="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>attributes</function> accessor returns a sequence containing
zero or more attribute nodes.</p>

<p>For element nodes, these are the attributes of the node. For all other nodes,
it always returns the empty sequence.</p>

</div3>

<div3 id="dm-namespaces">
<head><code>namespaces</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="namespaces" return-type="NamespaceNode" returnSeq="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>namespaces</function> accessor returns a sequence containing
zero or more namespace nodes.</p>

<p>For element nodes, these are the namespaces of the node. For all other nodes,
it always returns the empty sequence.</p>
</div3>

<!--
<div3 id="dm-unique-ID">
<head><code>unique-ID</code> Accessor</head>

<example role="signature">
  <proto class="dm" name="unique-ID" return-type="xs:ID" returnEmptyOk="yes">
    <arg name="n" type="Node"/>
  </proto>
</example>

<p>The <function>unique-ID</function> accessor returns a sequence containing
zero or one xs:ID values.</p>

<p>For element nodes, this is the value of the attribute or child
element with that is of type xs:ID, if it exists. For all other nodes,
it always returns the empty sequence.</p>
</div3>
-->
</div2>

<div2 id="DocumentNode">
  <head>Documents</head>

<!--
  <table role="node-summary">
    <thead>
      <tr><td>Document Node accessors</td><td>possible values</td></tr>
    </thead>
    <tbody>
      <tr><td><function>node-kind</function></td>    <td>"document"</td></tr>
      <tr><td><function>node-name</function></td>    <td>empty sequence</td></tr>
      <tr><td><function>base-uri</function></td>     <td>xs:anyURI</td></tr>
      <tr><td><function>string-value</function></td> <td>xs:string</td></tr>
      <tr><td><function>typed-value</function></td>  <td>empty sequence</td></tr>
      <tr><td><function>parent</function></td>       <td>empty sequence</td></tr>
      <tr><td><function>children</function></td>     <td>arbitrary sequence of one or more
                                element nodes, zero or more processing
                                instruction nodes, and zero or more
                                comment nodes</td></tr>
      <tr><td><function>attributes</function></td>   <td>empty sequence</td></tr>
      <tr><td><function>namespaces</function></td>   <td>empty sequence</td></tr>
      <tr><td><function>type</function></td>         <td>empty sequence</td></tr>
      <tr><td><function>unique-ID</function></td>    <td>empty sequence</td></tr>
    </tbody>
  </table>
-->

<div3 id="DocumentNodeOverview">
  <head>Overview</head>

  <p>Document nodes encapsulate XML documents. Documents have the following
properties:</p>

  <ulist>
  <item><p><emph role="dm-node-property">base-uri</emph>, possibly empty.
  </p></item>
  <item><p><emph role="dm-node-property">children</emph>
  </p></item>
  </ulist>

  <p>Document nodes must satisfy the following constraints.</p>

  <olist>
  <item><p>An document node's children may not contain two consecutive text
  nodes. Consecutive text nodes are collapsed by the
  document constructor into one text node.</p>
  </item>
  <item><p>If a node <emph>N</emph> is a child of a document <emph>D</emph>,
then the parent of <emph>N</emph> must be <emph>D</emph>.</p></item>
  <item><p>If a node <emph>N</emph> has a parent document <emph>D</emph>, then
<emph>N</emph> must be among the children of <emph>D</emph>.</p></item>
  <item><p>Every child of a document must be distinct.</p></item>
  </olist>

  <p>In a well-formed document, the children of the document node consist
  exclusively of element nodes, processing-instruction nodes, and comment
  nodes, and exactly one of these children is an element node.  A document
  node in the data model is more permissive: it allows more than one
  element node as a child and also permits text nodes as children.
  See
  <specref ref="Issue-0074"/>.
  </p>

  <note>
    <p>Document nodes and XPath 1.0 root nodes are essentially
    identical.</p>
  </note>
</div3>

<div3 id="DocumentNodeConstructor">
  <head>Constructor</head>

<p id="document-node">A document node can be constructed using
<function>document-node</function> which returns a new node with unique identity,
distinct from all other nodes.</p>

<p>The constructor takes an optional base URI value and a non-empty
sequence of children nodes as arguments.</p>

<example role="signature">
  <proto class="dm" name="document-node" return-type="DocumentNode" returnEmptyOk="no">
    <arg name="children" type="Node" seq="yes"/>
  </proto>
</example>

<example role="signature">
  <proto class="dm" name="document-node" return-type="DocumentNode" returnEmptyOk="no">
    <arg name="children" type="Node" seq="yes"/>
    <arg name="base-uri" type="xs:anyURI"/>
  </proto>
</example>

<p>The sequence of nodes passed as <code>$children</code> must not be
empty and must consist only of element, processing instruction,
comment, and text nodes. See <specref ref="Issue-0090"/>.</p>

<p>If consecutive text nodes are specified as the children of a document
node they are collapsed into one text node whose string value is the
concatenation of the string values of the consecutive text nodes.</p>
</div3>

<div3 id="DocumentNodeAccessors">
  <head>Accessors</head>

<table border="1" cellspacing="0" summary="Accessor summary">
  <thead>
    <tr>
      <td>Accessor</td>
      <td>Returns:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><function>base-uri</function></td>
      <td role="D">The value of the <emph role="dm-node-property">base-uri</emph>
property</td>
    </tr>
    <tr>
      <td><function>node-kind</function></td>
      <td role="D">"<code>document</code>"</td>
    </tr>
    <tr>
      <td><function>node-name</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>parent</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>string-value</function></td>
      <td role="D">The concatenation of the string-values of all the text node
descendants of the document in document order</td>
    </tr>
    <tr>
      <td><function>typed-value</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>type</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>children</function></td>
      <td role="D">The children of the document node</td>
    </tr>
    <tr>
      <td><function>attributes</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>namespaces</function></td>
      <td role="D">()</td>
    </tr>
<!--
    <tr>
      <td><function>unique-ID</function></td>
      <td role="D">()</td>
    </tr>
-->
  </tbody>
</table>

<p>Two additional accessors are defined on document nodes:</p>

<example role="signature">
  <proto class="dm" name="unparsed-entity-system-id" return-type="xs:string" returnEmptyOk="yes">
    <arg name="node" type="DocumentNode"/>
    <arg name="entityname" type="xs:string"/>
  </proto>
</example>

<p>The <function>unparsed-entity-system-id</function> accessor returns the
system identifier of an unparsed external entity declared in the
specified document. If no entity with the name specified in <code>$entityname</code>
exists, or if the entity is not an external unparsed entity, the empty sequence
is returned.</p>

<example role="signature">
  <proto class="dm" name="unparsed-entity-public-id" return-type="xs:string" returnEmptyOk="yes">
    <arg name="node" type="DocumentNode"/>
    <arg name="entityname" type="xs:string"/>
  </proto>
</example>

<p>The <function>unparsed-entity-public-id</function> accessor returns the
public identifier of an unparsed external entity declared in the
specified document.  If no entity with the name specified in <code>$entityname</code>
exists, or if the entity is not an external unparsed entity, or if the entity
has no public identifier, the empty sequence
is returned.</p>

</div3>

<div3 id="DocumentNodeIS2DM">
<head>PSVI to Datamodel Mapping</head>

<p>When a data model fragment is created from the PSVI, a
<emph role="info-item">document information item</emph> is mapped
to a Document Node. The precise transformation is described
by specifying the PSVI property corresponding to each argument
of the document node constructor:</p>

<table border="1" cellspacing="0" summary="Constructor summary for PSVI mapping">
  <colgroup>
    <col width="35%"/>
    <col width="65%"/>
  </colgroup>
  <thead>
    <tr>
      <td>Argument</td>
      <td>Value:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>$base-uri</code></td>
      <td>The value of the <emph role="infoset-property">base URI</emph> property</td>
    </tr>
    <tr>
      <td valign="top"><code>$children</code></td>
      <td>The sequence of nodes constructed from the information
items found in the <emph role="infoset-property">children</emph>
property.</td>
    </tr>
  </tbody>
</table>

<p>To construct the value of the <code>$children</code> argument, for
each element, processing instruction, comment, and maximal sequence of
adjacent characater information items found in the
<emph role="infoset-property">children</emph> property, a corresponding
Element, Processing Instruction, Comment, and Text node is constructed
and that sequence of nodes is used as the value. If present among the
<emph role="infoset-property">children</emph>, the
<emph role="infoset-property">document type declaration</emph> information item
is ignored.
</p>

<note><p>There is no way to determine what DTD might apply to the data model.
See <specref ref="Issue-0042"/>.</p></note>

<!-- ======================================================================
<p>A document node is constructed from a Document Information
Item by the <emph>infoitem-to-document-node</emph> function:</p>

<p role="definition" id="DocumentItem">
/* Accessors for document information items: */
function infoset-document-children(<loc href="#DocumentItem">DocumentItem</loc> $ii)
         returns (<loc href="#ElementItem">ElementItem</loc> | <loc href="#ProcessingInstructionItem">ProcessingInstructionItem</loc>
                  | <loc href="#CommentItem">CommentItem</loc> | DocTypeItem)*
function infoset-document-base-uri(<loc href="#DocumentItem">DocumentItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:anyURI</loc>
</p>

<p role="definition" id="infoitem-to-document-node">
define function infoitem-to-document-node(<loc href="#DocumentItem">DocumentItem</loc> $ii)
       returns <loc href="#DocumentNode">DocumentNode</loc>
{
  let $kids:= <loc href="#collapse-text-nodes">collapse-text-nodes</loc>(<loc href="#sequence-map">sequence-map</loc>(
        infoitem-to-node, infoset-document-children($ii)))
  return <loc href="#DocumentNode">dm:document-node</loc>(infoset-document-base-uri($ii),$kids)
}
</p>

  <p>The <code><loc href="#collapse-text-nodes">collapse-text-nodes</loc></code>
  function synthesizes a single text node from multiple text nodes.  The
  <code><loc href="#sequence-map">sequence-map</loc></code> function
  applies its first function argument to each member of its second
  sequence argument and returns a new sequence containing the result
  of applying the function to each member of the sequence.  In the pseudo-code
  above, <code>infoitem-to-node</code> is applied to each child of the
  <emph role="info-item">document information item</emph>
  <emph>$ii</emph> and a new sequence of children nodes is constructed,
  each of which is a <emph>Node</emph>.  The constructor
  <function>document-node</function> constructs the document node in the data
  model.</p>

  <p role="definition" id="sequence-map">
function sequence-map(<emph>function</emph> $map, <emph>Item</emph>* $seq)
         returns <emph>Item</emph>*</p>

  <p>The <emph>infoitem-to-node</emph> function maps an information item
  to a sequence of zero or one node.</p>

  <p id="infoitem-to-node" role="definition">
define function infoitem-to-node(<loc href="#InfoItem">InfoItem</loc> $ii) returns Node?
{
  return
    if (infoitem-kind($ii) = "element") then
      <loc href="#infoitem-to-element-node">infoitem-to-element-node</loc>($ii)
    else if (infoitem-kind($ii) = "character") then
      <loc href="#infoitem-to-single-character-text-node">infoitem-to-single-character-text-node</loc>($ii)
    else if (infoitem-kind($ii) = "processing-instruction") then
      if (not(ignore-processing-instructions)) then
        <loc href="#infoitem-to-processing-instruction-node">infoitem-to-processing-instruction-node</loc>($ii)
      else <loc href="#empty-sequence">empty-sequence</loc>()
    else if (infoitem-kind($ii) = "comment") then
      if (not(ignore-comments)) then
        <loc href="#infoitem-to-comment-node">infoitem-to-comment-node</loc>($ii)
      else <loc href="#empty-sequence">empty-sequence</loc>()
    else
      <loc href="#empty-sequence">empty-sequence</loc>()
}</p>
====================================================================== -->

</div3>

<div3 id="DocumentNodeDM2IS">
  <head>Data Model to Infoset Mapping</head>

<p>The mapping of the data model to the XML Information Set maps a
Document Node to a <emph role="info-item">document information item</emph>.
The properties of the <emph role="info-item">document information item</emph>
are constructed as follows:</p>

<table border="1" cellspacing="0" summary="PSVI mapping summary">
  <colgroup>
    <col width="35%"/>
    <col width="65%"/>
  </colgroup>
  <thead>
    <tr>
      <td>Property</td>
      <td>Value:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><emph role="infoset-property">base URI</emph></td>
      <td>The value returned by the <function>base-uri</function> accessor</td>
    </tr>
    <tr>
      <td valign="top"><emph role="infoset-property">children</emph></td>
      <td>The sequence of information items constructed from the nodes
returned by the <function>children</function> accessor. In other
words, for each node returned by the <function>children</function>
accessor, a corresponding information item is constructed and that
sequence of information items is used as the value for the
<emph role="infoset-property">children</emph> property.</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">notations</emph></td>
      <td rowspan="6">The values of these properties are implementation-defined but
must be consistent with the rest of InfoSet constructed.</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">unparsed entities</emph></td>
    </tr>
    <tr>
      <td><emph role="infoset-property">character encoding scheme</emph></td>
    </tr>
    <tr>
      <td><emph role="infoset-property">standalone</emph></td>
    </tr>
    <tr>
      <td><emph role="infoset-property">version</emph></td>
    </tr>
    <tr>
      <td><emph role="infoset-property">all declarations processed</emph></td>
    </tr>
  </tbody>
</table>

<note>
<p>Since Document Nodes are more permissive than
<emph role="info-item">document information item</emph>s, the
resulting InfoSet may be invalid.</p>
</note>

</div3>

</div2>


<div2 id="ElementNode">
  <head>Elements</head>

<!--
   <table role="node-summary">
    <thead>
      <tr><td>Element Node accessors</td><td>possible values</td></tr>
    </thead>
    <tbody>
      <tr><td><function>node-kind</function></td>    <td>"element"</td></tr>
      <tr><td><function>node-name</function></td>    <td>xs:QName</td></tr>
      <tr><td><function>base-uri</function></td>     <td>xs:anyURI</td></tr>
      <tr><td><function>string-value</function></td> <td>xs:string</td></tr>
      <tr><td><function>typed-value</function></td>  <td>sequence of zero or more atomic values</td></tr>
      <tr><td><function>parent</function></td>       <td>sequence of zero or one element or document node</td></tr>
      <tr><td><function>children</function></td>     <td>sequence of zero or more element nodes,
                                zero or more processing instruction nodes,
                                zero or more comment nodes, and zero or more
                                text nodes</td></tr>
      <tr><td><function>attributes</function></td>   <td>sequence of zero or more attribute nodes</td></tr>
      <tr><td><function>namespaces</function></td>   <td>sequence of zero or more namespace nodes</td></tr>
      <tr><td><function>type</function></td>         <td>xs:QName</td></tr>
      <tr><td><function>unique-ID</function></td>    <td>zero or one xs:ID</td></tr>
    </tbody>
  </table>
-->

<div3 id="ElementNodeOverview">
  <head>Overview</head>

  <p>Element nodes encapsulate XML elements. Elements have the following properties:</p>

  <ulist>
  <item><p><emph role="dm-node-property">base-uri</emph>, possibly empty.
  </p></item>
  <item><p><emph role="dm-node-property">node-name</emph>
  </p></item>
  <item><p><emph role="dm-node-property">parent</emph>
  </p></item>
  <item><p><emph role="dm-node-property">type</emph>, possibly empty
  </p></item>
  <item><p><emph role="dm-node-property">children</emph>, possibly empty
  </p></item>
  <item><p><emph role="dm-node-property">attributes</emph>, possibly empty
  </p></item>
  <item><p><emph role="dm-node-property">namespaces</emph>, possibly empty
  </p></item>
<!--
  <item><p><emph role="dm-node-property">unique-id</emph>, possibly empty
  </p></item>
-->
  </ulist>

<!--
  <p>
  Element nodes encapsulate XML elements.
  The information associated with an element node includes the following.
  </p>
  <ulist>
  <item><p>
the <emph role="dm-node-property">expanded-QName</emph> of the element as an xs:QName
  </p></item>
  <item><p>
the <emph role="dm-node-property">base-uri</emph> of the element as an xs:anyURI
  </p></item>
  <item><p>
the <emph role="dm-node-property">type</emph> of the element as an xs:QName
  </p></item>
  <item><p>
the <emph role="dm-node-property">children</emph> of the element as a sequence of zero or more element, processing instruction, comment or text nodes
  </p></item>
  <item><p>
the <emph role="dm-node-property">parent</emph> of the element as a sequence of zero or one element or document node
  </p></item>
  <item><p>
the <emph role="dm-node-property">attributes</emph> of the element as a sequence of zero or more attribute nodes
  </p></item>
  <item><p>
the <emph role="dm-node-property">namespaces</emph> of the element as a sequence of zero or more namespace nodes
  </p></item>
  <item><p>
the <emph role="dm-node-property">unique ID</emph> of the element (if any) as a sequence of zero or one xs:ID
  </p></item>
  </ulist>

  <p>The above information associated with an element node is set
  during construction and can be accessed later via the accessor functions.</p>
-->

  <p>Element nodes must satisfy the following constraints.</p>

  <olist>
  <item><p>An element node's children may not contain two consecutive text
  nodes. Consecutive text nodes are collapsed by the
  element constructor into one text node.
  </p></item>
  <item><p>If a node <emph>N</emph> is a child of an element <emph>E</emph>,
then the parent of <emph>N</emph> must be <emph>E</emph>.</p></item>
  <item><p>If a node <emph>N</emph> has a parent element <emph>E</emph>, then
<emph>N</emph> must be among the children of <emph>E</emph>.</p></item>
  <item><p>Every child of an element must be distinct.</p></item>
  <item><p>The attributes of an element must have distinct names.
  </p></item>
  <item><p>The namespace nodes of an element must have distinct names.
At most one of the namespace nodes of an element has no name (this is the
default namespace). A namespace node whose namespace URI is the zero-length
string must have no name. No namespace node may have the name "<code>xmlns</code>".
  </p></item>
  </olist>

  <p>The element node constructor assures that the first three constraints are
satisfied.
  </p>

<note>
<p>The data model does not enforce a constraint that the namespaces of an
element must be a superset of the namespaces of its parent, nor does it
enforce a constraint that the namespaces of an element must include
namespace nodes for each of the namespace URIs used in the element name and
the names of its attributes, or of namespace URIs used in the content of
elements and attributes of type xs:QName. Applications of the data model
(such as XSLT and XQuery) may enforce such constraints in particular
circumstances, but these constraints are not part of the data model.
</p>
</note>

</div3>

<div3 id="ElementNodeConstructor">
<head>Constructor</head>

<p id="element-node">An element node can be constructed using
<function>element-node</function> which returns a new node with unique identity,
distinct from all other nodes.</p>

<p>The constructor takes an expanded-QName, a sequence of namespace
nodes, a sequence of attribute nodes, a sequence of child nodes,
the node's type, and an optional base URI as arguments.</p>

<example role="signature">
  <proto class="dm" name="element-node" return-type="ElementNode" returnEmptyOk="no">
    <arg name="qname" type="xs:QName"/>
    <arg name="nsnodes" type="NamespaceNode" seq="yes" emptyOk="yes"/>
    <arg name="attrnodes" type="AttributeNode" seq="yes" emptyOk="yes"/>
    <arg name="children" type="Node" seq="yes" emptyOk="yes"/>
    <arg name="type" type="xs:QName"/>
  </proto>
</example>

<example role="signature">
  <proto class="dm" name="element-node" return-type="ElementNode" returnEmptyOk="no">
    <arg name="qname" type="xs:QName"/>
    <arg name="nsnodes" type="NamespaceNode" seq="yes" emptyOk="yes"/>
    <arg name="attrnodes" type="AttributeNode" seq="yes" emptyOk="yes"/>
    <arg name="children" type="Node" seq="yes" emptyOk="yes"/>
    <arg name="type" type="xs:QName"/>
    <arg name="base-uri" type="xs:anyURI"/>
  </proto>
</example>

<p>The sequence of nodes passed as <code>$children</code>
must consist only of element, processing instruction,
comment, and text nodes.</p>

<p>If consecutive text nodes are specified as the children of an element
node they are collapsed into one text node whose string value is the
concatenation of the string values of the consecutive text nodes.</p>

<p>To guarantee that the parent-child relationship is invertible,
(i.e. that the parent of any child of a node is itself and that any
node that has a parent is among its parent's children),
the element constructors logically create a copy of all of their
namespace, attribute, and children arguments and set the parent property
of these nodes to the newly created element node. As long as the parent-child
constraint is satisfied, an implementation of the data model may choose to
use specialized techniques to avoid creating physical copies of the
arguments to an element constructor.  See <specref ref="Issue-0052"/>.</p>

<p>The data model permits element nodes without parents.
In fact element nodes created by the element node constructor never
have parents unless they are enclosed in other node constructors.
Such nodes may represent partial results during expression processing.
</p>
</div3>

<div3 id="ElementNodeAccessors">
  <head>Accessors</head>

<table border="1" cellspacing="0" summary="Accessor summary">
  <thead>
    <tr>
      <td>Accessor</td>
      <td>Returns:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><function>base-uri</function></td>
      <td role="D">The base URI of the element or its parent</td>
    </tr>
    <tr>
      <td><function>node-kind</function></td>
      <td role="D">"<code>element</code>"</td>
    </tr>
    <tr>
      <td><function>node-name</function></td>
      <td role="D">The QName of the element</td>
    </tr>
    <tr>
      <td><function>parent</function></td>
      <td role="D">The parent element or document node</td>
    </tr>
    <tr>
      <td><function>string-value</function></td>
      <td role="D">The concatenation of the string-values of all the text node
descendants of the element in document order</td>
    </tr>
    <tr>
      <td><function>typed-value</function></td>
      <td role="D">The typed value of the node</td>
    </tr>
    <tr>
      <td><function>type</function></td>
      <td role="D">Returns the QName of the element's globally declared type or the
empty sequence if the element's type is locally declared or is unavailable</td>
    </tr>
    <tr>
      <td><function>children</function></td>
      <td role="D">The children of the element node</td>
    </tr>
    <tr>
      <td><function>attributes</function></td>
      <td role="D">The attributes of the element node</td>
    </tr>
    <tr>
      <td><function>namespaces</function></td>
      <td role="D">The namespaces of the element node</td>
    </tr>
<!--
    <tr>
      <td><function>unique-ID</function></td>
      <td role="D">The value of the attribute or element of type ID.</td>
    </tr>
-->
  </tbody>
</table>

<p>The <function>base-uri</function> accessor returns the
<emph role="dm-node-property">base-uri</emph> property of the element node,
if it exists. If it does not exist, the base URI of the element's parent is
returned. In other words, the value returned must be the same as the value
of <function>base-uri</function>(<function>parent</function>()) although implemetations
are not required to implement it in that way.</p>

<p>The accessors <function>namespaces</function> and <function>attributes</function>
return the same set of namespace and attribute nodes (respectively) that
were supplied to the constructor, but they are not constrained to return them
in the same order. See <specref ref="Issue-0086"/></p>

<p>The <function>parent</function> accessor returns the empty sequence
if the element has no parent.</p>

<p>If the element node's type is <code>xs:anyType</code>, the
<function>typed-value</function> accessor returns the node's string value
as <code>xs:anySimpleType</code>. If the type is a complex type with complex content,
invoking <function>typed-value</function> raises an error.</p>

<p>The <function>typed-value</function> accessor returns the typed-value
of the node, which is a sequence of zero or more atomic values.
The typed-value is closely related to the node's string-value and its
type. For instance when the node's string-value is "3.14" and its type
is <code>xs:decimal</code>, the typed-value is a sequence containing the
atomic value 3.14 of type decimal.
In fact, when the type is an atomic type, typed-value is always the
atomic-value constructed from the string-value and the type.</p>

<p>In the general case, <function>typed-value</function> constructs a
sequence of atomic values. These values are derived from the
string-value of the element and its type, in such a way as to be
consistent with validation.
</p>

<!-- ======================================================================
The accessor <function>node-kind</function> returns the string constant "element".
The accessor <function>parent</function> returns a sequence containing zero
or one nodes representing the parent of the element node.
The accessor <function>base-uri</function> returns a sequence containing zero
or one uri references representing the base URI of the element node.
The accessor <function>unique-ID</function> returns a sequence containing
the unique ID of the element if it has one; otherwise, it returns
the empty sequence.</p>

  <p role="definition">function dm:node-kind(<loc href="#ElementNode">ElementNode</loc> $n)  returns <loc href="#dt-atomic-value">xs:string</loc>
function dm:parent(<loc href="#ElementNode">ElementNode</loc> $n)
         returns (<loc href="#ElementNode">ElementNode</loc> | <loc href="#DocumentNode">DocumentNode</loc>)?
function dm:base-uri(<loc href="#ElementNode">ElementNode</loc> $n)   returns <loc href="#dt-atomic-value">xs:anyURI</loc>?
function dm:unique-ID(<loc href="#ElementNode">ElementNode</loc> $n)  returns <loc href="#dt-atomic-value">xs:ID</loc>?
</p>

  <p>The accessor <function>string-value</function> returns the string-value
  of the element which is defined to be the concatenation
  of the string-values of all text-node descendants of the element node in
  document order.</p>
====================================================================== -->

<ednote><edtext>The <function>string-value</function> of a
node and the result of casting the <function>typed-value</function> of
a node to a string may give different results under this definition.
The issue of whether to allow or possibly mandate that
<function>string-value</function> return the same result as the
string-value of the typed value is still under discussion. See
<specref ref="Issue-0079"/>. </edtext></ednote>

<!-- ======================================================================
 <p>
  The accessor function <function>typed-value</function> returns the typed-value
  of the node, which is a sequence of zero or more atomic values.
  The typed-value is closely related to the node's string-value and its
  type. For instance when the node's string-value is "3.14" and its type
  is xs:decimal the typed-value is a sequence containing the
  atomic value 3.14 of type decimal.
  In fact, when the type is an atomic type, typed-value is the
  atomic-value constructed from the string-value and the type.

  In the general case typed-value is defined via the function
  <function>atomic-value-sequence</function>, which constructs a sequence
  of atomic values from a string and a type, in such a way to
  be consistent with validation.
  </p>

  <p role="definition">
define function dm:typed-value(<loc href="#ElementNode">ElementNode</loc> $n)
       returns <loc href="#AtomicValue">AtomicValue</loc>*
{
  return <loc href="#ctor_atomic-value-sequence">dm:atomic-value-sequence</loc>(dm:string-value($n),
                                  dm:type($n))
}
</p>

  <p>
  It is noted that when the element node's type is xs:anyType then
  <function>typed-value</function> returns the node's string-value as xs:anySimpleType.
  If the type is a complex type with complex content
  then invoking <function>typed-value</function> raises an error.</p>
====================================================================== -->

</div3>

<div3 id="ElementNodePSVI2DM">
  <head>PSVI to Data Model Mapping</head>

  <p>When a data model fragment is created from the PSVI, an
  <emph role="info-item">element information item</emph> is mapped
  to an Element Node. The precise transformation is described
  by specifying the PSVI property corresponding to each argument
  of the element node constructor.</p>

<glist>
<gitem>
  <label><code>$qname</code></label>
  <def>
    <p>An <code>xs:QName</code> constructed from the
<emph role="infoset-property">local name</emph> property
and the
<emph role="infoset-property">namespace name</emph> property</p>
  </def>
</gitem>
<gitem>
  <label><code>$nsnodes</code></label>
  <def><p>A set of Namespace Nodes constructed from the
<emph role="info-item">namespace information items</emph>
appearing in the <emph role="infoset-property">in-scope namespaces</emph>
property.</p>
<p>Implementations may provide mechanisms to allow
some or all of the namespaces in the
<emph role="infoset-property">in-scope namespaces</emph> property
to be discarded from the data model.</p>
  </def>
</gitem>
<gitem>
  <label><code>$attrnodes</code></label>
  <def><p>A set of Attribute Nodes constructed from the
<emph role="info-item">attribute information items</emph>
appearing in the <emph role="infoset-property">attributes</emph>
property. This includes all of the <quote>special</quote> attributes
(<att>xml:lang</att>, <att>xml:space</att>, <att>xsi:type</att>, etc.)
but does not include namespace declarations (because they are not attributes).</p>
<p>The special nature of <att>xsi:nil</att> is still being discussed,
see <specref ref="Issue-0071"/>.</p>
  </def>
</gitem>
<gitem>
  <label><code>$children</code></label>
  <def>
  <ulist>
  <item><p>If the <emph role="infoset-property">schema normalized value</emph>
PSVI property exists, a single text node whose string value is the value
of that property.</p>
  </item>
  <item><p>Otherwise, the sequence of nodes constructed in the following way
from the information items found in the <emph role="infoset-property">children</emph>
property: for
each element, processing instruction, comment, and maximal sequence of
adjacent characater information items found in the
<emph role="infoset-property">children</emph> property, a corresponding
Element, Processing Instruction, Comment, and Text node is constructed.</p>
<p>
Because the data model requires
that all general entities be expanded, there will never be
<emph role="info-item">unexpanded entity reference information item</emph>
children.</p>
  </item>
  </ulist>
  </def>
</gitem>
<gitem>
  <label><code>$type</code></label>
  <def><p>The <code>xs:QName</code> computed as described in
<specref ref="PSVI2Types"/>.
Note that if the type referenced would be a union type then
type refers to the member type that actually validated the
schema normalized value.</p>
  </def>
</gitem>
</glist>

<p>The <emph role="dm-node-property">unique ID</emph> of the element node
is an identifier optionally assigned by the user.  It corresponds to the
<emph role="infoset-property">normalized value</emph> property
of the <emph role="info-item">attribute information item</emph>
in the <emph role="infoset-property">attributes</emph>
property that has a type <emph>ID</emph>, if one exists.</p>

<note><p>Using this definition, only IDs declared in a DTD are effective.
See <specref ref="Issue-0004"/>.  Even so, this definition is not
backward compatible with XPath 1.0.  See <specref ref="Issue-0038"/>.
Furthermore, it doesn't even work as spec'd, see <specref ref="Issue-0044"/>.
</p></note>

<!-- ======================================================================
  <p>An element node is constructed from an
  <emph role="info-item">element information item</emph> by the
  <emph>infoitem-to-element-node</emph> function:</p>

  <p role="definition" id="ElementItem">
/* Accessors for element information items: */
function infoset-element-namespace-name(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:anyURI</loc>?
function infoset-element-local-name(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:string</loc>
function infoset-element-children(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#InfoItem">InfoItem</loc>*
function infoset-element-attributes(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#AttributeItem">AttributeItem</loc>*
function infoset-element-in-scope-namespaces(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#NamespaceItem">NamespaceItem</loc>*
function infoset-element-base-uri(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:anyURI</loc>

function psvi-element-validity(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns xs:string
function psvi-element-type-definition(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#ElementItem">ElementItem</loc>
function psvi-element-schema-normalized-value(<loc href="#ElementItem">ElementItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:string</loc>
  </p>

  <p id="infoitem-to-element-node" role="definition">
define function infoitem-to-element-node(<loc href="#ElementItem">ElementItem</loc> $ii)
       returns <loc href="#ElementNode">ElementNode</loc>
{
  let $qname:= <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>(
                 infoset-element-namespace-name($ii),
                 infoset-element-local-name($ii)) ,
      $baseuri := infoset-element-base-uri($ii),
      $nsnodes:= <loc href="#sequence-map">sequence-map</loc>(
                   <loc href="#infoitem-to-namespace-node">infoitem-to-namespace-node</loc>,
                   infoset-element-in-scope-namespaces($ii)) ,
      $attrnodes:= <loc href="#sequence-map">sequence-map</loc>(
                     <loc href="#infoitem-to-attribute-node">infoitem-to-attribute-node</loc>,
                     infoset-element-attributes($ii)) ,
      $kids:= <loc href="#collapse-text-nodes">collapse-text-nodes</loc>(<loc href="#sequence-map">sequence-map</loc>(
                <loc href="#infoitem-to-node">infoitem-to-node</loc>,
                infoset-element-children($ii))) ,
      $type:= infoitem-to-type($ii)
  return <loc href="#element-node">dm:element-node</loc>(
           $baseuri, $qname, $nsnodes, $attrnodes, $kids, $type)
}</p>
====================================================================== -->

</div3>

<div3 id="ElementNodeDM2IS">
  <head>Data Model to Infoset Mapping</head>

<p>The mapping of the data model to the XML Information Set maps an
Element Node to an <emph role="info-item">element information item</emph>.
The properties of the <emph role="info-item">element information item</emph>
are constructed as follows:</p>

<table border="1" cellspacing="0" summary="PSVI mapping summary">
  <colgroup>
    <col width="35%"/>
    <col width="65%"/>
  </colgroup>
  <thead>
    <tr>
      <td>Property</td>
      <td>Value:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><emph role="infoset-property">namespace name</emph></td>
      <td>The namespace name of the QName returned by the <function>node-name</function> accessor</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">local name</emph></td>
      <td>The local name of the QName returned by the <function>node-name</function> accessor</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">prefix</emph></td>
      <td>An appropriate namespace prefix, as described below</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">children</emph></td>
      <td>The sequence of information items constructed from the nodes
      returned by the <function>children</function> accessor. In other
      words, for each node returned by the
      <function>children</function> accessor, a corresponding
      information item is constructed and that sequence of information
      items is used as the value for the
      <emph role="infoset-property">namespace name</emph> property.</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">attributes</emph></td>
      <td>The sequence of <emph role="info-item">attribute information item</emph>s
constructed from the nodes returned by the <function>attributes</function> accessor.</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">in-scope namespaces</emph></td>
      <td>The sequence of <emph role="info-item">namespace information item</emph>s
constructed from the nodes returned by the <function>namespaces</function> accessor.</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">base URI</emph></td>
      <td>The value returned by the <function>base-uri</function> accessor</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">parent</emph></td>
      <td>The information item constructed from the node returned by the
<function>parent</function> accessor. If the node has no parent, the property
must be left absent and the resulting InfoSet will not be valid.</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">namespace attributes</emph></td>
      <td>The sequence of <emph role="info-item">namespace information item</emph>s
constructed from the nodes that are present in the difference between the
sequence of nodes returned by the <function>namespaces</function> accessor
on this element and the sequence of nodes returned by the
<function>namespaces</function> accessor of this element's
<function>parent</function>.</td>
    </tr>
  </tbody>
</table>

<p>An implementation must construct the value of the
<emph role="infoset-property">prefix</emph> property as if the following
algorithm was applied: if
the element has at least one namespace node whose namespace URI is the
same as the namespace name of the QName returned by the <function>node-name</function>
accessor, it returns the local part of the name of that namespace node or
the empty string if the namespace node has no name. If there are several
such namespace nodes, it chooses one of them arbitrarily. If there is no
such namespace node, it generates an arbitrary prefix that is distinct from
the <function>node-name</function> of any of the element's namespaces.</p>

<p>If a new prefix is generated, a corresponding
<emph role="info-item">namespace information item</emph>
must be added to the
<emph role="infoset-property">in-scope namespaces</emph> property of the
<emph role="info-item">element information item</emph>. The
<emph role="info-item">namespace information item</emph> must associate
the generated prefix with the namespace name of the QName returned by
the element's <function>node-name</function> accessor.</p>

<note><p>If the implementation has allowed in-scope namespaces to be discarded
from the data model, then these namespaces may need to be reintroduced when
creating an InfoSet in order to ensure that the InfoSet corresponds to a
document that is namespace well-formed as defined in [XML Namespaces]. </p>
</note>

<note><p>The algorithm used to calculate namespace attributes
will need to be adjusted to cater for XML Namespaces 1.1, which allows
the "undeclaration" of all namespaces, whether they have a
prefix or not.</p>
</note>

</div3>
</div2>


<div2 id="AttributeNode">
  <head>Attributes</head>

<!--
   <table role="node-summary">
    <thead>
      <tr><td>Attribute Node accessors</td><td>possible values</td></tr>
    </thead>
    <tbody>
      <tr><td><function>node-kind</function></td>    <td>"attribute"</td></tr>
      <tr><td><function>node-name</function></td>    <td>xs:QName</td></tr>
      <tr><td><function>base-uri</function></td>     <td>empty sequence</td></tr>
      <tr><td><function>string-value</function></td> <td>xs:string</td></tr>
      <tr><td><function>typed-value</function></td>  <td>sequence of zero or more atomic values</td></tr>
      <tr><td><function>parent</function></td>       <td>sequence of zero or one element nodes</td></tr>
      <tr><td><function>children</function></td>     <td>empty sequence</td></tr>
      <tr><td><function>attributes</function></td>   <td>empty sequence</td></tr>
      <tr><td><function>namespaces</function></td>   <td>empty sequence</td></tr>
      <tr><td><function>type</function></td>         <td>xs:QName</td></tr>
      <tr><td><function>unique-ID</function></td>    <td>empty sequence</td></tr>
    </tbody>
  </table>
-->

<div3 id="AttributeNodeOverview">
  <head>Overview</head>

  <p>Attribute nodes encapsulate XML attributes. Attributes have the
following properties:</p>

  <ulist>
  <item><p><emph role="dm-node-property">node-name</emph>
  </p></item>
  <item><p><emph role="dm-node-property">parent</emph>
  </p></item>
  <item><p><emph role="dm-node-property">type</emph>, possibly empty
  </p></item>
  </ulist>

<!--
  <p>
  Attribute nodes encapsulate XML attributes.
  The information associated with an attribute node includes the following.
  </p>
  <ulist>
  <item><p>
the <emph role="dm-node-property">expanded-QName</emph> of the attribute as an xs:QName
  </p></item>
  <item><p>
the <emph role="dm-node-property">string-value</emph> of the attribute as an xs:string
  </p></item>
  <item><p>
the <emph role="dm-node-property">type</emph> of the attribute as an xs:QName
  </p></item>
  <item><p>
the <emph role="dm-node-property">parent</emph> of the attribute as a sequence of zero or one element node
  </p></item>
  </ulist>
-->

  <p>The above information associated with an attribute node is set
  during construction and can be accessed later via the accessor functions.</p>

  <p>For convenience, the element node that owns this attribute is called
  its "parent" even though an attribute node is not a "child" of its
  parent element. </p>
</div3>

<div3 id="AttributeNodeConstructor">
  <head>Constructor</head>

<p id="attribute-node">An attribute node can be constructed using
<function>attribute-node</function> which returns a new node with unique
identity, distinct from all other nodes.</p>

<p>The constructor takes the attribute's expanded-QName, string value, and type.</p>

<example role="signature">
  <proto class="dm" name="attribute-node" return-type="AttributeNode" returnEmptyOk="no">
    <arg name="qname" type="xs:QName"/>
    <arg name="value" type="xs:string"/>
    <arg name="type" type="xs:QName"/>
  </proto>
</example>

  <p>Like all other node constructors, the attribute node
  constructor has the effect of creating a new node with a unique
  identity, distinct from all other nodes.  </p>

</div3>

<div3 id="AttributeNodeAccessors">
  <head>Accessors</head>

<table border="1" cellspacing="0" summary="Accessor summary">
  <thead>
    <tr>
      <td>Accessor</td>
      <td>Returns:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><function>base-uri</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>node-kind</function></td>
      <td role="D">"<code>attribute</code>"</td>
    </tr>
    <tr>
      <td><function>node-name</function></td>
      <td role="D">The QName of the attribute</td>
    </tr>
    <tr>
      <td><function>parent</function></td>
      <td role="D">The parent element node</td>
    </tr>
    <tr>
      <td><function>string-value</function></td>
      <td role="D">The value of the attribute</td>
    </tr>
    <tr>
      <td><function>typed-value</function></td>
      <td role="D">The typed value of the node</td>
    </tr>
    <tr>
      <td><function>type</function></td>
      <td role="D">Returns the QName of the attributes's globally declared type or the
empty sequence if the attribute's type is locally declared or is unavailable</td>
    </tr>
    <tr>
      <td><function>children</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>attributes</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>namespaces</function></td>
      <td role="D">()</td>
    </tr>
<!--
    <tr>
      <td><function>unique-ID</function></td>
      <td role="D">()</td>
    </tr>
-->
  </tbody>
</table>

<!--
  <p>The accessors <function>node-name</function>, <function>string-value</function>
  and <function>type</function> return the values passed to the constructor
  as arguments.
  </p>
-->

<ednote><edtext>The <function>string-value</function> of a
node and the result of casting the <function>typed-value</function> of
a node to a string may give different results under this definition.
The issue of whether to allow or possibly mandate that
<function>string-value</function> return the same result as the
string-value of the typed value is still under discussion. See
<specref ref="Issue-0079"/>. </edtext></ednote>

<!-- ======================================================================
  <p role="definition">
function dm:node-name(<loc href="#AttributeNode">AttributeNode</loc> $n)    returns <loc href="#dt-atomic-value">xs:QName</loc>
function dm:string-value(<loc href="#AttributeNode">AttributeNode</loc> $n) returns <loc href="#dt-atomic-value">xs:string</loc>
function dm:type(<loc href="#AttributeNode">AttributeNode</loc> $n)         returns <loc href="#dt-atomic-value">xs:QName</loc></p>

  <p>
  The accessor <function>node-kind</function> returns the string constant
  "attribute".
  The accessor <function>parent</function> returns a sequence containing zero
  or one element nodes representing the parent of the attribute node.</p>

  <p role="definition">
function dm:node-kind(<loc href="#AttributeNode">AttributeNode</loc> $n) returns <loc href="#dt-atomic-value">xs:string</loc>
function dm:parent(<loc href="#AttributeNode">AttributeNode</loc> $n)    returns <loc href="#ElementNode">ElementNode</loc>?</p>

  <p>
  The accessor function <function>typed-value</function> returns the typed-value
  of the node, which is a sequence of zero or more atomic values.
  The typed-value is closely related to the node's string-value and its
  type. It is defined via the function
  <function>atomic-value-sequence</function>, which constructs a sequence
  of atomic values from a string and a type, in such a way to
  be consistent with validation.
  In particular when the type is an atomic type, typed-value is the
  atomic-value constructed from the string-value and the type.
  When the type of the attribute is xs:anySimpleType
  then <function>typed-value</function> returns its string value as
  xs:anySimpleType.
  </p>

  <p role="definition">
define function dm:typed-value(<loc href="#AttributeNode">AttributeNode</loc> $n)
                returns <loc href="#AtomicValue">AtomicValue</loc>*
{
  return <loc href="#ctor_atomic-value-sequence">dm:atomic-value-sequence</loc>(dm:string-value($n),
                                  dm:type($n))
}
</p>

  <p>
  The accessors <function>base-uri</function>, <function>attributes</function>,
  <function>children</function>, <function>namespaces</function> and
  <function>unique-ID</function>
  applied to an attribute node always return the empty sequence.</p>
====================================================================== -->

</div3>

<div3 id="AttributeNodePSVI2DM">
<head>PSVI to Data Model Mapping</head>

<p>When a data model fragment is created from the PSVI an
<emph role="info-item">attribute information item</emph> is mapped
to an Attribute Node. The precise transformation is described
by specifying the PSVI property corresponding to each argument
of the attribute node constructor.</p>

<glist>
<gitem>
  <label><code>$qname</code></label>
  <def>
    <p>An <code>xs:QName</code> constructed from the
<emph role="infoset-property">local name</emph> property
and the
<emph role="infoset-property">namespace name</emph> property</p>
  </def>
</gitem>
<gitem>
  <label><code>$value</code></label>
  <def>
  <ulist>
  <item><p>The <emph role="infoset-property">schema normalized value</emph>
PSVI property if that exists, or
  </p></item>
  <item><p>the <emph role="infoset-property">normalized value</emph> property.
  </p></item>
  </ulist>
  </def>
</gitem>
<gitem>
  <label><code>$type</code></label>
  <def><p>The <code>xs:QName</code> computed as described in
<specref ref="PSVI2Types"/>.
Note that if the type referenced would be a union type then
type refers to the member type that actually validated the
schema normalized value.</p>
  </def>
</gitem>
</glist>

<!-- ======================================================================
<p>The <emph role="dm-node-property">expanded-QName</emph> of the
attribute is obtained as follows.
Its local part corresponds to the
<emph role="infoset-property">local name</emph> property.
Its namespace URI corresponds to the
<emph role="infoset-property">namespace name</emph> property.</p>

  <p>The <emph role="dm-node-property">string value</emph> of the attribute
  node corresponds to the following:
  </p>
  <ulist>
  <item><p>
  the <emph role="infoset-property">schema normalized value</emph>
  PSVI property if that exists, or
  </p></item>
  <item><p>
  the <emph role="infoset-property">normalized value</emph> property.
  </p></item>
  </ulist>

  <p>The <emph role="dm-node-property">type</emph> of an attribute is an
  expanded-QName whose namespace and local name is computed as described
  in <specref ref="PSVI2Types"/>.
  It is noted that if the type referenced would be a union type then
  type refers to the member type that actually validated the
  schema normalized value.
  For an attribute in a well-formed document with no associated schema,
  type corresponds to xs:anySimpleType. Consequently the typed-value of such
  an attribute is its string value as an instance of xs:anySimpleType.
  </p>

  <p>An attribute node is constructed from an
  <emph role="info-item">attribute information item</emph>
  by the <emph>infoitem-to-attribute-node</emph> function:</p>

  <p role="definition" id="AttributeItem">
/* Accessors for attribute information items: */
function infoset-attribute-namespace-name(<loc href="#AttributeItem">AttributeItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:anyURI</loc>?
function infoset-attribute-local-name(<loc href="#AttributeItem">AttributeItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:string</loc>
function infoset-attribute-normalized-value(<loc href="#AttributeItem">AttributeItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:string</loc>
function infoset-attribute-owner-element(<loc href="#AttributeItem">AttributeItem</loc> $ii)
         returns <loc href="#ElementItem">ElementItem</loc>

function psvi-attribute-validity(<loc href="#AttributeItem">AttributeItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:string</loc>
function psvi-attribute-type-definition(<loc href="#AttributeItem">AttributeItem</loc> $ii)
         returns <loc href="#ElementItem">ElementItem</loc>
function psvi-attribute-schema-normalized-value(
           <loc href="#AttributeItem">AttributeItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:string</loc>
  </p>

  <p role="definition" id="infoitem-to-attribute-node">
define function infoitem-to-attribute-node(<loc href="#AttributeItem">AttributeItem</loc> $ii)
       returns <loc href="#AttributeNode">AttributeNode</loc>
{
  let $qname:= <loc href="#xf_QName-from-uri">xf:QName-from-uri</loc>(
                 infoset-attribute-namespace-name($ii),
                 infoset-attribute-local-name($ii)),
      $type:= infoitem-to-type($ii)
  return <loc href="#attribute-node">dm:attribute-node</loc>($qname,
           infoset-attribute-normalized-value($ii), $type)
} </p>

  <ednote><edtext>JM: Update the above to accommodate the possibility of
  schema-less and DTD validation.</edtext></ednote>
====================================================================== -->
</div3>


<div3 id="AttributeNodeDM2IS">
  <head>Data Model to Infoset Mapping</head>

<p>The mapping of the data model to the XML Information Set maps an
Attribute Node to an <emph role="info-item">attribute information item</emph>.
The properties of the corresponding
<emph role="info-item">attribute information item</emph> are constructed
as follows:
</p>

<table border="1" cellspacing="0" summary="PSVI mapping summary">
  <colgroup>
    <col width="35%"/>
    <col width="65%"/>
  </colgroup>
  <thead>
    <tr>
      <td>Property</td>
      <td>Value:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><emph role="infoset-property">namespace name</emph></td>
      <td>The namespace name of the QName returned by the <function>node-name</function> accessor</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">local name</emph></td>
      <td>The local name of the QName returned by the <function>node-name</function> accessor</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">prefix</emph></td>
      <td>An appropriate namespace prefix, as described below</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">normalized value</emph></td>
      <td>The value returned by the <function>string-value</function> accessor</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">owner element</emph></td>
      <td>The information item constructed from the node returned by the
<function>parent</function> accessor. If the node has no parent, the property
must be left absent and the resulting InfoSet will not be valid.</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">specified</emph></td>
      <td rowspan="3">The values of these properties are implementation-defined but
must be consistent with the rest of InfoSet constructed.</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">attribute type</emph></td>
    </tr>
    <tr>
      <td><emph role="infoset-property">references</emph></td>
    </tr>
  </tbody>
</table>

<!-- ======================================================================
  <table role="infoset-mapping">
    <colgroup><col width="35%"/><col width="65%"/></colgroup>
    <thead>
      <tr><td>Attribute Information Item properties</td><td> </td></tr>
    </thead>
    <tbody>
      <tr><td>
        <emph role="infoset-property">namespace name</emph>
      </td><td>
        <emph>xf:get-namespace-uri(dm:node-name($n))</emph>
      </td></tr>
      <tr><td>
        <emph role="infoset-property">local name</emph>
      </td><td>
        <emph>xf:get-local-name(dm:node-name($n))</emph>
      </td></tr>
      <tr><td>
        <emph role="infoset-property">prefix</emph>
      </td><td>
        <emph>get-namespace-prefix(dm:parent($n), xf:get-namespace-uri(dm:node-name($n)))</emph>
      </td></tr>
      <tr><td>
        <emph role="infoset-property">normalized value</emph>
      </td><td>
        <function>string-value($n)</function>
      </td></tr>
      <tr><td>
        <emph role="infoset-property">owner element</emph>
      </td><td>
        <function>nodes-to-infoitems(dm:parent($n))</function>
      </td></tr>
    </tbody>
  </table>
====================================================================== -->

<p>If the attribute node has a parent, an implementation must construct
the value of the
<emph role="infoset-property">prefix</emph> property in the following 
way: if
the attribute has a parent, in the same way that a prefix would be
constructed for that element, otherwise a prefix is chosen arbitrarily, and no
attempt is made to associate the prefix with the namespace URI.</p>

<!-- ======================================================================
  <p>
  The values of the properties
  <emph role="infoset-property">specified</emph>,
  <emph role="infoset-property">attribute type</emph> and
  <emph role="infoset-property">references</emph>
  are implementation-defined.
  </p>
====================================================================== -->
</div3>

</div2>

<div2 id="NamespaceNode">
  <head>Namespaces</head>

<!--
   <table role="node-summary">
    <thead>
      <tr><td>Namespace Node accessors</td><td>possible values</td></tr>
    </thead>
    <tbody>
      <tr><td><function>node-kind</function></td>    <td>"namespace"</td></tr>
      <tr><td><function>node-name</function></td>    <td>sequence of zero or one xs:QName</td></tr>
      <tr><td><function>base-uri</function></td>     <td>empty sequence</td></tr>
      <tr><td><function>string-value</function></td> <td>xs:string</td></tr>
      <tr><td><function>typed-value</function></td>  <td>empty sequence</td></tr>
      <tr><td><function>parent</function></td>       <td>empty sequence</td></tr>
      <tr><td><function>children</function></td>     <td>empty sequence</td></tr>
      <tr><td><function>attributes</function></td>   <td>empty sequence</td></tr>
      <tr><td><function>namespaces</function></td>   <td>empty sequence</td></tr>
      <tr><td><function>type</function></td>         <td>empty sequence</td></tr>
      <tr><td><function>unique-ID</function></td>    <td>empty sequence</td></tr>
    </tbody>
  </table>
-->

<div3 id="NamespaceNodeOverview">
  <head>Overview</head>

  <p>Namespace nodes encapsulate XML namespaces. Namespaces have the
following properties:</p>

  <ulist>
  <item><p><emph role="dm-node-property">prefix</emph>, possibly empty.
  </p></item>
  <item><p><emph role="dm-node-property">uri</emph>
  </p></item>
  </ulist>

<!--
  <p>
  Namespace nodes encapsulate XML namespaces.
  The information associated with a namespace node includes the following.
  </p>
  <ulist>
  <item><p>
the <emph role="dm-node-property">prefix</emph> of the namespace as an xs:string
  </p></item>
  <item><p>
the <emph role="dm-node-property">uri</emph> of the namespace as an xs:anyURI
  </p></item>
  </ulist>
-->

<p>In XPath 1.0, namespace nodes were directly accessible by applications, by
means of the namespace axis. In XPath 2.0 the namespace axis is deprecated,
and it is not available at all in XQuery 1.0. XPath 2.0 implementations are
not required to expose the namespace axis, though they may do so if they
wish to offer backwards compatibility. The information held in namespace
nodes is instead made available to applications using two functions defined
in [Functions and Operators], namely xf:get-in-scope-namespaces and
xf:get-namespace-uri-for-prefix. Certain properties of namespace nodes are
not exposed by these functions: in particular, properties related to the
identity of namespace nodes, their parentage, and their position in document
order. Implementations that do not expose the namespace axis can therefore
avoid the overhead of maintaining this information.</p>

  <p>The above information associated with a namespace node is set
  during construction and can be accessed later via the accessor functions.</p>

  <p>Each element node has
an associated set of namespace nodes corresponding to its in-scope
namespaces, and that element node acts as the parent of those namespace
nodes.</p>

</div3>

<div3 id="NamespaceNodeConstructor">
  <head>Constructor</head>

<p id="namespace-node">A namespace node can be constructed using
<function>namespace-node</function> which returns a new node with unique
identity, distinct from all other nodes.</p>

<p>The constructor takes a namespace prefix and the URI of the
namespace being declared.</p>

<example role="signature">
  <proto class="dm" name="namespace-node" return-type="NamespaceNode" returnEmptyOk="no">
    <arg name="prefix" type="xs:string" emptyOk="yes"/>
    <arg name="uri" type="xs:string"/>
  </proto>
</example>

<p>The namespace prefix may be the empty sequence. If the URI is the
zero-length string, the prefix must be the empty sequence.</p>

</div3>

<div3 id="NamespaceNodeAccessors">
  <head>Accessors</head>

<table border="1" cellspacing="0" summary="Accessor summary">
  <thead>
    <tr>
      <td>Accessor</td>
      <td>Returns:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><function>base-uri</function></td>
      <td role="D"><function>fn:error</function>, see <specref ref="Issue-0087"/>.</td>
    </tr>
    <tr>
      <td><function>node-kind</function></td>
      <td role="D">"<code>namespace</code>"</td>
    </tr>
    <tr>
      <td><function>node-name</function></td>
      <td role="D">A QName with the namespace prefix in the local-name and an empty URI</td>
    </tr>
    <tr>
      <td><function>parent</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>string-value</function></td>
      <td role="D">The namespace name (URI) of the node</td>
    </tr>
    <tr>
      <td><function>typed-value</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>type</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>children</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>attributes</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>namespaces</function></td>
      <td role="D">()</td>
    </tr>
<!--
    <tr>
      <td><function>unique-ID</function></td>
      <td role="D">()</td>
    </tr>
-->
  </tbody>
</table>

<!-- ======================================================================
  <p>The accessor <function>node-name</function> returns the namespace prefix
  as the local-part of an xs:QName (whose namespace URI is the empty sequence).
  The accessor <function>string-value</function> returns the namespace uri as
  an xs:string.</p>

  <p role="definition">
function dm:node-name(<loc href="#NamespaceNode">NamespaceNode</loc> $n)    returns <loc href="#dt-atomic-value">xs:QName</loc>?
function dm:string-value(<loc href="#NamespaceNode">NamespaceNode</loc> $n) returns <loc href="#dt-atomic-value">xs:string</loc>
  </p>

  <p> The accessor <function>node-kind</function> returns the string constant
  "namespace". </p>

  <p role="definition">
function dm:node-kind(<loc href="#NamespaceNode">NamespaceNode</loc> $n) returns <loc href="#dt-atomic-value">xs:string</loc> </p>

  <p>The accessors <function>base-uri</function>, <function>typed-value</function>,
  <function>parent</function>, <function>children</function>, <function>attributes</function>,
  <function>namespaces</function>, <function>type</function> and
  <function>unique-ID</function> applied to a namespace node always return the empty
  sequence.</p>
====================================================================== -->
</div3>

<div3 id="NamespaceNodePSVI2DM">
  <head>PSVI to Data Model Mapping</head>

  <p>When a data model fragment is created from the PSVI a
  <emph role="info-item">namespace information item</emph> is mapped
  to a Namespace Node. The precise transformation is described
  by specifying the PSVI property corresponding to each argument
  of the attribute node constructor.</p>

<glist>
<gitem>
  <label><code>$prefix</code></label>
  <def>
    <p>The <emph role="infoset-property">prefix</emph> property.</p>
  </def>
</gitem>
<gitem>
  <label><code>$uri</code></label>
  <def>
    <p>The <emph role="infoset-property">namespace name</emph> property.</p>
  </def>
</gitem>
</glist>

<!-- ======================================================================
  <p>The <emph role="dm-node-property">prefix</emph> argument
  corresponds to the <emph role="infoset-property">prefix</emph> property.</p>

  <p>The <emph role="dm-node-property">uri</emph> argument corresponds
  to the <emph role="infoset-property">namespace name</emph> property.</p>


  <p>A namespace node is constructed from a
  <emph role="info-item">namespace information item</emph> by the
  <emph>infoitem-to-namespace-node</emph> function:</p>

  <p role="definition" id="NamespaceItem">
function infoset-namespace-prefix(<loc href="#NamespaceItem">NamespaceItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:string</loc>?
function infoset-namespace-namespace-name(<loc href="#NamespaceItem">NamespaceItem</loc> $ii)
         returns <loc href="#dt-atomic-value">xs:string</loc>
  </p>

  <p role="definition" id="infoitem-to-namespace-node">
define function infoitem-to-namespace-node(<loc href="#NamespaceItem">NamespaceItem</loc> $ii)
       returns <loc href="#NamespaceNode">NamespaceNode</loc>
{
   return <loc href="#NamespaceNode">dm:namespace-node</loc>(infoset-namespace-prefix($ii),
            infoset-namespace-namespace-name($ii))
}</p>
====================================================================== -->

</div3>


<div3 id="NamespaceNodeDM2IS">
  <head>Data Model to Infoset Mapping</head>

<p>The mapping of the data model to the XML Information Set maps a
Namespace Node to a <emph role="info-item">namespace information item</emph>.
The properties of the <emph role="info-item">namespace information item</emph>
are constructed as follows:</p>

<table border="1" cellspacing="0" summary="PSVI mapping summary">
  <colgroup>
    <col width="35%"/>
    <col width="65%"/>
  </colgroup>
  <thead>
    <tr>
      <td>Property</td>
      <td>Value:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><emph role="infoset-property">prefix</emph></td>
      <td>An appropriate namespace prefix, as described below</td>
    </tr>
    <tr>
      <td><emph role="infoset-property">namespace name</emph></td>
      <td>The value returned by the <function>string-value</function> accessor</td>
    </tr>
  </tbody>
</table>

<!-- ======================================================================

  <table role="infoset-mapping">
    <colgroup><col width="35%"/><col width="65%"/></colgroup>
    <thead>
      <tr><td>Namespace Information Item properties</td><td> </td></tr>
    </thead>
    <tbody>
      <tr><td>
        <emph role="infoset-property">prefix</emph>
      </td><td>
        <emph>xf:get-local-name(dm:node-name($n))</emph>
      </td></tr>
      <tr><td>
        <emph role="infoset-property">namespace name</emph>
      </td><td>
        <function>string-value($n)</function>
      </td></tr>
    </tbody>
  </table>
====================================================================== -->
</div3>

</div2>

<div2 id="ProcessingInstructionNode">
  <head>Processing Instructions</head>

<!--
   <table role="node-summary">
    <thead>
      <tr><td>Processing Instruction Node accessors</td><td>possible values</td></tr>
    </thead>
    <tbody>
      <tr><td><function>node-kind</function></td>    <td>"processing-instruction"</td></tr>
      <tr><td><function>node-name</function></td>    <td>xs:QName</td></tr>
      <tr><td><function>base-uri</function></td>     <td>empty-sequence</td></tr>
      <tr><td><function>string-value</function></td> <td>xs:string</td></tr>
      <tr><td><function>typed-value</function></td>  <td>empty sequence</td></tr>
      <tr><td><function>parent</function></td>       <td>sequence of zero or one element or document nodes</td></tr>
      <tr><td><function>children</function></td>     <td>empty sequence</td></tr>
      <tr><td><function>attributes</function></td>   <td>empty sequence</td></tr>
      <tr><td><function>namespaces</function></td>   <td>empty sequence</td></tr>
      <tr><td><function>type</function></td>         <td>empty sequence</td></tr>
      <tr><td><function>unique-ID</function></td>    <td>empty sequence</td></tr>
    </tbody>
  </table>
-->

<div3 id="ProcessingInstructionNodeOverview">
  <head>Overview</head>

  <p>Processing instruction nodes encapsulate XML processing instructions.
Processing instructions have the following properties:</p>

  <ulist>
  <item><p><emph role="dm-node-property">target</emph>
  </p></item>
  <item><p><emph role="dm-node-property">content</emph>
  </p></item>
  <item><p><emph role="dm-node-property">parent</emph>, possibly empty
  </p></item>
  </ulist>

</div3>


<div3 id="ProcessingInstructionNodeConstructor">
  <head>Constructor</head>

<p id="processing-instruction-node">A processing instructoin node can
be constructed using <function>processing-instruction-node</function> which returns
a new node with unique identity, distinct from all other nodes.</p>

<p>The constructor takes an NCName, a string, and an optional base URI.</p>

<example role="signature">
  <proto class="dm" name="processing-instruction-node" return-type="ProcessingInstructionNode">
    <arg name="target" type="xs:NCName"/>
    <arg name="content" type="xs:string"/>
  </proto>
</example>

<example role="signature">
  <proto class="dm" name="processing-instruction-node" return-type="ProcessingInstructionNode">
    <arg name="target" type="xs:NCName"/>
    <arg name="content" type="xs:string"/>
    <arg name="base-uri" type="xs:anyURI"/>
  </proto>
</example>

<p>The string '?&gt;' may not occur within a processing instruction's
target or content value (<bibref ref="xml-rec"/>).</p>

</div3>

<div3 id="ProcessingInstructionNodeAccessors">
  <head>Accessors</head>

<table border="1" cellspacing="0" summary="Accessor summary">
  <thead>
    <tr>
      <td>Accessor</td>
      <td>Returns:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><function>base-uri</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>node-kind</function></td>
      <td role="D">"<code>processing-instruction</code>"</td>
    </tr>
    <tr>
      <td><function>node-name</function></td>
      <td role="D">A QName with the processing-instruction target in the local-name and an empty URI</td>
    </tr>
    <tr>
      <td><function>parent</function></td>
      <td role="D">The parent element or document node</td>
    </tr>
    <tr>
      <td><function>string-value</function></td>
      <td role="D">The content of the processing-instruction</td>
    </tr>
    <tr>
      <td><function>typed-value</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>type</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>children</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>attributes</function></td>
      <td role="D">()</td>
    </tr>
    <tr>
      <td><function>namespaces</function></td>
      <td role="D">()</td>
    </tr>
<!--
    <tr>
      <td><function>unique-ID</function></td>
      <td role="D">()</td>
    </tr>
-->
  </tbody>
</table>

<!-- ======================================================================
  <p>The accessor <function>node-name</function> returns the target of the
  processing instruction as the local-part of an xs:QName (whose namespace
  URI is the empty sequence).
  The accessor <function>string-value</function> returns the content of the
  processing instruction as an xs:string.</p>

  <p role="definition">
function dm:node-name(<loc href="#ProcessingInstructionNode">ProcessingInstructionNode</loc> $n)
         returns <loc href="#dt-atomic-value">xs:QName</loc>
function dm:string-value(<loc href="#ProcessingInstructionNode">ProcessingInstructionNode</loc> $n)
         returns <loc href="#dt-atomic-value">xs:string</loc>
  </p>

  <p>
  The accessor <function>node-kind</function> returns the string constant
  "processing-instruction".
  The accessor <function>parent</function> returns a sequence containing zero
  or one element or document node representing the parent of the
  processing instruction node.</p>

  <p role="definition">
function dm:node-kind(<loc href="#ProcessingInstructionNode">ProcessingInstructionNode</loc> $n)
         returns <loc href="#dt-atomic-value">xs:string</loc>
function dm:parent(<loc href="#ProcessingInstructionNode">ProcessingInstructionNode</loc> $n)
         returns (<loc href="#ElementNode">ElementNode</loc> | <loc href="#DocumentNode">DocumentNode</loc>)?</p>

  <p>The accessors <function>base-uri</function>, <function>typed-value</function>,
  <function>children</function>, <function>attributes</function>, <function>namespaces</function>,
  <function>type</function> and <function>unique-ID</function>
  applied to a processing-instruction node always return the empty sequence.
  </p>
====================================================================== -->
</div3>

<div3 id="ProcessingInstructionNodePSVI2DM">
  <head>PSVI to Data Model Mapping</head>

<p>When a data model fragment is created from the PSVI, a
<emph role="info-item">processing instruction information item</emph>
is mapped to a Processing Instruction Node.
The precise transformation is described
by specifying the PSVI property corresponding to each argument
of the processing instruction node constructor.</p>

<glist>
<gitem>
  <label><code>$target</code></label>
  <def>
    <p>The value of the <emph role="infoset-property">target</emph> property.</p>
  </def>
</gitem>
<gitem>
  <label><code>$content</code></label>
  <def>
    <p>The value of the <emph role="infoset-property">content</emph> property.</p>
  </def>
</gitem>
<gitem>
  <label><code>$base-uri</code></label>
  <def>
    <p>The value of the <emph role="infoset-property">base URI</emph> property.</p>
  </def>
</gitem>
</glist>

<p>There are no processing instruction nodes for processing instructions
that are children of a
<emph role="info-item">document type declaration information item</emph>.</p>

<!-- ======================================================================
  <p>The <emph role="dm-node-property">target</emph> argument of the
  processing instruction constructor corresponds
  to the <emph role="infoset-property">target</emph> property.</p>

  <p>The <emph role="dm-node-property">content</emph> argument
  corresponds to the <emph role="infoset-property">content</emph>
  property.</p>


  <p>A processing-instruction node is constructed from an
  <emph role="info-item">processing instruction information item</emph>
  by the <emph>infoitem-to-processing-instruction-node</emph> function:</p>

  <p role="definition" id="ProcessingInstructionItem">
/* Accessors for processing instruction information items */
function infoset-processing-instruction-target(
           <loc href="#ProcessingInstructionItem">ProcessingInstructionItem</loc> $ii)  returns <loc href="#dt-atomic-value">xs:string</loc>
function infoset-processing-instruction-content(
           <loc href="#ProcessingInstructionItem">ProcessingInstructionItem</loc> $ii)  returns <loc href="#dt-atomic-value">xs:string</loc>
</p>

  <p role="definition" id="infoitem-to-processing-instruction-node">
define function infoitem-to-processing-instruction-node(
         <loc href="#ProcessingInstructionItem">ProcessingInstructionItem</loc> $ii)
       returns <loc href="#ProcessingInstructionNode">ProcessingInstructionNode</loc>
{
  return dm:processing-instruction-node( <loc href="#xf_NCName">xf:NCName</loc>(
           infoset-processing-instruction-target($ii)),
           infoset-processing-instruction-content($ii))
}</p>
====================================================================== -->
</div3>


<div3 id="ProcessingInstructionNodeDM2IS">
  <head>Data Model to Infoset Mapping</head>

<p>The mapping of the data model to the XML Information Set maps a
Processing Instruction Node to a
<emph role="info-item">processing instruction information item</emph>.
The properties of the <emph role="info-item">processing instruction
information item</emph> are constructed as follows:</p>

<table border="1" cellspacing="0" summary="PSVI mapping summary">
  <colgroup>
    <col width="35%"/>
    <col width="65%"/>
  </colgroup>
  <thead>
    <tr>
      <td>Property</td>
      <td>Value:</td>
    </tr>
  </thead>
  <tbody>
    <tr>
 