Linux command
xmlstarlet 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Select nodes
xmlstarlet sel -t -v "[xpath]" [file.xml]
Format/pretty-print
xmlstarlet fo [file.xml]
Validate an XML file
xmlstarlet val -s [schema.xsd] [file.xml]
Edit an XML file
xmlstarlet ed -u "[xpath]" -v "[new_value]" [file.xml]
Transform XML
xmlstarlet tr [stylesheet.xsl] [file.xml]
List element structure
xmlstarlet el [file.xml]
Escape special characters
echo "[text]" | xmlstarlet esc
Check well-formedness
xmlstarlet val --well-formed [file.xml]
说明
XMLStarlet is a command-line XML toolkit that provides Unix-style utilities for processing XML files. It allows querying, transforming, validating, and editing XML documents using familiar shell command patterns, similar to how grep, sed, and awk work with plain text. The toolkit uses XPath for node selection and supports XSLT for transformations. It can validate against DTDs, XML Schema (XSD), and RelaxNG. Built on libxml2 and libxslt libraries, it provides robust XML processing capabilities. Each command has its own options; use xmlstarlet command --help for detailed help on specific commands.
参数
- sel (select)
- Select or query XML nodes using XPath expressions.
- ed (edit)
- Edit XML documents by inserting, deleting, updating, or renaming nodes.
- tr (transform)
- Perform XSLT transformations on XML documents.
- val (validate)
- Validate XML documents against DTDs or XML Schemas.
- fo (format)
- Format XML documents for readability with proper indentation.
- el (elements)
- Display element structure of an XML document.
- c14n (canonic)
- Canonicalize XML documents (C14N).
- ls (list)
- List directory contents as XML.
- esc (escape)
- Escape special XML characters in text.
- unesc (unescape)
- Unescape XML entities to original characters.
- pyx
- Convert XML to PYX format.
- p2x (depyx)
- Convert PYX back to XML.
- --version
- Display version information.
- --help
- Display help message.
FAQ
What is the xmlstarlet command used for?
XMLStarlet is a command-line XML toolkit that provides Unix-style utilities for processing XML files. It allows querying, transforming, validating, and editing XML documents using familiar shell command patterns, similar to how grep, sed, and awk work with plain text. The toolkit uses XPath for node selection and supports XSLT for transformations. It can validate against DTDs, XML Schema (XSD), and RelaxNG. Built on libxml2 and libxslt libraries, it provides robust XML processing capabilities. Each command has its own options; use xmlstarlet command --help for detailed help on specific commands.
How do I run a basic xmlstarlet example?
Run `xmlstarlet sel -t -v "[xpath]" [file.xml]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does sel (select) do in xmlstarlet?
Select or query XML nodes using XPath expressions.