XPath to find nearest ancestor element that contains an element that has an attribute with a certain value

前端 未结 4 1050
轻奢々
轻奢々 2020-12-28 14:31

ancestor::foo[bar[@attr=\"val\"]]

I thought this would work, but it\'s not. I need to find the nearest foo element going up in the tree tha

4条回答
  •  臣服心动
    2020-12-28 14:33

    This should work:

    //*[ancestor::foo[bar[@attr="val"]]]
    

    or alternatively

    root/foo/bar[ancestor::foo[bar[@attr="val"]]]
    

    matches the second element

    
        
            
        
        
            
        
        
            
        
    
    

提交回复
热议问题