How to add multiple test conditions in 'test' attribute expression of <xsl:if> tag

86,531

Solution 1

<xsl:if test="$var='ab' | $var='bc' | $var='ca' ">

This is wrong -- you are using the XPath union operator | on boolean values.

Solution: use the XPath or operator:

<xsl:if test="$var='ab' or $var='bc' or $var='ca' ">

The above XPath expression (the value of the test attribute) can be optimized, so that only one comparison is made and no or is necessary:

<xsl:if test="contains('|ab|bc|ca|', concat('|', $var, '|'))">

Solution 2

In XSLT 2.0 or above, you can test against a sequence of strings:

<xsl:if test="$var=('ab','bc','ca')">...</xsl:if>
Share:
86,531
User 4.5.5
Author by

User 4.5.5

currently working on widget,plugin development in Jive 4.5.5

Updated on July 28, 2020

Comments

  • User 4.5.5
    User 4.5.5 almost 4 years

    Is there any possibility to include multiple conditions in 'test' attribute expression in <xsl:if> tag of XSLT like

    <xsl:if test="$var='ab' | $var='bc' | $var='ca' ">
    </xsl:if>
    

    I am getting an error as "Expression must be evaluated to a node-set".

    Is there any possibility to code such multiple conditions in <xsl:if> Tag?

    Can anyone explain how can we achieve this multiple condition validation for the following tags in

    <xsl:if> in test attribute expression

    <xsl:for-each> in select attribute expression

    <xsl:template> in match attribute expression

    ?