xq: Query XML With jq Syntax
xq transcodes XML into JSON, runs a jq expression over it, and can serialize the result back to XML, so you can query XML with the jq language you already know.
XML still shows up in SOAP APIs, Maven POMs, and Android manifests. xq wraps jq so you can pull values out of XML in one line, mapping attributes to @-prefixed keys.
What it does
xq parses XML into a JSON structure where element text becomes values and attributes become keys prefixed with @, then pipes that JSON through jq. With -x it converts the jq result back to XML. Repeated child elements become arrays.
Common usage
xq '.project.version' pom.xml
cat manifest.xml | xq '.manifest["@package"]'
# turn a jq result back into XML
xq -x '.root' data.xmlOptions
| Flag | What it does |
|---|---|
| -x | Output XML (convert the jq result back to XML) |
| -r | Raw output: strings without JSON quotes |
| -c | Compact JSON output |
| --xml-item-depth <n> | Streaming depth for large XML documents |
In CI
xq is the pragmatic way to read a POM version or a manifest attribute without a full XML toolchain. Remember attributes are @-prefixed: a <dependency scope="test"> becomes ["@scope"]. Pair with -r to feed the value straight into another step.
Common errors in CI
"xq: Error: not well-formed (invalid token)" (or "syntax error") means the XML is malformed or has an unescaped ampersand. A jq path that misses because attributes need the @ prefix returns null; check with xq . file.xml first. Namespaced tags appear with their prefix (e.g. ["ns:tag"]) and must be quoted in the jq path.