Symfony CssSelector

Converts CSS selectors into equivalent XPath expressions for querying HTML and XML in PHP.

Library
Composer
vv8.1.6
7,419 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
71 /100 Good
Development Activity 56
Maintenance 68
Community 60
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
81 /100 Excellent
Architecture 86
Code Quality 88
Innovation 66
Learning Curve 85

The Symfony CssSelector component translates CSS selectors into equivalent XPath 1.0 expressions, so you can query HTML and XML documents using the familiar, concise CSS syntax instead of writing verbose XPath by hand. It is the piece that lets tools like Symfony’s DomCrawler and many web-scraping and testing libraries accept selectors such as “div.article > p” and run them against a DOM.

A port of Python’s cssselect library, the component parses a CSS selector into an abstract syntax tree and compiles it to XPath, supporting a broad range of selector features including combinators, attribute selectors, and pseudo-classes. It is dependency-light, well tested, and maintained as part of the Symfony ecosystem.

What You Get

  • A CssSelectorConverter class that turns a CSS selector string into an XPath expression
  • Support for combinators (descendant, child, adjacent, and general sibling)
  • Attribute selectors, class and ID selectors, and a wide set of pseudo-classes
  • An HTML mode and an XML mode for correct namespace and case handling
  • A clean parser/translator architecture that can be extended with custom pseudo-class handlers
  • A stable, well-tested component backed by the Symfony project

Common Use Cases

  • Powering DOM querying in web scrapers and crawlers with CSS selectors
  • Locating elements in functional and acceptance tests of web pages
  • Extracting data from HTML or XML feeds using concise selector strings
  • Building higher-level DOM traversal APIs on top of PHP’s DOMXPath

Under The Hood

Architecture - The component is split into a CSS layer and an XPath layer. A tokenizer and Parser build an AST of Node objects (ElementNode, ClassNode, AttributeNode, CombinedSelectorNode, and so on) from a selector string; a set of Translator classes then walk that AST and emit XPath, with HtmlExtension and XmlExtension supplying node/combination/pseudo-class handlers. The public CssSelectorConverter class wires these together and exposes a single toXPath() method.

Tech Stack - Pure PHP (92% of the codebase, with HTML test fixtures) targeting modern PHP versions, using PSR-4 autoloading under the Symfony\Component\CssSelector namespace. It has essentially no runtime dependencies beyond the PHP standard library and integrates with PHP’s native DOMXPath.

Code Quality - As a long-standing Symfony component it carries an extensive PHPUnit test suite, strict coding standards enforced via CI, and a decade-plus of maintenance across 88 contributors and 100+ tagged releases. Error handling surfaces parse problems through dedicated exception types (SyntaxErrorException, ExpressionErrorException).

API Design - The primary API is intentionally minimal: instantiate CssSelectorConverter (optionally in HTML or XML mode) and call toXPath() with a selector. Advanced users can register custom extensions, but the common path requires almost no boilerplate and reads naturally.

Used by 6 apps in this directory

PHP
78%
Other

Craft CMS

CMS

3,609

A developer-first PHP CMS with clean-slate content modeling, auto-generated GraphQL API, and a four-tier edition system that scales from solo projects to enterprise deployments.

View details
96
Repo Health
83
Technical
62
Dependency
Built with
PHP 78%
JavaScript 14%
Updated 5 days ago
PHP
91%
AGPL 3.0

FreeScout

Customer Support

4,565

Run your own help desk and shared inbox — a fully self-hosted, open-source alternative to Zendesk and Help Scout with no per-agent fees.

View details
93
Repo Health
55
Technical
63
Dependency
Built with
PHP 91%
Updated 4 days ago
PHP
63%
AGPL 3.0

Hyvor Relay

AI Development · Devops · Monitoring

908

Self-hosted, open-source email API that automates DNS, manages SMTP delivery, and provides deep observability — replacing SES, Mailgun, and SendGrid with infrastructure you fully own.

View details
68
Repo Health
79
Technical
70
Dependency
Built with
PHP 63%
Svelte 23%
Go 11%
Updated 1 weeks ago
PHP
86%
AGPL 3.0

Kimai

Invoicing Finance · Project Management

5,046

Professional open-source time tracking with invoicing, multi-user support, SAML/LDAP auth, and a full REST API—self-host it or use the cloud.

View details
94
Repo Health
78
Technical
64
Dependency
Built with
PHP 86%
Updated 4 days ago
TypeScript
44%
Other

Magic

AI Agents · Automation · Low Code Platforms

5,043

Magic is an enterprise-grade open-source AI agent platform combining a generalist AI agent, workflow engine, IM, and collaborative office system for running an AI-powered digital workforce.

View details
70
Repo Health
79
Technical
65
Dependency
Built with
TypeScript 44%
PHP 32%
Updated 1 months ago
PHP
84%
MIT

wallabag

Bookmarks Archiving

12,989

Self-hosted read-it-later app that saves clean, ad-free articles from any webpage for distraction-free reading across all your devices.

View details
93
Repo Health
81
Technical
59
Dependency
Built with
PHP 84%
Twig 13%
Updated 4 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers