Regex + XSLT 2.0 problem

Here should go questions about transforming XML with XSLT and FOP.
heavydawson
Posts: 14
Joined: Wed Sep 20, 2006 7:07 pm

Regex + XSLT 2.0 problem

Post by heavydawson »

I have some text I'd like to format, and need to use regex + xslt 2.0 do do it.
What I have is a list of items in the following format:
"xxxxxx\xxxxx\xxxxxxx\xxxx\abcd.java"

I need to remove everything except "abcd".
I'm a complete Regex novice, and even got my hands on a copy of Regex Buddy to help me out, but apparently the Regex implementation in XSLT does not quite implement the full PERL standard.

Can anyone help me out?
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

You should use the xsl:analyze-string element. Look at the regex attribute. The output of the following stylesheet:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:variable name="text" select="'xxxxxx\xxxxx\xxxxxxx\xxxx\abcd.java'"/>
<xsl:analyze-string select="$text" regex="(.*)\\(.*).java">
<xsl:matching-substring>
<javaClass name="{regex-group(2)}"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
is

Code: Select all

<javaClass name="abcd"/>
You can read about matching a regex in XSLT 2.0 here.


Regards,
Sorin
Post Reply