Browse DevX
Sign up for e-mail newsletters from DevX


Introduction to XQuery (Part 3 of 4) : Page 5

Learn how to use XQuery's FLWR (for, let where, and return) expressions to process and restructure data from one or more documents.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

Srinivas Pandrangi is an architect at Ipedo and a member of the W3C XML Query working group, where he is working on the standardization of XQuery. His article on XML performance techniques was published earlier this year in XML Journal. You can reach him at srinivas@ipedo.com.
Thanks for your registration, follow us on our social networks to keep up-to-date