While modern API ecosystems and web payloads frequently rely on JSON, extensive enterprise infrastructure, Maven builds, system configuration profiles, RSS feeds, and SVG graphics remain deeply anchored in XML (Extensible Markup Language). Processing complex XML trees using general text tools like grep, sed, or awk is brittle and error-prone because XML is hierarchically nested, whitespace-agnostic, and namespace-sensitive.
XMLStarlet (invoked as xmlstarlet or symlinked as xml) is a specialized, high-performance command-line XML toolkit built upon libxml2 and libxslt. It functions for XML much like jq functions for JSON. This tutorial covers how to install XMLStarlet across major Linux distributions, format messy documents, validate XML against schemas, query nodes using XPath, and update element values directly from the terminal.
Installing XMLStarlet on Modern Linux Distributions
XMLStarlet is available directly in the primary package repositories of all major Linux distributions.
On Ubuntu, Debian, and Linux Mint
sudo apt update && sudo apt install xmlstarlet -y
Note: On Debian-based systems, the utility is installed as xmlstarlet. You can create a handy alias or symlink to xml by running: sudo ln -s /usr/bin/xmlstarlet /usr/local/bin/xml.
On Fedora, RHEL, and CentOS
sudo dnf install xmlstarlet -y
On Arch Linux and Manjaro
sudo pacman -S xmlstarlet
Confirm the installation and inspect the installed version:
xmlstarlet --version
Formatting and Pretty-Printing XML Documents
Minified or unindented XML files are virtually impossible to review visually. The fo (format) sub-command parses the document and prints a neatly indented representation:
xmlstarlet fo --indent-spaces 2 unformatted.xml
Useful formatting options include:
-s <n>or--indent-spaces <n>: Sets the number of indentation spaces per nested level (e.g.2or4).-tor--indent-tab: Indents using physical tabs instead of spaces.-oor--omit-decl: Suppresses the standard<?xml version="1.0"?>header declaration.
To pretty-print a file and save it in place without redirecting to a temporary file:
xmlstarlet fo -s 2 data.xml > temp.xml && mv temp.xml data.xml
Validating XML Syntax and Schemas
The val (validate) sub-command tests documents for well-formedness and schema conformity.
Checking Well-Formedness
xmlstarlet val -e data.xml
If the XML contains unclosed tags or syntax defects, -e prints explicit line numbers and error diagnostics.
Validating Against an XSD Schema
To ensure a configuration document adheres to an official XML Schema Definition:
xmlstarlet val --err --xsd schema.xsd config.xml
Querying and Extracting Data Using XPath (sel)
The sel (select) sub-command enables rapid extraction of values and attributes using XPath syntax. Consider an example file named inventory.xml:
<?xml version="1.0"?>
<servers>
<server id="web-01" status="active">
<hostname>prod-web-01.internal</hostname>
<ip>10.0.1.15</ip>
</server>
<server id="db-01" status="maintenance">
<hostname>prod-db-01.internal</hostname>
<ip>10.0.1.50</ip>
</server>
</servers>
Extracting All Hostnames
Extract all <hostname> text elements separated by newlines:
xmlstarlet sel -t -v "//hostname" -n inventory.xml
-t: Initiates an output template.-v "//hostname": Evaluates the XPath expression and extracts matching text values.-n: Emits a newline character between output items.
Querying Attribute Values with Conditional Filtering
To extract the IP address of only the server whose status attribute equals "active":
xmlstarlet sel -t -v "//server[@status='active']/ip" -n inventory.xml
Formatting Custom Tabular Output
You can construct structured command-line outputs by matching loops (-m) and combining attributes with text outputs:
xmlstarlet sel -t -m "//server" -v "@id" -o " | " -v "hostname" -o " | " -v "ip" -n inventory.xml
Output:
web-01 | prod-web-01.internal | 10.0.1.15
db-01 | prod-db-01.internal | 10.0.1.50
Editing and Modifying XML Elements in Place (ed)
The ed (edit) sub-command modifies nodes, values, and attributes without manual text editing.
Updating an Element’s Text Value
To update the IP address of db-01 to 10.0.1.99:
xmlstarlet ed -u "//server[@id='db-01']/ip" -v "10.0.1.99" inventory.xml
Updating or Adding an Attribute
To change the status of db-01 from maintenance to active:
xmlstarlet ed -u "//server[@id='db-01']/@status" -v "active" inventory.xml
Deleting an Element or Node
To delete a decommissioned server entry completely:
xmlstarlet ed -d "//server[@id='web-01']" inventory.xml
By pairing XMLStarlet queries and edits into shell scripts, automated deployment pipelines can inspect and modify XML-based application profiles reliably without depending on bulky scripting runtimes.