codealfa / css2xpath
CSS to XPath Converter
3.0.1
2026-09-01 16:02 UTC
Requires
- php: >=8.1
- codealfa/regextokenizer: ^3.0
Requires (Dev)
- phpunit/phpunit: ^9.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Installation
Run the following command to use the library in your projects.
composer require codealfa/css2xpath
Basic Usage
use CodeAlfa\Css2Xpath\Css2XpathConverter; use CodeAlfa\Css2Xpath\SelectorFactory; $converter = new Css2XpathConverter(new SelectorFactory()); $xPath = $converter->convert('p#main, div.container'); var_dump($xPath);
Output:
descendant-or-self::p[@id="main"]|descendant-or-self::div[@class and contains(concat(" ", normalize-space(@class), " "), " container ")]
Notes
-
The Selector classes are extendable if you need to add or change functionality. You'll just need to create your own
SelectorFactoryor extend the existing one to inject into theCss2XpathConverterclass. -
Some CSS pseudo-selectors only make sense in the context of a web browser. The following pseudo-classes are supported:
- :enabled
- :disabled
- :read-only
- :read-write
- :checked
- :required
- :root
- :empty
- :first-child
- :last-child
- :only-child
- :first-of-type
- :last-of-type
- :only-of-type
- :nth-child()
- :nth-last-child()
- :nth-of-type()
- :nth-last-of-type()
- :not
- :has
- :is
- :where
Unsupported pseudo-classes are ignored. CSS pseudo-elements such as
::before,::after,::first-line, and::first-letterare recognized but do not add a node to the XPath result.
License
GPL-3.0 or later