views:

32

answers:

1

I'm using SimpleXML to load and parse an XML document with PHP.

I'm not too familiar with XPath and I would prefer using CSS selectors to parse it. What are my options? Is there anything pre-build for SimpleXML, or should I be using something different altogether ?

+2  A: 

SimpleXML doesn't support CSS selectors, but I know that some PHP and Javascript librairies can convert a CSS selector to an XPath query. IIRC, there's a PHP library that mimicks jQuery's selectors but I can't remember its name. Hopefully it will jumpstart someone else's memory.


Turns out the library I was thinking about doesn't support CSS selectors, but the good news is the Zend Framework has a CSS-to-XPath translator:

include 'Zend/Dom/Query/Css2Xpath.php';  

$xpath = Zend_Dom_Query_Css2Xpath::transform('div.class span');

$mySimpleXMLElement->xpath($xpath);
Josh Davis
Thanks! Exactly what I was looking for. Works great.
Andrei