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

时间:2017-08-17 10:27:25

标签: xml xslt saxon

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()是首选吗?

1 个答案:

答案 0 :(得分:2)

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

您正在将节点集与字符串进行比较。这种比较的rules表示:

  

当且仅当在中有节点时,比较才为真   node-set使得执行比较的结果   节点的字符串值和另一个字符串为真。

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

由于您的节点集为空,因此无论您使用哪个运算符,都不会有比较返回true的节点。

习惯使用:

not(node = 'string')

否定:

node = 'string'