Creating XML: XML Constructors

Now that we have seen how to locate anything in an XML document or a relational table, let's learn how to create new XML structures using XML constructors.

Literal XML constructors

The most simple constructors are literal XML constructors, which use the same syntax as XML. For example, the following XML text is also an XQuery expression that creates the equivalent XML structure.

<stock role='eg'> 
   <ticker>AMZN</ticker> 
   <shares>3000</shares> 
</stock> 

This example uses only elements and attributes, but processing instructions, comments, and CDATA sections can also be used in XML constructors.

Enclosed Expressions

In literal XML constructors, you can use curly braces ({ }) to add content that is computed when the query is run. This is called an enclosed expression. For example, the following expression creates a date element whose content is the current date, which is computed when the query is run.

<date>{ current-date() }</date> 

The result would be an element named date with the current date.

To see why enclosed expressions are necessary, consider the following expression:

<date> current-date() </date> 

This expression evaluates to the following XML element:

<date> current-date() </date> 

Path expressions are frequently used in enclosed expressions. The following expression creates a portfolio element for Minollo, and then extracts Minollo's holdings from the holdings table.

<portfolio name='Minollo'> 
   { collection('holdings')/holdings[userid='Minollo'] } 
</portfolio> 

Try DataDirect XQuery® Free!

Put the power, scalability, and performance of DataDirect XQuery® to work for you today! Our free trial lets you see for yourself how easy it is to build data integration applications that access relational, EDI, and other file formats as XML!

Download DataDirect XQuery® today!

Stay Informed!

XQuery is one of the hottest XML technologies being developed today. Stay informed with vital news about standards, tools, and trends by signing up for the DataDirect XQuery® newsletter.

Online Video Tutorials!

Our easy-to-follow online video tutorials are a great way to get acquainted with the many features of DataDirect XQuery®.

And if you like what you see, download a free copy today and try DataDirect XQuery® for yourself!