Querying XML with XPath


XPath is a language used for querying information contained within XML documents. An explanation of the XPath language itself is outside the scope of this chapter. If you're looking for a tutorial on XPath, you might try http://www.w3schools.com/xpath. Several other extremely good tutorials are also found at this site.

The basic premise behind XPath is that an XPath expression is essentially a description of the result set. More specifically, anything in the source document that satisfies the XPath expression will be returned when the expression is used to select nodes. Hierarchy levels within an XML document are represented in an XPath expression using the forward slash (/). If the double slash (//) is used, it indicates that position within the document tree is irrelevant to whether or not a node satisfies the expression. This is often referred to as a "deep" search.

During filtering and selecting, attributes are specified using the @ prefix, and predicates (conditions that must be satisfied) on a node are specified within square brackets ([]).

The simplest thing you can do with XPath is to select a list of nodes without filtering the results. To do that, you simply specify the nodes you want, as shown in the following XPath statement:

/items/item 


This will select all item nodes that have the items node as a parent. If you want to select just the items node, you can use the XPath expression /items. To select all item nodes without regard to their location within the hierarchy, you can use the expression //item.

Using the square bracket ([]) notation, you can also select nodes based on their position within the current context. So, to select the second item beneath the items parent, you would use the following expression:

/items/item[1] 


Note the zero-based indexing when using the square bracket notation.

Before getting into the more complex XPath statements, let's take the simple expressions and execute them in some .NET code, as shown in Listing 8.3.

Listing 8.3. Simple XPath Expressions

using System; using System.Xml; using System.Collections.Generic; using System.Text; namespace XpathDemo { class Program { static void Main(string[] args) {     XmlDocument docItems = new XmlDocument();     docItems.Load(@"..\..\..\..\items.xml");     XmlNodeList allItems = docItems.SelectNodes("/items/item");     Console.WriteLine("Found {0} items", allItems.Count);     XmlNode thirdItem = docItems.SelectSingleNode("/items/item[2]");     Console.WriteLine("Third node is {0}", thirdItem.Attributes["name"].Value);     Console.ReadLine(); } } } 

And now take a look at some code that uses some more advanced XPath expressions:

// return a list of all attributes where the weight is > 10... XmlNodeList heavyAttribs = docItems.SelectNodes(     "/items/item/attribute[@value > 10 and @name='weight']"); Console.WriteLine("Heavy items:"); foreach (XmlNode heavyAttrib in heavyAttribs) {     Console.WriteLine(heavyAttrib.ParentNode.Attributes["name"].Value); } // return all capacities and weights of all items XmlNodeList cw = docItems.SelectNodes(     "//attribute[@name='capacity'] | //attribute[@name='weight']"); // do something with the list of capacities and weights... Console.WriteLine("Heavy Items (descendant axis predicate):"); XmlNodeList heavyItems = docItems.SelectNodes(     "//items/item[descendant::attribute[@name='weight' and @value > 10]]"); foreach (XmlNode heavyItem in heavyItems) {     Console.WriteLine(heavyItem.Attributes["name"].Value); } 




Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net