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

后端 未结 9 1544
予麋鹿
予麋鹿 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 12:02

    John C is the closest, but XPath is case sensitive, so the correct XPath would be:

    /bla/a[contains(@prop, 'Foo')]
    
    0 讨论(0)
  • 2020-11-27 12:03
    descendant-or-self::*[contains(@prop,'Foo')]
    

    Or:

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

    Or:

    /bla/a[position() <= 3]
    

    Dissected:

    descendant-or-self::
    

    The Axis - search through every node underneath and the node itself. It is often better to say this than //. I have encountered some implementations where // means anywhere (decendant or self of the root node). The other use the default axis.

    * or /bla/a
    

    The Tag - a wildcard match, and /bla/a is an absolute path.

    [contains(@prop,'Foo')] or [position() <= 3]
    

    The condition within [ ]. @prop is shorthand for attribute::prop, as attribute is another search axis. Alternatively you can select the first 3 by using the position() function.

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

    If you also need to match the content of the link itself, use text():

    //a[contains(@href,"/some_link")][text()="Click here"]

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