user2360414
user2360414

Reputation: 27

Dynamic choose condition in XSLT

How to use the choose condition in XSLT when the below requirement is needed

<xsl:choose>
<xsl:when test="contains(@name,'top %d holdings' ) ">
<!--code-->
</xsl:when>
</xsl:choose>

It should select all the data containing....

  1. top 5 holdings
  2. top 10 holdings
  3. top 30 holdings
  4. top 27 holdings
  5. top * holdings

Upvotes: 1

Views: 261

Answers (2)

Mads Hansen
Mads Hansen

Reputation: 66723

You can use substring-before() and substring-after() to get the text between top and holdings, and then use the translate() function to remove numbers and the * character, and then verify that the result is an empty string.

<xsl:choose>
  <xsl:when 
       test="translate(
               substring-before(substring-after(@name, 'top '), ' holdings' ),
               '0123456789*', 
               '') = '' ">
        <!--code-->
    </xsl:when>
</xsl:choose>

Upvotes: 1

Tim C
Tim C

Reputation: 70618

If you were using XSLT2.0 here, you could use the matches function which allows you to match text by means of a regular expression

<xsl:when test="matches(@name, '.*top \d+ holdings.*')">

On the other hand, if you were using XSLT 1.0, then the matches function is not available. One way you could do it in your very specific case is extract the text before "holdings" that occurs before "top" and check it is a number:

<xsl:when test="string(number(substring-before(substring-after(@name, 'top '), ' holdings' ) )) != 'NaN'">

Upvotes: 1

Related Questions