Re: [xsl] Using regular expressions in stylesheet

Subject: Re: [xsl] Using regular expressions in stylesheet
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 17 May 2002 10:50:00 +0100
Hi Trang,

> I am new to XSLT technology and would like to find out whether the
> latest version of XPath already supports regular expressions (I am
> currently using Xalan for transformations).

No -- XPath 1.0 doesn't support regular expressions. It's something
that's promised in XPath 2.0. You could write an extension function
for Xalan that would use Java's regexp processing, possibly.

> I would like to use regular expressions to extract digits out of a
> string containing alphanumeric characters (for example, extract '5'
> out of '5a'). Can you tell me of an appropriate function that allows
> me to do this in my stylesheet?

It depends on how complex the structure of the string is, but you can
probably do this using the basic string manipulation functions in
XPath 1.0. Specifically, you can remove the non-numeric characters
from a string using:

  translate($string, translate($string, '0123456789', ''), '')

The inner translate() removes the numeric characters from the string,
so that you're left with a string that contains all the non-numeric
characters in the string. The outer translate() then removes all those
characters from the original string, so you're left with only the
numeric characters.

Or you could write a recursive template that goes through the string
one character at a time to extract the numeric characters prior to the
first non-numeric character; as I say, it depends on the structure of
your string.



Jeni Tennison

 XSL-List info and archive:

Current Thread