LinuxCommandLibrary

xml-format

Format and indent XML files

TLDR

Format an XML document, indenting with tabs

$ xml [[fo|format]] [[-t|--indent-tab]] [path/to/input.xml|URI] > [path/to/output.xml]
copy

Format an HTML document, indenting with 4 spaces
$ xml [[fo|format]] [[-H|--html]] [[-s|--indent-spaces]] [4] [path/to/input.html|URI] > [path/to/output.html]
copy

Recover parsable parts of a malformed XML document, without indenting
$ xml [[fo|format]] [[-R|--recover]] [[-n|--noindent]] [path/to/malformed.xml|URI] > [path/to/recovered.xml]
copy

Format an XML document from stdin, removing the DOCTYPE declaration
$ cat [path\to\input.xml] | xml [[fo|format]] [[-D|--dropdtd]] > [path/to/output.xml]
copy

Format an XML document, omitting the XML declaration
$ xml [[fo|format]] [[-o|--omit-decl]] [path\to\input.xml|URI] > [path/to/output.xml]
copy

Display help
$ xml [[fo|format]] --help
copy

SYNOPSIS

N/A
There is no universally recognized 'xml-format' command with a standard synopsis. The method for formatting XML depends on the specific tool used, such as xmllint or xmlstarlet.

DESCRIPTION

The command xml-format is not a standard, standalone utility widely available across all Linux distributions.
Typically, when users refer to 'xml-format' on Linux, they are describing the action of pretty-printing or re-indenting an XML document for better readability, rather than invoking a specific command named exactly 'xml-format'.

The functionality of formatting XML is commonly provided by various XML processing tools available in Linux environments. These tools read an XML input (from a file or standard input) and output a reformatted version, often with proper indentation and line breaks, adhering to XML well-formedness rules.

Common utilities used for this purpose include xmllint (part of the libxml2-utils package) and xmlstarlet (a separate command-line XML toolkit). These tools offer robust features for parsing, validating, transforming, and formatting XML documents.

CAVEATS

The primary caveat is that xml-format is not a direct command. Attempting to execute 'xml-format' directly on most Linux systems will result in a 'command not found' error.

Users must install and utilize specific XML processing tools (like xmllint or xmlstarlet) to achieve XML formatting. Each tool has its own syntax, options, and capabilities, which need to be consulted via their respective manual pages.

COMMON METHODS FOR XML FORMATTING

While 'xml-format' is not a direct command, here are the most common ways to achieve XML formatting on Linux:

1. Using xmllint:
xmllint is a command-line tool for parsing, validating, and formatting XML and HTML documents. It's often pre-installed or easily available via the 'libxml2-utils' package.
Syntax: xmllint --format [file.xml]
Example: cat input.xml | xmllint --format - > output.xml

2. Using xmlstarlet:
xmlstarlet is a powerful command-line XML toolkit that can validate, transform, query, and edit XML documents. Its 'format' subcommand is specifically for pretty-printing.
Syntax: xmlstarlet fo [file.xml]
Example: xmlstarlet fo input.xml > output.xml

SEE ALSO

xmllint(1), xmlstarlet(1), tidy(1)

Copied to clipboard