问题描述
我最近一直在使用xslt,但在理解|之间的区别时遇到了麻烦.和/或何时应该使用哪个.我了解我可以使用错误消息来确定我需要使用哪一个,但我有兴趣了解为什么我不能使用其中一个.
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?
推荐答案
|
与节点有关,or
与真相"(即布尔值)有关.
|
is concerned with nodes, or
is concerned with "truth", that is, Boolean values.
|
或union
运算符
The |
or union
operator
此运算符返回两个序列的 union ,在这种情况下,这两个序列被解释为两组节点.一个有趣的细节是,联合运算符删除了所有重复的节点.而且,它仅接受node()*
类型的操作数,即节点序列.节点按文档顺序返回.
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.
or
运算符
The or
operator
此运算符的技术术语是布尔除法运算.它有两个参数,两个参数都必须分别求值为布尔值("true"或"false").或者,更准确地说,通过将or
的操作数转换为xs:boolean
,可以用其有效布尔值来判断操作数.顺便说一下,所有这一切也适用于and
运算符.
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.
使用union
运算符通常在模板匹配中放大一组节点:
Use the union
operator to enlarge a set of nodes, typically in a template match:
<xsl:template match="Root|Jon">
为什么在这里不使用or
运算符?因为match
属性需要一组节点作为其值. union
返回一组节点,而or
运算符返回布尔值.您无法为布尔值定义模板匹配.
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.
使用or
运算符在XSLT代码中实现替代,主要使用xsl:if
或xsl:choose
:
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">
如果此or
操作的两个操作数中的任何一个求值为true
,则xsl:if
的内容也将求值.但是不仅像小于"(lt
)之类的比较具有布尔值,以下内容也完全合法:
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">
如果两个变量中的至少一个是非空序列,则上面的表达式的计算结果为true
.这是因为将空序列定义为具有有效的布尔值false
.
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
.
请注意,由于XSLT将内容强制转换为适当类型的方式,因此在某些上下文中可以使用任一运算符.考虑以下两个条件:
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>
第一个条件测试名称为Root
的子集与名称为Jon
的子集的并集是否为非空.表达式Root | Jon
返回一个节点序列,然后该序列被强制为布尔值,因为if
需要一个布尔值;如果序列为非空,则有效布尔值为true.
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.
第二个条件测试用于测试两组孩子(名为Root
的孩子和名为Jon
的孩子)是否为非空.表达式Root or Jon
返回一个布尔值,并且由于运算符or
需要布尔参数,因此将这两个集合分别强制为布尔值,然后应用or
运算符.
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 see) the two expressions reach that outcome in subtly different ways.
这篇关于"or"和"|"有什么区别在xslt中编程时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!