1. Operators

    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
    In the example expression above, the integers 5 and 3 are the operands and '>' is the operator.
    1. XPath Operators

      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.
      2forThe 'for' operator is used in 'for' expressions to iterate over items in a sequence.
      2letThe 'let' operator is used for assigning variables in 'let' expressions.
      2some, everyThe 'some' and 'every' operators are used in quantified expressions.
      2ifThe 'if' operator is used in conditional expressions.
      3orThe 'or' operator is used in logical expressions.
      4andThe 'and' operator is used in logical expressions.
      5eq, ne, lt, le, gt, geThe '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.
      5is, <<, >>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.
      7toThe '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, modThe '*', '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.
      10union, |The 'union' operator which is symbolized by 'union' or '|' is used in sequence combining expressions.
      11intersect, exceptThe 'intersect' and 'except' operators are used in sequence combining expressions.
      12instance ofThe 'instance of' operator is used in expressions on 'sequenceTypes'.
      13treat asThe 'treat as' operator is used in expressions on 'sequenceTypes'.
      14castable asThe 'castable as' operator is used in expressions on 'sequenceTypes'.
      15cast asThe '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.
      1. New operators in XPath 3.0

        There are two new operators in XPath 3.0, the concat operator '||' and the map operator '!'.

        1. Concat operator

          The '||' operator is convenient because it is no longer necessary to call the concat function to concatenate two strings.

          concat('hello', ' world')
          result:
          ('hello world')
          This example shows the conventional way of concatenating strings by calling the 'concat' function
          'hello' || ' world'
          result:
          ('hello world')
          This example shows how to concatenate strings by using the '||' operator. This is a much simpler syntax than having to explicitly call the built-in 'concat' function.
        2. Map operator

          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
          result:
          ('Hi John', 'Hi John', 'Hi Mary' 'Hi Peter', 'Hi Mark')
          This example shows how to process each item in a sequence by using a 'for' expression.
          /company/office/employee/first_name ! ('Hi ' || .)
          result:
          ('Hi John', 'Hi John', 'Hi Mary' 'Hi Peter', 'Hi Mark')
          This example shows an alternate way of processing each item in a sequence by using the '!' operator. Using the map operator is less verbose and it is not necessary to bind variables as in the 'for' expression.
          ('brother', 'sister', 'mum', 'dad') ! ('Hi ' || .)
          result:
          ('Hi brother', 'Hi sister', 'Hi mum', 'Hi dad')
          '!' is similar in nature to the '/' used in location paths, but there are differences. Whereas everything to the left of '/' in an XPath expression must evaluate to a sequence of nodes, the sequence to the left of '!' can contain any items. This example has a sequence of strings to the left of '!'. The concatenation operation to the right of '!' is applied to each item in the sequence on the left.

          The following examples are based on the XML document below.

          /employees/employee/(@id, @first, @last, @age)
          result:
          ('chris', 'avery', '35', '1', 'smith', 'mike', '2', '25')
          Attribute order is undefined in XML. Using '/' in a location path will result in an XPath returing the results in document order i.e. the order in which the nodes appear in the source document.
          /employees/employee!(@id, @first, @last, @age)
          result:
          ('1', 'chris', 'avery', '35', '2', 'mike', 'smith', '25')
          In contrast with the previous example, if you want the resulting sequence to return items in a specific order as opposed to document order '!' can be used instead of '/'.
          !employees/employee
          result:
          err:XPST0003 (not a valid instance of the grammar)
          '!' can be used to replace the location path '/' everywhere except where '/' is used to represent the document node i.e. at the beginning of an XPath expression. This example shows an invalid XPath expression and returns an error.