Skip to content
Language Features

XPath 3.1 Language Features

goxpath supports the full XPath 3.1 expression language including all operators, constructors, and control-flow expressions.

Expressions

Path Expressions

Standard XPath path expressions with all axes:

/root/child                    (: child axis :)
//descendant                   (: descendant-or-self :)
../sibling                     (: parent :)
ancestor::div                  (: ancestor axis :)
following-sibling::p           (: following-sibling :)
preceding::*                   (: preceding axis :)
self::node()                   (: self axis :)

For Expressions

for $x in 1 to 5 return $x * 2
(: Result: 2, 4, 6, 8, 10 :)

for $x in (1,2,3), $y in (10,20) return $x + $y
(: Result: 11, 21, 12, 22, 13, 23 :)

Let Expressions

let $x := 42 return $x * 2
(: Result: 84 :)

let $base := 100, $tax := 0.19 return $base * (1 + $tax)
(: Result: 119 :)

If Expressions

if ($price > 100) then "expensive" else "cheap"

Quantified Expressions

some $x in (1, 2, 3) satisfies $x > 2       (: true :)
every $x in (1, 2, 3) satisfies $x > 0       (: true :)

Operators

String Concatenation (||)

"Hello" || " " || "World"     (: "Hello World" :)

Simple Map Operator (!)

(1, 2, 3) ! (. * 2)           (: 2, 4, 6 :)
("a", "b") ! upper-case(.)    (: "A", "B" :)

Arrow Operator (=>)

"hello world" => upper-case() => tokenize(" ")
(: "HELLO", "WORLD" :)

Range Operator (to)

1 to 5                        (: 1, 2, 3, 4, 5 :)

Comparison Operators

Value comparisons (eq, ne, lt, le, gt, ge) compare single values. General comparisons (=, !=, <, <=, >, >=) compare sequences.

5 eq 5                        (: true :)
(1, 2, 3) = 2                 (: true — existential :)

Node Comparisons

$a is $b                      (: same node identity :)
$a << $b                      (: $a before $b in document order :)

Constructors

Map Constructor

map { "name": "Alice", "age": 30 }
map { 1: "one", 2: "two" }

Array Constructor

[ 1, 2, 3 ]                   (: square array constructor :)
array { 1 to 5 }              (: curly array constructor :)

String Constructor

``[Hello {$name}, you are {$age} years old]``

Functions

Named Function References

upper-case#1                   (: reference to upper-case with arity 1 :)
math:sqrt#1                   (: reference to math:sqrt :)
Q{http://www.w3.org/2005/xpath-functions}abs#1

Inline Functions

function($x) { $x * 2 }
function($x, $y) { $x + $y }
let $double := function($x) { $x * 2 } return $double(21)

Dynamic Function Calls

Function references and inline functions can be called dynamically:

let $f := abs#1 return $f(-5)                  (: 5 :)
let $ops := [abs#1, floor#1] return $ops?1(-3)  (: 3 :)
function-lookup(xs:QName("fn:abs"), 1)(-42)     (: 42 :)

Lookup Operator (?)

Access map entries or array members:

map { "a": 1, "b": 2 } ? a              (: 1 :)
[10, 20, 30] ? 2                         (: 20 :)
$map ?*                                  (: all values :)

Type Expressions

Cast and Castable

"42" cast as xs:integer                  (: 42 :)
"42" castable as xs:integer              (: true :)
xs:date("2024-01-15") cast as xs:string  (: "2024-01-15" :)

Instance Of

42 instance of xs:integer                (: true :)
42 instance of xs:decimal                (: true — integer is subtype :)
xs:long(5) instance of xs:short          (: false — long is not short :)

Treat As

$x treat as xs:integer                   (: passes through or raises XPDY0050 :)

Namespaces

The following namespace prefixes are pre-defined:

Prefix URI
fn http://www.w3.org/2005/xpath-functions
xs http://www.w3.org/2001/XMLSchema
math http://www.w3.org/2005/xpath-functions/math
map http://www.w3.org/2005/xpath-functions/map
array http://www.w3.org/2005/xpath-functions/array