Friday 25 November 2016

unix - How to pretty print XML from the command line?




Related: How can I pretty-print JSON in (unix) shell script?



Is there a (unix) shell script to format XML in human-readable form?



Basically, I want it to transform the following:



lorem



... into something like this:




lorem



Answer



libxml2-utils




This utility comes with libxml2-utils:



echo 'lorem' |
xmllint --format -


Perl's XML::Twig



This command comes with XML::Twig module, sometimes xml-twig-tools package:




echo 'lorem' |
xml_pp


xmlstarlet



This command comes with xmlstarlet:



echo 'lorem' |
xmlstarlet format --indent-tab



tidy



Check the tidy package:



echo 'lorem' |
tidy -xml -i -



Python



Python's xml.dom.minidom can format XML (both python2 and python3):



echo 'lorem' |
python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print(xml.dom.minidom.parseString(s).toprettyxml())'


saxon-lint




You need saxon-lint:



echo 'lorem' |
saxon-lint --indent --xpath '/' -


saxon-HE



You need saxon-HE:




 echo 'lorem' |
java -cp /usr/share/java/saxon/saxon9he.jar net.sf.saxon.Query \
-s:- -qs:/ '!indent=yes'

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...