xmlstarlet is a set of XML tools in one command: sel extracts data by XPath, ed edits a document, val checks well-formedness and schemas, fo formats. For XML it's what jq is for JSON.
Syntax
xmlstarlet sel [options] -t template [file…]
xmlstarlet ed [options] action… [file…]
xmlstarlet val [options] file…
xmlstarlet fo [options] [file]
Without a file (or with -) it reads stdin.
Subcommands
| Command | What it does |
|---|
sel (select) | select data by XPath, output text or XML |
ed (edit) | change, add, delete, rename, move nodes |
val (validate) | check that the XML is well-formed; against a DTD, XSD or RelaxNG |
fo (format) | format with indentation |
el (elements) | show the structure: the paths of all elements |
tr (transform) | apply XSLT |
esc / unesc | escape / unescape <, >, & |
sel: selection
Global options go before -t, the template — after.
| Global option | What it does |
|---|
-T | output as text: without it -o '<a&b>' prints <a&b> |
-N p=URI | declare a namespace prefix |
-Q | print nothing, just the exit code |
-I | indent XML output |
-B | remove insignificant whitespace |
| Template option | What it does |
|---|
-t | start a template (there can be several) |
-v xpath | output a value (text) |
-c xpath | output a copy of the node with tags |
-m xpath | for each node found, run the following actions (a loop) |
-o 'string' | output a string as is |
-n | newline |
-i xpath / --elif / --else | condition |
-s A:N:- xpath | sorting inside -m: A/D — ascending/descending, N/T — as numbers/text |
-e name / -a name | create an element / attribute in the output |
-b | end the current -m or -i |
-f | output the input file name |
--var name=xpath | a variable $name |
ed: editing
| Action | What it does |
|---|
-u xpath -v value | replace a node's text or an attribute's value |
-u xpath -x expression | replace with the result of an XPath: -x '. * 2' |
-d xpath | delete nodes |
-s xpath -t elem|attr|text -n name -v value | add a child node (subnode) — at the end |
-i xpath -t … -n name -v … | insert before the node; with -t attr — add an attribute to that node |
-a xpath -t … -n name -v … | insert after the node |
-r xpath -v new_name | rename an element or attribute |
-m xpath from xpath to | move a node |
ed option | What it does |
|---|
-L | edit the file in place |
-P | preserve spaces and line breaks as they were |
-O | don't print <?xml …?> |
-N p=URI | namespace prefix (goes last among the global options) |
val, fo, el
| Command and option | What it does |
|---|
val file… | well-formedness only: file - valid / invalid |
val -e | show what is wrong (line, column, reason) |
val -s schema.xsd / -d file.dtd / -r schema.rng | schema validation |
val -q | print nothing, just the exit code |
val -b / -g | list only bad / only good files |
fo | 2-space indent; -s N — N spaces, -t — tabs, -o — no <?xml?>, -R — try to repair broken XML |
el | element paths; -a — with attributes, -v — with their values, -u — without duplicates |
Exit codes
sel — 1 if the output is empty, otherwise 0. The code depends on the output itself: -v '//nope' -n prints a newline and returns 0. To check "does the node exist", use -m: sel -t -m '//nope' -v . -n returns 1 if there are no nodes. val — 0 if all files passed, otherwise 1.
Examples
cat > books.xml <<'EOF'
<catalog>
<book id="b1" lang="en"><title>Linux Basics</title><price>10.5</price></book>
<book id="b2" lang="de"><title>Bash</title><price>20</price></book>
<book id="b3" lang="en"><title>Networks</title><price>15</price></book>
</catalog>
EOF
# all titles, one per line
xmlstarlet sel -t -v '//title' -n books.xml
# an attribute value
xmlstarlet sel -t -v '//book[2]/@id' -n books.xml
# a table: id and title for each book
xmlstarlet sel -T -t -m '//book' -v '@id' -o $'\t' -v 'title' -n books.xml
# filter by attribute and by number
xmlstarlet sel -t -m '//book[@lang="en"]' -v title -n books.xml
xmlstarlet sel -t -m '//book[price > 12]' -v title -n books.xml
# XPath functions: count, sum
xmlstarlet sel -t -v 'count(//book)' -n books.xml
xmlstarlet sel -t -v 'sum(//price)' -n books.xml
# sort by price, descending
xmlstarlet sel -T -t -m '//book' -s D:N:- price -v 'concat(@id,",",price)' -n books.xml
# change the price of a specific book — to stdout
xmlstarlet ed -u '//book[@id="b1"]/price' -v 12 books.xml
# the same, but in the file itself
xmlstarlet ed -L -u '//book[@id="b1"]/price' -v 12 books.xml
# add an attribute, a child element, delete a node
xmlstarlet ed -i '//book[1]' -t attr -n year -v 2024 books.xml
xmlstarlet ed -s '//book[1]' -t elem -n author -v 'A. Author' books.xml
xmlstarlet ed -d '//book[@lang="de"]' books.xml
# recompute all prices with an expression
xmlstarlet ed -u '//price' -x '. * 2' books.xml
# check well-formedness, with a detailed error
xmlstarlet val -e books.xml
# validate against an XSD
xmlstarlet val -e -s schema.xsd books.xml
# format one-line XML
echo '<a><b>1</b><c/></a>' | xmlstarlet fo
# the structure of an unfamiliar document
xmlstarlet el -u books.xml
Pitfalls
- The default namespace. If the root declares
xmlns="…", then //item finds nothing: the element is actually in a namespace. Declare a prefix with -N x=URI and write //x:item, or use the built-in prefix _: //_:item.
- Without
-n, the last line has no newline — in a terminal, the prompt "sticks" to the output.
-v with a node set prints all values (separated by newlines), not just the first, as in XSLT 1.0.
ed reformats the document. One-line elements are spread across lines with indentation. If you need the original layout, add -P, and before the actions: ed -P -u …, not at the end.
ed without -L doesn't change the file, it only prints the result. And -L silently overwrites the file — check the output without it first.
- By default
val doesn't say what's wrong — just invalid. Add -e.
val without a schema checks only the syntax. A document with wrong values stays valid until you pass -s, -d or -r.
- Quotes. Put the XPath in single shell quotes, and strings inside it in double quotes:
'//book[@lang="en"]'.
sel returns 1 on empty output. In scripts with set -e this stops execution; while -n in the template, on the contrary, masks "nothing found" as success.
See also
xmllint · jq · yq · iconv