Using XPath string functions in XSLT templates
Takeaway: Mapping XML data to target fields in another system can get tricky when there isn't a one-to-one match. Fortunately, XPath includes string functions that will help you perform advanced string translations. See how you can put these functions to work.
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:
<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 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:
<FullName>
<xsl:value-of select='concat(/CustomerRecord/FirstName, " ",
/CustomerRecord/LastName)'/>
</FullName>
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:
<CustomerNumber>
<xsl:value-of select='substring
(/CustomerRecord/CustomerId, 6, 5)'/>
</CustomerNumber>
String-length(string1)
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:
<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 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:
<CustomerFileName>
<xsl:value-of select='translate
(/CustomerRecord/RecordType, " ", "_")'/>
</CustomerFileName>
<CustomerData>
<xsl:value-of select='translate
(/CustomerRecord/RecordData, ":", ",")'/>
</CustomerData>
Summary
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.
White Papers, Webcasts, and Downloads
- Building the Virtualized Enterprise with VMware Infrastructure VMware This paper explains how adopting a virtual infrastructure -- comprised of server, storage, and networking virtualization technologies -- can help your organization build a sustainable competitive ... Download Now
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
- The True Costs of Virtual Server Solutions VMware Discover ways to streamline and simplify your assessment of the total acquisition costs of a server virtualization environment. Download Now
- Email Security and Archiving - Clearer in the Cloud Google The time is NOW for businesses and organizations of all sizes to implement ... Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET


