Skip to main content
Version: 4.17.2

Using XPath expressions

XPath (2.0) expressions are supported by the following components:

Selecting nodes

General

ExpressionDescription
nodenameSelects all nodes with the name "nodename"
/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 of the current node
@Selects attributes

Things to keep in mind

  • Use *: to ignore namespaces

Examples

This xml is used in the examples below.


<?xml version="1.0" encoding="utf-8"?>
<orders>
<order>
<header orderid="123">
<ordernr>12345</ordernr>
<datetime>2019-04-27T08:58:40.375Z</datetime>
</header>
<orderlines>
<orderline>
<item>1</item>
<value>50</value>
</orderline>
<orderline>
<item>2</item>
<value>100</value>
</orderline>
<orderline>
<item>3</item>
<value>150</value>
</orderline>
</orderlines>
</order>
</orders>

Function examples

Selects path from the root node

Expression: /orders/order/header

Result:


<header orderid="123">
<ordernr>12345</ordernr>
<datetime>2019-04-27T08:58:40.375Z</datetime>
</header>

Selects nodes in the document from the current node that match the selection no matter where they are

Expression: //ordernr

Result:


<ordernr>12345</ordernr>

Selects the textual value of first ordernr element

Expression: //ordernr/text()

Result:


12345

Selects attribute orderid

Expression: //@orderid

Result:


orderid=123

Matches any attribute node

Expression: //@*

Result:


orderid=123

Selects all header elements which have at least one attribute of any kind

Expression: //header[@*]

Result:


<header orderid="123">
<ordernr>12345</ordernr>
<datetime>2019-04-27T08:58:40.375Z</datetime>
</header>

Computes two node-sets with |

Expression: //ordernr | //orderline/value

Result:


<ordernr>12345</ordernr>
<value>50</value>
<value>100</value>
<value>150</value>

Returns a sequence of items from the position specified by the start argument and position

Expression: subsequence(//orderline/value,1)

Result:


<value>50</value>
<value>100</value>
<value>150</value>

Returns the reversed order of the items specified

Expression: reverse(//orderline/value)

Result:


<value>150</value>
<value>100</value>
<value>50</value>

Returns the positions within the sequence of items that are equal to the search item argument

Expression: index-of(//orderline/value/text(),"100")

Result:


2

Returns the argument that is greater than the others

Expression: max(//orderline/item)

Result:


3

Returns the argument that is less than the others

Expression: min(//orderline/item)

Result:


1

Returns the count of nodes

Expression: count(//orderlines/orderline/item)

Result:


3

Returns the sum of the numeric value of each node in the specified node-set

Expression: sum(//order/orderlines/orderline/value)

Result:


300

Returns the average of the argument values

Expression: avg(//order/orderlines/orderline/value)

Result:


100

Returns the length of the specified string

Expression: string-length(//ordernr[1]/text())

Result:


5

Returns the unique items separated by two dashes for all values

Expression: distinct-values(string-join((//item),"--"))

Result:


1--2--3

Returns the name of the current node or the first node in orders

Expression: name(//orders/*[1])

Result:


order

Returns a string that is created by replacing the given pattern with the replace argument

Expression: replace(name(//orders/*[1]),"e","a")

Result:


ordar

Returns the current dateTime (with timezone)

Expression: current-dateTime()

Result:


2019-05-03T10:23:18.246Z

Returns an integer that represents the year component in the localized value of the argument

Expression: year-from-dateTime(//datetime/text())

Result:


2019

Returns an integer that represents the month component in the localized value of the argument

Expression: month-from-dateTime(//datetime/text())

Result:


4

Returns an integer that represents the day component in the localized value of the argument

Expression: day-from-dateTime(//datetime/text())

Result:


27

Selects the first orderline element that is the child of the orderlines element

Expression: //orderlines[1]

Result:


<orderline>
<item>1</item>
<value>50</value>
</orderline>

Selects the last ‘orderline’ element that is the child of the ‘orderlines’ element

Expression: //orderlines/orderline[last()]

Result:


<orderline>
<item>3</item>
<value>150</value>
</orderline>

Selects the last but one ‘orderline’ element that is the child of the orderlines element

Expression: //orderlines/orderline[last()-1]

Result:


<orderline>
<item>2</item>
<value>100</value>
</orderline>

Selects the first two orderline elements that are children of the orderlines element

Expression: //orderlines/orderline[position()<3]

Result:


<orderline>
<item>1</item>
<value>50</value>
</orderline>
<orderline>
<item>2</item>
<value>100</value>
</orderline>

Operators

OperatorDescription
|Computes two node-sets
+Addition
-Subtraction
*Multiplication
divDivision
=Equal
!=Not equal
<Less than
<=Less than or equal to
>Greater than
=>Greater than or equal to
oror
andand
modModulus (division remainder)

Operator examples

Expression
max(//orderline/item)>5
max(//orderline/item)+min(//orderline/item)=4

Useful references

For more detailed information go to: