['#'] 和 [.='#'] 之间到底有什么区别? [英] What's exactly the difference between ['#'] and [.='#']?
问题描述
['#']
和 [.='#']
之间到底有什么区别?有什么区别吗?
What's exactly the difference between ['#']
and [.='#']
? Is there any difference at all?
例如以下表达式:
<xsl:template match="a/@href[.='#']">...</xsl:template>
<xsl:template match="a/@href['#']">...</xsl:template>
推荐答案
一个谓词过滤器,如果包含的表达式不为真.[.='#']
测试当前上下文 (.
) 的字符串内容是否等于 #
,因此第一个模板将返回所有@href
链接的属性,例如 ...</a>
.
A predicate filters, if the contained expression is not true. [.='#']
tests if the string content of the current context (.
) equals #
, thus the first template would return all @href
attributes for links like <a href="#">...</a>
.
第二个模板不包含布尔语句,它也不是数字(所以它是一个位置测试).它将被评估为 boolean
功能:
The second template does not contain a boolean statement, and it also isn't numerical (so it would be a positional test). It will be evaluated as given by the boolean
function:
函数:boolean boolean(object)
布尔函数将其参数转换为布尔值,如下所示:
The boolean function converts its argument to a boolean as follows:
- 一个数为真当且仅当它既不是正数也不是负数零或 NaN
- 节点集为真当且仅当它是非空的
- 字符串为真当且仅当其长度不为零
- 将四种基本类型以外的类型的对象转换为boolean 依赖于该类型的方式
这里,我们有一个具有有效布尔值 true
的非空字符串,因此您的第二个模板中的谓词将永远不会过滤任何内容.
Here, we have a non-empty string with effective boolean value true
, thus the predicate in your second template will never filter anything.
//a[@href]
中的谓词另一方面将过滤所有包含 @href
属性的链接(这里,我们过滤一个节点-set).
A predicate like in //a[@href]
on the other hand would filter for all links containing an @href
attribute (here, we filter for a node-set).
这篇关于['#'] 和 [.='#'] 之间到底有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!