Introduction


When it comes to manipulating strings, XSLT 1.0 certainly lacks the heavy artillery of Perl. XSLT is a language optimized for processing XML markup, not strings. However, since XML is simply a structured form of text, string processing is inevitable in all but the most trivial transformation problems. Unfortunately, XSLT 1.0 has only nine standard functions for string processing. Java, on the other hand, has about two dozen, and Perl, the undisputed king of modern text-processing languages, has a couple dozen plus a highly advanced regular-expression engine.

With the emergence of XSLT 2.0 implementations, XSLT developers can dispense with their Perl string envy. XPath 2.0 now provides 20 functions related to string processing. The functions include support for regular expressions. In addition, XSLT 2.0 adds facilities for parsing unstructured text via regular expressions so it can be converted to proper XML.

XSLT 1.0 programmers have two choices when they need to perform advanced string processing. First, they can call out to external functions written in Java or some other language supported by their XSLT processor. This can be extremely convenient if portability is not an issue and fairly heavy-duty string manipulation is needed. Second, they can implement the advanced string-handling functionality directly in XSLT. This chapter shows that quite a bit of common string manipulation can be done within the confines of XSLT 1.0 and also how the same problems are more easily handled in XSLT 2.0.

You can implement advanced string functions in XSLT 1.0 by combining the capabilities of the native string functions and by exploiting the power of recursion, which is an integral part of all advanced uses of XSLT. In fact, recursion is such an important technique in XSLT that it is worthwhile to look through some of these recipes even if you have no intention of implementing your string-processing needs directly in XSLT.

This book also refers to the excellent work of EXSLT.org, a community initiative that helps standardize extensions to the XSLT language. You may want to check out their site at http://www.exslt.org.

When I implement a solution in XSLT 2.0 that is more than a line or so of code, I use the new XSLT 2.0 ability to write first-class XPath functions in XSLT. By contrast, the 1.0 solutions use named templates, which you can invoke only via xsl:call-template.





XSLT Cookbook
XSLT Cookbook: Solutions and Examples for XML and XSLT Developers, 2nd Edition
ISBN: 0596009747
EAN: 2147483647
Year: 2003
Pages: 208
Authors: Sal Mangano

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net