In addition to location paths expressions, a number of other types of expressions are also available in XPath 3.0.
There are different ways to specify literals depending on which type they represent.
'523'
523
523E6
5.23
fn:true()
xs:date('2014-11-05')
An XPath expression beginning with the keyword 'if' signifies a conditional expression.
if (/company/office[@location = 'Boston']/employee[1]/age = 35) then 'is 35' else 'is not 35'.
Conditional Expression Examples
if (/company/office[@location='Boston']/employee[1]/age = 35) then 'is 35' else 'is not 35'
'is not 35'
if (count(/company/office/employee) = 5) then true() else false()
true()
for $i in /company/office/employee return if ($i/age >= 30) then upper-case($i/last_name) else lower-case($i/last_name)
('smith', 'JONES', 'BROWN', 'DAVIS', 'MASON')
A 'logical' expression is either an 'and' expression or an 'or' expression. The evaluation of a logical expression returns the boolean value 'true' or 'false'.
first_name = 'John' and age = 40
Logical Expression Examples
The following examples are based on the XML document below. The examples assume that the context node is the first 'employee' element in the Boston 'office'.
first_name = 'John' and age = 40
false()
first_name = 'John' or age = 40
true()
Sequences are fundamental to XPath. In XPath 3.0 operators are available to construct, combine and filter sequences.
Constructing Sequences:
(employee/first_name, employee/age)
(1 to 10)
Combining Sequences:
(company/office[@location = 'Boston']/employee/age union company/office[@location = 'Vienna']/employee/age)
Sequence Construction Examples
Some of the examples below are based on the XML document below. The examples assume that the context node is the first 'office' element (with a location attribute value of 'Boston').
(5, 7, 3, 2, 8)
(5, 7, 3, 2, 8)
(5, (7, 3), (), 2, 8)
(5, 7, 3, 2, 8)
(employee/first_name, employee/age)
('John', 'John', 25, 30)
(1 to 5)
(1, 2, 3, 4, 5)
Sequence Combining Examples
The examples below are based on the XML document below. The examples assume that the context node is the 'cities_visited' element.
traveller[@name = 'John']/city union traveller[@name = 'Jane']/city
('Hong Kong', 'Los Angeles', 'New York', 'Vancouver', 'London', 'Los Angeles', 'New York', 'Sydney')
traveller[@name = 'John']/city | traveller[@name = 'Jane']/city
('Hong Kong', 'Los Angeles', 'New York', 'Vancouver', 'London', 'Los Angeles', 'New York', 'Sydney')
traveller/city intersect traveller[@name = 'John']/city
('Hong Kong', 'Los Angeles', 'New York', 'Vancouver')
traveller[@name = 'John']/city intersect traveller[@name = 'Jane']/city
()
traveller/city except traveller[@name = 'John']/city
('London', 'Los Angeles', 'New York', 'Sydney')
The 'for' expression is used to iterate over sequences.
for $i in company/office/employee return $i/first_name
For Expression Examples
for $i in /company/office/employee return $i/first_name
('John', 'John', 'Mary', 'Peter', 'Mark')
distinct-values(for $i in /company/office/employee return $i/first_name)
('John', 'Mary', 'Peter', 'Mark')
for $i in (1 to 5) return $i*10
(10, 20, 30, 40, 50)
For Expression Examples Continued
for $x in /cities/city, $y in /cities/city return concat('from: ', $x, ' to: ', $y)
('from: Los Angeles to: Los Angeles', 'from: Los Angeles to: Paris', 'from: Paris to: Los Angeles', 'from: Paris to: Paris')
for $x in /cities/city, $y in /cities/city return if ($x != $y) then concat('from: ', $x, ' to: ', $y) else ()
('from: Los Angeles to: Paris', 'from: Paris to: Los Angeles')
A quantified expression tests to see if any or all items in a sequence match a particular condition.
some $i in company/office/employee satisfies $i/age < 30
Quantified Expression Examples
some $i in /company/office/employee satisfies $i/age < 30
true()
every $i in /company/office/employee satisfies $i/age < 30
false()
In XPath 3.0 there are three kinds of comparison expressions:value comparisons, general comparisons and node comparisons.
Value Comparison:
traveller[@name = 'John']/city[1] eq 'Hong Kong'
General Comparison:
traveller[@name = 'John']/city = 'Los Angeles'
Node Comparison:
traveller[@name = 'John']/city is traveller[1]/city[1]
Value Comparison Examples
The following examples are based on the XML document below. The examples assume that the context node is the root element 'cities_visited'.
traveller[@name = 'John']/city[1] eq 'Hong Kong'
true()
traveller[@name = 'John']/city[1] eq 'New York'
false()
traveller[@name = 'John']/city eq 'Hong Kong'
err:XPTY0004 (type error)
traveller[@name = 'John']/city[2] eq traveller[@name = 'Jane']/city[2]
true()
traveller[1]/@age lt traveller[2]/@age
false()
traveller[1]/@age gt traveller[2]/@age
true()
General Comparison Examples
Some of the following examples are based on the XML document below. The examples assume that the context node is the root element 'cities_visited'.
traveller[@name = 'John']/city[1] = 'Hong Kong'
true()
traveller[@name = 'John']/city[1] = 'New York'
false()
traveller[@name = 'John']/city = 'New York'
true()
traveller[1]/@age > traveller[2]/@age
true()
(5, 10, 15) = (15, 20, 25)
true()
(5, 10, 15) != (15, 20, 25)
true()
(1, 3) > (4, 2)
true()
Node Comparison Examples
The following examples are based on the XML document below. The examples assume that the context node is the root element 'cities_visited'.
traveller[@name = 'John']/city[ . = 'Los Angeles'] is traveller[1]/city[1]
false()
traveller[@name = 'John']/city[ . = 'Los Angeles'] is traveller[1]/city[2]
true()
traveller[@name = 'John']/city[ . = 'Los Angeles'] << traveller[1]/city[1]
false()
traveller[@name = 'John']/city[ . = 'Los Angeles'] >> traveller[1]/city[1]
true()
traveller[@name = 'John']/city[ . = 'Los Angeles'] is traveller[@name = 'Jane']/city[ . = 'Los Angeles']
false()
In XPath 3.0 there are six arithmetic operators for addition, subtraction, multiplication, division, integer division, and modulus. These are respectively: +, -, *, div, idiv and mod.
5 + 4 * 20 + 3 - 2 * 2 div 3
Arithmetic Operators
5 + 4 * 6 + 3 - 2 * 2
Operand Types
5 + 12.5
Arithmetic Expression Examples
Some of the following examples are based on the XML document below. The examples assume that the context node is the root element 'orders'.
5 + 4 * 6 + 3 div 2
30.5
(5 + 4 * 6 + 3) div 2
16.
5 idiv 2
2
for $i in ./order return $i/product/quantity * $i/product/price
(450, 2439.6)
sum(for $i in ./order return $i/product/quantity * $i/product/price
2889.6
order[1]/date_shipped + order[1]/date_received
err:XPTY0004 (tpye error)
order[1]/date_shipped - order[1]/date_received
xs:dayTimeDuration('P4D')
A string concatenation expression simply concatenates the string representation of values.
traveller[1]/@name || traveller[2]/@name
String Concatenation Examples
Some of the following examples are based on the XML document bwloe. The examples assume that the context node is the root element 'cities_visited'.
traveller[1]/@name || traveller[2]/@name
'JohnJane'
'XPath ' || 'is ' || 'awesome'
'XPath is awesome'
'One thousand is ' || 1E3
'One thousand is 1000'
A let expression allows a variable to be declared and bound to a value. The scope of the variable declaration is the scope of the let expression.
let $x := 2, $y := 3 return $x * $y
Let Expression Examples
Some of the following examples are based on the XML document below. The examples assume that the context node is the root element 'company'.
let $x := office[@location = 'Boston'], $y := office[@location = 'Vienna'] return (avg($x/employee/age), avg($y/employee/age))
(27.5, 36)
let $r := 5, $pi := 3.14 return 'area = ' || $pi * ($r * $r)
'area = 78.5'
let $pi := 3.14, $area := function ($arg) { 'area = ' || $pi * $arg * $arg }, $r := 5 return $area($r)
'area = 78.5'
The 'instance of', 'cast', 'castable', and 'treat' operators are used in expressions on sequence types.
instance of
'hello world' instance of xs:string
cast as
'6' cast as xs:integer
castable
'hello world' castable as xs:integer
treat as
product/stock treat as xs:integer
Instance of Expression Examples
'hello world' instance of xs:integer
false()
10 instance of xs:integer
true()
(10, 100) instance of xs:integer
false()
(10, 100) instance of xs:integer*
true()
/orders/order instance of element()
false()
/orders/order instance of element()*
true()
/orders/order[1]/date_received instance of xs:date
false()
data(/orders/order[1]/date_received) instance of xs:date
true()
('hello world', 12345) instance of item()*
true()
Cast as and castable Expression Examples
'hello world' cast as xs:integer
err:FORG0001 (Invalid value for cast/constructor error)
'8' cast as xs:integer
8
'10.0E3' cast as xs:double
10000
6.5 cast as xs:integer
6
/orders/order[1]/date_received cast as xs:dateTime
xs:dateTime('2014-06-06T00:00:00')
('2014-02-31') cast as xs:date
err:FORG0001 (Invalid value for cast/constructor error)ror
'hello world' castable as xs:integer
false()
'100' castable as xs:integer
true()