Functions and operators[edit]

XPath 1.0 defines four data types: node-sets (sets of nodes with no intrinsic order), strings, numbers and booleans.

The available operators are:

  • The "/", "//" and "[...]" operators, used in path expressions, as described above.
  • A union operator, "|", which forms the union of two node-sets.
  • Boolean operators "and" and "or", and a function "not()"
  • Arithmetic operators "+", "-", "*", "div" (divide), and "mod"
  • Comparison operators "=", "!=", "<", ">", "<=", ">="

The function library includes:

  • Functions to manipulate strings: concat(), substring(), contains(), substring-before(), substring-after(), translate(), normalize-space(), string-length()
  • Functions to manipulate numbers: sum(), round(), floor(), ceiling()
  • Functions to get properties of nodes: name(), local-name(), namespace-uri()
  • Functions to get information about the processing context: position(), last()
  • Type conversion functions: string(), number(), boolean()


Node set functions[edit]

  • position()
  • returns a number representing the position of this node in the sequence of nodes currently being processed (for example, the nodes selected by an xsl:for-each instruction in XSLT).
  • count(node-set)
  • returns the number of nodes in the node-set supplied as its argument.


String functions[edit]

  • string(object?)
  • converts any of the four XPath data types into a string according to built-in rules. If the value of the argument is a node-set, the function returns the string-value of the first node in document order, ignoring any further nodes.
  • concat(stringstringstring*)
  • starts-with(s1s2)
  • returns true if s1 starts with s2
  • contains(s1s2)
  • returns true if s1 contains s2
  • substring(stringstartlength?)
  • example: substring("ABCDEF",2,3) returns "BCD".
  • substring-before(s1s2)
  • example: substring-before("1999/04/01","/") returns 1999
  • substring-after(s1s2)
  • example: substring-after("1999/04/01","/") returns 04/01
  • string-length(string?)
  • returns number of characters in string
  • normalize-space(string?)
  • all leading and trailing whitespace is removed and any sequences of whitespace characters are replaced by a single space. This is very useful when the original XML may have been prettyprint formatted, which could make further string processing unreliable.



Boolean functions[edit]

  • not(boolean)
  • negates any boolean expression.
  • true()
  • evaluates to true.
  • false()
  • evaluates to false.


Number functions[edit]

  • sum(node-set)
  • converts the string values of all the nodes found by the XPath argument into numbers, according to the built-in casting rules, then returns the sum of these numbers.



Further Information:

https://developer.mozilla.org/en-US/docs/Web/XPath/Functions