Using XQuery 1.0, XQuery 3.0 and XQuery 3.1

This guide explains how to use XQuery 1.0, XQuery 3.0 and XQuery 3.1 with XmlPrime.

This topic contains the following sections.

Loading the Input Document

In this example we will be using the bstore1.example.com/bib.xml document as described in XML Query Use Cases. The document is as follows:

 
<bib>
    <book year="1994">
        <title>TCP/IP Illustrated</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
 
    <book year="1992">
        <title>Advanced Programming in the Unix environment</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
 
    <book year="2000">
        <title>Data on the Web</title>
        <author><last>Abiteboul</last><first>Serge</first></author>
        <author><last>Buneman</last><first>Peter</first></author>
        <author><last>Suciu</last><first>Dan</first></author>
        <publisher>Morgan Kaufmann Publishers</publisher>
        <price>39.95</price>
    </book>
 
    <book year="1999">
        <title>The Economics of Technology and Content for Digital TV</title>
        <editor>
               <last>Gerbarg</last><first>Darcy</first>
                <affiliation>CITI</affiliation>
        </editor>
            <publisher>Kluwer Academic Publishers</publisher>
        <price>129.95</price>
    </book>
 
</bib>
 

This document is assumed to be in the file bib.xml.

To query the document, we load it into an XdmDocumentXdmDocumentXdmDocument. For more information about document representations, see Document Representations.

 
XdmDocument document = new XdmDocument("bib.xml", XmlSpace.Preserve);
 

Compiling a Query

We will run the following query over our document, which returns the books in ascending order of price.

 
for $book in /bib/book
order by xs:decimal($book/price)
return $book
 

The next stage is to compile the query. To describe how the query should be compiled we need set up an XQuerySettingsXQuerySettingsXQuerySettings object. This describes all the settings used for compilation. In particular, we will set the context item type, which by default is set to none, and so the context item cannot be set unless we override the type here. We can then compile the query using the XQuery.CompileXQuery.CompileXQuery::Compile method. This returns us an XQueryXQueryXQuery object encapsulating the query.

 
XQuerySettings querySettings = new XQuerySettings();
querySettings.ContextItemType = XdmType.Node;

string program = "for $book in /bib/book" +
                  "order by xs:decimal($book/price)" +
                  "return $book";

XQuery query = XQuery.Compile(program, querySettings);
 
 
 

Executing the Query

Now we have our query object, we now just need to evaluate it. We use the DynamicContextSettingsDynamicContextSettingsDynamicContextSettings class which describes the parameters used to evaluate the query. In particular we will set the context item to be the document that we loaded earlier. We then pass these settings to the XQuery.Evaluate (DynamicContextSettings)XQuery.Evaluate (DynamicContextSettings)XQuery::Evaluate (DynamicContextSettings^) method to evaluate the query.

 
XPathNavigator contextItem = document.CreateNavigator();

DynamicContextSettings settings = new DynamicContextSettings();
settings.ContextItem = contextItem;

IEnumerable<XPathItem> result = query.Evaluate(settings);
 
 
 
Security note
For security reasons, by default the doc and collection functions always raise an error. This behaviour is changed by setting the DocumentSetDocumentSetDocumentSet property on the DynamicContextSettingsDynamicContextSettingsDynamicContextSettings. See Loading External Resources for more information.
 
 
 
Note
If the query returns zero or one items, then the XQuery.EvaluateToItem ()XQuery.EvaluateToItem ()XQuery::EvaluateToItem () method can be used instead of XQuery.Evaluate ()XQuery.Evaluate ()XQuery::Evaluate ().
 
 
 
Note
If the context item is the only thing that is being set in the DynamicContextSettingsDynamicContextSettingsDynamicContextSettings, then we can instead use the overload of XQuery.Evaluate (XPathItem)XQuery.Evaluate (XPathItem)XQuery::Evaluate (XPathItem^) that takes a context item in place of the settings.
 

The result is an enumerable collection of XPathItemXPathItemXPathItem instances. Each item returned from the query is either an instance of XPathNavigatorXPathNavigatorXPathNavigator (if the item is a node), XPathAtomicValueXPathAtomicValueXPathAtomicValue (if the item is an atomic value), or XPathFunctionItemXPathFunctionItemXPathFunctionItem (if the item is a function item). .

 
 
Note
The query is only evaluated whilst enumerating. If the result is enumerated more than once, this causes the query to be re-evaluated, so if you will be using the results multiple times they should be copied into another collection.
 

Serializing the Results of a Query

To save result to a file we can use the Serialize (IEnumerable<XPathItem>, string, XdmWriterSettings)Serialize (IEnumerable(Of XPathItem), String, XdmWriterSettings)Serialize (IEnumerable<XPathItem^>^, String^, XdmWriterSettings^) method specifying a file to which the results should be written and the XdmWriterSettingsXdmWriterSettingsXdmWriterSettings from the SerializationSettingsSerializationSettingsSerializationSettings property of our compiled query.

 
XdmWriter.Serialize(result, "output.xml", query.SerializationSettings);
 

Directly Serializing the Results of a Query

Instead of evaluating the results of a query then serializing the results, it can often be more efficient to serialize the results directly. This is possible using the SerializeSerializeSerialize method.

 
XPathNavigator contextItem = document.CreateNavigator();

DynamicContextSettings settings = new DynamicContextSettings();
settings.ContextItem = contextItem;

query.Serialize("output.xml", settings);
 

For more details on serialization see Serialization