phpgt / cssxpath
Convert CSS selectors to XPath queries.
Fund package maintenance!
PhpGt
Installs: 8 097 682
Dependents: 2
Suggesters: 0
Security: 0
Stars: 20
Watchers: 5
Forks: 11
Open Issues: 6
Requires
- php: >=8.0
Requires (Dev)
- ext-dom: *
- ext-libxml: *
- phpstan/phpstan: ^v1.8
- phpunit/phpunit: ^v9.5
- dev-master
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v0.2.0
- v0.1.0
- v0.0.2
- v0.0.1
- dev-nth-last-of-type-tests
- dev-case-insensitive-attribute-names--ci
- dev-dependabot/composer/phpstan/phpstan-1.10.6
- dev-dependabot/composer/phpunit/phpunit-9.6.5
- dev-22-checkbox
- dev-circleci
- dev-4-pseudo-selectors
This package is auto-updated.
Last update: 2024-11-10 14:22:42 UTC
README
A lightweight and dependency free CSS to XPath translator. This repository is used to bring modern DOM functionality like querySelectorAll()
to PHP in the PHP.Gt/Dom project.
Example usage
use Gt\CssXPath\Translator; $html = <<<HTML <form> <label> Name <input name="name" /> </label> <label> Code: <input name="code" /> </label> <button name="do" value="submit">Submit code</button> </form> HTML; $document = new DOMDocument(); $document->loadHTML($html); $xpath = new DOMXPath($document); $inputElementList = $xpath->query(new Translator("form>label>input");
Using this library with XML Documents
To correctly work with XML documents, where the attributes are case-sensitive, pass false
to the htmlMode
property of the constructor.
$translator = new Translator("[data-FOO='bar']", htmlMode: false);
It's perhaps worth noting that for XML-style matching to work, you must load the document content with DOMDocument->load/DOMDocument->loadXML instead of DOMDocument->loadHTMLFile/DOMDocument->loadHTML, as the HTML loading methods automatically convert the tags and attribute names to lowercase. This is handled automatically when using PHP.Gt/Dom.