Documents

Document nodes represent entire XML documents in the XQuery data model. When an input document is opened using the doc function, a document node is returned. The document node should not be confused with the outermost element node, which is its child.

Not all XML data selected or constructed by queries has a document node at its root. Some implementations will allow you to query XML fragments, such as an element or a sequence of elements that are not part of a document. When XML is stored in a relational database, it often holds elements without any containing document. It is also possible, using element constructors, to create result elements that are not part of a document.

The root function can be used to determine whether a node is part of a document. It will return the root of the hierarchy, whether it is a document node or simply a standalone element.

Document Nodes and the Data Model

A document node is the root of a node hierarchy, and therefore has no parent. The children of a document node are the comments and processing instructions that appear outside of any element, and the outermost element node. For example, the document shown in Example 21-4 would be represented by a single document node that has two children: the xml-stylesheet processing-instruction node and the businessDocument element node.

The string value of a document node is the string value of all its text node descendants, concatenated together. In Example 21-4, that would simply be 2006-10-15. Its typed value is the same as its string value, but with the type xs:untypedAtomic.

Document nodes do not have names. In particular, the base URI of a document node is not its name. Therefore, calling any of the name-related functions with a document node will result in the empty sequence or a zero-length string, depending on the function.

Document Nodes and Sequence Types

The document-node( ) keyword can be used in sequence types to match document nodes. Used with nothing in between the parentheses, it will match any document node. It is also possible to include an element test in between the parentheses. For example:

document-node(element(product))

tests for a document whose only element child (the outermost element) is named product. The document-node( ) keyword can also be used with a schema element test, as in:

document-node(schema-element(product))

Schema element tests are described in "Sequence Types and Schemas" in Chapter 13.

Constructing Document Nodes

Documents can be explicitly constructed using XQuery. This is generally not necessary, because the results of a query do not have to be an XML document node; they can be a single element, or a sequence of multiple elements, or even any combination of nodes and atomic values. If the results of a query are serialized, they become an XML "document" automatically, regardless of whether a document node was constructed in the query.

However, being able to construct a document node is useful if the application that processes the results of the query expects a complete XML document, with a document node. It's also useful when you are doing schema validation. Validation of a document node gives a more thorough check than validation of the outermost element, because it checks schema-defined identity constraints and ID/IDREF integrity.

A computed document constructor is used to construct a complete XML document. Its syntax, shown in Figure 21-3, consists of an expression enclosed in document{ and }. An example of a computed document constructor is shown in Example 21-7.

Syntax of a computed document constructor

Figure 21-3. Syntax of a computed document constructor

The enclosed expression must evaluate to a sequence of nodes. If it contains (directly) any attribute nodes, a type error is raised.

Example 21-7. Computed document constructor

Query
document {
  element product {
    attribute dept { "ACC" },
    element number { 563 },
    element name { attribute language {"en"}, "Floppy Sun Hat"}
  }
}
Results
<product dept="ACC">
  <number>563</number>
  <name language="en">Floppy Sun Hat</name>
</product>

No validation is performed on the document node, unless it is enclosed in a validate expression. XQuery does not require that a document node only contains one single element node, although XML syntax does require a document to have only one outermost element. If you want a result document that is well-formed XML, you should ensure that the enclosed expression evaluates to only one element node.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.79.11