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.
useGT\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 = newDOMDocument();
$document->loadHTML($html);
$xpath = newDOMXPath($document);
$inputElementList = $xpath->query(newTranslator("form>label>input"));To correctly work with XML documents, where the attributes are case-sensitive, pass false to the htmlMode property of the constructor.
$translator = newTranslator("[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.