xpath-cli
A command-line tool for evaluating XPath expressions on XML or HTML documents.
xpath-cli provides a single command, xpath, that evaluates XPath expressions
against XML or HTML input. It is a small, focused wrapper around the
battle-tested libxml2 library, bringing the full power of XPath 1.0 to the
terminal in a composable, pipe-friendly form.
It pairs naturally with its sibling tool
grex, which flattens XML into a greppable
line-oriented format.
Features
- XPath 1.0 — full expression support backed by libxml2
- HTML support — parses real-world HTML documents, not just well-formed XML
- Pipe-friendly — reads from stdin or a file; outputs one result per line
- Composable — combine with
curl,grep,sed, and other UNIX tools naturally
Installation
cargo install xpath-cli
Or build from source:
git clone https://github.com/jake-low/xpath-cli
cd xpath-cli
cargo install --path .
Note:
xpath-clidepends onlibxml2. On most Linux systems it is already present. On macOS, install it withbrew install libxml2.If you need to install the build dependency explicitly:
# Debian / Ubuntu apt install libxml2-dev # Fedora dnf install libxml2-devel
Usage
# Evaluate an XPath expression on a local XML file
xpath '//book/title/text()' library.xml
# Works equally well on HTML — fetch a Wikipedia page and extract the title
curl -sL https://en.wikipedia.org/wiki/Special:Random | xpath '//h1/span/text()'
# Extract an XML attribute from a remote API response
curl -sL https://osm.org/api/0.6/changeset/157745397 \
| xpath '//changeset/tag[@k = "comment"]/@v'
# Count nodes matching a predicate
curl -sL https://example.com/feed.xml \
| xpath 'count(//item)'
# Read from stdin explicitly
cat sitemap.xml | xpath '//url/loc/text()'Practical examples
# Extract all hyperlinks from an HTML page
curl -sL https://example.com | xpath '//a/@href'
# Pull every price out of a product feed
xpath '//product[@available="true"]/price/text()' catalog.xml
# Check the value of a specific attribute
xpath '//configuration/database/@host' config.xml
# List all XML namespaces declared in a document
xpath '//namespace::*' document.xmlRelationship to grex
xpath-cli and grex are complementary
tools by the same author. Use grex to flatten XML into a line-oriented format
for use with grep, awk, and diff; use xpath when you want to query the
document with a precise XPath expression and extract specific values directly.
# Two ways to get the same data
xpath '//menu/pizza/@price' pizzeria.xml
grex pizzeria.xml | grep '@price'