phpzend-dom-query

Zend_Dom_Query selector for second element


<div id="test">
<span>Don't select me</span>
<span>Select me</span>
</div>

I want to select second span in div#test.
Exist eq() selector (like jQuery) or something like that?


Solution

  • Unfortunately Zend_Dom_Query doesn't support the :nth-child(n) selector from css, but reverting back to xpath to query you could write:

     $result = $dom->queryXpath('//div[@id="test"]/span[2]');
    

    The Zend_Dom_Query library uses xpath behind a scenes to execute CSS style queries so, if you got struck with xpath syntax, you can take a sneak peek into what's it generated with the getXpathQuery method on the result objects.