mankey
mankey

Reputation: 33

XSLT equal and not equal operators returns same result for absent node. How is it possible?

XML:

<root></root>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" encoding="UTF-8"/>
    <xsl:template match="/root">
        absent_node EQUALS zero-length string [<xsl:value-of select="absent_node=''"/>];
        absent_node NOT EQUALS zero-length string via != [<xsl:value-of select="absent_node!=''"/>]
    </xsl:template>
</xsl:stylesheet>

Result:

absent_node EQUALS zero-length string [false];
absent_node NOT EQUALS zero-length string [false]

I saw similar issue with python but need an explanation here.

Is using not() preferred if I want to get just opposite result without explicit value type casting via text() or string()?

Upvotes: 3

Views: 4488

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

That is the expected result in this type of comparison.

You are comparing a node-set to a string. The rules for such comparison state that:

the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true.

This rule is defined uniformly for all comparison operators (=, !=, <=, <, >= and >).

Since your node-set is empty, there will never be a node in it for which the comparison returns true - no matter which operator you use.


It is customary to use:

not(node = 'string')

as the negation of:

node = 'string'

Upvotes: 3

Related Questions