An XPath query tool for XML files. Extract nodes, text, attributes, or JSON from any XML document using XPath expressions.
- Query XML files with standard XPath expressions
- Multiple output modes: XML, text-only, attribute values, JSON
- Node count mode for quick statistics
- XML namespace support with manual or auto-detection
- Clean exit codes for scripting (0 = match, 1 = no match, 2 = error)
- No external dependencies — uses PHP's built-in DOMDocument + DOMXPath
- PHP 8.2+
ext-dom,ext-libxml,ext-json(included in standard PHP)
git clone https://github.com/sen-ltd/xml-query.git
cd xml-query
chmod +x bin/xml-query# Print matching nodes as XML
xml-query data.xml "//book/title"
# Count matching nodes
xml-query --count data.xml "//item"
# Extract text content only
xml-query --text data.xml "//p"
# Extract attribute values
xml-query --attr href data.xml "//a"
# Output as JSON
xml-query --json data.xml "//record"
# With namespace support
xml-query --ns atom=http://www.w3.org/2005/Atom feed.xml "//atom:entry/atom:title"
# Auto-detect namespaces from document root
xml-query --auto-ns feed.xml "//atom:entry"| Code | Meaning |
|---|---|
| 0 | Success — matches found |
| 1 | No matches found |
| 2 | Usage or parse error |
docker build -t xml-query .
docker run --rm -v "$PWD/data.xml:/data/input.xml:ro" xml-query /data/input.xml "//book/title"composer install
vendor/bin/phpunitMIT
