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

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

问题描述:

I'm not fully familiar with 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:-

//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)

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.

I hope I didn't made the question over complicated.

我对XPath并不完全熟悉。 我做了大量的搜索,只为直系孩子找到了解决方案。 p>

要求选择一个没有任何孩子,大孩子,大孩子或任何孩子的元素。 存在于其家族树中,其中包含一个文本节点。 p>

一个简单的例子是: - p>

 &lt; div&gt;&lt;  p为H.&LT; / p为H.&LT p为H.;&LT;跨度&GT;&LT; /跨度&GT;&LT; / p为H.&LT p为H.;&LT;跨度&GT; notemptychild&LT; /跨度&GT;&LT; / p为H.&LT; p为H. notempty&LT; /  p&gt;&lt; / div&gt; 
  code>  pre> 
 
 

如果我运行以下XPath查询: - p>

  // p  [not(text())] 
  code>  pre> 
 
 

它提供以下输出: - p>

  Element ='&lt;  p /&gt;'
Element ='&lt; p&gt; 
&lt; span /&gt; 
&lt; / p&gt;'
Element ='&lt; p&gt; 
&lt; span&gt; notemptychild&lt; / span&gt; 
&lt;  / p&gt;'
  code>  pre> 
 
 

(我正在使用freeformatter xpath工具: http://www.freeformatter.com/xpath-tester.html ) p>

冷杉 t元素是有效选择。 p>

第二个元素是有效选择。 p>

第三个元素不是有效选择,因为 p code>的子 span code>中有一个文本节点。 p>

我希望我没有提出复杂的问题。 p> \ n div>

This XPath,

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

will select all elements in the document without text node descendants.


This XPath,

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

does the same but allows text nodes that consist of just whitespace.