jq for HTML/XML — query documents with CSS selectors, XPath, or a pasted
Chrome DevTools JS Path (document.querySelector(...)), with automatic
HTML5-vs-XML mode detection.
Full design rationale lives in plan.md.
➜ ~ curl -sS 'https://news.ycombinator.com/' | selx '#hnmain > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(2) > span > b > a'
<a href="news">Hacker News</a>
➜ ~
➜ ~ curl -sS 'https://news.ycombinator.com/' | selx '//*[@id="hnmain"]/tbody/tr[1]/td/table/tbody/tr/td[2]/span/b/a' --text
Hacker News
➜ ~
➜ ~ curl -sS 'https://news.ycombinator.com/' | selx '//*[@id="hnmain"]/tbody/tr[1]/td/table/tbody/tr/td[2]/span/b/a' --json
{"#text":"Hacker News","@href":"news"}cargo build --release
# binary at target/release/selxselx [OPTIONS] <QUERY> [FILE]
<QUERY>: a CSS selector, an XPath expression, or a DevTools JS Path.[FILE]: input file; reads from stdin if omitted.
--xml/--html— explicit override (highest precedence).- File extension
.xml(case-insensitive). - Content sniffing: input starts with
<?xml ...?>. - Default: tolerant WHATWG HTML5 parsing.
document.querySelector[All]('...')→ unwrapped and routed to CSS.- Starts with
/,./, or an XPath axis (ancestor::,descendant::, ...) → XPath. - Otherwise → CSS.
Override auto-detection with --css / --xpath.
- (default): outer HTML/XML of each match.
-t, --text: inner text only.-a, --attr <NAME>: a specific attribute's value.-j, --json: NDJSON, one JSON object per matched node (@attr,#text, nested/array children — see plan.md for the full mapping).
By default, unprefixed queries match by local name regardless of namespace
URI, and any xmlns:prefix found in the document is auto-registered so
prefixed queries (//dc:title) work with no extra flags. Pass --strict-ns
to enforce literal W3C namespace resolution instead.
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | No match (only with -e / --exit-status) |
| 2 | I/O error |
| 3 | Fatal XML parse error (XML mode only; HTML never fails to parse) |
| 4 | Invalid CSS/XPath query syntax |
| 5 | Serialization error |
selx "ul > li" index.html
selx "a" --attr href index.html
selx "document.querySelectorAll('li.active')" index.html
selx "//book[price<10]" catalog.xml
selx "//dc:title" catalog.xml --text
selx "ul > li" --json index.html | jq '.'