Hobbes
Hobbes

Reputation: 2125

XSLT 'matches' function: can I combine regex with a variable?

I've got an XSLT which compares HTML nodes with a variable value:

<xsl:for-each select="//section[contains(text(), $interval)]">

The $interval variable contains a maintenance interval in hours, so possible values are 100, 250 and 1000. You can see where this goes wrong: if $interval=100 it'll also match sections that contain '1000'.

The HTML:

<section>maintenance interval: 100 hours</section>
<section>maintenance interval: 250 hours</section>
<section>maintenance interval: 1000 hours</section>

So I need a 'whole word' comparison. This would work:

<xsl:for-each select="//section[matches(text(), '\s100\s')]">

But I'd like to keep using the variable. This doesn't work though, as the variable is interpreted as literal text:

<xsl:for-each select="//section[matches(text(), '\s$interval\s')]">

I could add spaces to the variable definition, $interval=' 100 ' but that limits me to using only spaces there.

Is there a way to use variables inside a regular expression?

Upvotes: 0

Views: 318

Answers (1)

sspsujit
sspsujit

Reputation: 301

use concat() for this purpose:

<xsl:for-each select="//section[matches(text(), concat('\s',$interval,'\s'))]">

Upvotes: 2

Related Questions