I have been working with xslt recently, and I have been having trouble understanding the difference between | and or and when I should use which. I understand I can just use the error messages to figure out which one I need to be using but I am interested in learning why I can't use one or the other.
Would anyone be able to help point me in the right direction to someplace where I can learn the difference?
<? xml version="1.0" encoding="UTF-8"?>
You can format your XSLT stylesheet to go to a specific node, and then loop through the given node set. You create an XSLT loop with the <xsl:for-each> tag. The value of the select attribute in this tag is an XPath expression that allows you to specify the data element to loop through.
XPath. At bottom, XSLT is a language that lets you specify what sorts of things to do when a particular element is encountered. But to write a program for different parts of an XML data structure, you need to specify the part of the structure you are talking about at any given time. XPath is that specification language ...
|
is concerned with nodes, or
is concerned with "truth", that is, Boolean values.
The |
or union
operator
This operator returns the union of two sequences that, in this case, are interpreted as two sets of nodes. An interesting detail is that the union operator removes any duplicate nodes. Also, it only accepts operands of the type node()*
, i.e. a sequence of nodes. The nodes are returned in document order.
The or
operator
The technical term for this operator is Boolean Disjunction. It takes two arguments, both of which must evaluate to a Boolean value ("true" or "false") individually. Or, more precisely, the operands of or
are jugded by their effective Boolean value by converting them to xs:boolean
. All of this also applies to the and
operator, by the way.
Use the union
operator to enlarge a set of nodes, typically in a template match:
<xsl:template match="Root|Jon">
Why not use the or
operator here? Because the match
attribute expects a set of nodes as its value. union
returns a set of nodes, whereas the or
operator returns a Boolean value. You cannot define a template match for a Boolean.
Use the or
operator to implement alternatives in XSLT code, mainly using xsl:if
or xsl:choose
:
<xsl:if test="$var lt 354 or $var gt 5647">
If any of the two operands of this or
operation evaluates to true
, then the content of xsl:if
will be evaluated, too. But not only comparisons like "less than" (lt
) have a Boolean value, the following is also perfectly legal:
<xsl:if test="$var1 or $var2">
The above expression only evaluates to true
if at least one of the two variables is a non-empty sequence. This is because an empty sequence is defined to have the effective Boolean value of false
.
Note that because of the way XSLT coerces things to appropriate types, there are some contexts where either operator can be used. Consider these two conditionals:
<xsl:if test="Root | Jon"> ... <xsl:if>
<xsl:if test="Root or Jon"> ... <xsl:if>
The first conditional tests whether the union of the set of children named Root
and the set of children named Jon
is non-empty. The expression Root | Jon
returns a sequence of nodes, and then that sequence is coerced to a boolean value because if
requires a boolean value; if the sequence is non-empty, the effective boolean value is true.
The second conditional tests whether either of the two sets of children (children named Root
and children named Jon
) is non-empty. The expression Root or Jon
returns a boolean value, and since the operator or
requires boolean arguments, the two sets are each coerced to boolean, and then the or
operator is applied.
The outcome is the same, but (as you can see) the two expressions reach that outcome in subtly different ways.
From Documentation
|
- The union operator. For example, the match attribute in the element <xsl:template match="a|b">
matches all <a>
and <b>
elements
or
- Tests whether either the first or second expressions are true
. If the first expression is true
, the second is not evaluated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With