Skip to content

wudi/selx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

selx

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.

Examples

~ 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"}

Build

cargo build --release
# binary at target/release/selx

Usage

selx [OPTIONS] <QUERY> [FILE]
  • <QUERY>: a CSS selector, an XPath expression, or a DevTools JS Path.
  • [FILE]: input file; reads from stdin if omitted.

Mode detection (HTML vs. XML)

  1. --xml / --html — explicit override (highest precedence).
  2. File extension .xml (case-insensitive).
  3. Content sniffing: input starts with <?xml ...?>.
  4. Default: tolerant WHATWG HTML5 parsing.

Query routing (CSS vs. XPath vs. JS Path)

  1. document.querySelector[All]('...') → unwrapped and routed to CSS.
  2. Starts with /, ./, or an XPath axis (ancestor::, descendant::, ...) → XPath.
  3. Otherwise → CSS.

Override auto-detection with --css / --xpath.

Output formatting

  • (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).

Namespaces (XML)

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.

Exit codes

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

Examples

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 '.'

About

selx - XPath/CSS selector for HTML/XML

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages