How can I match on an attribute that contains a certain string?

后端 未结 10 1967
一整个雨季
一整个雨季 2020-11-22 03:14

I am having a problem selecting nodes by attribute when the attributes contains more than one word. For example:

10条回答
  •  粉色の甜心
    2020-11-22 03:57

    mjv's answer is a good start but will fail if atag is not the first classname listed.

    The usual approach is the rather unwieldy:

    //*[contains(concat(' ', @class, ' '), ' atag ')]
    

    this works as long as classes are separated by spaces only, and not other forms of whitespace. This is almost always the case. If it might not be, you have to make it more unwieldy still:

    //*[contains(concat(' ', normalize-space(@class), ' '), ' atag ')]
    

    (Selecting by classname-like space-separated strings is such a common case it's surprising there isn't a specific XPath function for it, like CSS3's '[class~="atag"]'.)

提交回复
热议问题