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

nodename

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

last()

Returns the last element in a set

position()

Returns the position of the node in a node set

count(node-set)

Returns the number of nodes in a node set

id(string)

Selects elements by their unique ID

local-name(node)

Returns the local part of the name of the node

name(node)

Returns the qualified name of the node

string(object)

Converts its argument to a string

concat(string1, string2, string3,…​)

Concatenates two or more strings

starts-with(string1, string2)

Returns true if string1 starts with string2

contains(string1, string2)

Returns true if string1 contains string2

substring(string, start, length)

Returns a subset of the string starting at a given position with a given length

substring-before(string1, string2)

Returns the part of string1 before string2

substring-after(string1, string2)

Returns the part of string1 after string2

string-length(string)

Returns the length of a string

normalize-space(string)

Removes leading and trailing spaces from the string

translate(string, oldSet, newSet)

Replaces characters in a string with characters in a new set

boolean(object)

Converts its argument to a boolean

not(boolean)

Returns true if the argument is false, and vice versa

true()

Returns the boolean value true

false()

Returns the boolean value false

number(object)

Converts its argument to a number

sum(node-set)

Returns the sum of the numeric values of the nodes in a node set

floor(number)

Returns the largest integer less than or equal to the number

ceiling(number)

Returns the smallest integer greater than or equal to the number

round(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.