Document node constructors

All document node constructors are computed constructors. A document node constructor creates a document node for which the content of the node is computed based on an enclosed expression.

A document node constructor is useful when the result of a query is a complete document. The result of a document node constructor is a new document node that has its own node identity.

Important: No validation is performed on the constructed document node. The XQuery document node constructor does not enforce the XML 1.0 rules that govern the structure of an XML document. For example, a document node is not required to have exactly one child that is an element node.

Syntax

Read syntax diagramSkip visual syntax diagram document{content-expression}
document
A keyword that indicates that the text that follows it constructs a document node.
content-expression
An expression that generates the content of the constructed document node. The value of content-expression can be any sequence of nodes and atomic values except for an attribute node. Attribute nodes in the content sequence result in an error. Document nodes in the content sequence are replaced by their children. For each node that is returned by content-expression, a new copy is made of the node and all of its descendants, which retain their original type annotations. Any atomic values that are returned by the content expression are converted to strings and stored in text nodes, which become children of the constructed document node. Adjacent text nodes are merged into a single text node.

Example

The following query has a document node constructor that includes a content expression that returns an XML document. The XML document contains a root element named item-list:
SELECT XMLQUERY(
  'declare namespace ipo="http://www.example.com/IPO";
  let $i := $po/ipo:purchaseOrder/items/item
  return
    document
    {
      <item-list>
       {$i}
      </item-list>
    }'
  PASSING XMLAGG(PORDER) as "po")
FROM PURCHASEORDER

Suppose that the PORDER column in the PURCHASEORDER table contains the following data:

<ipo:purchaseOrder
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:ipo="http://www.example.com/IPO"
   orderDate="2008-12-01">
  <shipTo exportCode="1" xsi:type="ipo:UKAddress">
    <name>Helen Zoe</name>
    <street>55 Eden Street</street>
    <city>San Jose</city>
    <state>CA</state>
    <postcode>CB1 1JR</postcode>
  </shipTo>
  <shipTo exportCode="1" xsi:type="ipo:UKAddress">
    <name>Joe Lee</name>
    <street>66 University Avenue</street>
    <city>Palo Alto</city>
    <state>CA</state>
    <postcode>CB1 1JR</postcode>
  </shipTo>
  <billTo xsi:type="ipo:USAddress">
    <name>Robert Smith</name>
    <street>8 Oak Avenue</street>
    <city>Old Town</city>
    <state>PA</state>
    <zip>95819</zip>
  </billTo>
  <items>
    <item partNum="833-AA">
      <productName>Lapis necklace</productName>
      <quantity>1</quantity>
      <USPrice>99.95</USPrice>
      <ipo:comment>Want this for the holidays!</ipo:comment>
      <shipDate>2008-12-05</shipDate>
    </item>
    <item partNum="945-ZG">
      <productName>Sapphire Bracelet</productName>
      <quantity>2</quantity>
      <USPrice>178.99</USPrice>
      <shipDate>2009-01-03</shipDate>
    </item>
  </items>
</ipo:purchaseOrder>

This query returns the following result:

<item-list>
  <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ipo="http://www.example.com/IPO" partNum="833-AA">
    <productName>Lapis necklace</productName>
    <quantity>1</quantity>
    <USPrice>99.95</USPrice>
    <ipo:comment>Want this for the holidays!</ipo:comment>
    <shipDate>2008-12-05</shipDate>
  </item>
  <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ipo="http://www.example.com/IPO" partNum="945-ZG">
    <productName>Sapphire Bracelet</productName>
    <quantity>2</quantity>
    <USPrice>178.99</USPrice>
    <shipDate>2009-01-03</shipDate>
  </item>
</item-list>