dsfsfsfsfs55656 2016-12-15 21:52
浏览 252
已采纳

没有文本节点后代的文档中所有元素的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.

  • 写回答

1条回答 默认 最新

  • douyun6781 2016-12-15 21:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型