The CssSelector ComponentEdit this page
Warning: You are browsing the documentation for Symfony 2.3, which is no longer maintained.
Read the updated version of this page for Symfony 6.3 (the current stable version).
The CssSelector Component
The CssSelector component converts CSS selectors to XPath expressions.
You can install the component in 2 different ways:
- Install it via Composer (
- Use the official Git repository (https://github.com/symfony/css-selector).
Then, require the
vendor/autoload.php file to enable the autoloading mechanism
provided by Composer. Otherwise, your application won't be able to find the classes
of this Symfony component.
Why to Use CSS selectors?
When you're parsing an HTML or an XML document, by far the most powerful method is XPath.
XPath expressions are incredibly flexible, so there is almost always an XPath expression that will find the element you need. Unfortunately, they can also become very complicated, and the learning curve is steep. Even common operations (such as finding an element with a particular class) can require long and unwieldy expressions.
Many developers -- particularly web developers -- are more comfortable
using CSS selectors to find elements. As well as working in stylesheets,
CSS selectors are less powerful than XPath, but far easier to write, read and understand. Since they are less powerful, almost all CSS selectors can be converted to an XPath equivalent. This XPath expression can then be used with other functions and classes that use XPath to find elements in a document.
The CssSelector Component
The component's only goal is to convert CSS selectors to their XPath equivalents:
1 2 3
use Symfony\Component\CssSelector\CssSelector; var_dump(CssSelector::toXPath('div.item > h4 > a'));
This gives the following output:
descendant-or-self::div[@class and contains(concat(' ',normalize-space(@class), ' '), ' item ')]/h4/a
You can use this expression with, for instance, DOMXPath or SimpleXMLElement to find elements in a document.
The Crawler::filter() method uses the CssSelector component to find elements based on a CSS selector string. See the The DomCrawler Component for more details.
Limitations of the CssSelector Component
Not all CSS selectors can be converted to XPath equivalents.
There are several CSS selectors that only make sense in the context of a web-browser.
- link-state selectors:
- selectors based on user action:
- UI-state selectors:
:first-letter) are not supported because they select portions of text
rather than elements.
Several pseudo-classes are not yet supported:
*:only-of-type. (These work with an element name (e.g.
li:first-of-type) but not with