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

后端 未结 10 1955
一整个雨季
一整个雨季 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:53

    Be aware that bobince's answer might be overly complicated if you can assume that the class name you are interested in is not a substring of another possible class name. If this is true, you can simply use substring matching via the contains function. The following will match any element whose class contains the substring 'atag':

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

    If the assumption above does not hold, a substring match will match elements you don't intend. In this case, you have to find the word boundaries. By using the space delimiters to find the class name boundaries, bobince's second answer finds the exact matches:

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

    This will match atag and not matag.

提交回复
热议问题