Path expressions allows you to specify objects and object values from the Document Object Model using expressions. The expressions are defined using the W3C XPath's functions and the results are returned within the framework of DOM APIs in a standard, interoperable way. Path expressions conform to the XPath 1.0 standard, and are represented as XDMPath objects.
It is often more concise to use reference paths to locate data items in XML structures. This approach allows for a clearer distinction between the document structure, and the processing. The result of a path expression may be a nodeset, or an atomic value (text, number or boolean). In this Curl Cue, we will describe general XPath expressons that will get you started.
First, we need to describe general XPath expressions.
Expression | Description |
nodename | Selects child nodes with the specified name |
/ | Selects from the root node |
// | Selects nodes in the document from the current node that match the selection (no matter where they are) |
. | Selects the current node |
.. | Selects the parent node |
@attname | Selects attribute with the specified name |
* | Matches any element node |
@* | Selects any attribute node |
\[n\] | Selects the nth element |
Now we can apply the expressions above to the example XML data where
inventory is the root node
item is a child node of inventory
title, part, and price are children of item
loc is an attribute
<inventory>
<item>
<title loc = "us">File folder</title>
<part>ff123</part>
<price>9.99</price>
</item>
<item>
<title loc = "us">Notepad</title>
<part>np213</part>
<price>6.95</price>
</item>
</inventory>
Let's take a look at the following XPath expressions...
1. /inventory/item
Reading from left to right:
/: is the root node of the document
inventory/: specifies go to the inventory tag
item: find all elements with an item tag
In this case, the result will be all item nodes in the samples root element.
2. /inventory/item/title/@loc
Reading from left to right:
/: is the root node of the document
inventory/: specifies to go to the inventory tag
item/: go to all elements with an item tag
title/: go to all elements with a title tag
@loc: select loc attributes
In this case, the result will be all loc attributes from title nodes under item.
3. //title\[@loc\]
Reading from left to right:
//: search the entire document
title\[@loc\]: find all title elements that have a loc attribute
In this case, the result will be all the item elements that have an attribute named loc. Note that example 2 will return the loc attributes, while this example will return item elements that have a loc attribute.
4. //title\[@loc ="us"\]
Reading from left to right:
//: search the entire document
title\[@loc\]: find all title elements that have a loc attribute with the value of 'us'
In this case, the result will be the item elements that have a loc attribute equal to 'us'.
5. /inventory/item\[1\]
Reading from left to right:
/: is the root node of the document
inventory/: specifies to go to the inventory tag
item\[1\]: select the first item element
In this case, the result will be the first item element that is the child of the inventory element. Note that expressions enclosed in \[ \] are predicates. A predicate is used to find a specific node or node that contains a specific value.
Now that we understand some XPath syntax, the following applet allows us to experiment with the expressions. The XDMPath Display applet has three panes:
The top pane allows us to select an XML file from a dropdown list.
The left pane displays the entire XML structure includijkng nodes, elements, attributes, and associated values.
The right pane allows us to input an XPath query and display the results.
Once you have entered the desired XPath expression, click on the 'Select' button. The results of the query will be displayed in the right pane. Selecting the 'Highlight' button will highlight the corresponding nodes withing the XML display on the left pane.
Since the above applet allows us to quickly view XPath query results, XDMPath Display is definitely an asset for developers. We have made this available for you to download on your machine to use with other XML files. Please select the following link to download a zip file of the XPath Explorer: XPath Explorer. Once you unzip the files, select xpath-explorer.curl to run the applet. Note that this Curl applet is also included within the WSDK documentation.
Just to summarize using XPath expressions, we have put together a list of points worth noting:
Path expressions navigate nodes in an XML document (these resemble filesystem paths).
Paths return collections of nodes (nodesets)
Absolute paths, starting with '/' begin at the start of the document. Other paths are relative.
Paths consist of steps separated by slashes '/'.
Each step builds on the nodeset from the previous step.
Steps can follow different "axes": child elements (by name), attributes (name prefixed with '@'), descendants (starting wtih '//').
Steps can be filtered using a predicate inside square brackets \[ \].
Of course, this is just a short list. There is a lot more on XPath such as datatypes, functions, axes, shorthand, context-node, and operators. For a complete listing of XPath expressions, please see W3C XML Path Language (XPath).
There are no comments on this document