How to select all leaf nodes using XPath expression?

吃可爱长大的小学妹 提交于 2019-11-26 21:55:46

Use:

//node()[not(node())]

In case only element leaf nodes are wanted (and this needs clarification -- are elements that have non-element children considered leaf nodes?), then the following XPath expression selects them:

//*[not(*)]

Both expressions above are probably the shortest that select the desired nodes (either any-node or element -- leaf nodes).

Any elements with no element child

//*[not(child::*)]

Why less or equal to 1 ?

xmlNode.SelectNodes("//*[count(child::*) = 0]")

Make tests etc at this site http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

Pretty helpful ..

I'm adding this XSLT answer since it seems google's front matches lack such a solution:

After a long struggle with extracting CDATA as XML, eventually, this expression worked best for me:

<xsl:template match="*[not(child::*)]/text()">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!