Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if variable is Null or empty in XSLT?

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>  
like image 463
Oleg_08 Avatar asked Mar 16 '17 10:03

Oleg_08


People also ask

How to check not empty in XSLT?

XSLT: Check if a string is null or empty. To test if the value of a certain node is empty it depends on what is meant by empty. * Contains no text content: not(string(.)) * Contains no text other than whitespace: not(normalize-space(.))

How do I find the value of a variable in XSLT?

The XSLT <xsl:value-of> element is used to extract a value from an expression defined in the select attribute. The expression defined in the mandatory select attribute is either an XPATH expression (for nodes and/or values) or a variable reference. The return of the <xsl:value-of> function is a literal value.

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit)


1 Answers

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

like image 198
Michael Kay Avatar answered Oct 11 '22 03:10

Michael Kay