xml - XSLT 等于和不等于运算符对不存在的节点返回相同的结果。这怎么可能?

标签 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()?

最佳答案

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

您正在将节点集与字符串进行比较。 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 >) 统一定义。

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


习惯上使用:

not(node = 'string')

作为以下的否定:

node = 'string'

关于xml - XSLT 等于和不等于运算符对不存在的节点返回相同的结果。这怎么可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45732643/

相关文章:

html - 通用 xml -> 使用 xslt 和约定的 html 转换?

xml - 使用 XSL 在第一个位置插入 XML 节点

java - (撒克逊 dtd)java.net.SocketException : Unexpected end of file fromserver

xml - 模式\d{10} 是什么意思?

c# - 是否可以使用属性指定 XML 节点名称和深度

xml - <xs :group> and <xs:attributeGroup>? 和有什么区别

Python XML 解析器将数字加载为字符串

xml - XSL+XPATH : Compare previous node attribute to current node attribute

java - Maven XML 插件 - 如何使用 document() 函数定义 URIResolver 或 SystemID 进行转换?

Java 在 XSL 转换中将 byte[] 发送到 base64Binary