XPath Notation
XPath is a language used for navigating through elements and attributes in an XML document. It provides a way to select nodes using a variety of criteria.
Expressions
Expression |
Description |
|
Selects all nodes with the name "nodename" |
|
Root node |
|
Selects nodes in the document from the current node that match the selection no matter where they are |
|
Current node |
|
Parent of the current node |
|
Selects attributes |
Operators
Operator | Description |
---|---|
|
Union, concatenates two node sets |
|
Addition |
|
Subtraction |
|
Equals |
|
Not equal |
|
Less than |
|
Less than or equal to |
|
Greater than |
|
Greater than or equal to |
Functions
Function | Description |
---|---|
|
Returns the last element in a set |
|
Returns the position of the node in a node set |
|
Returns the number of nodes in a node set |
|
Selects elements by their unique ID |
|
Returns the local part of the name of the node |
|
Returns the qualified name of the node |
|
Converts its argument to a string |
|
Concatenates two or more strings |
|
Returns true if string1 starts with string2 |
|
Returns true if string1 contains string2 |
|
Returns a subset of the string starting at a given position with a given length |
|
Returns the part of string1 before string2 |
|
Returns the part of string1 after string2 |
|
Returns the length of a string |
|
Removes leading and trailing spaces from the string |
|
Replaces characters in a string with characters in a new set |
|
Converts its argument to a boolean |
|
Returns true if the argument is false, and vice versa |
|
Returns the boolean value true |
|
Returns the boolean value false |
|
Converts its argument to a number |
|
Returns the sum of the numeric values of the nodes in a node set |
|
Returns the largest integer less than or equal to the number |
|
Returns the smallest integer greater than or equal to the number |
|
Rounds the number to the nearest integer |
XPath Examples
Given the XML data:
<catalog>
<book id="bk001">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk002">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
<book id="bk003">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
</book>
<book id="bk004">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.</description>
</book>
</catalog>
/catalog/book
This selects all book
elements that are children of the catalog
element.
/catalog/book/@id
This selects the id
attribute of all book
elements that are children of the catalog
element.
count(/catalog/book)
This counts the number of book
elements that are children of the catalog
element.
/catalog/book[price>35.00]
This selects all book
elements with a price
element value greater than 35.00 that are children of the catalog
element.