How to select all leaf nodes using XPath expression?

后端 未结 4 1394
傲寒
傲寒 2020-11-28 05:25

I believe it\'s possible but couldn\'t figure out the syntax. Something like this:

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

bu

相关标签:
4条回答
  • 2020-11-28 05:54

    Any elements with no element child

    //*[not(child::*)]
    
    0 讨论(0)
  • 2020-11-28 06:07

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

    0 讨论(0)
  • 2020-11-28 06:08

    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 ..

    0 讨论(0)
  • 2020-11-28 06:16

    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 讨论(0)
提交回复
热议问题