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

后端 未结 9 1526
予麋鹿
予麋鹿 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:44

    For the code above... //*[contains(@prop,'foo')]

    0 讨论(0)
  • 2020-11-27 11:49
    //a[contains(@prop,'Foo')]
    

    Works if I use this XML to get results back.

    <bla>
     <a prop="Foo1">a</a>
     <a prop="Foo2">b</a>
     <a prop="3Foo">c</a>
     <a prop="Bar">a</a>
    </bla>
    

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

    0 讨论(0)
  • 2020-11-27 11:53

    /bla/a[contains(@prop, "foo")]

    0 讨论(0)
  • 2020-11-27 11:55

    This XPath will give you all nodes that have attributes containing 'Foo' regardless of node name or attribute name:

    //attribute::*[contains(., 'Foo')]/..
    

    Of course, if you're more interested in the contents of the attribute themselves, and not necessarily their parent node, just drop the /..

    //attribute::*[contains(., 'Foo')]
    
    0 讨论(0)
  • 2020-11-27 11:58

    Have you tried something like:

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

    I've never used the contains function before but suspect that it should work as advertised...

    0 讨论(0)
  • 2020-11-27 12:01

    try this:

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

    that should work for any "a" tags in the document

    0 讨论(0)
提交回复
热议问题