Using XPath string functions in XSLT templates
When using 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 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
provided by XPath for working with string data.
Contains(string1,
string2)
One of the most
common tasks is determining if a string contains another string. The contains()
function returns either true or false depending on whether string1 contains
string2. This function can be used in a scenario where you want to translate a
single-character Boolean value into a consistent value. For example, 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.
Example:
<IsBillable>
<xsl:if test='contains("TtYy1", /CustomerRecord/IsBillable)'>YES</xsl:if>
<xsl:if test='contains("FfNn0", /CustomerRecord/IsBillable)'>NO</xsl:if>
</IsBillable>
Concat(string1,
string2, string3, ...)
The concat() function
concatenates, or appends, multiple strings together. 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 a separate argument to
this function. Suppose you wanted to combine the FirstName and LastName
elements for an address. The following example illustrates this use.
Example:
<FullName>
<xsl:value-of select='concat(/CustomerRecord/FirstName, " ", /CustomerRecord/LastName)'/>
</FullName>
Substring(string1,
number1, number2)
The substring()
function provides the capability to 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 example, suppose a
customer ID field contains data such as the customer's state and area code
followed by a 5-digit customer number. You could use the following to extract
the customer number:
Example:
<CustomerNumber>
<xsl:value-of select='substring(/CustomerRecord/CustomerId, 6, 5)'/>
</CustomerNumber>
String-length(string1)
The
string-length() function is fairly simple. This 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 different
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.
Example:
<CustomerNumber>
<xsl:if test='string-length(/CustomerRecord/CustomerId)=10'>
<xsl:value-of select='substring(/CustomerRecord/CustomerId, 6, 5)'/>
</xsl:if>
<xsl:if test='not(string-length(/CustomerRecord/CustomerId)=10)'>
<xsl:value-of select='substring(/CustomerRecord/CustomerId, 1, 5)'/>
</xsl:if>
</CustomerNumber>
Translate(string1,
string2, string3)
The translate()
function provides a method for searching 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.
Example:
<CustomerFileName>
<xsl:value-of select='translate(/CustomerRecord/RecordType, " ", "_")'/>
</CustomerFileName>
<CustomerData>
<xsl:value-of select='translate(/CustomerRecord/RecordData, ":", ",")'/>
</CustomerData>
Summary
The XPath language provides a nice set of functions for working with XML data within XSL 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.