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 ?

Andrei
  • 1,606
  • 4
  • 19
  • 31
  • possible duplicate of [PHP CSS Selector Library?](http://stackoverflow.com/questions/260605/php-css-selector-library) There's actually some pretty good info in that thread too... – ircmaxell Aug 26 '10 at 14:00
  • I think the question should stay. This one is specific to SimpleXML. – Josh Davis Aug 26 '10 at 14:06
  • Yep, read it, but the only solution mentioned that would of fit the bill is "CssSelector\Parser", a Symphony 2 component, linked to a post which mentioned a broken Github link. Browsing through Symphony's repo on Github I was able to find it here: http://github.com/fabpot/symfony/tree/master/src/Symfony/Component/CssSelector/. Not a duplicate question though because it was for XHTML and most answers are about DOM parsers. – Andrei Aug 26 '10 at 14:10
  • Well, there are a few resources for [converting css to xpath](http://blog.verkoyen.eu/blog/p/detail/css-selector-to-xpath-query). But personally, I would use either [phpQuery](http://code.google.com/p/phpquery/) or [domQuery](http://svn.assembla.com/svn/php_domquery/trunk/DomQuery.php) since they both work on the native DomDocument (and as such can be converted back and forth between `simplexml`... – ircmaxell Aug 26 '10 at 14:13
  • I would still recommend using SimpleXML because after finding the nodes you have to read them, read their children/attributes, etc.. and SimpleXML is the simplest way to do that. My advice: convert your CSS selectors to XPath during development and you'll learn XPath along the way. – Josh Davis Aug 26 '10 at 14:17
  • @Andrei: changing tags because this doesn't relates to XPath expressions. –  Aug 26 '10 at 20:30

2 Answers2

4

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
  • 28,400
  • 5
  • 52
  • 67
2

The Zend CSS-to-XPath translator is not full proof. It fails for index selectors - something like tr:eq(1) will not be converted to tr[2]

Akshay Raje
  • 852
  • 9
  • 20
  • @Andrei Recently discovered this JS function to convert css selectors to xpath - [james.padolsey.com/scripts/javascript/css2xpath.js](http://james.padolsey.com/scripts/javascript/css2xpath.js). Looks quite good but will have to be ported to PHP.4 Note: css2xpath.js is what [YQL](http://developer.yahoo.com/yql/console/?q=use%20'http%3A%2F%2Fyqlblog.net%2Fsamples%2Fdata.html.cssselect.xml'%20as%20data.html.cssselect%3B%20select%20*%20from%20data.html.cssselect%20where%20url%3D%22www.yahoo.com%22%20and%20css%3D%22%23news%20a%22) uses to parse css selectors too. – Akshay Raje Oct 12 '11 at 03:15