开发者

How to select all leaf nodes using XPath expression?

开发者 https://www.devze.com 2023-01-20 11:52 出处:网络
I believe it\'s possible but cou开发者_JS百科ldn\'t figure out the syntax. Something like this: xmlNode.SelectNodes(\"//*[count(child::*) <= 1]\")

I believe it's possible but cou开发者_JS百科ldn't figure out the syntax. Something like this:

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

but this is not correct.


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()">
0

精彩评论

暂无评论...
验证码 换一张
取 消