← Full reference

xmlstarlet

XPath queries on XML, editing elements and attributes, checking and formatting — all from the command line

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

CommandWhat 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 / unescescape / unescape <, >, &

sel: selection

Global options go before -t, the template — after.

Global optionWhat it does
-Toutput as text: without it -o '<a&b>' prints &lt;a&amp;b&gt;
-N p=URIdeclare a namespace prefix
-Qprint nothing, just the exit code
-Iindent XML output
-Bremove insignificant whitespace
Template optionWhat it does
-tstart a template (there can be several)
-v xpathoutput a value (text)
-c xpathoutput a copy of the node with tags
-m xpathfor each node found, run the following actions (a loop)
-o 'string'output a string as is
-nnewline
-i xpath / --elif / --elsecondition
-s A:N:- xpathsorting inside -m: A/D — ascending/descending, N/T — as numbers/text
-e name / -a namecreate an element / attribute in the output
-bend the current -m or -i
-foutput the input file name
--var name=xpatha variable $name

ed: editing

ActionWhat it does
-u xpath -v valuereplace a node's text or an attribute's value
-u xpath -x expressionreplace with the result of an XPath: -x '. * 2'
-d xpathdelete nodes
-s xpath -t elem|attr|text -n name -v valueadd 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_namerename an element or attribute
-m xpath from xpath tomove a node
ed optionWhat it does
-Ledit the file in place
-Ppreserve spaces and line breaks as they were
-Odon't print <?xml …?>
-N p=URInamespace prefix (goes last among the global options)

val, fo, el

Command and optionWhat it does
val file…well-formedness only: file - valid / invalid
val -eshow what is wrong (line, column, reason)
val -s schema.xsd / -d file.dtd / -r schema.rngschema validation
val -qprint nothing, just the exit code
val -b / -glist only bad / only good files
fo2-space indent; -s N — N spaces, -t — tabs, -o — no <?xml?>, -R — try to repair broken XML
elelement 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