CJ7
CJ7

Reputation: 23275

difference between '|' and 'or' in XSL/XPath

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

Which is preferred?

This is an example of XSL where this XPath is used:

<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>

Upvotes: 0

Views: 71

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

"or" compares two booleans, whereas "|" forms the union of two node-sets.

Using a node-set where a boolean is expected gives you an implicit test on whether the node-set is empty. In effect it tests exists(N) where exists() returns true if N is non-empty.

So if M and N are node-sets, then

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

  • not(M | N) means 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.

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").

Upvotes: 3

Related Questions