XPath String Functions

A list of XPath string functions:

Index Function Description
1) starts-with(string1, string2) It returns true when first string starts with the second string.
2) contains(string1, string2) It returns true when the first string contains the second string.
3) substring(string, offset, length?) It returns a section of the string. The section starts at offset up to the length provided.
4) substring-before(string1, string2) It returns the part of string1 up before the first occurrence of string2.
5) substring-after(string1, string2) It returns the part of string1 after the first occurrence of string2.
6) string-length(string) It returns the length of string in terms of characters.
7) normalize-space(string) It trims the leading and trailing space from string.
8) translate(string1, string2, string3) It returns string1 after any matching characters in string2 have been replaced by the characters in string3.
9) concat(string1, string2, ...) It is used to concatenate all strings.
10) format-number(number1, string1, string2) It returns a formatted version of number1 after applying string1 as a format string. String2 is an optional locale string.

XPath String Function Example

Let's take an example to create a table of <employee> element with their names and length of names, by iterating over each employee. It calculates length of the employee name after concatenating firstname and lastname and then displays the employee details.

Example.xml

snippet
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
   <employee id = "001">
      <firstname>Abhiram</firstname>
      <lastname>Kushwaha</lastname>
      <nickname>Manoj</nickname>
      <salary>15000</salary>
   </employee>
   <employee id = "002">
      <firstname>Akash</firstname>
      <lastname>Singh</lastname>
      <nickname>Bunty</nickname>
      <salary>25000</salary>
   </employee>
    <employee id = "003">
      <firstname>Brijesh</firstname>
      <lastname>Kaushik</lastname>
      <nickname>Ballu</nickname>
      <salary>20000</salary>
   </employee>
    <employee id = "004">
      <firstname>Zoya</firstname>
      <lastname>Mansoori</lastname>
      <nickname>Sonam</nickname>
      <salary>30000</salary>
   </employee>
</class>

Example.xsl

snippet
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
   <xsl:template match = "/">
      <html>
         <body>
            <h2>Employee</h2>				
            <table border = "1">
               <tr bgcolor = "pink">	
                  <th>Name</th>     
                  <th>Length of Name</th>
               </tr>					
               <xsl:for-each select = "class/employee">				
                  <tr>
                     <td><xsl:value-of select = "concat(firstname,' ',lastname)"/></td>
                     <td><xsl:value-of select = "string-length(concat(firstname,' ',lastname))"/></td>
                  </tr>	
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output:

XPATH String function 1
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +