简体   繁体   English

normalize-space(。)和normalize-space(text())之间有什么区别?

[英]What is the difference between normalize-space(.) and normalize-space(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? 我正在写一个XPath表达式,我有一个奇怪的错误,我修复了,但以下两个XPath表达式之间有什么区别?

"//td[starts-with(normalize-space()),'Posted Date:')]"

and

"//td[starts-with(normalize-space(text()),'Posted Date:')]"  

Mainly, what will the first XPath expression catch? 主要是,第一个XPath表达式会捕获什么? Because I was getting a lot of strange results. 因为我得到了很多奇怪的结果。 So what does the text() make in the matching? 那么text()在匹配中做了什么? Also, is there is a difference if I said normalize-space() & normalize-space(.) ? 另外,如果我说normalize-space()normalize-space(.)会有区别吗?

Well, the real question is: what's the difference between . 嗯,真正的问题是:它们之间有什么区别. and text() ? text()

. is the current node. 是当前节点。 And if you use it where a string is expected (ie 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. 如果你在预期字符串的地方使用它(即作为normalize-space()的参数),引擎会自动将节点转换为节点的字符串值,对于元素,元素是连接元素中的所有文本节点。 (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. 另一方面, text()仅选择作为当前节点的直接子节点的文本节点。

So for example given the XML: 例如,给定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. 假设<a>是您当前的节点, normalize-space(.)将返回Foo Bar lish ,但normalize-space(text())将失败,因为text()返回两个文本节点的节点集( Foolish ) , normalize-space()不接受。

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. 如果要选择特定的文本节点,请使用text() ,但始终记住,尽管名称为text() ,但text()返回一个节点集,如果它只有一个元素,则只会自动转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM