In programming languages an operator is a symbol or keyword which represents a specific action.
An example of an operator is the '+' symbol which adds two operands together.
An operand is an object upon which the operator operates.
5 > 3
The table below lists the XPath 3.0 operators in order of increasing operator precedence (with 1 being the operator with the lowest precedence and 19 the operator with the highest precedence). Operator precedence determines the order in which operators are evaluated.
1 | , | The comma ',' operator is used in sequence expressions to construct a sequence. |
2 | for | The 'for' operator is used in 'for' expressions to iterate over items in a sequence. |
2 | let | The 'let' operator is used for assigning variables in 'let' expressions. |
2 | some, every | The 'some' and 'every' operators are used in quantified expressions. |
2 | if | The 'if' operator is used in conditional expressions. |
3 | or | The 'or' operator is used in logical expressions. |
4 | and | The 'and' operator is used in logical expressions. |
5 | eq, ne, lt, le, gt, ge | The 'eq', 'ne', 'lt', 'le', 'gt' and 'ge' operators are used in value comparisons. These symbols stand for 'equal', 'not equal', 'less than', 'less than or equal', 'greater than' and 'greater than or equal' respectively. |
5 | =, !=, <, <=, >, >= | The '=' , '!=', '<', '<=', '>', '>=' operators are used in general comparisons. These symbols stand for 'equal', 'not equal', 'less than', 'less than or equal', 'greater than' and 'greater than or equal' respectively. |
5 | is, <<, >> | The 'is', '<<' and '>>' operators are used in node comparisons. |
6 | || | The concat operator '||' was introduced in XPath 3.0. It used in string concatenation expressions to concatenate two strings. |
7 | to | The 'to' operator is used in sequence expressions to construct a sequence of items of type 'xs:integer' from a range specified by the left and right operands. |
8 | +, - (binary) | The '+' and '-' operators i.e. addition and subtraction, are used in arithmetic expressions and operate on two operands. |
9 | *, div, idiv, mod | The '*', 'div', 'idiv' and 'mod' operators i.e. multiplication, division, integer division (rounds result to integer) and modulo (returns remainder of division) operators are used in arithmetic expressions. |
10 | union, | | The 'union' operator which is symbolized by 'union' or '|' is used in sequence combining expressions. |
11 | intersect, except | The 'intersect' and 'except' operators are used in sequence combining expressions. |
12 | instance of | The 'instance of' operator is used in expressions on 'sequenceTypes'. |
13 | treat as | The 'treat as' operator is used in expressions on 'sequenceTypes'. |
14 | castable as | The 'castable as' operator is used in expressions on 'sequenceTypes'. |
15 | cast as | The 'cast as' operator is used in expressions on 'sequenceTypes'. |
16 | +, - (unary) | The '+' and '-' operators can be used in unary form to denote positive or negative values. |
17 | ! | The '!' operator known as the 'map' operator is new to XPath 3.0 and is similar in funcionality to a 'for' expression. |
18 | /, // | The '/' and '//'operators are used in location path expressions. |
19 | [ ] | The '[ ]' operator is used to denote a predicate. |
There are two new operators in XPath 3.0, the concat operator '||' and the map operator '!'.
The '||' operator is convenient because it is no longer necessary to call the concat function to concatenate two strings.
concat('hello', ' world')
('hello world')
'hello' || ' world'
('hello world')
The ''!' operator is similar to a 'for' expression. It allows us to process items in a sequence.
Some of the following examples are based on the XML document below.
for $i in /company/office/employee/first_name return 'Hi ' || $i
('Hi John', 'Hi John', 'Hi Mary' 'Hi Peter', 'Hi Mark')
/company/office/employee/first_name ! ('Hi ' || .)
('Hi John', 'Hi John', 'Hi Mary' 'Hi Peter', 'Hi Mark')
('brother', 'sister', 'mum', 'dad') ! ('Hi ' || .)
('Hi brother', 'Hi sister', 'Hi mum', 'Hi dad')
The following examples are based on the XML document below.
/employees/employee/(@id, @first, @last, @age)
('chris', 'avery', '35', '1', 'smith', 'mike', '2', '25')
/employees/employee!(@id, @first, @last, @age)
('1', 'chris', 'avery', '35', '2', 'mike', 'smith', '25')
!employees/employee
err:XPST0003 (not a valid instance of the grammar)