Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpgt/cssxpath
Translate CSS selectors to XPath queries
https://github.com/phpgt/cssxpath
css css-selector css-selector-parser dom phpgt translate-css-selectors xpath xpath-queries
Last synced: 3 days ago
JSON representation
Translate CSS selectors to XPath queries
- Host: GitHub
- URL: https://github.com/phpgt/cssxpath
- Owner: PhpGt
- License: mit
- Created: 2018-01-10T23:27:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-07-10T13:36:01.000Z (4 months ago)
- Last Synced: 2024-11-14T12:33:42.577Z (5 days ago)
- Topics: css, css-selector, css-selector-parser, dom, phpgt, translate-css-selectors, xpath, xpath-queries
- Language: PHP
- Homepage: https://www.php.gt/cssxpath
- Size: 268 KB
- Stars: 20
- Watchers: 5
- Forks: 11
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Translate CSS selectors to XPath queries
========================================A lightweight and dependency free CSS to XPath translator. This repository is used to bring modern DOM functionality like [`querySelectorAll()`][qsa] to PHP in the [PHP.Gt/Dom][gt-dom] project.
***
Example usage
-------------```php
use Gt\CssXPath\Translator;$html = <<
Name
Code:
Submit codeHTML;
$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.
```php
$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][gt-dom].
[qsa]: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
[gt-dom]: https://www.php.gt/dom