Creating Element Constructors
Several of the preceding examples contain element constructors, but here's a more formal introduction. You use element constructors to create elements that appear in the output or intermediate results of an expression. The December 2001 draft of the XQuery specification proposes two different syntaxes for element constructors; you'll see both in this section. The rest of this article refers to these two syntaxes as the
XML notation constructor syntax and the
computed constructor syntax.
XML notation constructor syntax
Here's a very simple element constructor example:
<greeting complexity="simple">Hello World!!</greeting>
When elements and attribute values contain constants, the XML notation syntax for element constructors is the same as the syntax for XML. Element constructors have to follow all the same rules for well-formedness as XML. But things start getting more interesting when you want to embed dynamic content in constructed elements. For example, to create an element containing the average price of items, you could write the following query:
# see XQuery37.ixq in samples.zip
namespace ns="http://www.ipedo.com/XQueryExample"
<avg_price>{avg(document("data/items.xml")/ns:items/item/price)}</avg_price>
Notice that the nested expression is enclosed in curly braces ({ }). This element constructor creates an element named
avg_price that has a child text node whose value (returned by the call to the built-in function
avg()) is the average price of all items.
You can assign attribute values similarly. The following query returns a list of all items with their descriptions and prices listed as attributes of the constructed
item elements.
# see XQuery38.ixq in samples.zip
namespace ns="http://www.ipedo.com/XQueryExample"
<item_list>
{
for $i in document("data/items.xml")/ns:items/item
return
<item description={$i/description} price={$i/price}/>
}
</item_list>
The Computed Constructor Syntax
Computed constructor syntax uses the keywords
element and
attribute to construct elements and attributes. The next example creates the same greeting element constructed in the first example from the previous section, but uses computed constructor syntax:
element greeting {
attribute complexity { "simple"},
"Hello World!!"
}
Similarly, here's the computed constructor syntax equivalent constructor for the second example from the preceding section:
# see XQuery39.ixq in samples.zip
namespace ns="http://www.ipedo.com/XQueryExample"
element item_list {
{ for $i in document("data/items.xml")/ns:items/item
return {
element item {
attribute description {$i/description},
attribute price {$i/price}
}
}
}
The primary purpose of computed constructor syntax is to let you construct elements and attributes whose
names are not static (they are computed). For example, instead of hard-coding an attribute named
description in the result, if you wanted to use a tag named after the language encoding used for the
description, you could write:
element item_list {
{ for $i in document("data/items.xml")/items/item
return {
element item {
attribute {lang($i)} {$i/description},
attribute price {$i/price}
}
}
}
In the next and last part of the series, you'll look at some of the more powerful expressions such as
conditional expressions,
quantified expressions, and see how to include built-in functions and create user-defined functions in your expressions.