“或"和“|"有什么区别在 xslt 中编程时? [英] What is the difference between 'or' and '|' when programming in xslt?

查看:24
本文介绍了“或"和“|"有什么区别在 xslt 中编程时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用 xslt,我一直无法理解 | 之间的区别.和或何时我应该使用which.我知道我可以只使用错误消息来确定我需要使用哪一个,但我有兴趣了解为什么我不能使用其中一个.

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

此运算符返回两个序列的联合,在这种情况下,这两个序列被解释为两组节点.一个有趣的细节是联合运算符删除了任何重复的节点.此外,它只接受 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

此运算符的技术术语是 Boolean Disjunction.它需要两个参数,这两个参数必须单独评估为布尔值(真"或假").或者,更准确地说,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代码中实现alternatives,主要使用xsl:ifxsl: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 需要一个布尔值;如果序列非空,则有效布尔值为真.

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 can see) the two expressions reach that outcome in subtly different ways.

这篇关于“或"和“|"有什么区别在 xslt 中编程时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆