Using XQuery 1.0

This guide explains how to use XQuery 1.0 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.

First, we create a new XmlNameTableXmlNameTableXmlNameTable instance. This will be used to share information such as element and attribute names between the XML documents and the query. Next we create an XmlReaderSettingsXmlReaderSettingsXmlReaderSettings instance and set its NameTableNameTableNameTable property.

 
XmlNameTable nameTable = new NameTable();

XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.NameTable = nameTable;
 
 
 
Warning
In order for XmlPrime to work correctly all documents passed in to XQuery must be loaded with the XmlNameTableXmlNameTableXmlNameTable used to compile the query.
 

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

 
XdmDocument document;

using (XmlReader reader = XmlReader.Create("bib.xml", xmlReaderSettings))
{
    document = new XdmDocument(reader);
}
 

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. In order 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 name table used by the query to match the one we used earlier and we will set the context item type. By default the context item type 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 CompileCompileCompile method. This returns us an XQueryXQueryXQuery object encapsulating the query.

 
XQuerySettings querySettings = new XQuerySettings(nameTable);
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 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 Evaluate ()Evaluate ()Evaluate () method to evaluate the query.

 
XPathNavigator contextItem = document.CreateNavigator();

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

XPathItemCollection 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 EvaluateToItem ()EvaluateToItem ()EvaluateToItem () method can be used instead of Evaluate ()Evaluate ()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 Evaluate (XPathItem)Evaluate (XPathItem)Evaluate (XPathItem^) that takes a context item in place of the settings.
 

The XPathItemCollectionXPathItemCollectionXPathItemCollection is an enumerable collection of the items returned from the query. Each item returned from the query is either an instance of XPathNavigatorXPathNavigatorXPathNavigator (if the item is a node), or XPathAtomicValueXPathAtomicValueXPathAtomicValue (if the item is an atomic value).

 
 
Note
The query is only evaluated whilst enumerating over the XPathItemCollectionXPathItemCollectionXPathItemCollection. If the XPathItemCollectionXPathItemCollectionXPathItemCollection 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 Serialize (string, DynamicContextSettings)Serialize (String, DynamicContextSettings)Serialize (String^, DynamicContextSettings^) method.

 
XPathNavigator contextItem = document.CreateNavigator();

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

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

For more details on serialization see Serialization