New features in XPath 3.1
Some of the new features in XPath 3.1 are as follows. For full details, see the W3C specifications, and for further Saxon implementation details see the Conformance section.
-
The arrow operator
=>
is available to allow function chaining. The expressionsubstring-before(substring-after($in, '['), ']')
can now be replaced by the more legible$in => substring-after('[') => substring-before(']')
. -
Maps are available (for more details see Maps in XPath). They provide a similar capability to "objects" in Javascript, or "associative arrays" in some other languages. But as befits a function language like XPath, they are immutable. A collection of functions is available to operate on maps (see the Function Library), and in addition there is new syntax for a map constructor (of the form
map{ key : value, key : value }
) where both the keys and values are arbitrary expressions. There is a sequence type for mapsmap(K, V)
, defining the types of the key and value parts. Maps are functions, so given a map$M
, the entry for a key$K
can be obtained as the result of the function call$M($K)
. If the key to be accessed is known statically, and if it takes the form of anNCName
, then a lookup expression of the form$employee?name
can be used. A unary form of this operator is also available, allowing$employees[?name='John']
. -
Arrays are available (for more details see Arrays in XPath). Arrays differ from the traditional sequences in that they can be nested. The members of an array can be arbitrary values, including sequences and arrays. An array is an item, so some care is needed: the syntax
$A[1]
returns the array item unchanged rather than selecting a member of the array. A library of functions is available to operate on arrays ((see the Function Library). The expression[1, 5, 7, (10 to 20)]
can be used to construct an array where the number of members is known statically (this example creates an array with four members, of which the last is a sequence of 11 items), and the expressionarray{ 1, 5, 7, (10 to 20)}
can be used to construct an array with a variable number of members, provided that each member is a single item (in this case the array has 14 members, each a single integer). A member of an array can be accessed using the syntax$A(3)
or$A?3
.
A number of new functions are available, including random-number-generator()
,
sort()
, parse-ietf-date()
, and functions to read and write
data in JSON format. For details see the Functions
Library.