XPATH Functions

XPATH FunctionsXPATH has a rich set of functions that can be used to evaluate, manipulate and otherwise traverse an XML document.

XPATH functions are grouped by and return one of the following four types:

  • Boolean
  • String
  • Number
  • Node-Set

XAPTH functions are NOT strongly typed; the XPATH processor will attempt to do the necessary translations where appropriate and where it can. The function will fail if the XPATH processor is unable to do the translation. In such cases the function will return the default value or indicate an error condition, eg. NaN.

XPATH Functions: Boolean

Boolean functions return values of type boolean – that is either true or false.

true()returns true
false()returns false
not()takes a boolean argument and returns the inverse value.
boolean()Takes an argument and converts it into a boolean value.

  • 0 and NaN return false; any other number returns true
  • Nodes return false if they are empty; otherwise the return is true
  • Empty strings return false; otherwise they return true. Note: By this rule the string ‘false’ will return true.

XPATH Functions: String

XPATH String functions are typically used to manipulate text values:

substring-before(s1, s2)Returns the substring of s1 that precedes the
first occurrence of s2
substring-after(s1, s2)Returns the substring of s1 that follows the first
occurrence of s2
translate(s1, s2, sr)Looks in s1 for any characters in s2 and
replaces them with characters in s3
string-length(s)Returns the number of characters in the
substring(s, index, [length])Returns the substring of s starting at index,
continuing for length characters
starts-with(s1, s2)Returns true if s1 starts with s2; otherwise returns false.
contains(s1, s2)Returns true if s2 is a substring of s1; otherwise returns false.

XPATH Functions: Number

XPATH provides some simple numeric functions:

number()Returns the numeric value of the argument
abs()Returns the absolute value of the argument
floor()Returns the smallest integer that is greater than the number
ceiling()Returns the largest integer that is not greater than the number
round()Rounds the number argument to the nearest integer
sum()Adds up the parameters passed in and returns a sum

XPATH Functions: Node-Set

count()Returns the number of nodes in a node-set.
position()Returns a number equal to the current position of a node in the node-set; i.o.w. returns the index value of the node.

Posted in XPATH and tagged .

Leave a Reply