freshrss/edge/lib/phpgt/cssxpath
README| modified | Tuesday 15 September 2026 |
|---|
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.
1use Gt\CssXPath\Translator;
2
3$html = <<<HTML
4<form>
5 <label>
6 Name
7 <input name="name" />
8 </label>
9 <label>
10 Code:
11 <input name="code" />
12 </label>
13 <button name="do" value="submit">Submit code</button>
14</form>
15HTML;
16
17$document = new DOMDocument();
18$document->loadHTML($html);
19
20$xpath = new DOMXPath($document);
21$inputElementList = $xpath->query(new Translator("form>label>input"));
To correctly work with XML documents, where the attributes are case-sensitive, pass false to the htmlMode property of the constructor.
1$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.