What is the correct XPath for choosing attributes that contain “foo”?

后端 未结 9 1634
予麋鹿
予麋鹿 2020-11-27 11:15

Given this XML, what XPath returns all elements whose prop attribute contains Foo (the first three nodes):


 

        
9条回答
  •  没有蜡笔的小新
    2020-11-27 11:49

    //a[contains(@prop,'Foo')]
    

    Works if I use this XML to get results back.

    
     a
     b
     c
     a
    
    

    Edit: Another thing to note is that while the XPath above will return the correct answer for that particular xml, if you want to guarantee you only get the "a" elements in element "bla", you should as others have mentioned also use

    /bla/a[contains(@prop,'Foo')]
    

    This will search you all "a" elements in your entire xml document, regardless of being nested in a "blah" element

    //a[contains(@prop,'Foo')]  
    

    I added this for the sake of thoroughness and in the spirit of stackoverflow. :)

提交回复
热议问题