本文介绍了为什么normalize-space(text())在通过文本选择时忽略内部节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在下面的例子中我可以使用 // label [text()[normalize-space()='some label']] 或 // label [normalize-space(text())='some label'] 按文本选择标签并忽略span的内容?为什么?我真的很想理解这个问题。在没有关于这个功能的信息。这正是我想要的,但我也非常想知道为什么这个解决方案可以工作:)

顺便说一句,哪种语法更好: // label [text()[normalize-space()='some label']] vs // label [normalize-space(text())='some label'] 和为什么?

 < label> 
< span>一些跨度< / span>
一些标签
< / label>

< label>
其他标签
< span>其他范围< / span>
< / label>

我正在寻找您有用的答案:)

解决方案

这与normalize-space()无关,以及与 text() p>

text()是 child :: text(),并选择标签元素的直接子元素的文本节点。除非是剥离空白文本节点,否则示例中的标签元素具有两个子文本节点,其中一个是全部空白,另一个包含由空白包围的某个标签。

 顺便说一句,哪种语法更好:// label [text()[normalize-space()='some label']] vs // label [normalize-space(text( ))='一些标签'],为什么? 

他们做了不同的事情;在XPath 1.0中,第一个表达式选择具有子文本节点的标签元素,其值,值为0,在空白标准化之后,等于某个标签。第二个选择标签元素,其空白标准化后的第一个子文本节点等于某个标签。这是因为normalize-space()(如所有期望字符串的函数一样),如果您给它一个节点集,它将获取节点集中第一个节点的字符串值。

在XPath 2.0中,第一个表达式选择具有子空间文本节点的标签元素,其空白标准化后的值等于某个标签。第二个选择标签元素,如果他们有一个子元素文本节点,在空白标准化之后,等于某个标签,但如果该标签元素具有多个子文本节点则引发错误。这是因为normalize-space()(与所有期望字符串的函数一样)将其参数进行原子化,并且如果原子化序列的长度大于1,则报告类型错误。


why in following example I can use //label[text()[normalize-space() = 'some label']] or //label[normalize-space(text()) = 'some label'] to select label by text and ignore span's content? Why? I really want to understand this issue. In http://www.w3.org/TR/xpath/#function-normalize-space there is no info about this functionality. This is exactly what I want, but I also desperately want to know why this solution works:)

BTW, which syntax is better: //label[text()[normalize-space() = 'some label']] vs //label[normalize-space(text()) = 'some label'] and why?

<label>
<span>some span</span>
  some label   
</label>

<label>
    other label
<span>other span</span>
</label>

I'm looking for your helpful answer:)

解决方案

This has nothing to do with normalize-space(), and everything to do with text().

text() is short for child::text(), and selects the text nodes that are immediate children of the label element. Unless you are stripping whitespace text nodes, the label element in your example has two child text nodes, one of which is all whitespace, the other contains "some label" surrounded by whitespace.

BTW, which syntax is better: //label[text()[normalize-space() = 'some label']] vs //label[normalize-space(text()) = 'some label'] and why?

They do different things; the one that is better is the one that does what you want to achieve.

In XPath 1.0, the first expression selects label elements that have a child text node whose value, after whitespace normalization, equals "some label". The second selects label elements whose first child text node, after whitespace normalization, equals "some label". That's because normalize-space() (like all functions that expect a string), if you give it a node-set, takes the string value of the first node in the node-set.

In XPath 2.0, the first expression selects label elements that have a child text node whose value, after whitespace normalization, equals "some label". The second selects label elements if they have a child text node, after whitespace normalization, equals "some label", but raises an error if the label element has more than one child text node. That's because normalize-space() (like all functions that expect a string), atomizes its argument, and reports a type error if the length of the atomized sequence is greater than one.

这篇关于为什么normalize-space(text())在通过文本选择时忽略内部节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 17:45