8

I definded the following variables:

<xsl:variable name="pica036E"
                        select="recordData/record/datafield[@tag='036E']" />
<xsl:variable name="pica036F"
                        select="recordData/record/datafield[@tag='036F']" />

Now I need to do a condition if variable pica036E isn't empty and pica036F is empty show the following message otherwise show another message. That's my code, but I don't ge any output. Is "null or empty" correct defined?

<xsl:choose>
                        <xsl:when test="$pica036E != '' and $pica036F = ''">
                        <xsl:message>
                         036F no 036E yes      
                            </xsl:message>
                        </xsl:when>

                         <xsl:otherwise>
                        <xsl:message>
                                036E no 036F yes
                            </xsl:message>  
                        </xsl:otherwise> 
                    </xsl:choose>  
Oleg_08
  • 447
  • 1
  • 4
  • 23

3 Answers3

9

In XPath, X=Y means (if some pair x in X, y in Y satisfy x = y), while X != Y means (if some pair x in X, y in Y satisfy x != y).

This means that if either X or Y is an empty sequence, then both X=Y and X!=Y are false.

For example, $pica036E != '' tests whether there is a value in $pica036E that is not a zero-length string. If there are no values in $pica036E then there is no value that satisfies this condition.

As a result, using != in XPath is always a code smell. Usually, rather than X != Y, you should be writing not(X = Y).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Is it possible to assign a value to a variable if it is null. For example, if a variable x doesn't have a value. – vishnu Oct 19 '21 at 12:10
  • @vishnu, please don't ask questions by commenting on an answer to a different question. Start a new thread, and give all the information needed to answer it. Focus on what you are trying to achieve, on what you did, and on how it failed. – Michael Kay Oct 19 '21 at 14:28
4

Check following Code. I think your output get

<xsl:when test="not($pica036E = '') and $pica036F = ''">
Ajeet Singh
  • 1,056
  • 1
  • 6
  • 21
4

In XSLT a variable with text content can also serve as a boolean variable. Not empty content means true, empty content means false.

So the condition can be also written as:

<xsl:when test="$pica036E and not($pica036F)">

Remember that not is a function (not an operator).

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41