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>  
Prev: "Path Expressions"
Next: "FLWOR Expressions"


USING XQUERY

Visit the DataDirect Resource Library

Ready To Learn More?

Be sure to visit the Progress DataDirect Resource Library regularly because we are continually adding new tutorials.