Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL if: test with multiple test conditions

Tags:

xml

xslt

[Solved]

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> 

[Problem]

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. Thanks in advance.

[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> 
like image 218
codingbbq Avatar asked Jan 27 '14 12:01

codingbbq


People also ask

Can we use multiple When in XSLT?

yes, they are mutually exclusive - refer to stackoverflow.com/questions/18742988/…

How do you write multiple and/or condition in XSLT?

XSLT choose is defined as the if-else construct that contains a sequence of conditions with multiple variables. To go with multiple options, we are happened to use the <choose> element and tests a Boolean Expression. They assist conditional tests of complex types to use against the XML source file.

Can we use if-else in XSLT?

XSLT if-else elements contain a sequence of conditions with the expression values that work when a condition is true with the <xsl: when> statement. this <xsl:when> specifies unlimited instructions. XSL specifications implies a function<xsl: choose> to implement the functionality of if-else statements.

Which of the following elements can be used along with xsl choose to specify a multiple conditional test against the content of nodes?

The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.


2 Answers

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> 
like image 171
codingbbq Avatar answered Sep 17 '22 06:09

codingbbq


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.

like image 30
Mathias Müller Avatar answered Sep 19 '22 06:09

Mathias Müller