How to Pretty-Print and Validate XML Files in Linux Terminal Using xmllint

Extensible Markup Language (XML) continues to serve as a cornerstone data format across modern enterprise software engineering. From Apache Maven build configurations (pom.xml) and Android application manifests to SVG graphics, RSS web feeds, SAML single-sign-on assertions, and SOAP web services, XML is ubiquitous. However, automated systems and web APIs frequently export minified or raw XML stripped of line breaks and indentation, rendering files nearly impossible to parse with the human eye.

Just as jq is the premier utility for manipulating JSON in the terminal, xmllint is the quintessential command-line tool for formatting, validating, and querying XML. Included as part of the core libxml2 suite, xmllint provides rapid syntax checks, schema validation, and XPath querying. This tutorial demonstrates how to install xmllint, reformat minified files, validate documents against schemas, and extract nodes from the Linux shell.

Installing xmllint on Linux

The xmllint executable is packaged within the standard libxml2 utilities repository on all major distributions:

Ubuntu, Debian, and Linux Mint

sudo apt update
sudo apt install libxml2-utils

Fedora, RHEL, and CentOS

sudo dnf install libxml2

Arch Linux and Manjaro

sudo pacman -S libxml2

Confirm the tool is available and check its installed build by running:

xmllint --version

How to Pretty-Print and Indent Minified XML Files

To transform a single-line, unformatted XML document into a clean, human-readable hierarchy with standard two-space indentation, use the --format flag:

xmllint --format input.xml

This command prints the formatted output directly to your terminal standard output (stdout). To save the formatted code into a clean new file, use the --output parameter or standard shell redirection:

# Using the native --output flag
xmllint --format input.xml --output formatted.xml

# Or using shell redirection
xmllint --format input.xml > formatted.xml

If you want to customise the indentation spacing (for instance, using four spaces instead of two), declare the XMLLINT_INDENT environment variable before running the command:

XMLLINT_INDENT="    " xmllint --format input.xml --output formatted.xml

How to Validate XML Syntax (Checking for Well-Formedness)

Before deploying configuration files or executing database migrations, verifying that an XML document is syntactically “well-formed” (with matching opening/closing tags, valid character encodings, and proper quote closures) is essential.

Use the --noout flag to suppress printing the document contents while capturing syntax validation results:

xmllint --noout config.xml
  • If the file is valid: xmllint outputs nothing and terminates with an exit status code of 0.
  • If the file contains syntax errors: xmllint prints the exact line number, column index, and error description (e.g. “Opening and ending tag mismatch”) and exits with a non-zero error code.

This makes xmllint --noout ideal for automated Git pre-commit hooks and CI/CD pipelines:

xmllint --noout config.xml && echo "XML is valid!" || echo "XML syntax error detected!"

Validating XML Against DTD and XSD Schemas

Beyond basic well-formedness, enterprise workflows often require validating that XML data adheres strictly to a Document Type Definition (DTD) or XML Schema Definition (XSD):

Validating Against an XSD Schema:

xmllint --noout --schema schema.xsd data.xml

Validating Against a DTD Specification:

xmllint --noout --dtdvalid schema.dtd data.xml

If any element violates schema definitions—such as missing mandatory attributes, invalid numeric data types, or out-of-order sequence nodes—xmllint pinpoints the exact structural discrepancy.

Querying XML Nodes Using XPath in the Terminal

Extracting specific values from deep XML hierarchies without writing a custom Python or Perl script is simple using the --xpath flag:

# Extract all title text elements from a catalog
xmllint --xpath '//book/title/text()' catalog.xml

# Extract the value of a specific attribute
xmllint --xpath 'string(//server/@port)' server.xml

# Count matching nodes
xmllint --xpath 'count(//user)' users.xml

Piping Live HTTP Streams Directly into xmllint

When debugging external REST/SOAP APIs or fetching remote RSS/Atom feeds with curl, you can pipe data straight into xmllint by substituting the filename with a dash (-):

curl -s https://example.com/feed.xml | xmllint --format -

This eliminates the need to create temporary files on disk and formats remote API responses immediately inside your terminal pager.

Troubleshooting XML Encoding Errors

If xmllint reports encoding errors when parsing international characters or special entity symbols:

  • Explicit Encoding Flag: Specify the encoding explicitly using --encode UTF-8 or --encode ISO-8859-1 to match your source document declaration.
  • Recovering Damaged XML: If you are inspecting a slightly corrupted file with small entity errors, append --recover to instruct xmllint to bypass minor flaws and format whatever portion of the tree can be salvaged.

Adding xmllint to your Linux toolkit simplifies XML debugging, ensures configuration integrity across production clusters, and makes inspection seamless.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.