规范化空间(.)和规范化空间(文本())有什么区别?
我正在编写一个 XPath 表达式,并且修复了一个奇怪的错误,但是以下两个 XPath 表达式有什么区别?
"//td[starts-with(normalize-space()),'Posted Date:')]"
主要是
"//td[starts-with(normalize-space(text()),'Posted Date:')]"
,第一个 XPath 表达式将捕获什么?因为我得到了很多奇怪的结果。那么 text()
在匹配中做了什么?另外,如果我说 normalize-space()
& 是否有区别? 标准化空间(.)
?
I was writing an XPath expression, and I had a strange error which I fixed, but what is the difference between the following two XPath expressions?
"//td[starts-with(normalize-space()),'Posted Date:')]"
and
"//td[starts-with(normalize-space(text()),'Posted Date:')]"
Mainly, what will the first XPath expression catch? Because I was getting a lot of strange results. So what does the text()
make in the matching? Also, is there is a difference if I said normalize-space()
& normalize-space(.)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,真正的问题是:
.
和text()
之间有什么区别?.
是当前节点。如果你在需要字符串的地方使用它(即作为normalize-space()
的参数),引擎会自动将节点转换为节点的字符串值,这对于一个元素来说就是全部元素内的文本节点连接在一起。 (因为我猜测问题实际上与元素有关。)另一方面,
text()
仅选择作为当前节点的直接子节点的文本节点。例如,给定 XML:
并假设
是当前节点,
normalize-space(.)
将返回Foo Bar lish
,但是normalize-space(text())
将失败,因为text()
返回两个文本节点(Foo
和>lish
),其中normalize-space()
不接受。长话短说,如果您想标准化元素内的所有文本,请使用
.
。如果要选择特定的文本节点,请使用text()
,但请始终记住,尽管它的名称如此,text()
返回一个节点集,该节点集仅转换为如果它有单个元素,则自动字符串。Well, the real question is: what's the difference between
.
andtext()
?.
is the current node. And if you use it where a string is expected (i.e. as the parameter ofnormalize-space()
), the engine automatically converts the node to the string value of the node, which for an element is all the text nodes within the element concatenated. (Because I'm guessing the question is really about elements.)text()
on the other hand only selects text nodes that are the direct children of the current node.So for example given the XML:
and assuming
<a>
is your current node,normalize-space(.)
will returnFoo Bar lish
, butnormalize-space(text())
will fail, becausetext()
returns a nodeset of two text nodes (Foo
andlish
), whichnormalize-space()
doesn't accept.To cut a long story short, if you want to normalize all the text within an element, use
.
. If you want to select a specific text node, usetext()
, but always remember that despite its name,text()
returns a nodeset, which is only converted to a string automatically if it has a single element.