XML technologies / XSLT / XSLT Cookbook / Strings
Strings
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
"I believe everybody in the world should have guns.
Citizens should have bazookas and rocket launchers too.
I believe that all citizens should have their weapons of choice.
However, I also believe that only I should have the ammunition.
Because frankly, I wouldn't trust the rest of the goobers with anything
more dangerous than [a] string."
Scott Adams
Introduction
For string manipulation, XSLT 1.0 lacks the heavy artillery available in Perl. XSLT is optimized for XML markup, not strings. Yet XML is structured text, so all but the simplest transformations need string processing. Unfortunately, XSLT 1.0 has only nine standard string functions. Java has about two dozen, while Perl, the undisputed king of modern text-processing languages, has several dozen and advanced regular-expression support.
With implementations of XSLT 2.0, XSLT developers need no longer envy Perl’s strings. XPath 2.0 now has 20 string-related functions, including regular expressions. XSLT 2.0 also parses unstructured text with regular expressions, turning it into proper XML.
XSLT 1.0 programmers needing advanced string processing have two options. First, external functions in Java or another language supported by their XSLT processor. This is convenient when portability is unimportant and powerful manipulation essential. Alternatively, implement the functionality in XSLT itself. This chapter demonstrates common string operations in XSLT 1.0 and simpler solutions in XSLT 2.0.
Advanced XSLT 1.0 string functions combine native functions and recursion, integral to sophisticated XSLT. Recursion is so important that these recipes are useful even without plans to implement your own string processing.
The book also highlights the excellent work of EXSLT.org, a community effort helping standardize XSLT extensions. Visit its website.
Note:
XSLT 2.0 solutions longer than one line use its new ability to write first-class XPath functions in XSLT. The 1.0 solutions instead use named templates, callable only through xsl:call-template.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de