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.


<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
   <employee id = "001">
   <employee id = "002">
    <employee id = "003">
    <employee id = "004">


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


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 +