views:

51

answers:

2

I'm trying to match a pattern into a string in XSLT/XPath using the matches function, as follows:

<xsl:when test="matches('awesome','awe')">
   ...
</xsl:when>

However, in both Firefox 3.5.9 and IE8, it doesn't show up. IE8 tells me that "'matches' is not a valid XSLT or XPath function." Is this due to XSLT 2.0 not being supported, and is there a way around this?

+1  A: 

Regular Expressions are supported only in XSLT 2.x/XPath 2.x.

As at this date, no publicly available browser supports XSLT 2.x/XPath 2.x.

In your concrete case you can use:

starts-with('awesome','awe')

other useful XPath 1.0 functions are:

  • contains()

  • substring()

  • substring-before()

  • substring-after()

  • normalize-space()

  • translate()

  • string-length()

Dimitre Novatchev
Thanks! fn:matches would have been nice, but I think I'll have to make do with contains and customizing my strings.
A: 

In addition to the correct hint that browsers don't support XSLT/XPath 2.0: note that Firefox (since version 3.0 I think) supports some EXSLT functions, in particular for regular expressions: https://developer.mozilla.org/en/EXSLT#Regular_expressions so with Firefox you could consider EXSLT functions. For IE using MSXML you could incorporate http://exslt.org/regexp/functions/match/regexp.match.msxsl.xsl which implements the EXSLT match function using the msxsl:script element.

Martin Honnen