Prefixes and advanced usage
Prefixes and advanced usage
Custom XPath prefix
By default each translated selector starts with .//. We can customise this by passing prefix.
echo new Translator("h1, p", prefix: "descendant-or-self::");
// descendant-or-self::h1 | descendant-or-self::p
This is useful when integrating into existing XPath strategies.
Using asXPath() vs casting
Both of these are equivalent:
$xpathA = (string)new Translator(".content");
$xpathB = (new Translator(".content"))->asXPath();
Use whichever reads better in your codebase.
Combining predicates
Complex selectors can stack class, id, attribute, and pseudo predicates together.
main article .content[data-categories~=test]:nth-child(2)
Here we can keep selectors readable while still producing a precise XPath query.
Current scope
This repository is intentionally lightweight and dependency-free. It focuses on a practical subset of CSS selectors commonly used for DOM traversal and querySelector-style APIs.
Return to Home.