XPath Functions

XPath have some core XPath functions like Node functions, Numeric functions, String functions, Boolean functions. You must have to write XPath function name in lowercase letter.

XPath Functions with Examples

XPath functions divide following four different types,

XPath Functions with Examples

Following XML document is our example document we are apply function on this document.

jobsalary.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
  <head>
    <title>Salary Report</title>
    <style type="text/css">
    body{
      font-family:'Arial',verdana,sans-serif;
      font-size:14px;
    }
    h3 {
      font-size:20px;
      font-weight: normal;
      color: #75A54B;
      border-bottom: 2px solid #EFF0F1;
    }
    table tr th{
      background-color:#99CC66;
    }
    </style>
  </head>
  <body>
    <h3>Salary Report</h3>
    <table width="100%">
      <tr>
        <th>Name</th>
        <th>Salary (€)</th>
        <th>Address</th>
        <th>Contact Number</th>       
      </tr> 
      <xsl:for-each select="jobsalary/report">
        <tr>
          <td><xsl:value-of select="name"/></td>
          <td><xsl:value-of select="salary"/></td>
          <td><xsl:value-of select="address"/></td>
          <td><xsl:value-of select="contact"/></td>
        </tr>
        </xsl:for-each> 
    </table>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>
jobsalary.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="jobsalary.xsl"?>
<jobsalary>
  <report id="1">
    <name>Opal Kole</name>
    <salary>1130.00</salary>
    <address>63 street Ct.</address>
    <contact>000-444-8291</contact>       
  </report>
  <report id="2">
    <name>Max Miller</name>
    <salary>1210.00</salary>
    <address>41 NEW ROAD.</address>
    <contact>000-444-8736</contact>   
  </report>
  <report id="3">
    <name>Beccaa Moss</name>
    <salary>1375.00</salary>
    <address>2500 green city.</address>
    <contact>000-444-8030</contact>       
  </report>
  <report id="4">
    <name>Paul Singh</name>
    <salary>1300.00</salary>
    <address>1343 Prospect St</address>
    <contact>000-444-8029</contact>       
  </report> 
  <report id="5">
    <name>Ken Myer</name>
    <salary>1250.00</salary>
    <address>137 Clay Road</address>
    <contact>000-444-7972</contact>       
  </report>     
</jobsalary>

Run it...   »

Node Functions

XPath node function provide the node related function like last(), position(), id(), count().

node(): function return node value.

text(): function return text value of specific node.

comment(): function matches comment node and return that specific comment node.

last(): function return size of total context in given context. name of this function last so it's means function not return last node value.

position(): function return the position of an element in the set (list) of elements in a given context.

id(dtd_id): function return nodes base on passed DTD unique ID.

name(node_expression): function return the string name of the last expression (node set).

local-name(node_expression): function return the local string name of the last expression.

<xsl:value-of select="position()"/>
<xsl:value-of select="name/text()"/>
<xsl:value-of select="salary/node()"/>
<xsl:value-of select="last()"/>
<p>name() function example : <xsl:value-of select="name(//report/salary)"/> </p>
<p>local-name() function example : <xsl:value-of select="local-name(//report/salary)"/> </p>

Run it...   »


name() and local-name() both function return same string name.

namespace-uri(node_expression) : function return the namespace URL of expression node otherwise return empty value.


Numeric functions

XPath numeric function provide a mathematical function like number(), sum(), div, floor(), ceiling(), round().

count(node_expression): function count number of element in a specified node.

sum(node_expression): function return the sum of element value in a specified node.

div: XPath div function does not take any parameter its you between two numeric functions. and given to a divided value.

<p>Total Employee : <xsl:value-of select="count(//report)"/> </p>
<p>Total Given Salary : <xsl:value-of select="sum(//report/salary)"/> €</p>
<p>Average Salary : <xsl:value-of select="sum(//report/salary) div count(//report)"/> €</p>

Run it...   »


number(): XPath number function converting string to a number.

floor(): XPath foor function return largest integer round value that is equal to or less then to a parameter value. Another way we can say last round value that value return of the function.
eg. FLOOR(10.45) return 10.

ceiling(): XPath ceiling function return smallest integer round value that is greater then or equal to a parameter value. Another way we can say next round value that value return of the function.
eg. CEILING(10.45) return 11.

round(): XPath round function return the round number of specified nth number of decimal place.
eg. ROUND(10.45) return 10.

<p>10.45 string to number : <xsl:value-of select="number('10.45')"/> </p>
<p>10.45 floor Value : <xsl:value-of select="floor(10.45)"/> </p>
<p>10.45 ceiling Value : <xsl:value-of select="ceiling(10.45)"/> </p>
<p>10.45 round Value : <xsl:value-of select="round(10.45)"/> </p>

Run it...   »

String functions

string(): XPath string function converting number to a string.

concat(string, ...): XPath concat function concatenated number of arguments and return to a concatenated string.

starts-with(string, string): XPath start-with function return True/False. Return True if second argument string is start with first argument.

contains(string, string) - XPath contains function return True/False. Return True if second argument string is a contain of first argument.

substring(string, number, number) - XPath substring() function return a selected string character from a full string.

Parameters:

  • string is original string.
  • start_position is position to start substring.
  • substring_length is number of character we are extract from the original string (Optional).

string-length(string): XPath string-length function return the length of string.

substring-after(string, string): XPath substring-after function return the substring of the first argument string base on first occurrence of the second argument string after all character.

substring-before(string, string): XPath substring-before function return the substring of the first argument string base on first occurrence of the second argument string before all character.

normalize-space(string): XPath normalize-space function sequence of whitespace combine into single normalize space and removing leading and trailing whitespace.

<p>concat() function example : <xsl:value-of select="concat('You are ','learning ','XML XPath lesson.')"/> </p>
<p>starts-with() function example : <xsl:value-of select="starts-with('You are learning XML XPath lesson.','You')"/> </p>
<p>contains() function example : <xsl:value-of select="contains('You are learning XML XPath lesson.','XPath')"/> </p>
<p>substring() function example : <xsl:value-of select="substring('You are learning XML XPath lesson','18','9')"/> </p>
<p>string-length() function example : <xsl:value-of select="string-length('You are learning XML XPath lesson.')"/> </p>
<p>substring-after() function example : <xsl:value-of select="substring-after('March 14, 1879', ',')"/> </p>
<p>substring-before() function example : <xsl:value-of select="substring-before('March 14, 1879', ',')"/> </p>
<p>normalize-space() function example : <xsl:value-of select="normalize-space('    XPath       function     example     ')"/> </p>

Run it...   »


Boolean functions

XPath Boolean functions are used for convert argument(as number, string) to a boolean and return either True or False.

boolean(number|string|node-expression|object): function convert to it’s argument to a boolean.

Possible condition :

  • number returns True if number value does not zero, NaN, negative.
  • string returns True if string length does not zero.
  • node-expression returns True if node-expression referred node does not empty. object convert into dependent type.
  • object convert into dependent type.

not(): not function returns true, If it’s argument is false. Otherwise return false.

true(): true() function returns true if passed string is a normal string.

false(): false() function returns false if passed string is not a normal string.

lang(): lang() function returns true if context node language same as specified string argument.

Example:

<p xml:lang="en-us">salary</p>
<p xml:lang="en">salary</p>
<p xml:lang="EN">salary</p>