且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

normalize-space(.) 和 normalize-space(text()) 有什么区别?

更新时间:2022-10-19 16:01:57

嗯,真正的问题是:.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.