Search code examples
htmlxmldomxpathsimple-html-dom

XPath - how to select node with multiple attribute values?


Assume:

<foo bar="one two three">

How do I match all the foo tags that has the attribute bar with the value one (and I don't care what other attribute values might exist). This doesn't seem to work:

//foo[@bar="one"]

Solution

  • XPath 1.0 or 2.0

    The standard idiom for that is:

    //foo[contains(concat(' ', normalize-space(@bar), ' '), ' one ')]
    

    XPath 2.0

    //foo[tokenize(@bar,'\s+')='one']