10

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>  

3 Answers 3

11

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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, 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.
5

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).

Comments

4

Check following Code. I think your output get

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.