Using XPath 2.0, XPath 3.0 and XPath 3.1

This guide explains how to use XPath 2.0, XPath 3.0 and XPath 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 XdmDcoument("bib.zml", XmlSpace.Preserve);
 

Compiling an Expression

We will run the following expression over our document, which returns the second book element.

 
/bib/book[2]
 

We compile the expression, with the XPath.CompileXPath.CompileXPath::Compile method, passing the XPath 2.0 expression we wish to compile.

 
string xpath="/bib/book[2]";
XPath expression = XPath.Compile(xpath);
 
 
 
Note

Overloads of this function are provided. One takes a set of in-scope namespaces, and the other takes an XPathSettingsXPathSettingsXPathSettings object allowing for full customization of all the XPath 2.0 static context components.

 

Evaluating the Expression

Now we have our expression object we now just need to evaluate it. This is done with the XPath.EvaluateXPath.EvaluateXPath::Evaluate and XPath.EvaluateToItemXPath.EvaluateToItemXPath::EvaluateToItem methods, which return a sequence of items, or zero or one items respectively.

Each item returned from the expression 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).

To identify the type and number of the items returned, we can examine the XPath.StaticTypeXPath.StaticTypeXPath::StaticType property. The static type contains TypeCodeTypeCodeTypeCode and QuantifierQuantifierQuantifier properties which describe the type and number of arguments that will be returned. This can be used to identify the best function to call.

Since we know our expression will only return a single node, we can call the EvaluateToItem (XPathItem)EvaluateToItem (XPathItem)EvaluateToItem (XPathItem^) method passing an XPathNavigatorXPathNavigatorXPathNavigator over the XML document as the context item, and cast our result to an XPathNavigatorXPathNavigatorXPathNavigator.

 
XPathNavigator contextItem = document.CreateNavigator();
XPathNavigator result = expression.EvaluateToItem(contextItem) as XPathNavigator;
 
 
 
Note
If we need to set more of the runtime parameters, then we can use overloads of the Evaluate ()Evaluate ()Evaluate () methods that take a DynamicContextSettingsDynamicContextSettingsDynamicContextSettings.
 
 
 
Note
If the expression is evaluated as an item sequence, then the results are evaluated lazily during enumeration. If it is enumerated more than once, this causes the expression to be evaluated more than once, so if you will be enumerating the results multiple times, they should be copied into another collection.
 

Extension methods

The XmlPrime.ExtensionMethodsXmlPrime.ExtensionMethodsXmlPrime.ExtensionMethods namespace provides extensions methods to provide XPath 2.0 functionality on XPathNavigatorXPathNavigatorXPathNavigator and XmlNodeXmlNodeXmlNode with similar methods to existing XPath functionality.

To use these extension methods, a reference to the XmlPrime.ExtensionMethods.dll must be added to your project, and your project must target .NET 3.5 or above.

The extensions methods are drop in replacements, taking and returning the same arguments as the XPath 1.0 equivalents. However, exceptions thrown may vary.

With extension methods the previous example can be rewritten.

 
using XmlPrime.ExtensionMethods;

...

XPathNavigator contextItem = document.CreateNavigator();
XPathNavigator result = contextItem.XPathSelectSingleNode("/bib/book[2]");