A. M. Mérida
A. M. Mérida

Reputation: 2618

Regex XSLT, extract match

I am using XSLT 2.0 and trying to extract the caracterer regex needed.

I tried it out with tokenize(), but is more puzzling.

  <xsl:value-of select="tokenize('2.0-Cloverleaf-3d', '-')" />

By the result is: 2.0, the result should be returned is: 3. The string is (2.0-Cloverleaf-*3*d).

I preferred to do it in this way:

  <xsl:if test="matches(x:td/x:p, '.+3d$')">
    true
  </xsl:if>

Is there any function to get the character you need, without requiring an "if"?. Thanks.

EDIT

I think I'll leave it like that.

<xsl:variable name="places">
  <xsl:if test="matches(x:td/x:p, '.+3d$')">3</xsl:if>
  <xsl:if test="matches(x:td/x:p, '.+4d$')">4</xsl:if>
  <xsl:if test="matches(x:td/x:p, '.+5d$')">5</xsl:if>
</xsl:variable>

But if there is a better way to do it. Thank you.

Upvotes: 0

Views: 2144

Answers (2)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

You can do this with tokenize() but collect the tokens into a variable first.

<xsl:variable name="tokens" select="tokenize(x:td/x:p,'-')"/>

Then select the last token and use substring() to get the digit.

<xsl:value-of select="substring(item-at($tokens, 3), 1, 1)" />

Upvotes: 0

BeniBela
BeniBela

Reputation: 16917

You can use replace, to replace everything with the part you need.

In XPath 2:

replace(x:td/x:p, '^.*([0-9])d$', '$1')

In XSL:

<xsl:value-of select="replace(x:td/x:p, '^.*([0-9])d$', '$1')" /> 

Upvotes: 4

Related Questions