Prince
Prince

Reputation: 33

Why is my XSLT test using logical OR failing?

I need to check 3 conditions, if any of the condition is true I need to execute some some stuff, I've tried below:

Value='Java'; 

<xsl:if  test="Value!='Java'  or Value!='Python' or Value='James' ">
    Do some Stuff.... 
</xsl:if>

If I'm passing Java as Value it is going inside if...anything incorrect?

Upvotes: 0

Views: 607

Answers (1)

kjhughes
kjhughes

Reputation: 111491

You have a logical error and possibly a misunderstanding about the way variables work in XSLT.

Problem #1: Logical Error

Of course your test is passing when Value is 'Java' because then it necessarily does not equal 'Python' and therefore the second clause passes and the logical OR expression is then true and the test passes.

If you want to make sure the test passes only when Value is some other value, then use a conjunction rather than a disjunction:

<xsl:if test="Value!='Java' and Value!='Python' and Value!='James' ">

   <!-- do something -->

</xsl>

This way the condition will pass if Value is anything other than 'Java', 'Python', or 'James'.


Update: Per OP comment:

Used and as suggested by you but when I pass SAP as Value it is going inside if condition.. As per my logic it should go only incase of James

When Value is SAP, it is none of Java, Python, or James, so of course the condition will be true. If you want it to go on only in the case of James, then simplify the condition to

<xsl:if test="Value='James'">

   <!-- do something -->

</xsl>

Problem #2: Misunderstanding about Variables in XSLT

In

<xsl:if test="Value!='Java' and Value!='Python' and Value!='James' ">

Value is not a variable, it is a test against the child elements of the current context node. If there is a Value child element, its string value is used in the tests shown.

Had you written

<xsl:variable name="Value" select="'Java'"/>
<xsl:if test="$Value!='Java' and $Value!='Python' and $Value!='James' ">

then Value would be a variable.

Upvotes: 1

Related Questions