XSL if: test with multiple test conditions
Asked Answered
M

3

39

I am trying to create a xsl condition to check if combinations of node are empty or not. I have tried below conditions but they do not work, does anyone have an idea as to how to get it working

<xsl:if test=" node/ABC!='' and node/DEF='' and node/GHI='' ">
This does not work
</xsl:if>

I have also tried

<xsl:when test="((node/ABC!='') and (node/DEF='') and (node/GHI=''))">
This does not work either..
</xsl:when>

And also tried

<xsl:if test="(node/ABC!='')>
<xsl:if test="(node/DEF='')>
<xsl:if test="(node/GHI='')">
Nope not working..
</xsl:if>
</xsl:if>
</xsl:if>

I, then tried with single xsl:if conditions, and below is the observation

<xsl:if test="node/ABC!=''>
**This is working fine**
</xsl:if>

However if i try to search for empty condition, i.e

<xsl:if test="node/ABC=''>
**This does not work**
</xsl:if>

Also, if i try with a == (double equal to), then it gives xslt error. i.e

<xsl:if test="node/ABC==''>
***This gives a compilation error***
</xsl:if>

I would like help in figuring out how to get my xsl:if test working to check for multiple conditions.

[Edit] : Just to update here that the if condition where all the nodes are not empty works, it does not work when i try to check for any other node out of the three nodes that are empty.

For eg :

<xsl:if test=" node/ABC!='' and node/DEF!='' and node/GHI!='' ">
This condition works perfectly fine.
</xsl:if>
Mundy answered 27/1, 2014 at 12:34 Comment(7)
Show an example of your XML. Also be aware that elements containing whitespace are not empty as far as xpath is concerned. You may wish to look up the normalize-space function.Alphonsa
hey @IanRoberts, I am working inside an web app which needs xslt to view data. Not sure how to get the xml. However, i have also tried with the normalize-space function (normalize-space says that the node may have spaces(?)). The column that I am testing has no value.Mundy
You could drop in an identity transformation which simply gives back the same XML it started with. There are many reasons why it might not be working, such as extraneous whitespace, namespace mismatches, case sensitivity, the context not being what you thought it was, etc. - it's impossible to diagnose without seeing the input.Alphonsa
Thanks @IanRoberts, I understand that my question is incomplete, I will try to get more information from the support.Mundy
What do you mean with "it does not work"? Does the if statement match too often (which I would expect) or not al all if you combine the 3 expressions into one?Tenace
The if statement does not work when i combine the 3 expressions. I am not able to check for the empty node. If all the 3 nodes have values then the if statement works fine.Mundy
Hi @IanRoberts, Thanks, the normalize-space function worked for me.Mundy
M
40

Thanks to @IanRoberts, I had to use the normalize-space function on my nodes to check if they were empty.

<xsl:if test="((node/ABC!='') and (normalize-space(node/DEF)='') and (normalize-space(node/GHI)=''))">
  This worked perfectly fine.
</xsl:if>
Mundy answered 22/2, 2014 at 17:20 Comment(0)
S
7

Just for completeness and those unaware XSL 1 has choose for multiple conditions.

<xsl:choose>
 <xsl:when test="expression">
  ... some output ...
 </xsl:when>
 <xsl:when test="another-expression">
  ... some output ...
 </xsl:when>
 <xsl:otherwise>
   ... some output ....
 </xsl:otherwise>
</xsl:choose>
Shaum answered 22/10, 2020 at 7:11 Comment(0)
M
3

Try to use the empty() function:

<xsl:if test="empty(node/ABC/node()) and empty(node/DEF/node())">
    <xsl:text>This should work</xsl:text>
</xsl:if>

This identifies ABC and DEF as empty in the sense that they do not have any child nodes (no elements, no text nodes, no processing instructions, no comments).

But, as pointed out by @Ian, your elements might not be empty really or that might not be your actual problem - you did not show what your input XML looks like.

Another cause of error could be your relative position in the tree. This way of testing conditions only works if the surrounding template matches the parent element of node or if you iterate over the parent element of node.

Metallic answered 27/1, 2014 at 12:53 Comment(1)
Hi @Mathias, I did try the above if condition, but it gives me a XSLT invalid error in the web application that i am using.Mundy

© 2022 - 2024 — McMap. All rights reserved.