LinuxCommandLibrary

xmlstarlet

Query, transform, and validate XML documents

TLDR

Format an XML document and print to stdout

$ xmlstarlet format [path/to/file.xml]
copy

XML document can also be piped from stdin
$ [cat path/to/file.xml] | xmlstarlet format
copy

Print all nodes that match a given XPath
$ xmlstarlet select --template --copy-of [xpath] [path/to/file.xml]
copy

Insert an attribute to all matching nodes, and print to stdout (source file is unchanged)
$ xmlstarlet edit --insert [xpath] --type attr --name [attribute_name] --value [attribute_value] [path/to/file.xml]
copy

Update the value of all matching nodes in place (source file is changed)
$ xmlstarlet edit --inplace --update [xpath] --value [new_value] [file.xml]
copy

Delete all matching nodes in place (source file is changed)
$ xmlstarlet edit --inplace --delete [xpath] [file.xml]
copy

Escape or unescape special XML characters in a given string
$ xmlstarlet [un]escape [string]
copy

List a given directory as XML (omit argument to list current directory)
$ xmlstarlet ls [path/to/directory]
copy

SYNOPSIS

xmlstarlet command [options] [arguments]

PARAMETERS

help
    Displays the help message.

version
    Displays the version information.

validate
    Validates XML file(s) against DTD or schema.

format
    Formats XML document for readability.

edit
    Edits XML documents (insert, update, delete).

xpath
    Executes XPath queries on XML documents.

transform
    Applies XSLT transformations to XML documents.

namespace
    Manages namespaces within XML documents.

c14n
    Canonicalizes XML document.

DESCRIPTION

The xmlstarlet command is a versatile command-line XML toolkit. It can be used to query, transform, validate, and edit XML documents. It's particularly useful for automating XML processing tasks within scripts.

xmlstarlet supports XPath 1.0, allowing you to precisely extract and manipulate data from XML files. It also supports XSLT transformations enabling sophisticated restructuring and formatting. It's commonly used for tasks such as parsing XML configuration files, extracting data for reporting, or converting XML data to other formats. Its command-line nature makes it ideal for batch processing and integration into automated workflows.

It provides a wide range of subcommands for different tasks, including validating documents against schemas, formatting for readability, performing namespace operations, and more. xmlstarlet is designed to be efficient and scriptable, making it a valuable tool for system administrators, developers, and anyone working with XML data on the command line.

EXIT STATUS

The xmlstarlet command returns an exit status of 0 for success and a non-zero status for errors.

EXAMPLES

Extracting data: `xmlstarlet xpath -t -v '//book/title' books.xml`
Formatting XML: `xmlstarlet format books.xml`

HISTORY

xmlstarlet was developed to provide a command-line utility for processing XML documents. Its development focused on providing tools for querying, transforming, and editing XML data within shell scripts and other automated workflows. It has seen widespread adoption in environments where XML data needs to be manipulated from the command line.

SEE ALSO

xsltproc(1), xmllint(1)

Copied to clipboard