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?
From Documentation
|
- The union operator. For example, the match attribute in the element<xsl:template match="a|b">
matches all<a>
and<b>
elementsor
- Tests whether either the first or second expressions aretrue
. If the first expression istrue
, the second is not evaluated.|
is concerned with nodes,or
is concerned with "truth", that is, Boolean values.Explanation
The
|
orunion
operatorThis 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
operatorThe 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 toxs:boolean
. All of this also applies to theand
operator, by the way.Examples
Use the
union
operator to enlarge a set of nodes, typically in a template match:Why not use the
or
operator here? Because thematch
attribute expects a set of nodes as its value.union
returns a set of nodes, whereas theor
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 usingxsl:if
orxsl:choose
:If any of the two operands of this
or
operation evaluates totrue
, then the content ofxsl:if
will be evaluated, too. But not only comparisons like "less than" (lt
) have a Boolean value, the following is also perfectly legal: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 offalse
.Coercion
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:
The first conditional tests whether the union of the set of children named
Root
and the set of children namedJon
is non-empty. The expressionRoot | Jon
returns a sequence of nodes, and then that sequence is coerced to a boolean value becauseif
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 namedJon
) is non-empty. The expressionRoot or Jon
returns a boolean value, and since the operatoror
requires boolean arguments, the two sets are each coerced to boolean, and then theor
operator is applied.The outcome is the same, but (as you see) the two expressions reach that outcome in subtly different ways.