normalize-space(.) 和 normalize-space(text()) 有什么区别? [英] What is the difference between normalize-space(.) and normalize-space(text())?

查看:47
本文介绍了normalize-space(.) 和 normalize-space(text()) 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 XPath 表达式,但我修复了一个奇怪的错误,但是以下两个 XPath 表达式之间有什么区别?

"//td[starts-with(normalize-space()),'发布日期:')]"

"//td[starts-with(normalize-space(text()),'发布日期:')]"

主要是,第一个 XPath 表达式将捕获什么?因为我得到了很多奇怪的结果.那么 text() 在匹配中做了什么?另外,如果我说 normalize-space() & 是否有区别?normalize-space(.)?

解决方案

嗯,真正的问题是:.text() 之间有什么区别?>

. 是当前节点.如果你在需要字符串的地方使用它(即作为normalize-space()的参数),引擎会自动将节点转换为节点的字符串值,对于一个元素来说就是所有元素内的文本节点连接起来.(因为我猜这个问题实际上是关于元素的.)

text() 另一方面,只选择作为当前节点的直接子节点的文本节点.

例如给定 XML:

Foo<b>条形</b>英语</a>

并假设 是您当前的节点,normalize-space(.) 将返回 Foo Bar lish,但 normalize-space(text()) 将失败,因为 text() 返回两个文本节点(Foolish),normalize-space() 不接受.

长话短说,如果您想对元素中的所有文本进行规范化,请使用 ..如果要选择特定的文本节点,请使用 text(),但请始终记住,尽管名称如此,text() 返回一个节点集,该节点集仅转换为如果它只有一个元素,则自动生成字符串.

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(.)?

Well, the real question is: what's the difference between . and text()?

. is the current node. And if you use it where a string is expected (i.e. as the parameter of normalize-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:

<a>Foo
    <b>Bar</b>
  lish
</a>

and assuming <a> is your current node, normalize-space(.) will return Foo Bar lish, but normalize-space(text()) will fail, because text() returns a nodeset of two text nodes (Foo and lish), which normalize-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, use text(), 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.

这篇关于normalize-space(.) 和 normalize-space(text()) 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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