XPath String Functions

Below are some of the common string functions used in XSLT 1) format-number() – Converts a number into a string 2) starts-with(string1, string2) – Returns true if the first string starts with the second string. 3) contains(string1, string2) – Returns true if the first string contains the second string. 4) normalize-space(string) – Trims the leading and trailing space from string. Also replaces consecutive occurrences of white space with a single space. 5) translate(string1, string2, string3) – Returns string1 after any matching characters in string2 have been replaced by the characters in string3. 6) concat(string1, string2, …) – Concatenates all strings (i.e. joins them together). Some more functions — 7) current() – Returns the current node 8) document() – Used to access the nodes in an external XML document 9) element-available() – Tests whether the element specified is supported by the XSLT processor 10) function-available() – Tests whether the function specified is supported by the XSLT processor 11) generate-id() – Returns a string value that uniquely identifies a specified node 12) key() – Returns a node-set using the index specified by an element

Read More →

Substring function in xslt

Substring function returns the first argument starting at the position specified in the second argument and the length specified in the third argument. string substring(string, number, number) Example –  The following function call returns “456”: substring(“34567”,2,3) The following function call returns “234”: substring(“1234”,2) The following function returns “sheet”: substring(“harsheet”,4) Substring-after substring-after(string,string) The substring-after function returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string. The following function call returns “99/04/01”: substring-after("1999/04/01","19") Substring-before substring-before(string, string) Returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string. The following function call returns “1999”: substring-before("1999/04/01","/")

Read More →

How to get string length

XML to be used for our example: <FoodItems> <Item> <Name>Juice</Name> </Item> <Item> <Name>Bread</Name> </Item> <Item> <Name>Eggs</Name> </Item> <FoodItems> Now use this xml in xslt as <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="FoodItems"> <table border="1" style="background-color:#cccc00"> <tr> <th>Food Item</th> <th>String Length</th> </tr> <xsl:for-each select="FoodItems"> <tr> <td><xsl:value-of select="Name"/></td> <td><xsl:value-of select="string-length(Name)"/></td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet>   Output  FoodItem String Length Juice 5 Bread 5 Eggs 4

Read More →