Understanding XPath
Access and Manipulate XML Data: Use XPath to query XML data.
To pass the exam, you should also have basic knowledge of XPath. XPath is another W3C standard, formally known as the XML Path Language. XPath is described by the W3C as "a language for addressing parts of an XML document." The .NET implementation of XPath supports the Version 1.0 Recommendation standard for XPath, which you can find at http://www.w3.org/TR/xpath.
You can think of XPath as being a query language, conceptually similar to SQL. Just as SQL allows you to select a set of information from a table or group of tables, XPath allows you to select a set of nodes from the DOM representation of an XML document. In this section, I'll introduce you to the basic syntax of XPath, and then show you how you can use XPath in the .NET System.Xml namespace.
The XPath Language
XPath is not itself an XML standard. XPath expressions are not valid XML documents. Rather, XPath is a language for talking about XML. By writing an appropriate XPath expression, you can select particular elements or attributes within an XML document.
XPath starts with the notion of current context. The current context defines the set of nodes that an XPath query will inspect. In general, there are four choices for specifying the current context for an XPath query:
./ uses the current node as the current context.
/ uses the root of the XML document as the current context.
.// uses the entire XML hierarchy starting with the current node as the current context.
// uses the entire XML document as the current context.
To use XPath to identify a set of elements, you use the path down the tree structure to those elements, separating elements by forward slashes. For example, this XPath expression selects all the Author elements in the Books.xml file:
/Books/Book/Author
You can also select all the Author elements without worrying about the full path to get to them by using this expression:
//Author
You can use * as a wildcard at any level of the tree. So, for example, this expression selects all the Author nodes that are grandchildren of the Books node:
/Books/*/Author
XPath expressions select a set of elements, not a single element. Of course, the set might have only a single member, or no members at all. In the context of the XmlDocument object, an XPath expression can be used to select a set of XmlNode objects to operate on later.
TIP
Be Explicit when Possible In general, the expression with the explicit path (/Books/Book/@Pages) can be evaluated more rapidly than the expression that searches the entire document (//@Pages). The former has to search only a limited number of nodes to return results, whereas the latter needs to look through the entire document.
To identify a set of attributes, you trace the path down the tree to the attributes, just as you do with elements. The only difference is that attribute names must be prefixed with an @ character. For example, this XPath expression selects all the Pages attributes from Book elements in the Books.xml file:
/Books/Book/@Pages
Of course, in the Books.xml file, only Book elements have a Pages attribute. So in this particular context, this XPath expression is equivalent to the previous one:
//@Pages
You can select multiple attributes with the @* operator. To select all attributes of Book elements anywhere in the XML, use this expression:
//Book/@*
XPath also offers a predicate language to enable you to specify smaller groups of nodes or even individual nodes in the XML tree. You might think of this as a filtering capability similar to a SQL WHERE clause. One thing you can do is specify the exact value of the node that you'd like to work with. To find all Publisher nodes with the value Addison Wesley you could use the XPath expression
/Books/Book/Publisher[.="Addison Wesley"]
Here the [] operator specifies a filter pattern and the dot operator stands for the current node. Filters are always evaluated with respect to the current context. Alternatively, you can find all Book elements published by Addison Wesley:
/Books/Book[./Publisher="Addison Wesley"]
Note that there is no forward slash between an element and a filtering expression in XPath.
Of course, you can filter on attributes as well as elements. You can also use operators and Boolean expressions within filtering specifications. For example, you might want to find Books that have a thousand or more pages:
/Books/Book[./@Pages>=1000]
Because the current node is the default context, you can simplify this expression a little bit:
/Books/Book[@Pages>=1000]
XPath also supports a selection of filtering functions. For example, to find books whose title starts with A you could use this XPath expression:
/Books/Book[starts-with(Title,"A")]
Table 2.6 lists some additional XPath functions.
Table 2.6 - Selected XPath Functions
Function |
Description |
concat() |
Concatenates strings |
contains() |
Determines whether one string contains another |
count() |
Returns the number of nodes in an expression |
last() |
Specifies the last element in a collection |
normalize-space() |
Removes whitespace from a string |
not() |
Negates its argument |
number() |
Converts its argument to a number |
position() |
Specifies the ordinal of a node within its parent |
starts-with() |
Determines whether one string starts with another |
string-length() |
Returns the number of characters in a string |
substring() |
Returns a substring from a string |
Square brackets are also used to indicate indexing. Collections are indexed starting at one. To return the first Book node, you'd use this expression:
/Books/Book[1]
To return the first title of the second book:
/Books/Book[2]/Title[1]
To return the first author in the XML file, regardless of the book:
(/Books/Book/Author)[1]
The parentheses are necessary because the square brackets have a higher operator precedence than the path operators. Without the brackets, the expression would return the first author of every book in the file. There's also a last() function that you can use to return the last element in a collection, no matter how many elements are in the collection:
/Books/Book[last()]
Another useful operator is the vertical bar, which is used to form the union of two sets of nodes. This expression returns all the authors for books published by Addison Wesley or Microsoft Press:
/Books/Book[./Publisher="Addison Wesley"]/Author | _ /Books/Book[./Publisher="Microsoft Press"]/Author
One way to see XPath in action is to use the SelectNodes() method of the XmlNode object, as shown in Step-by-Step 2.7.
STEP BY STEP 2.7 - Selecting Nodes with XPath
-
Add a new form to the project. Name the new form StepByStep2_7.cs.
-
Add a Label control, a TextBox control (txtXPath), a Button control (btnEvaluate), and a ListBox control (lbNodes) to the form.
-
Switch to the code view and add the following using directive:
using System.Xml;
-
Double-click the Button control and add the following code to handle the button's Click event:
private void btnEvaluate_Click( object sender, System.EventArgs e) { // Load the Books.xml file XmlTextReader xtr = new XmlTextReader(@"..\..\Books.xml"); xtr.WhitespaceHandling = WhitespaceHandling.None; XmlDocument xd = new XmlDocument(); xd.Load(xtr); // Retrieve nodes to match the expression XmlNodeList xnl = xd.DocumentElement.SelectNodes( txtXPath.Text); // And dump the results lbNodes.Items.Clear(); foreach (XmlNode xnod in xnl) { // For elements, display the corresponding // Text entity if (xnod.NodeType == XmlNodeType.Element) { lbNodes.Items.Add( xnod.NodeType.ToString() + ": " + xnod.Name + " = " + xnod.FirstChild.Value); } else { lbNodes.Items.Add( xnod.NodeType.ToString()+ ": " + xnod.Name + " = " + xnod.Value); } } // Clean up xtr.Close(); }
-
Insert the Main() method to launch the form. Set the form as the startup object for the project.
-
Run the project. Enter an XPath expression in the TextBox control. Click the button to see the nodes that the expression selects from the Books.xml file, as shown in Figure 2.7.
Figure 2.7 You can use XPath expressions to query an XML document.
The SelectNodes() method of the XmlNode object takes an XPath expression and evaluates that expression over the document. The resulting nodes are returned in an XmlNodeList object, which is just a collection of XML nodes.
Using the XPathNavigator Class
You've seen how you can use the XmlReader class to move through an XML document. But the XmlReader allows only forward-only, read-only access to the document. There is another set of navigation classes in the System.Xml.XPath namespace. In particular, the XPathNavigator class provides you with read-only, random access to XML documents.
You can perform two distinct tasks with an XPathNavigator object:
Selecting a set of nodes with an XPath expression
Navigating the DOM representation of the XML document
In the remainder of this section, I'll show you how to use the XPathNavigator class for these tasks.
Selecting Nodes with XPath
To use the XPathNavigator class, you should start with an XmlDocument, XmlDataDocument, or XPathDocument object. In particular, if you're mainly interested in XPath operations, you should use the XPathDocument class. The XPathDocument class provides a representation of the structure of an XML document that is optimized for query operations. You can construct an XPathDocument object from a URI (including a local file name), a stream, or a reader containing XML.
The XPathDocument object has a single method of interest, CreateNavigator(). (You'll also find this method on the XmlDocument and XmlDataDocument objects.) As you've probably guessed, the CreateNavigator() method returns an XPathNavigator object that can perform operations with the XML document represented by the XPathDocument object. Table 2.7 lists the important members of the XPathNavigator object.
Table 2.7 - Important Members of the XPathNavigator Class
Member |
Type |
Description |
Clone() |
Method |
Creates a duplicate of this object with the current state |
ComparePosition() |
Method |
Compares two XPathNavigator objects to determine whether they have the same current node |
Compile() |
Method |
Compiles an XPath expression for faster execution |
Evaluate() |
Method |
Evaluates an XPath expression |
HasAttributes |
Property |
Indicates whether the current node has any attributes |
HasChildren |
Property |
Indicates whether the current node has any children |
IsEmptyElement |
Property |
Indicates whether the current node is an empty element |
Matches() |
Method |
Determines whether the current node matches an XSLT pattern |
MoveToFirst() |
Method |
Moves to the first sibling of the current node |
MoveToFirstAttribute() |
Method |
Moves to the first attribute of the current node |
MoveToFirstChild() |
Method |
Moves to the first child of the current node |
MoveToNext() |
Method |
Moves to the next sibling of the current node |
MoveToNextAttribute() |
Method |
Moves to the next attribute of the current node |
MoveToParent() |
Method |
Moves to the parent of the current node |
MoveToPrevious() |
Method |
Moves to the previous sibling of the current node |
MoveToRoot() |
Method |
Moves to the root node of the DOM |
Name |
Property |
Specifies the qualified name of the current node |
Select() |
Method |
Uses an XPath expression to select a set of nodes |
Value |
Property |
Specifies the value of the current node |
Like the XmlReader class, the XPathNavigator class maintains a pointer to a current node in the DOM at all times. But the XPathNavigator brings additional capabilities to working with the DOM. For example, you can use this class to execute an XPath query, as shown in Step-by-Step 2.8.
TIP
XPathNavigator Object Can Move Backward Note that unlike the XmlReader class, the XPathNavigator class implements methods such as MoveToPrevious() and MoveToParent() that can move backward in the DOM. The XPathNavigator class provides random access to the entire XML document.
STEP BY STEP 2.8 - Selecting Nodes with an XPathNavigator Object
-
Add a new form to the project. Name the new form StepByStep2_8.cs.
-
Add a Label control, a TextBox control (txtXPath), a Button control (btnEvaluate), and a ListBox control (lbNodes) to the form.
-
Switch to the code view and add the following using directive:
using System.Xml.XPath;
-
Double-click the Button control and add the following code to handle the button's Click event:
private void btnEvaluate_Click( object sender, System.EventArgs e) { // Load the Books.xml file XPathDocument xpd = new XPathDocument(@"..\..\Books.xml"); // Get the associated navigator XPathNavigator xpn = xpd.CreateNavigator(); // Retrieve nodes to match the expression XPathNodeIterator xpni = xpn.Select(txtXPath.Text); // And dump the results lbNodes.Items.Clear(); while (xpni.MoveNext()) { lbNodes.Items.Add( xpni.Current.NodeType.ToString() + ": " + xpni.Current.Name + " = " + xpni.Current.Value); } }
-
Insert the Main() method to launch the form. Set the form as the startup object for the project.
-
Run the project. Enter an XPath expression in the TextBox control. Click the button to see the nodes that the expression selects from the Books.xml file.
The Select() method of the XPathNavigator class returns an XPathNodeIterator object, which lets you visit each member of the selected set of nodes in turn. It has Count and Current properties, and (as you saw in the code for Step-by-Step 2.8) a MoveNext() method that advances it through the set of nodes.
Navigating Nodes with XPath
You can also use the XPathNavigator object to move around in the DOM. Step-by-Step 2.9 demonstrates the MoveTo methods of this class.
STEP BY STEP 2.9 - Navigating with an XPathNavigator Object
-
Add a new form to the project. Name the new form StepByStep2_9.cs.
-
Add four Button controls (btnParent, btnPrevious, btnNext, and btnChild), and a ListBox control (lbNodes) to the form.
-
Switch to the code view and add the following using directive:
using System.Xml.XPath;
-
Double-click the form and add the following code to the class definition:
XPathDocument xpd; XPathNavigator xpn; private void StepByStep2_9_Load( object sender, System.EventArgs e) { // Load the Books.xml file xpd = new XPathDocument(@"..\..\Books.xml"); // Get the associated navigator xpn = xpd.CreateNavigator(); xpn.MoveToRoot(); ListNode(); } private void ListNode() { // Dump the current node to the listbox lbNodes.Items.Add(xpn.NodeType.ToString() + ": " + xpn.Name + " = " + xpn.Value); }
-
Double-click the Button controls and add the following code to their Click event handlers:
private void btnParent_Click( object sender, System.EventArgs e) { // Move to the parent of the current node if(xpn.MoveToParent()) ListNode(); else lbNodes.Items.Add("No parent node"); } private void btnChild_Click( object sender, System.EventArgs e) { // Move to the first child of the current node if(xpn.MoveToFirstChild()) ListNode(); else lbNodes.Items.Add("No child node"); } private void btnPrevious_Click( object sender, System.EventArgs e) { // Move to the previous sibling // of the current node if(xpn.MoveToPrevious()) ListNode(); else lbNodes.Items.Add("No previous node"); } private void btnNext_Click( object sender, System.EventArgs e) { // Move to the next sibling of the current node if(xpn.MoveToNext()) ListNode(); else lbNodes.Items.Add("No next node"); }
-
Insert the Main() method to launch the form. Set the form as the startup object for the project.
-
Run the project. Experiment with the buttons. You'll find that you can move around in the DOM, as shown in Figure 2.8.
Figure 2.8 You can navigate an XML Document by using an XpathNavigator object.
Step-by-Step 2.9 demonstrates two important things about the XPathNavigator class. First, the value of a node is the concatenated text of all the nodes beneath that node. Second, the MoveTo methods of the XPathNavigator will never throw an error, whether or not there is an appropriate node to move to. Instead, they simply return false when the requested navigation cannot be performed.
REVIEW BREAK
XPath is a language for specifying or selecting parts of an XML document. XPath is a query language for XML.
An XPath expression returns a set of zero or more nodes from the DOM representation of an XML document.
The SelectNodes() method of the XmlNode object returns a set of nodes selected by an XPath expression.
The XPathDocument and XPathNavigator objects are optimized for fast execution of XPath queries.
The XPathNavigator object allows random-access navigation of the structure of an XML document.