How do you format an XML document to make it easy to read element attributes?
I have a xml based webservice that returns one or two elements, but with hundreds of attributes. As I'm doing development, I sometimes need to debug this service, but it can be hard since the output is just one blob.
Consider this:
$ echo '<root><foo z="26" y="25" x="24" a="1" b="2" c="3" d="something more"/></root>' | xmllint --format -
<?xml version="1.0"?>
<root>
<foo z="26" y="25" x="24" a="1" b="2" c="3" d="something more"/>
</root>
I have found that tr works pretty good, but not ideal:
$ echo '<root><foo z="26" y="25" x="24" a="1" b="2" c="3" d="something more"/></root>' | xmllint --format - | tr ' ' \\\n
<?xml
version="1.0"?>
<root>
<foo
z="26"
y="25"
x="24"
a="1"
b="2"
c="3"
d="something
more"/>
</root>
Ideally the output would be something in between the xmllint and a funky hack
<?xml version="1.0"?>
<root>
<foo
z="26"
y="25"
x="24"
a="1"
b="2"
c="3"
d="something more"/>
</root>
That way i can grep for things, or sort or whatever.
sed
/grep
/awk
. Alternatively, usexmlstarlet
instead ofxmllint
.formattedCurl.sh http://foo/service | grep prop | sort
. You're right I can use xmllint's xpath functionality, but the trick is the XML data from the service is not consistent. I triedxmlstartlet format
, but it appears to work exactly likexmllint -format
. Perhaps I should look into rendering the document with xslt, or perhaps a carefully craftedsed
orawk
statement.