Normalizes xml files. Options include sorting siblings, remove nodes, whitespace normalization and pretty print.
npm install xml_normalize


npm i -g xml_normalize or run directly with npx xml_normalize.
text
Usage: npx xml_normalize [options]
Options:
-i, --input-file input file
-o, --output-file output file - if not provided result is printed to stdout
-r, --remove-path simple XPath(s) to remove elements - e.g. "/html/head[1]/script"
-s, --sort-path simple XPath that references an attribute to sort - e.g. "/html/head[1]/script/@src"
--no-pretty Disable pretty format output
--no-trim Disable trimming of whitespace at the beginning and end of text nodes (trims only pure text nodes)
--no-attribute-trim Disable trimming whitespace at the beginning and end of attribute values
-tf, --trim-force Trim the whitespace at the beginning and end of text nodes (trims as well text adjacent to nested nodes)
-n, --normalize-whitespace Normalize whitespaces inside text nodes and attribute values
-d, --debug enable debug output
-h, --help display help for command
`
Options and Examples
$3
Allows to sort siblings at a specific path
with the same tag name lexicographically
based on a specific attribute value.
Example:
`xml
should be last
should be first
should be last
should be first
`
npx xml_normalize -s /root/node/child/@id will create:
`xml
should be first
should be last
should be first
should be last
`
$3
Allows to remove nodes in a specific path.
Example:
`xml
should be removed
should be removed
should stay
should stay
`
npx xml_normalize -r /root/node[1]/child will create:
`xml
should stay
should stay
`
npx xml_normalize -r /root/node/child instead, will create:
`xml
`
$3
This option replaces any number of consecutive whitespace, tab, new line characters with a single whitespace (in text nodes).
Example:
`xml
some xml
has messed up
formatting
some more mess
`
npx xml_normalize --normalize-whitespace will create:
`xml
some xml has messed up formatting
some more mess
`
$3
Paths are a simple subset of XPaths.
`
/ROOT/NODE_NAME[INDEX]/ANOTHER_NODE
`
Supported:
* Only absolute paths
* Index access (note in XPath indices are 1-based!)
* Simple predicates using the following functions (parameters can be string (double quotes) or XPaths):
* starts-with(str,prefix)
* contains(str,contained)
Node wildcard - e.g /root/ to select all nodes in root of any type.
* Attribute reference in last node - e.g. /root/node/@id`.