XML technologies / XSLT / XSLT Cookbook / Strings / Regular expressions in use
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
You have heard that regular expressions (regex, regular expressions) are a powerful new tool in XSLT 2.0, but you are unsure how to use that power.
Solution
Matching text patterns
The most basic regex application is matching or filtering text patterns. Use matches( ) in a template pattern to extend XSLT’s filtering to a node’s text:
<!-- Ein Datum der Form May 3, 1964 -->
<xsl:template match="birthday[matches(.,'^[A-Z][a-z]+\s[0-9]+,\s[0-9]+$')]">
<!-- ... -->
</xsl:template>
<!-- Ein Datum der Form 1964-05-03 -->
<xsl:template match="birthday[matches(.,'^[0-9]+-[0-9]+-[0-9]+$')]">
<!-- ... -->
</xsl:template>
<!-- Ein Datum der Form 3 May 1964 -->
<xsl:template match="birthday[matches(.,'^[0-9]+\s[A-Z][a-z]+\s[0-9]+$')]">
<!-- ... -->
</xsl:template>Alternatively, filters can appear in a xsl:if- or xsl:choose instruction:
<xsl:choose>
<xsl:when test="matches($date,'^[A-Z][a-z]+\s[0-9]+,\s[0-9]+$')"> </xsl:when>
<xsl:when test="matches($date,'^[0-9]+-[0-9]+-[0-9]+$')"> </xsl:when>
<xsl:when test="matches($date,'^[0-9]+\s[A-Z][a-z]+\s[0-9]+$')"> </xsl:when>
</xsl:choose>Tokenizing text
Regexes often split a string into tokens:
(: Ein ISO-Datum (YYYY-MM-DD) in eine Sequenz aufteilen, die aus Jahr, Monat, Tag besteht :)
tokenize($date, '-')
(: Ein ISO-DatumUhrzeit (YYYY-MM-DDThh:mm:ss) in eine Sequenz aufteilen, die aus Jahr, Monat, Tag, Stunde, Minute, Sekunde besteht :)
tokenize($date, '-|T|:')
(: Eine Sequenz in Wörter aufteilen :)
tokenize($text, '\W+')
Replacing and extending text
There are two ways to use the XPath function replace( ).
The first simply replaces a string’s matched patterns with other text. Sometimes the replacement is an empty string ('') to remove matching text:
(: Ersetzen des Tages in einem ISO-Datum durch 01 :)
replace($date,'\d\d$','01')
(: Entfernen aller Elemente bis auf das Jahr in einem ISO-Datum :)
replace($date,'-\d\d-\d\d$','')
The second use of replace inserts text at a matched location while leaving the matched part unchanged. Using a function named replace for insertion may seem counterintuitive, but backreferences produce exactly that effect.
(: Einfügen eines Leerzeichens nach Interpunktionszeichen, hinter denen ein Leerzeichen fehlt :)
replace($text, '([,;:])\S', '$1 ')
Parsing text into XML
More powerful than tokenize( ) or replace( ) is the new XSLT 2.0 instruction xsl:analyze-string. It goes beyond text replacement to create new XML content from text. Exploiting XSLT 2.0’s potential provides recipes using xsl:analyze-string.
Discussion
Regexes are such a broad and powerful text-processing tool that they merit a whole book. Indeed, Jeffrey E. F. Friedl’s “Reguläre Ausdrücke” is a classic I highly recommend.
Regexes derive their power from pattern matching, also XSLT’s central strength. XSLT excels at matching patterns in XML structure; regexes are optimized for patterns in ad hoc text. Their pattern language is more complex than XSLT’s XPath expressions, inevitably so because ad hoc text lacks XML’s uniform tree structure.
Mastery requires practice and judicious reuse of others’ examples. Besides Friedl’s book, examples appear in many Perl books and online at RegExLib.com.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de