Linux command
xmllint 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Check if an XML file is well-formed
xmllint [file.xml]
Format and pretty-print
xmllint --format [file.xml]
Validate against a DTD
xmllint --valid [file.xml]
Validate against an XSD schema
xmllint --schema [schema.xsd] [file.xml]
Extract data using XPath
xmllint --xpath "[//element/@attribute]" [file.xml]
Format in place
xmllint --format [file.xml] -o [file.xml]
Remove blank nodes
xmllint --noblanks [file.xml]
说明
xmllint is a command-line XML parser and processor from the libxml2 library. It can parse XML files to check for well-formedness, validate documents against DTDs or schemas (XSD, RelaxNG), format XML for readability, and query documents using XPath expressions. The tool is indispensable for working with XML data in shell scripts and automated workflows. It can detect syntax errors, validate against various schema types, extract specific data using XPath, and transform or clean up XML output. Common uses include validating configuration files, extracting data from XML APIs, formatting machine-generated XML for human readability, and preprocessing XML before further transformation with XSLT.
参数
- --format
- Reformat and reindent the output
- --valid
- Validate against the DTD declared in the document
- --schema _file_
- Validate against the specified XSD schema
- --relaxng _file_
- Validate against a RelaxNG schema
- --dtdvalid _file_
- Validate against the specified DTD
- --xpath _expression_
- Evaluate XPath expression and print results
- --output _file_, -o _file_
- Write output to specified file
- --noout
- Suppress normal output (useful with validation)
- --noblanks
- Remove ignorable blank nodes from output
- --encode _encoding_
- Output in specified encoding
- --html
- Parse input as HTML instead of XML
- --recover
- Try to recover from parsing errors
- --nonet
- Don't fetch external entities from network
- --c14n
- Canonicalize output (for digital signatures)
- --shell
- Enter interactive shell mode
- --version
- Display version information
FAQ
What is the xmllint command used for?
xmllint is a command-line XML parser and processor from the libxml2 library. It can parse XML files to check for well-formedness, validate documents against DTDs or schemas (XSD, RelaxNG), format XML for readability, and query documents using XPath expressions. The tool is indispensable for working with XML data in shell scripts and automated workflows. It can detect syntax errors, validate against various schema types, extract specific data using XPath, and transform or clean up XML output. Common uses include validating configuration files, extracting data from XML APIs, formatting machine-generated XML for human readability, and preprocessing XML before further transformation with XSLT.
How do I run a basic xmllint example?
Run `xmllint [file.xml]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --format do in xmllint?
Reformat and reindent the output