Wednesday, February 18, 2026

XSLT String Functions

 XSLT  String Functions

Use string functions to perform various manipulations on strings.

For example, you can combine two strings, find the length of a string, extract a specific part of a string, and replace one string with another. You can also convert a numeric value to a string datatype.

The concat() Function

The concat() function combines two or more strings into one string. You can pass more than one string as an argument to this function. 

syntax :
concat(value1,value2,value3,...) string

Use the concat() function when you require multiple <xsl:value-of> elements to display a list of string values.

Ex:  
Doctors.xml File

<?xml version="1.0"?> <Doctor-List> <Doctor> <Name>James Cobot</Name> <Specialist>Trauma </Specialist> </Doctor> <Doctor> <Name>Richard Albert </Name> <Specialist>Heart</Specialist> </Doctor> <Doctor> <Name>Adam Maxton</Name> <Specialist>Brain</Specialist> </Doctor> </Doctor-List>

Doctors_concat.xsl File


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:template match="/"> <html> <body> <center><table border="1"> <tr><td>Name</td><td>Specialist</td></tr> <xsl:apply-templates/> </table></center> </body> </html> </xsl:template> <xsl:template match="*"> <xsl:for-each select="Doctor"> <tr> <td><xsl:value-of select="concat('Dr.',Name)"/></td><td><xsl:value-of select="Specialist"/></td></tr> </xsl:for-each> </xsl:template> </xsl:stylesheet>

contains() Function

The contains() function checks the existence of a substring in a given string. 

syntax 
contains(value,substring)

If the string checked by the contains() function includes a substring, the function returns the Boolean value true. Otherwise, it returns false. 
If the arguments passed to the contains() function are not of the string datatype, then the contains() function converts the arguments passed into the string datatype.

Ex:
For example, if you want to view only those doctors names that contain the word, Albert,

CheckSubstring.xsl File


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:template match="/"> <html> <body> <center><table border="1"> <xsl:apply-templates/> </table></center> </body> </html> </xsl:template> <xsl:template match="*"> <xsl:for-each select="Doctor"> <xsl:if test="contains(Name,'Albert')"> <tr> <td><xsl:value-of select="Name"/></td><td><xsl:value-of select="Specialist"/> Specialist</td> </tr> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>

In the above code, the contains(Name,'Albert') function checks for the string, Albert, in the Name node. If the string Albert exists, then the CheckSubstring.xsl file displays all the information related to doctor ‘Albert’

string() Function

The string() function converts a given value to string data type.

syntax 

string() string(value)

When no parameters are passed, the string() function takes the current node value.



No comments:

Post a Comment

SOA Overview Part-1

  Middleware It provides a mechanism for the process to interact with other processes running on multiple network machines. Advantages...