LinuxCommandLibrary

xmllint

Validate and format XML documents

TLDR

Return all nodes (tags) named "foo"

$ xmllint --xpath "//[foo]" [source_file.xml]
copy

Return the contents of the first node named "foo" as a string
$ xmllint --xpath "string(//[foo])" [source_file.xml]
copy

Return the href attribute of the second anchor element in an HTML file
$ xmllint --html --xpath "string(//a[2]/@href)" webpage.xhtml
copy

Return human-readable (indented) XML from file
$ xmllint --format [source_file.xml]
copy

Check that an XML file meets the requirements of its DOCTYPE declaration
$ xmllint --valid [source_file.xml]
copy

Validate XML against DTD schema hosted online
$ xmllint --dtdvalid [URL] [source_file.xml]
copy

SYNOPSIS

xmllint [options] XML-file...

PARAMETERS

--version
    Display the version of libxml2 used.

--valid
    Check if the document is valid.

--noout
    Suppress output. Normally, xmllint outputs the parsed document.

--schema <file>
    Validate using the specified XML Schema.

--dtdvalid <URL>
    Validate against the specified DTD.

--xpath <xpath-expression>
    Evaluate the XPath expression and print the result.

--format
    Reformat and indent the output XML.

--encode <encoding>
    Specify the output encoding.

--html
    Parse input as HTML.

--xinclude
    Process XInclude directives.

--sax1
    Use the SAX1 interface for parsing.

--stream
    Streaming XML processing, useful for very large documents.

--shell
    Run an interactive shell to navigate and manipulate XML documents.

DESCRIPTION

The xmllint command is a command-line XML parser and validator. It is part of the libxml2 library and is used to check the syntax and validity of XML documents. It can also be used to perform transformations on XML documents using XSLT stylesheets, and to extract information from XML documents using XPath expressions. xmllint is a powerful tool for developers and system administrators who need to work with XML data.

xmllint can be used for:
- Validating XML documents against a DTD or XML Schema.
- Formatting XML documents for readability.
- Transforming XML documents using XSLT stylesheets.
- Extracting data from XML documents using XPath expressions.
- Identifying syntax errors in XML documents.
It supports various features such as validating against DTDs and XML Schemas, applying XSLT transformations, and working with namespaces. It's a versatile utility suitable for both quick checks and more complex XML processing tasks.

CAVEATS

Validation may not be completely accurate if external resources are not available or if the document is malformed. Using --stream has limitations on features like validation.

EXIT STATUS

xmllint returns 0 on success, 1 if an error occurred, and 2 if a validation error was found.

EXAMPLES

Validate an XML file: xmllint --valid mydocument.xml
Format an XML file: xmllint --format mydocument.xml
Validate against a schema: xmllint --schema myschema.xsd mydocument.xml
Extract data using XPath: xmllint --xpath '//book/title/text()' mydocument.xml

HISTORY

xmllint is part of the libxml2 library, which was initially developed by Daniel Veillard and is now a widely used XML processing library. It has been around since the late 1990s/early 2000s and has been continuously improved and updated. It quickly became a popular tool for developers due to its comprehensive feature set and command-line interface.

SEE ALSO

xsltproc(1), xmlcatalog(1)

Copied to clipboard