By Brian Schaffner

When using Extensible Stylesheet Language Transformations (XSLT) templates, it’s often necessary to perform transformations that are more complex than simple one-to-one entity maps. Using the functions included in XPath, the XML path language, you can create sophisticated translations without the need for external programs. In this article, we’ll look at some of the functions XPath provides for working with string data.

Contains(string1, string2)
One of the most common programming tasks is determining whether a string contains another string. The contains() function returns either true or false depending on whether string1 contains string2. You can use this function in a scenario where you want to translate a single-character Boolean value into a consistent value. For instance, suppose a binary value is stored as a flag in a certain field. Because of inconsistencies in the data, the values might be 1 and 0, Y and N, or T and F. The following example illustrates how to convert all of these values to a consistent scheme using the contains() function:
<xsl:if test=’contains(“TtYy1”, /CustomerRecord/IsBillable)
‘>YES</xsl:if><xsl:if test=’contains
(“FfNn0”, /CustomerRecord/IsBillable)’>NO</xsl:if>

Concat(string1, string2, string3…)
The concat() function concatenates multiple strings. This function is useful for combining various elements into a single element. You can concatenate as many strings as you like. Each string should be passed as a separate argument to the function. Suppose you wanted to combine the FirstName and LastName elements for an address. The following example illustrates this use:
<xsl:value-of select=’concat(/CustomerRecord/FirstName, ” “, 

Substring(string1, number1, number2)
The substring() function lets you extract a smaller substring from an existing string. The substring is the value of string1, starting at the character at number1 and going for a length of number2. The number2 argument is optional. If omitted, the length goes to the end of string1. This function is useful for extracting data from fixed-length strings. For instance, suppose a customer ID field contains data such as the customer’s state and area code followed by a five-digit customer number. You could use the following to extract the customer number:
<xsl:value-of select=’substring
(/CustomerRecord/CustomerId, 6, 5)’/>

The string-length() function returns the number of characters in the provided string. This can be useful for validating data as well as data types. For example, suppose you are integrating two systems and each uses a different number of digits for the customer ID (which might contain the customer number). This function will allow you to extract the correct substring based on the length of the customer ID:
<xsl:if test=’string-length
<xsl:value-of select=’substring(/CustomerRecord/CustomerId, 6, 5)’/>
<xsl:if test=’not(string-length(/CustomerRecord/CustomerId)=10)’>
<xsl:value-of select=’substring(/CustomerRecord/CustomerId, 1, 5)’/>

Translate(string1, string2, string3)
The translate() function provides a method for finding and replacing characters in a string. The result of this function is that for each character in string2, its respective character in string3 is replaced in string1. For example, suppose you wanted to replace all of the spaces with underscores and all of the colons with commas:
<xsl:value-of select=’translate
(/CustomerRecord/RecordType, ” “, “_”)’/>
<xsl:value-of select=’translate
(/CustomerRecord/RecordData, “:”, “,”)’/>

The XPath language provides a handy set of functions for working with XML data within XSLT templates. Using the string functions, you can easily create advanced data translations. In this article, we’ve illustrated how to use five of the most popular XPath string functions.