close
Skip to content

sen-ltd/xml-query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xml-query

An XPath query tool for XML files. Extract nodes, text, attributes, or JSON from any XML document using XPath expressions.

Screenshot

Features

  • 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

Requirements

  • PHP 8.2+
  • ext-dom, ext-libxml, ext-json (included in standard PHP)

Installation

git clone https://github.com/sen-ltd/xml-query.git
cd xml-query
chmod +x bin/xml-query

Usage

# 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"

Exit Codes

Code Meaning
0 Success — matches found
1 No matches found
2 Usage or parse error

Docker

docker build -t xml-query .
docker run --rm -v "$PWD/data.xml:/data/input.xml:ro" xml-query /data/input.xml "//book/title"

Testing

composer install
vendor/bin/phpunit

License

MIT

Links

About

PHP CLI that queries XML files with XPath and outputs matches as XML, text, attributes, or JSON

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors