codealfa / css2xpath
CSS to XPath Converter
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/codealfa/css2xpath
Requires
- php: >=8.0
- codealfa/regextokenizer: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2025-12-08 19:50:26 UTC
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:
p[@id="main"]|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, so only the following pseudo-selectors are
implemented. All others are ignored:
- :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
- :not
- :has
License
GPL-3.0 or later