XQuery 1.0:XML查询语言 工作草案-3-2

TransWiki - W3CHINA.ORG开放翻译计划(OTP)

摘要_文档状态_目录 第1节 第2节 3.1~3.3节 3.4~3.6节 3.7节 3.8~3.13节 第4节 附录A 附录B,C,D 附录E,F,G,H,I


目录

3.7 Constructors

XQuery provides constructors that can create XML structures within a query. Constructors are provided for element, attribute, document, text, comment, and processing instruction nodes. Two kinds of constructors are provided: direct constructors, which use an XML-like notation, and computed constructors, which use a notation based on enclosed expressions.



















[84]    Constructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Constructor)    ::=   

DirectConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirectConstructor)

| ComputedConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ComputedConstructor)
[85]    DirectConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirectConstructor)    ::=   

DirElemConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirElemConstructor)

| DirCommentConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirCommentConstructor)

| DirPIConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirPIConstructor)
[86]    DirElemConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirElemConstructor)    ::=    "<" QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QName) DirAttributeList (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirAttributeList) ("/>" | (">" DirElemContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirElemContent)* "</" QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QName) S (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-S)? ">"))
[91]    DirElemContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirElemContent)    ::=   

DirectConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirectConstructor)

| ElementContentChar (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ElementContentChar)

| CDataSection (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CDataSection)

| CommonContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CommonContent)
[143]    ElementContentChar (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-ElementContentChar)    ::=    Char (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Char) - [{}<&] - [{}<&]
[92]    CommonContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CommonContent)    ::=    PredefinedEntityRef (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-PredefinedEntityRef) | CharRef (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CharRef) | "Template:"" | EnclosedExpr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-EnclosedExpr)
[97]    CDataSection (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CDataSection)    ::=    "<![CDATA[" CDataSectionContents (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CDataSectionContents) "]]>"
[98]    CDataSectionContents (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CDataSectionContents)    ::=    (Char (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Char)* - (Char* ']]>' Char*))
[87]    DirAttributeList (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirAttributeList)    ::=    (S (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-S) (QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QName) S (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-S)? "=" S (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-S)? DirAttributeValue (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirAttributeValue))?)*
[88]    DirAttributeValue (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirAttributeValue)    ::=   

('"' (EscapeQuot (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-EscapeQuot) | QuotAttrValueContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-QuotAttrValueContent))* '"')

| ("'" (EscapeApos (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-EscapeApos) | AposAttrValueContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-AposAttrValueContent))* "'")
[89]    QuotAttrValueContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QuotAttrValueContent)    ::=   

QuotAttrContentChar (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-QuotAttrContentChar)

| CommonContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CommonContent)
[90]    AposAttrValueContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-AposAttrValueContent)    ::=   

AposAttrContentChar (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-AposAttrContentChar)

| CommonContent (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CommonContent)
[144]    QuotAttrContentChar (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QuotAttrContentChar)    ::=    Char (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Char) - ["{}<&] - ["{}<&]
[145]    AposAttrContentChar (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-AposAttrContentChar)    ::=    Char (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Char) - ['{}<&] - ['{}<&]
[141]    EscapeQuot (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-EscapeQuot)    ::=    '""'
[142]    EscapeApos (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-EscapeApos)    ::=    ""
[24]    EnclosedExpr (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-EnclosedExpr)    ::=    "{" Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr) "}"

This section contains a conceptual description of the semantics of various kinds of constructor expressions. An XQuery implementation is free to use any implementation technique that produces the same result as the processing steps described in this section.

3.7.1 Direct Element Constructors

An element constructor creates an element node. [Definition: A direct element constructor is a form of element constructor in which the name of the constructed element is a constant.] Direct element constructors are based on standard XML notation. For example, the following expression is a direct element constructor that creates a book element containing attributes, subelements, and text:


<book isbn="isbn-0060229357">

    <title>Harold and the Purple Crayon</title>
<author>
<first>Crockett</first>
<last>Johnson</last>
</author>

</book>


If the element name used in a direct element constructor has a namespace prefix, the namespace prefix is resolved to a namespace URI using the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces). If the element name has no namespace prefix, it is implicitly qualified by the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns). Note that both the statically known namespaces and the default element/type namespace may be affected by namespace declaration attributes (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-namespace-decl-attr) found inside the element constructor.
In a direct element constructor, the name used in the end tag must exactly match the name used in the corresponding start tag, including its prefix or absence of a prefix.
In a direct element constructor, curly braces { } delimit enclosed expressions, distinguishing them from literal text. Enclosed expressions are evaluated and replaced by their value, whereas material outside curly braces is simply treated as literal text, as illustrated by the following example:


<example>

   <p> Here is a query. </p>
<eg> $i//title </eg>
<p> Here is the result of the query. </p>
<eg>{ $i//title }</eg>

</example>


The above query might generate the following result (whitespace has been added for readability to this result and other result examples in this document):


<example>

  <p> Here is a query. </p>
<eg> $i//title </eg>
<p> Here is the result of the query. </p>
<eg><title>Harold and the Purple Crayon</title></eg>

</example>


Since XQuery uses curly braces to denote enclosed expressions, some convention is needed to denote a curly brace used as an ordinary character. For this purpose, a pair of identical curly brace characters within the content of an element or attribute are interpreted by XQuery as a single curly brace character (that is, the pair "{{" represents the character "{" and the pair "}}" represents the character "}".) Alternatively, the character references &#x7b; and &#x7d; can be used to denote curly brace characters. A single left curly brace ("{") is interpreted as the beginning delimiter for an enclosed expression. A single right curly brace ("}") without a matching left curly brace is treated as a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error).[err:XP0003 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0003)]
The result of an element constructor is a new element node, with its own node identity. All the attribute and descendant nodes of the new element node are also new nodes with their own identities, even if they are copies of existing nodes.
The Base URI (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri) of a constructed element node is taken from the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri). The Base URIs of the copied descendant nodes are also taken from the static context rather than by preserving their original Base URIs. If no Base URI is defined in the static context, the Base URIs of the constructed and copied nodes are set to the empty sequence.

3.7.1.1 Attributes



The start tag of a direct element constructor may contain one or more attributes. As in XML, each attribute is specified by a name and a value. In a direct element constructor, the name of each attribute is specified by a constant QName, and the value of the attribute is specified by a string of characters enclosed in single or double quotes. As in the main content of the element constructor, an attribute value may contain expressions enclosed in curly braces, which are evaluated and replaced by their value during processing of the element constructor.
If an attribute name has a namespace prefix, the prefix is resolved to a namespace URI using the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces). If the attribute name has no namespace prefix, the attribute is in no namespace. Note that the statically known namespaces for an attribute may be affected by namespace declaration attributes (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-namespace-decl-attr) that are found inside the same element constructor. If the attributes in a direct element constructor do not have distinct names, a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) is raised. [err:XQ0040 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0040)]
Each attribute in a direct element constructor creates a new attribute node, with its own node identity, whose parent is the constructed element node. However, note that namespace declaration attributes (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-namespace-decl-attr) (see 3.7.1.2 Namespace Declaration Attributes) do not create attribute nodes.
Conceptually, an attribute (other than a namespace declaration attribute) in a direct element constructor is processed by the following steps:


Each consecutive sequence of literal characters in the attribute content is treated as a string containing those characters. Whitespace in attribute content is normalized according to the rules for "Attribute Value Normalization" in [XML 1.0 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XML)] (each whitespace character is replaced by a space (#x20) character.)


Predefined entity references and character references in the attribute content are expanded into their referenced strings, as described in 3.1.1 Literals.


Each enclosed expression is converted to a string as follows:


Atomization (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-atomization) is applied to the value of the enclosed expression, converting it to a sequence of atomic values.


If the result of atomization is an empty sequence, the result is the zero-length string. Otherwise, each atomic value in the atomized sequence is cast into a string. If any of these atomic values cannot be cast into a string, a dynamic error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-dynamic-error) [err:XQ0052 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0052)] is raised.


The individual strings resulting from the previous step are merged into a single string by concatenating them with a single space character between each pair.


Adjacent strings resulting from the above steps are concatenated with no intervening blanks. The resulting string becomes the string value of the attribute node. The attribute node is given an initial type annotation of xdt:untypedAtomic (this type annotation may change during validation of the parent element).


Example:


<shoe size="7"/>


The string value of the size attribute is "7".


Example:


<shoe size="{7}"/>


The string value of the size attribute is "7".


Example:


<shoe size="{()}"/>


The string value of the size attribute is the zero-length string.


Example:


<chapter ref="[{1, 5 to 7, 9}]"/>


The string value of the ref attribute is "[1 5 6 7 9]".


Example:


<shoe size="As big as {$hat/@size}"/>


The string value of the size attribute is the string "As big as ", concatenated with the string value of the node denoted by the expression $hat/@size.


=3.7.1.1 属性



一个直接元素构造器的开始标签可能包含一个或多个属性。与xml一样,每个属性由一个名字和一个

值指定。在一个直接元素构造器中,每个属性的名字由一固定的QName指定,值由单引号或双引号括

起来的字符串指定。与元素构造器的主内容相似,元素值可以包括内嵌的括号,它在元素构造器处理

期间由它们的值进行计算并替换。
如果一个元素名有命名空间前缀,前缀用static-namespaces statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-)解析成命名空间URI。如果没有,属性就不在命

名空间内。注意属性的静态命名空间可能会被20040723/#dt-namespace-decl-attr namespace declaration attributes (http://www.w3.org/TR/2004/WD-xquery-)影响,后者位于同一元素

构造器中。如果在直接元素构造器的属性没有唯一的名字,就会引发一个

static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error)错误。

[err:XQ0040 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0040)]
在直接元素构造器中的每个属性都会创建一属性节点,带其所属节点的标识符,其父节点是被构造的

元素节点。然而,要注意它并没创建属性节点,见:20040723/#dt-namespace-decl-attr namespace declaration attributes (http://www.w3.org/TR/2004/WD-xquery-) (见 3.7.1.2

Namespace Declaration Attributes)。
在概念上,一个在一直接元素构造器中的属性(除了命名空间声明属性),它是通过如下步骤进行处

理的:


属性内容中的每个连续字符都作为字符串对待。属性内容中的空格根据

[XML 1.0 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XML)] (each whitespace character

is replaced by a space (#x20) character.)中定义的“属性值Normalization”进行normalized。



属性内容中的预定义的实体引用字符引用扩展成它们所引用的字符串,它在3.1.1

Literals中描述。


每个内嵌的表达式按如下转换成一个字符串:


3.7.1.2 Namespace Declaration Attributes



The names of a constructed element and its attributes may be qualified names that include namespace prefixes. Namespace prefixes can be bound to namespaces in the Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) or by namespace declaration attributes. It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) to use a namespace prefix that has not been bound to a namespace.[err:XP0008 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0008)]
[Definition: A namespace declaration attribute is used inside a direct element constructor. Its purpose is to bind a namespace prefix or to set the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns) for the constructed element node, including its attributes.] Syntactically, a namespace declaration attribute has the form of an attribute with namespace prefix xmlns, or with name xmlns and no namespace prefix. The value of a namespace declaration attribute must be a StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral) containing a valid URI, or a zero-length string; otherwise a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) is raised. [err:XQ0022 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0022)][err:XQ0046 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0046)] Each namespace declaration attribute is processed as follows:


If the name of the namespace declaration attribute has the prefix xmlns, the local part of the name is interpreted as a namespace prefix and the value of the attribute is interpreted as a namespace URI. The given prefix and URI are added to the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces) of the constructor expression (overriding any existing binding of the given prefix), and are also added as a namespace binding to the in-scope namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-namespaces) of the constructed element. If the namespace URI is a zero-length string, the following rules apply:


If the implementation supports [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)], any existing namespace binding for the given prefix is removed from the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces) of the constructor expression and from the in-scope namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-namespaces) of the constructed element.


If the implementation does not support [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)], a static error is raised. [err:XQ0053 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0053)]


It is implementation-defined (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-implementation-defined) whether a given implementation supports [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)].


If the name of the namespace declaration attribute is xmlns with no prefix, the value of the attribute is interpreted as a namespace URI. This URI specifies the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns) of the constructor expression (overriding any existing default), and is added (with no prefix) to the in-scope namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-namespaces) of the constructed element (overriding any existing namespace binding with no prefix). If the namespace URI is a zero-length string, the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns) of the constructor expression is set to "none," and any no-prefix namespace binding is removed from the in-scope namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-namespaces) of the constructed element.
A namespace declaration attribute does not cause an attribute node to be created.
The following examples illustrate namespace declaration attributes:


In this element constructor, a namespace declaration attribute is used to set the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns) to http://example.org/animals:


<cat xmlns = "http://example.org/animals">

  <breed>Persian</breed>

</cat>



In this element constructor, namespace declaration attributes are used to bind the namespace prefixes metric and english:


<box xmlns:metric = "http://example.org/metric/units"

     xmlns:english = "http://example.org/english/units">
<height> <metric:meters>3</metric:meters> </height>
<width> <english:feet>6</english:feet> </width>
<depth> <english:inches>18</english:inches> </depth>

</box>


3.7.1.3 Content



The part of a direct element constructor between the start tag and the end tag is called the content of the element constructor. This content may consist of literal text characters, nested element constructors, CdataSections (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CDataSection), character and predefined entity references, and expressions enclosed in curly braces. In general, the value of an enclosed expression may be any sequence of nodes and/or atomic values. Enclosed expressions can be used in the content of an element constructor to compute both the content and the attributes of the constructed node.
Conceptually, the content of an element constructor is processed as follows:


The content is evaluated to produce a sequence of nodes called the content sequence, as follows:


Predefined entity references and character references are expanded into their referenced strings, as described in 3.1.1 Literals. Characters inside a CDataSection (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CDataSection), including special characters such as < and &, are treated as literal characters rather than as delimiters (except for the sequence ]]>, which terminates the CDataSection).


Each consecutive sequence of literal characters evaluates to a single text node containing the characters. However, if the sequence consists entirely of boundary whitespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-boundary-whitespace) and the Prolog does not specify xmlspace = preserve, then no text node is generated.


Each nested element constructor is evaluated according to the rules in this section, resulting in a new element node.


Enclosed expressions are evaluated as follows:


For each adjacent sequence of one or more atomic values returned by an enclosed expression, a new text node is constructed, containing the result of casting each atomic value to a string, with a single blank character inserted between adjacent values. If any of these atomic values cannot be cast into a string, a dynamic error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-dynamic-error) [err:XQ0052 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0052)] is raised.
Note:
The insertion of blank characters between adjacent values applies even if one or both of the values is a zero-length string.


For each node returned by an enclosed expression, a new copy is made of the given node and all nodes that have the given node as an ancestor, collectively referred to as copied nodes. The properties of the copied nodes are as follows:


Each copied node receives a new node identity.


The parent, children, and attributes properties of the copied nodes are set so as to preserve their inter-node relationships. For the topmost node (the node directly returned by the enclosed expression), the parent property is empty.


The base-uri properties of the copied nodes are taken from Base URI (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri). If no Base URI is defined in the static context, the base-uri properties are empty.


If construction mode (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-construction-mode) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context) is strip:


The type property of each copied element node is set to xdt:untyped and its nilled property is set to false.


The type property of each copied attribute node is set to xdt:untypedAtomic.


The string-value of each copied element and attribute node remains unchanged, and its typed-value becomes equal to its string-value as an instance of xdt:untypedAtomic.
On the other hand, if construction mode (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-construction-mode) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context) is preserve, the type, nilled, string-value, and typed-value properties of the copied nodes are preserved.


All other properties of the copied nodes, including the in-scope-namespaces of copied element nodes, are preserved.


Adjacent text nodes in the content sequence are merged into a single text node by concatenating their contents, with no intervening blanks. After concatenation, any text node whose content is a zero-length string is deleted from the content sequence.


If the content sequence contains a document node, the document node is replaced in the content sequence by its children.


If the content sequence contains an attribute node following a node that is not an attribute node, a type error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-type-error) is raised.[err:XQ0024 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0024)]


The properties of the newly constructed element node are determined as follows:


node-name is the expanded QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-expanded-qname) resulting from resolving the element name in the start tag, as described in 3.7.1 Direct Element Constructors.


base-uri is taken from Base URI (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri). If no Base URI is defined in the static context, the base-uri property is empty.


parent is empty.


attributes consist of all the attributes specified in the start tag as described in 3.7.1.1 Attributes, together with all the attribute nodes in the content sequence, in implementation-dependent (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-implementation-dependent) order. If two or more of these attributes have the same name, a dynamic error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-dynamic-error) is raised.[err:XQ0025 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0025)]


children consist of all the element, text, comment, and processing instruction nodes in the content sequence.


in-scope-namespaces consist of all the namespace bindings resulting from namespace declaration attributes as described in 3.7.1.2 Namespace Declaration Attributes, and possibly additional namespace bindings as described in 3.7.4 In-scope Namespaces of a Constructed Element.


The nilled property is false.


The string-value property is equal to the concatenated contents of the text-node descendants in document order.


The typed-value property is equal to the string-value property, as an instance of xdt:untypedAtomic.


If construction mode (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-construction-mode) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context) is strip, the type property is xdt:untyped. On the other hand, if construction mode is preserve, the type value is xs:anyType.


Example:


<a>{1}</a>


The constructed element node has one child, a text node containing the value "1".


Example:


<a>{1, 2, 3}</a>


The constructed element node has one child, a text node containing the value "1 2 3".


Example:


<c>{1}{2}{3}</c>


The constructed element node has one child, a text node containing the value "123".


Example:


<b>{1, "2", "3"}</b>


The constructed element node has one child, a text node containing the value "1 2 3".


Example:


<fact>I saw 8 cats.</fact>


The constructed element node has one child, a text node containing the value "I saw 8 cats.".


Example:


<fact>I saw {5 + 3} cats.</fact>


The constructed element node has one child, a text node containing the value "I saw 8 cats.".


Example:


<fact>I saw <howmany>{5 + 3}</howmany> cats.</fact>


The constructed element node has three children: a text node containing "I saw ", a child element node named howmany, and a text node containing " cats.". The child element node in turn has a single text node child containing the value "8".

3.7.1.4 Whitespace in Element Content



In a direct element constructor, whitespace characters may appear in element content. In some cases, enclosed expressions and/or nested elements may be separated only by whitespace characters. For example, in the expression below, the end-tag </title> and the start-tag <author> are separated by a newline character and four space characters:


<book isbn="isbn-0060229357">

    <title>Harold and the Purple Crayon</title>
<author>
<first>Crockett</first>
<last>Johnson</last>
</author>

</book>


[Definition: Whitespace characters that occur by themselves in the boundaries between tags and/or enclosed expressions are called boundary whitespace.] The Prolog contains a declaration called xmlspace that controls whether boundary whitespace is preserved by direct element constructors. If xmlspace is not declared in the prolog or is declared as xmlspace = strip, boundary whitespace is not considered significant and is discarded. On the other hand, if xmlspace = preserve is declared in the prolog, boundary whitespace is considered significant and is preserved.


Example:


<cat>

   <breed>{$b}</breed>
<color>{$c}</color>

</cat>


The constructed cat element node has two child element nodes named breed and color. Whitespace surrounding the child elements has been stripped away by the element constructor (assuming that the Prolog did not specify xmlspace = preserve.)


Example:


<a> {"abc"} </a>


If xmlspace is not declared or is declared as xmlspace = strip, this example is equivalent to <a>abc</a>. However, if xmlspace = preserve is declared, this example is equivalent to <a>  abc  </a>.


Example:


<a> z {"abc"}</a>


Since the whitespace surrounding the z is not boundary whitespace, it is always preserved. This example is equivalent to <a> z abc</a>.
For the purpose of the above rule, whitespace characters generated by character references such as &#x20; or by CdataSections (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CDataSection) are not considered to be boundary whitespace, and are always preserved. Similarly, whitespace characters that are adjacent to a character reference or a CDataSection (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CDataSection) are always preserved.


Example:


<a>&#x20;{"abc"}</a>


This example is equivalent to <a> abc</a>, regardless of the declaration of xmlspace.
It is important to remember that whitespace generated by an enclosed expression is never considered to be boundary whitespace, and is always preserved.


Example:


<a>{" "}</a>


This example is equivalent to <a>  </a>, regardless of the declaration of xmlspace.

3.7.2 Other Direct Constructors

XQuery allows a query to generate a processing instruction or XML comment directly into the query result. In each case, this is accomplished by using a constructor expression whose syntax is based on the syntax of the equivalent construct in XML.






[95]    DirPIConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirPIConstructor)    ::=    "<?" PITarget (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-PITarget) (S (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-S) DirPIContents (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirPIContents))? "?>"
[96]    DirPIContents (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirPIContents)    ::=    (Char (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Char)* - (Char* '?>' Char*))
[93]    DirCommentConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirCommentConstructor)    ::=    "<!--" DirCommentContents (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DirCommentContents) "-->"
[94]    DirCommentContents (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DirCommentContents)    ::=    ((Char (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Char) - '-') | ('-' (Char (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Char) - '-')))*

The content of a processing instruction may not contain the string "?>", and the content of a comment may not contain two consecutive hyphens or end with a hyphen. These rules are enforced by the grammar shown above.
The following example illustrates a constructed processing instruction:


<?format role="output" ?>


The following example illustrates a constructed XML comment:


<!-- Tags are ignored in the following section -->


Note that an XML comment constructor actually constructs a comment node in the data model. An XQuery comment, on the other hand, (see 2.7 Comments) is simply a comment used in documenting a query, and is not evaluated. Consider the following example.


(: This is an XQuery comment :)
<!-- This is an XML comment -->


The result of evaluating the above expression is as follows.


<!-- This is an XML comment -->


3.7.3 Computed Constructors



[99]    ComputedConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-ComputedConstructor)    ::=   

CompDocConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CompDocConstructor)

| CompElemConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CompElemConstructor)

| CompAttrConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CompAttrConstructor)

| CompTextConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CompTextConstructor)

| CompCommentConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CompCommentConstructor)

| CompPIConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CompPIConstructor)

An alternative way to create nodes is by using a computed constructor. A computed constructor begins with a keyword that identifies the type of node to be created: element, attribute, document, text, processing-instruction, or comment.
For those kinds of nodes that have names (element, attribute, and processing instruction nodes), the keyword that specifies the node kind is followed by the name of the node to be created. This name may be specified either as a QName or as an expression enclosed in braces, called the name expression, that returns a string or a QName.
The final part of a computed constructor is an expression enclosed in braces, called the content expression, that generates the content of the node.
The following example illustrates the use of computed element and attribute constructors in a simple case where the names of the constructed nodes are constants. This example generates exactly the same result as the first example in 3.7.1 Direct Element Constructors:


element book {

   attribute isbn {"isbn-0060229357" }, 
element title { "Harold and the Purple Crayon"},
element author {
element first { "Crockett" },
element last {"Johnson" }
}

}


3.7.3.1 Computed Element Constructors





[101]    CompElemConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CompElemConstructor)    ::=    (("element" QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QName) "{") | ("element" "{" Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr) "}" "{")) CompElemBody (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CompElemBody)? "}"
[102]    CompElemBody (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CompElemBody)    ::=    (LocalNamespaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-LocalNamespaceDecl) | ExprSingle (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ExprSingle)) ("," (LocalNamespaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-LocalNamespaceDecl) | ExprSingle (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ExprSingle)))*
[103]    LocalNamespaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-LocalNamespaceDecl)    ::=    "namespace" NCName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-NCName)? "{" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral) "}"

[Definition: A computed element constructor creates an element node, allowing both the name and the content of the node to be computed.]
The name expression of a computed element constructor is processed as follows:


Atomization (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-atomization) is applied to the value of the name expression. If the result of atomization is not a single atomic value of type xs:QName, xs:string, or xdt:untypedAtomic, a type error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-type-error) is raised.[err:XP0004 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0004)][err:XP0006 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0006)]


If the atomized value of the name expression is of type xs:QName, that expanded QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-expanded-qname) is used as the name of the constructed element.


If the atomized value of the name expression is of type xs:string or xdt:untypedAtomic, that value is converted to an expanded QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-expanded-qname). If the string value contains a namespace prefix, that prefix is resolved to a namespace URI using the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces). If the string value contains no namespace prefix, it is treated as a local name in the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns). Note that both the statically known namespaces and the default element/type namespace may be affected by local namespace declarations (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-local-namespace-decl) found inside the computed element constructor.
In a computed element constructor, the name expression is followed by the body, which consists of zero or more local namespace declarations (LocalNamespaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-LocalNamespaceDecl)) followed an optional content expression (ExprSingle (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ExprSingle)). If more than one content expression is present, or if a local namespace declaration follows the content expression, a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) is raised.[err:XQ0042 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0042)]
[Definition: A local namespace declaration in a computed element constructor contains an optional namespace prefix and a namespace URI expressed as a string literal.] Each local namespace declaration is processed as follows:


If a namespace prefix is present, the prefix and URI are added to the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces) of the constructor expression (overriding any existing binding of the given prefix), and are also added as a namespace binding to the in-scope namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-namespaces) of the constructed element. If the namespace URI is a zero-length string, the following rules apply:


If the implementation supports [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)], any existing namespace binding for the given prefix is removed from the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces) of the constructor expression and from the in-scope namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-namespaces) of the constructed element.


If the implementation does not support [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)], a static error is raised. [err:XQ0053 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0053)]


It is implementation-defined (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-implementation-defined) whether a given implementation supports [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)].


If no namespace prefix is present, the URI specifies the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns) of the constructor expression (overriding any existing default), and is added (with no prefix) to the in-scope namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-namespaces) of the constructed element (overriding any existing namespace binding with no prefix). If the namespace URI is a zero-length string, the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns) of the constructor expression is set to "none," and any no-prefix namespace binding is removed from the in-scope namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-namespaces) of the constructed element.
If two or more local namespace declarations in the same computed element constructor have the same prefix or have no prefix, a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) is raised. [err:XQ0043 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0043)] The use of local namespace declarations in a computed element constructor is illustrated by the following example, in which the default element/type namespace is set to http://example.org/ns1 and the prefix space2 is bound to the namespace URI http://example.org/ns2:


element {$computed-name}

   {
namespace {"http://example.org/ns1"},
namespace space2 {"http://example.org/ns2"},
$content
}


The content expression of a computed element constructor is processed in exactly the same way as an enclosed expression in the content of a direct element constructor, as described in Step 1d of 3.7.1.3 Content. The result of processing the content expression is a sequence of nodes called the content sequence. Processing of the computed element constructor then proceeds as follows:


Adjacent text nodes in the content sequence are merged into a single text node by concatenating their contents, with no intervening blanks. After concatenation, any text node whose content is a zero-length string is deleted from the content sequence.


If the content sequence contains a document node, the document node is replaced in the content sequence by its children.


If the content sequence contains an attribute node following a node that is not an attribute node, a type error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-type-error) is raised.[err:XQ0024 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0024)]


The properties of the newly constructed element node are determined as follows:


node-name is the expanded QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-expanded-qname) resulting from processing the name expression, as described above.


base-uri is taken from Base URI (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri). If no Base URI is defined in the static context, the base-uri property is empty.


parent is empty.


attributes consist of all the attribute nodes in the content sequence, in implementation-dependent (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-implementation-dependent) order. If two or more of these attributes have the same name, a dynamic error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-dynamic-error) is raised.[err:XQ0025 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0025)]


children consist of all the element, text, comment, and processing instruction nodes in the content sequence.


in-scope-namespaces consist of all the namespace bindings resulting from local namespace declarations (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-local-namespace-decl), as described above, and possibly additional namespace bindings as described in 3.7.4 In-scope Namespaces of a Constructed Element.


The nilled property is false.


The string-value property is equal to the concatenated contents of the text-node descendants in document order.


The typed-value property is equal to the string-value property, as an instance of xdt:untypedAtomic.


If construction mode (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-construction-mode) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context) is strip, the type property is xdt:untyped. On the other hand, if construction mode is preserve, the type value is xs:anyType.
A computed element constructor might be used to make a modified copy of an existing element. For example, if the variable $e is bound to an element with numeric content, the following constructor might be used to create a new element with the same name and attributes as $e and with numeric content equal to twice the value of $e:


element {fn:node-name($e)}

   {$e/@*, 2 * fn:data($e)}


In this example, if $e is bound by the expression let $e := <length units="inches">{5}</length>, then the result of the example expression is the element <length units="inches">10</length>.
Note:
The static type of the expression fn:node-name($e) is xs:QName?, denoting zero or one QName. Therefore, if the Static Typing Feature (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-typing-feature) is in effect, the above example raises a static type error, since the name expression in a computed element constructor is required to return exactly one string or QName. In order to avoid the static type error, the name expression fn:node-name($e) could be rewritten as fn:exactly-one(fn:node-name($e)). If the Static Typing Feature (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-typing-feature) is not in effect, the example can be successfully evaluated as written, provided that $e is bound to exactly one element node with numeric content.
One important purpose of computed constructors is to allow the name of a node to be computed. We will illustrate this feature by an expression that translates the name of an element from one language to another. Suppose that the variable $dict is bound to a sequence of entries in a translation dictionary. Here is an example entry:


<entry word="address">

   <variant lang="German">Adresse</variant>
<variant lang="Italian">indirizzo</variant>

</entry>


Suppose further that the variable $e is bound to the following element:


<address>123 Roosevelt Ave. Flushing, NY 11368</address>


Then the following expression generates a new element in which the name of $e has been translated into Italian and the content of $e (including its attributes, if any) has been preserved. The first enclosed expression after the element keyword generates the name of the element, and the second enclosed expression generates the content and attributes:


  element 
{$dict/entry[word=name($e)]/variant[lang="Italian"]}
{$e/@*, $e/*}


The result of this expression is as follows:


<indirizzo>123 Roosevelt Ave. Flushing, NY 11368</indirizzo>


Note:
As in the previous example, if the Static Typing Feature (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-typing-feature) is in effect, the enclosed expression that computes the element name in the above computed element constructor must be wrapped in a call to the fn:exactly-one function in order to avoid a static type error.
Additional examples of computed element constructors can be found in G.4 Recursive Transformations.

3.7.3.2 Computed Attribute Constructors



[104]    CompAttrConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CompAttrConstructor)    ::=    (("attribute" QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QName) "{") | ("attribute" "{" Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr) "}" "{")) Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr)? "}"

A computed attribute constructor creates a new attribute node, with its own node identity.
The name expression of a computed attribute constructor is processed as follows:


Atomization (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-atomization) is applied to the value of the name expression. If the result of atomization (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-atomization) is not a single atomic value of type xs:QName, xs:string, or xdt:untypedAtomic, a type error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-type-error) is raised.[err:XP0004 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0004)][err:XP0006 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0006)]


If the atomized value of the name expression is of type xs:QName, that expanded QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-expanded-qname) is used as the name of the constructed attribute.


If the atomized value of the name expression is of type xs:string or xdt:untypedAtomic, that value is converted to an expanded QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-expanded-qname). If the string value contains a namespace prefix, that prefix is resolved to a namespace URI using the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces). If the string value contains no namespace prefix, it is treated as a local name in no namespace. Note that the statically known namespaces for a constructed attribute may be affected by local namespace declarations (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-local-namespace-decl) found in an enclosing element constructor. The resulting expanded QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-expanded-qname) is used as the name of the constructed attribute.


A dynamic error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-dynamic-error) [err:XQ0044 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0044)] is raised if the URI part of the resulting QName is http://www.w3.org/TR/REC-xml-names (corresponding to namespace prefix xmlns) or if the resulting QName is in no namespace and has local name xmlns.
The content expression of a computed attribute constructor is processed as follows:


Atomization (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-atomization) is applied to the value of the content expression, converting it to a sequence of atomic values.


If the result of atomization is an empty sequence, the value of the attribute is the zero-length string. Otherwise, each atomic value in the atomized sequence is cast into a string. If any of these atomic values cannot be cast into a string, a dynamic error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-dynamic-error) [err:XQ0052 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0052)] is raised.


The individual strings resulting from the previous step are merged into a single string by concatenating them with a single space character between each pair. The resulting string is the string value of the new attribute node. The type property of the new attribute node is xdt:untypedAtomic. Its parent property is empty.


Example:


attribute size {4 + 3}


The value of the size attribute is "7".


Example:


attribute

   { if ($sex = "M") then "husband" else "wife" }
{ <a>Hello</a>, 1 to 3, <b>Goodbye</b> }


The name of the constructed attribute is either husband or wife. Its string value is "Hello 1 2 3 Goodbye".

3.7.3.3 Document Node Constructors



[100]    CompDocConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CompDocConstructor)    ::=    "document" "{" Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr) "}"

All document node constructors are computed constructors. The result of a document node constructor is a new document node, with its own node identity.
A document node constructor is useful when the result of a query is to be a document in its own right. The following example illustrates a query that returns an XML document containing a root element named author-list:


document

   {
<author-list>
{fn:doc("bib.xml")//book/author}
</author-list>
}


The content expression of a document node constructor is processed in exactly the same way as an enclosed expression in the content of a direct element constructor, as described in Step 1d of 3.7.1.3 Content. The result of processing the content expression is a sequence of nodes called the content sequence. Processing of the document node constructor then proceeds as follows:


Adjacent text nodes in the content sequence are merged into a single text node by concatenating their contents, with no intervening blanks. After concatenation, any text node whose content is a zero-length string is deleted from the content sequence.


If the content sequence contains a document node, the document node is replaced in the content sequence by its children.


If the content sequence contains an attribute node, a type error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-type-error) is raised.[err:XP0004 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0004)][err:XP0006 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0006)]


The properties of the newly constructed document node are determined as follows:


base-uri is taken from Base URI (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-base-uri). If no Base URI is defined in the static context, the base-uri property is empty.


children consist of all the element, text, comment, and processing instruction nodes in the content sequence.


The unparsed-entities and document-uri properties are empty.


The string-value and typed-value properties are derived from the children property as described in [XQuery 1.0 and XPath 2.0 Data Model (http://www.w3.org/TR/2004/WD-xquery-20040723/#datamodel)].
No validation is performed on the constructed document node. The [XML 1.0 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XML)] rules that govern the structure of an XML document (for example, the document node must have exactly one child that is an element node) are not enforced by the XQuery document node constructor.

3.7.3.4 Text Node Constructors



[105]    CompTextConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CompTextConstructor)    ::=    "text" "{" Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr) "}"

All text node constructors are computed constructors. The result of a text node constructor is a new text node, with its own node identity.
The content expression of a text node constructor is processed as follows:


Atomization (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-atomization) is applied to the value of the content expression, converting it to a sequence of atomic values.


If the result of atomization is an empty sequence, no text node is constructed. Otherwise, each atomic value in the atomized sequence is cast into a string. If any of these atomic values cannot be cast into a string, a dynamic error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-dynamic-error) [err:XQ0052 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0052)] is raised.


The individual strings resulting from the previous step are merged into a single string by concatenating them with a single space character between each pair. The resulting string becomes the content of the constructed text node.
Note:
It is possible for a text node constructor to construct a text node containing a zero-length string. However, if used in the content of a constructed element or document node, such a text node will be deleted or merged with another text node.
The following example illustrates a text node constructor:


text {"Hello"}


3.7.3.5 Computed Processing Instruction Constructors



[107]    CompPIConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-CompPIConstructor)    ::=    (("processing-instruction" NCName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-NCName) "{") | ("processing-instruction" "{" Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr) "}" "{")) Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr)? "}"

A computed processing instruction constructor (CompPIConstructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-CompPIConstructor)) constructs a new processing instruction node with its own node identity. The name expression of a computed processing instruction constructor is processed as follows:


Atomization (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-atomization) is a