XPath: How to select nodes which have no attributes?

前端 未结 3 1599
Happy的楠姐
Happy的楠姐 2020-12-02 08:16

Using XPath, how to select nodes which have no attributes (where attribute count = 0)?

For example:


    

        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 08:51

    To address Marek Czaplicki's comment and expand the answer

    //node[not(@*) or not(string-length(@*))]
    

    ....will select all node elements with zero attributes OR which have attributes that are all empty. If it was just a particular attribute you are interested in, rather than all of them, then you could use

    //node[not(@attribute1) or not(string-length(@attribute1))]
    

    ...and this would select all node elements that either don't have an attribute called attribute1 OR which have an attribute1 attribute that is empty.

    That is, the following elements would be picked out by either of these xpath expressions

    
        
         
        
         
    
    

    See jsfiddle example here

提交回复
热议问题