简体   繁体   English

“|”之间的区别和 XSL/XPath 中的“或”

[英]difference between '|' and 'or' in XSL/XPath

In XSLT 1, is not(self::DEFINEDTERM|self::TEXT) equivalent to not(self::DEFINEDTERM or self::TEXT)? XSLT 1、 not(self::DEFINEDTERM|self::TEXT)等价于not(self::DEFINEDTERM or self::TEXT)?

Which is preferred?哪个是首选?

This is an example of XSL where this XPath is used:这是使用XPathXSL示例:

<xsl:template name="DEFINITION" match="DEFINITION">
    
        <xsl:element name="body">
            <xsl:attribute name="break">before</xsl:attribute>
            <xsl:element name="defn">
                <xsl:attribute name="id" />
                <xsl:attribute name="scope" />
                <xsl:value-of select="DEFINEDTERM" />
            </xsl:element>
            <xsl:apply-templates select="TEXT" />
        </xsl:element>
        
        <xsl:apply-templates select="*[not(self::DEFINEDTERM|self::TEXT)]" />
        
</xsl:template>

"or" compares two booleans, whereas "|" “或”比较两个布尔值,而“|” forms the union of two node-sets. forms 两个节点集的并集。

Using a node-set where a boolean is expected gives you an implicit test on whether the node-set is empty.使用预期为 boolean 的节点集可以隐式测试节点集是否为空。 In effect it tests exists(N) where exists() returns true if N is non-empty.实际上,它测试exists(N) ,如果 N 非空,则 exists() 返回 true。

So if M and N are node-sets, then所以如果 M 和 N 是节点集,那么

  • not(M or N) means not(exists(M) or exists(N)) not(M or N)表示not(exists(M) or exists(N))

  • not(M | N) means not(exists(M|N)) not(M | N)表示not(exists(M|N))

and since the union of two node-sets is empty only if both node-sets are empty, the two expressions always produce the same result.并且由于只有当两个节点集都为空时,两个节点集的并集才为空,因此两个表达式总是产生相同的结果。

I would think it likely that any decent XPath processor is going to evaluate both expressions in exactly the same way, so there's no reason to prefer one over the other.我认为任何像样的 XPath 处理器都可能以完全相同的方式计算这两个表达式,因此没有理由偏爱一个。

Note that this equivalence only applies if both operands are node-sets.请注意,此等价性仅适用于两个操作数都是节点集的情况。 (If they aren't, the union operator will fail, so you need to use "or"). (如果不是,联合运算符将失败,因此您需要使用“或”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM