Online XPath tester








What is XPath?

XPath is a query language used for selecting nodes from XML and processing them.
It may perform operations on strings, numbers and boolean values.

XPath 2.0 introduced many new features XQuery-cośtam:
- Added support for all XML simple types
- Many new functions (tripled instruction count)
- All expressions evaluate to sequence
- Introduces conditional expressions and for-loops

XPath 3.0
- Dynamic function calls (function may be called without being referenced by name (find function in collection and call)
- Inline functions
- Namespace literals - Namespace may be embedded into function name
- Support for union types - collections containing elements of different types
- Mapping operator - '!' performs evaluation for each element in sequence and concatenates results
- Introduced maps

XPath 3.1
- New operator for function chaining '=>'
- Introduced maps that store data in pair 'key:value' - 'map{ key : value, key : value }'
- Introduced arrays - they differ from sequences in that they can be nested 'array{1, 5, 7, (10 to 20)}'

XPath 1.0 & 2.0 functions

fn:position()
Returns the position of the current context node

W3C Documentation reference: Node-Set-Functions
fn:count(node-set)
Returns the number of nodes in the node-set
Arguments and return type:
Type Description
node-set Node-set to count nodes in
Examples:
Expression Result
count(//b:book) 5
count(//person[@id>5]) 17

W3C Documentation reference: Node-Set-Functions
fn:id(object)
Returns the element specified by it's unique id, requires DTD

W3C Documentation reference: Node-Set-Functions
fn:local-name(node-set)
Returns the local-name for the first node in the node-set
Arguments and return type:
Type Description
node-set Extract first node and return its local name
Examples:
Expression Result
local-name(//b:books) b:book
local-name(//b:book) b:title

W3C Documentation reference: Node-Set-Functions
fn:local-name()
Returns the local-name for the context node

W3C Documentation reference: Node-Set-Functions
fn:namespace-uri(node-set)
Returns the namespace-uri for the first node in the node-set
Arguments and return type:
Type Description
node-set Extract first node and return the namespace URI
Examples:
Expression Result
namespace-uri(//b:book) http://www.book.com

W3C Documentation reference: Node-Set-Functions
fn:namespace-uri()
Returns the namespace-uri for the context node

W3C Documentation reference: Node-Set-Functions
fn:name(node-set)
Returns the name for the first node in the node-set
Arguments and return type:
Type Description
node-set Extract first node and return QName
Examples:
Expression Result
name(//b:books/*) b:book
name(//b:book/*) b:title

W3C Documentation reference: Node-Set-Functions
fn:name()
Returns the name for the context node
Examples:
Expression Result
current context node Extract first node and return QName

W3C Documentation reference: Node-Set-Functions
fn:boolean(object)
The boolean function converts its argument to a boolean as follows:
  • a number is true if and only if it is neither positive or negative zero nor NaN
  • a node-set is true if and only if it is non-empty
  • a string is true if and only if its length is non-zero
  • an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type
Arguments and return type:
Type Description
object The object to convert to a boolean
Examples:
Expression Result
boolean("Release11") true
boolean("") false

W3C Documentation reference: Boolean-Functions
fn:not()
The not function returns true if its argument is false, and false otherwise.

W3C Documentation reference: Boolean-Functions
fn:true()
The true function returns true.

W3C Documentation reference: Boolean-Functions
fn:false()
The true function returns false.

W3C Documentation reference: Boolean-Functions
fn:lang(string)
The lang function returns true or false depending on whether the language of the context node as specified by xml:lang attributes is the same as or is a sublanguage of the language specified by the argument string. The language of the context node is determined by the value of the xml:lang attribute on the context node, or, if the context node has no xml:lang attribute, by the value of the xml:lang attribute on the nearest ancestor of the context node that has an xml:lang attribute. If there is no such attribute, then lang returns false. If there is such an attribute, then lang returns true if the attribute value is equal to the argument ignoring case, or if there is some suffix starting with - such that the attribute value is equal to the argument ignoring that suffix of the attribute value and ignoring case.
Arguments and return type:
Type Description
string Language that will be looked for in context node
Examples: Look W3C documentation

W3C Documentation reference: Boolean-Functions
fn:last()
Returns the position of the last node in the context list

W3C Documentation reference: Node-Set-Functions
fn:boolean(object)
The boolean function converts its argument to a boolean as follows:
  • a number is true if and only if it is neither positive or negative zero nor NaN
  • a node-set is true if and only if it is non-empty
  • a string is true if and only if its length is non-zero
  • an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type
Arguments and return type:
Type Description
object The object to convert to a boolean
Examples:
Expression Result
boolean("Release11") true
boolean("") false

W3C Documentation reference: Boolean-Functions
fn:string(object)
Returns the string representation of the object argument
Arguments and return type:
Type Description
string The object to convert to a string
Examples:
Expression Result
string((1<0)) false
string(.11) 0.11

W3C Documentation reference: String-Functions
fn:number(object)
The number function converts its argument to a number as follows:
  • a string that consists of optional whitespace followed by an optional minus sign followed by a Number followed by whitespace is converted to the IEEE 754 number that is nearest (according to the IEEE 754 round-to-nearest rule) to the mathematical value represented by the string; any other string is converted to NaN
  • boolean true is converted to 1; boolean false is converted to 0
  • a node-set is first converted to a string as if by a call to the string function and then converted in the same way as a string argument
  • an object of a type other than the four basic types is converted to a number in a way that is dependent on that type
Arguments and return type:
Type Description
object The object to convert to a number
Examples:
Expression Result
boolean("Release11") true
boolean("") false

W3C Documentation reference: Numeric-Functions
fn:avg((arg,arg,...))
Returns the average of the argument values
Arguments and return type:
Type Description
sequence* returns average value of provided elements
Examples:
Expression Result
avg((1,2,3)) 2

W3C Documentation reference: #func-avg
fn:adjust-date-to-timezone(date,timezone)
If the timezone argument is empty, it returns a date without a timezone. Otherwise, it returns a date with a timezone
Arguments and return type:
Type Description
date date to be adjusted
timezone timezone to be imposed into date
Examples:
Expression Result
adjust-date-to-timezone(xs:date('2011-11-15'), xs:dayTimeDuration("PT10H")) 2011-11-15+10:00

W3C Documentation reference: #func-adjust-date-to-timezone
fn:error()
https://www.w3.org/TR/xpath-functions/#func-error

W3C Documentation reference: #func-error
fn:nilled(node)
Returns a Boolean value indicating whether the argument node is nilled

W3C Documentation reference: #func-nilled
fn:last()
Returns the position of the last node in the context list

W3C Documentation reference: Node-Set-Functions
fn:boolean(object)
The boolean function converts its argument to a boolean as follows:
  • a number is true if and only if it is neither positive or negative zero nor NaN
  • a node-set is true if and only if it is non-empty
  • a string is true if and only if its length is non-zero
  • an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type
Arguments and return type:
Type Description
object The object to convert to a boolean
Examples:
Expression Result
boolean("Release11") true
boolean("") false

W3C Documentation reference: Boolean-Functions
fn:string(object)
Returns the string representation of the object argument
Arguments and return type:
Type Description
string The object to convert to a string
Examples:
Expression Result
string((1<0)) false
string(.11) 0.11

W3C Documentation reference: String-Functions
fn:number(object)
The number function converts its argument to a number as follows:
  • a string that consists of optional whitespace followed by an optional minus sign followed by a Number followed by whitespace is converted to the IEEE 754 number that is nearest (according to the IEEE 754 round-to-nearest rule) to the mathematical value represented by the string; any other string is converted to NaN
  • boolean true is converted to 1; boolean false is converted to 0
  • a node-set is first converted to a string as if by a call to the string function and then converted in the same way as a string argument
  • an object of a type other than the four basic types is converted to a number in a way that is dependent on that type
Arguments and return type:
Type Description
object The object to convert to a number
Examples:
Expression Result
boolean("Release11") true
boolean("") false

W3C Documentation reference: Numeric-Functions
fn:adjust-date-to-timezone(date,timezone)
If the timezone argument is empty, it returns a date without a timezone. Otherwise, it returns a date with a timezone
Arguments and return type:
Type Description
date date to be adjusted
timezone timezone to be imposed into date
Examples:
Expression Result
adjust-date-to-timezone(xs:date('2011-11-15'), xs:dayTimeDuration("PT10H")) 2011-11-15+10:00

W3C Documentation reference: #func-adjust-date-to-timezone
fn:error()
https://www.w3.org/TR/xpath-functions/#func-error

W3C Documentation reference: #func-error
fn:nilled(node)
Returns a Boolean value indicating whether the argument node is nilled

W3C Documentation reference: #func-nilled
fn:for-each(sequence*, function)
Applies function item to every element in sequence

W3C Documentation reference: #func-for-each