XPath defines standard functions which can be used in creating expressions. The functions can be divided into below given groups depending on the data types they operate on:

Node functions

The node functions functions relate to the node tree and node set. They count the number of nodes, position of nodes …etc.

The position() function

The position() function determines numeric position of a node from the expression evaluation context.

Example of position() XPath method

//name[position()=5] – This statement matches the 5th name element in the document

The last() function

The last() function determines last node in the node set.

Example of last() XPath method

/document[last()] – Selects last “document” element

The count() function

The count() functions returns the count of nodes in node set.

Example of count() XPath method

/data/employee/office[count(staff)<4]/name – Selects the names of the staff whose count is less than 4

String functions

XPath string functions manipulate strings of text node. Few string functions are:

  • concat() – Concatenate two strings
  • starts-with() – Returns if string begins with other string or not.
  • contains() – Returns true or false if a string is contained in another string or not.
  • substring() – Returns a substring of specified length with given starting index.
  • string.length() – Returns length of the string.

Boolean function

Boolean functions operate on Boolean, “true/false” values. They are:

  • not() – Negates the Boolean value.
  • lang() – Determines if a language is being used. It queries the node to determine the language.

Number function

Few commonly used number functions which operates on numbers are:

  • ceiling() – Returns rounded value to nearest decimal number in upper boundary.
  • floor() – Returns rounded value to nearest decimal number in lower boundary.
  • round() – Rounds a decimal value to nearest integer.
  • sum() – Add a set of numeric value.

 

›› go to examples ››