Constructors are used to construct XML structures in XQuery expressions.
Constructors are available to construct the following nodes:
There are two kinds of constructor:
Direct constructors use XML notation to create XML structures.
Direct element constructors are used to create element nodes.
Direct element constructors:
example: Direct element constructor with constant for element content
<employee>jennifer</employee>
<employee>jennifer</employee>
example: Direct element constructor with enclosed expression for element content
<employee> { /people/person/name } </employee>
<employee> <name>mary</name> <name>cindy</name> <name>john</name> <name>peter</name> </employee>
example: Direct element constructor with enclosed expression (which also contains a direct element constructor with enclosed expression)
<employees> { for $i in /people/person return <employee> { data($i/name) } </employee> } </employees>
<employees> <employee>mary</employee> <employee>cindy</employee> <employee>john</employee> <employee>peter</employee> </employees>
Direct attribute constructors are used to create attribute nodes.
Direct attribute constructors:
example: Direct attribute constructor with constant for attribute value
<employee department='human resources'>jennifer</employee>
<employee department='human resources'>jennifer</employee>
example: Direct attribute constructor with enclosed expression for attribute value
<employees> { for $i in /people/person return <employee department="{$i/@dept}"> { data($i/name) } </employee> } </employees>
<employees> <employee department="sales">mary</employee> <employee department="marketing">cindy</employee> <employee department="sales">john</employee> <employee department="accounting">peter</employee> </employees>
Direct namespace constructors are used inside direct element constructors to bind a namespace prefix or set the default namespace for the constructed element node and its attributes.
Direct namespace constructors
example: Direct namespace constructor for default namespace declaration
<abc:people xmlns:abc="http://my-company"></people>
<abc:people xmlns:abc="http://my-company"></people>
Direct comment constructors are used to create comment nodes in XQuery expressions.
Direct comment constructors
example: Direct comment constructor
<!-- this is a comment-->
<!-- this is a comment-->
example: Direct comment constructor inside a direct element constructor
<people><!-- this is a comment--></people>
<people><!-- this is a comment--></people>
Direct processing instruction constructors are used to create processing instruction nodes in XQuery expressions.
Direct processing instruction constructors
example: Direct processing instruction constructor
<?xml-stylesheet type="text/xsl" href="people.xslt"?>
<?xml-stylesheet type="text/xsl" href="people.xslt"?>
example: Direct processing instruction constructor appearing before root element of computed document node
let $pi := <?xml-stylesheet type="text/xsl" href="people.xslt"?> let $x := /* return document {($pi , $x)}
<?xml-stylesheet type="text/xsl" href="people.xslt"?> <people> <person dept="sales"> <name>mary</name> <age>20</age> </person> <person dept="marketing"> <name>cindy</name> <age>25</age> </person> <person dept="sales"> <name>john</name> <age>40</age> </person> <person dept="accounting"> <name>peter</name> <age>35</age> </person> </people>
Unlike direct constructors which use XML notation, computed constructors use a non XML notation to create XML structures in XQuery expressions.
Computed element constructors use a non XML notation to create element nodes in XQuery expressions.
Computed element constructors:
A computed element constructor begins with the keyword element followed by the name of the element.
The name of the element can be specified as a constant OR be computed using an enclosed expression.
The constant or enclosed expression used for the element name is followed by an enclosed expression to compute the element content.
example: Computed element constructor with constant for element name and enclosed expression for element content
<employees> { for $i in /people/person return element employee {data($i/name)} } </employees>
<employees> <employee>mary</employee> <employee>cindy</employee> <employee>john</employee> <employee>peter</employee> </employees>
example: Computed element constructor with enclosed expression for element name and enclosed expression for element content
<people> { for $i in /people/person return element {$i/name} {data($i/age)} } </people>
<people> <mary>20</mary> <cindy>25</cindy> <john>40</john> <peter>35</peter> </people>
Computed attribute constructors use a non XML notation to create attribute nodes in XQuery expressions.
Computed attribute constructors:
A computed attribute constructor begins with the keyword attribute followed by the name of the attribute.
The name of the attribute can be specified as a constant OR be computed using an enclosed expression.
The constant or enclosed expression used for the attribute name is followed by an enclosed expression to compute the attribute value.
example: Computed attribute constructor with constant for attribute name and enclosed expression for attribute value
<people> { attribute total { count(/people/person) }, for $i in /people/person return $i } </people>
<people total="4"> <person dept="sales"> <name>mary</name> <age>20</age> </person> <person dept="marketing"> <name>cindy</name> <age>25</age> </person> <person dept="sales"> <name>john</name> <age>40</age> </person> <person dept="accounting"> <name>peter</name> <age>35</age> </person> </people>
example: Computed attribute constructor with enclosed expressions for attribute name and attribute value
let $p := concat('number_of_' , name(/*)) return <people> { attribute { $p } { count(/people/person) }, for $i in /people/person return $i } </people>
<people number_of_people="4"> <person dept="sales"> <name>mary</name> <age>20</age> </person> <person dept="marketing"> <name>cindy</name> <age>25</age> </person> <person dept="sales"> <name>john</name> <age>40</age> </person> <person dept="accounting"> <name>peter</name> <age>35</age> </person> </people>
Computed namespace constructors use a non XML notation to create namespace nodes in XQuery expressions.
Computed namespace constructors:
A computed namespace constructor begins with the keyword namespace followed by a constant or enclosed expression to specify or compute the namespace prefix.
The constant or enclosed expression used for the namespace prefix is followed by an enclosed expression to compute the namespace.
example: Computed namespace constructor with constant for namespace prefix
<people> { namespace xsi {'http://www.w3.org/2001/XMLSchema-instance'}, attribute xsi:noNamespaceSchemaLocation {'people.xsd'}, element {'person'} {} } </people>
<people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="people.xsd"> <person/> </people>
Computed text constructors use a non XML notation to create text nodes in XQuery expressions.
Computed text constructors:
A computed text node constructor begins with the keyword text followed by an enclosed expression to compute the text node content.
example: Computed text constructor with enclosed expression to compute text node content
<div> { for $i in /people/person return element p { text { concat ($i/name, ' is ' , $i/age) } } } </div>
<div> <p>mary is 20</p> <p>cindy is 25</p> <p>john is 40</p> <p>peter is 35</p> </div>
Document constructors use a non XML notation to create document nodes (highest level of the XML DOM node hierarchy) in XQuery expressions.
Document constructors:
A document constructor begins with the keyword document followed by an enclosed expression to compute the document content.
example: Document constructor which creates a document from a sequence of nodes
let $pi := <?xml-stylesheet type="text/xsl" href="people.xslt"?> let $x := /* return document { ($pi , $x) }
<?xml-stylesheet type="text/xsl" href="people.xslt"?> <people> <person dept="sales"> <name>mary</name> <age>20</age> </person> <person dept="marketing"> <name>cindy</name> <age>25</age> </person> <person dept="sales"> <name>john</name> <age>40</age> </person> <person dept="accounting"> <name>peter</name> <age>35</age> </person> </people>
Computed comment constructors use a non XML notation to create comment nodes in XQuery expressions.
Computed comment constructors:
A computed comment constructor begins with the keyword comment followed by an enclosed expression to compute the comment content.
example: Computed comment constructor with enclosed expression to compute content of comment node
<people> { for $i in /people/person count $x return ( comment { concat( "comment describing person # " , $x ) }, $i ) } </people>
<people> <!--comment describing person # 1--> <person dept="sales"> <name>mary</name> <age>20</age> </person> <!--comment describing person # 2--> <person dept="marketing"> <name>cindy</name> <age>25</age> </person> <!--comment describing person # 3--> <person dept="sales"> <name>john</name> <age>40</age> </person> <!--comment describing person # 4--> <person dept="accounting"> <name>peter</name> <age>35</age> </person> </people>
Computed processing instruction constructors use a non XML notation to create processing instruction nodes in XQuery expressions.
Computed processing instructions:
A computed processing instruction begins with the keyword processing-instruction followed by a constant or an enclosed expression for the processing instruction's target property.
The constant or enclosed expression used for the target property is followed by an enclosed expression to compute the content of the processing instruction.
example: computed processing instruction node with enclosed expressions for target and content
let $target := 'xml-stylesheet' let $type := 'type="text/xsl"' let $file := concat(name(/*), '.xslt') let $href := concat('href="', $file, '"') let $content := ($type , $href) let $pi := processing-instruction {$target }{$content } let $x := /* return document {($pi , $x)}
<?xml-stylesheet type="text/xsl" href="people.xslt"?> <people> <person dept="sales"> <name>mary</name> <age>20</age> </person> <person dept="marketing"> <name>cindy</name> <age>25</age> </person> <person dept="sales"> <name>john</name> <age>40</age> </person> <person dept="accounting"> <name>peter</name> <age>35</age> </person> </people>
In XQuery enclosed expressions are expressions contained within { and }.
Enclosed expressions are used in three ways:
The following sections will show how enclosed expressions are used with direct and computed constructors.
When using direct constructors it is important to enclose those parts of the XQuery expression which need to be evaluated with { and } to prevent them from being output as literal text.
example: XPath statement in XQuery expression output as literal text
<root>/people/person</root>
<root>/people/person</root>
example: XPath statement contained within enclosed expression in XQuery expression is evaluated.
<root>{/people/person}</root>
<root> <person dept="sales"> <name>mary</name> <age>20</age> </person> <person dept="marketing"> <name>cindy</name> <age>25</age> </person> <person dept="sales"> <name>john</name> <age>40</age> </person> <person dept="accounting"> <name>peter</name> <age>35</age> </person> </root>
example: Function call to compute attribute value in XQuery expression is output as literal text.
<root total="count(/people/person)"></root>
<root total="count(/people/person)"></root>
example: Function call to compute attribute value in XQuery expression is evaluated
<root total="{count(/people/person)}"></root>
<root total="4"/>
Depending on the type of computed constructor, multiple enclosed expressions may be used to specify properties of the node being constructed.
example: Enclosed expressions in a computed element constructor used to compute element name and value
for $i in /people/person return element { concat( $i/name , '_from_' , $i/@dept ) } { data( $i/age ) }
<mary_from_sales>20</mary_from_sales> <cindy_from_marketing>25</cindy_from_marketing> <john_from_sales>40</john_from_sales> <peter_from_accounting>35</peter_from_accounting>
example: Enclosed expression in a text constructor used to compute text value
let $ages := string-join( /people/person/age , ', ' ) return text { concat ( 'The age of all employees: ' , $ages ) }
'The age of all employees: 20, 25, 40, 35'