简体   繁体   English

文档中所有没有文本节点后代的元素的Xpath?

[英]Xpath for all elements in the document without text node descendants?

I'm not fully familiar with XPath. 我对XPath并不完全熟悉。 I did an extensive search and only found a solution for immediate children. 我进行了广泛的搜索,仅找到了针对直系孩子的解决方案。

Requirement is to select an element who does not have any child, grand child, grand grand child, or any child that is present in its family tree which has a text node in it. 要求是选择一个元素,该元素不包含任何子代,孙代,孙代或家族树中存在文本节点的任何子代。

A simple example is:- 一个简单的例子是:

<div><p></p><p><span></span></p><p><span>notemptychild</span></p><p>notempty</p></div>

If I run the following XPath query:- 如果我运行以下XPath查询:

//p[not(text())]

It gives following output:- 它给出以下输出:

Element='<p/>'
Element='<p>
   <span/>
</p>'
Element='<p>
   <span>notemptychild</span>
</p>'

(I'm using freeformatter xpath tool: http://www.freeformatter.com/xpath-tester.html ) (我正在使用freeformatter xpath工具: http ://www.freeformatter.com/xpath-tester.html)

The first Element is a valid selection. 第一个元素是有效选择。

The second Element is a valid selection. 第二个元素是有效选择。

The third Element is NOT a valid selection because p 's child span has a text node in it. 第三个元素不是有效的选择,因为p的子span有一个文本节点。

I hope I didn't made the question over complicated. 我希望我不要因为复杂而提出这个问题。

This XPath, 这个XPath

//*[not(.//text())]

will select all elements in the document without text node descendants. 将选择文档中没有文本节点后代的所有元素。


This XPath, 这个XPath

//*[not(.//text()[normalize-space()])]

does the same but allows text nodes that consist of just whitespace. 这样做相同,但允许仅由空格组成的文本节点。

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

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