Format XML
xmlfmt [options] [file|-]
If no input file is provided, xmlfmt reads from stdin. If no mode is specified, xmlfmt defaults to pretty output.
- --pretty: Emit pretty-printed XML
- --compact: Emit compact XML
- --indent: Pretty indent style, either tab or an integer number of spaces (default: 2)
Only one of --pretty or --compact may be used.
- --sort-elements: Sort child elements recursively by tag name
- --default-namespace: Set or switch the default namespace on the root element
- --clear-default-namespace: Remove the default namespace from the root element
- --remove-namespace: Remove a namespace by prefix or URI (repeatable)
- --named-entities-to-numeric: Convert HTML named entities to numeric entities
- --remove-doctype: Remove the DOCTYPE directive
- --set-doctype: Set the DOCTYPE directive (with or without leading DOCTYPE)
- --remove-declaration: Remove the XML declaration
- --set-declaration: Set XML declaration content, for example version="1.0" encoding="UTF-8"
Only one of --remove-doctype or --set-doctype may be used. Only one of --remove-declaration or --set-declaration may be used.
- --trailing-newline: Emit a trailing newline
- --eol: End-of-line style, one of lf, cr, or crlf (default: lf)
- --help, -h: Show help message
- --version: Print version information
Pretty print a file with 4-space indentation:
xmlfmt --pretty --indent 4 input.xml
Compact output from stdin:
cat input.xml | xmlfmt --compact
Sort elements and change the default namespace:
xmlfmt --sort-elements --default-namespace urn:example input.xml
Remove one namespace prefix and convert named entities:
xmlfmt --remove-namespace a --named-entities-to-numeric input.xml
Remove declaration and doctype:
xmlfmt --remove-declaration --remove-doctype input.xml