XSLT 等于和不等于运算符 returns 缺少节点的相同结果。这怎么可能?

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>

结果:

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

我看到了 similar issue with python 但这里需要解释。

如果我想在不通过 text() 或 string() 进行显式值类型转换的情况下获得恰好相反的结果,是否首选 not()?

这是此类比较的预期结果。

您正在将节点集与字符串进行比较。此类比较的 rules 状态为:

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.

此规则对所有比较运算符(=, !=, <=, <, >= and >)统一定义。

由于您的节点集是空的,因此其中永远不会有比较 returns 为真的节点 - 无论您使用哪个运算符。


习惯使用:

not(node = 'string')

作为以下的否定:

node = 'string'