Search code examples
phpjqueryxpathdomxpath

DOMXPath Query To Extract Specific li Element


I'm trying to extract a list element based on its child's content. The li has an anchor tag with a title of 'something something' (space included).

Example:

<li><a href="#" title="something something"></a></li>

What would the query look like? Thanks!


Solution

  • To retrieve an element with its attribute, use this XPath query:

    /html/body/li/a[@title='something something']
    

    That will get any a tag with a title attribute with a value of something something that's a child of a li tag (that's inside html and body tag, it's better/faster if you know the full structure before hand).

    <?php
    $html = '<li><a href="#" title="something something">test</a></li>';
    $html .= '<li><a href="#" title="something something2">test2</a></li>';
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xpath = new DOMXPath($doc);
    $lis = $xpath->query("/html/body/li/a[@title='something something']");
    foreach ($lis as $li) {
        var_dump($li->nodeValue);
    }
    

    Result

    /Applications/MAMP/htdocs/trello/up.php:9:string 'test' (length=4)

    Demo