4

This is a follow-up question to How to pretty print XML from the command line?.

Is there any tool in libxml2 that will allow me to align the attributes of each node as well? I have a large XML document whose logical structure I cannot change, but I would like to turn

<a attr="one" bttr="two" tttr="three" fttr="four"/>

into

<a attr   = "one"
   bttr   = "two"
   tttr   = "three"
   fttr   = "four"
   longer = "attribute" />
Community
  • 1
  • 1
Sean Allred
  • 3,358
  • 2
  • 32
  • 67

1 Answers1

3

xmllint has an option --pretty which supports three levels of prettyness. If this output:

<?xml version="1.0"?>
<a
    attr="one"
    bttr="two"
    tttr="three"
    fttr="four"
/>

is ok for you, then use --pretty 2 :

xmllint --pretty 2 - <<< '<a attr="one" bttr="two" tttr="three" fttr="four"/>'
hek2mgl
  • 143,113
  • 25
  • 227
  • 253
  • My `xmllint` has no such option... what version do you have? I'm using `libxml version 20706` – Sean Allred Sep 17 '14 at 18:25
  • I'm using `xmllint: using libxml version 20901 ` – hek2mgl Sep 17 '14 at 18:26
  • `:(` Therein lies the problem, I suppose. My copy was last packaged `2013-01-30 14:59`... sigh. – Sean Allred Sep 17 '14 at 18:33
  • Several years later, and while this is the best answer I've found, it's still pretty broken. While it does pretty well with the attributes, it completely uglifies the rest of the elements: `xmllint --pretty 2 - <<< 'something'` is horrible. – rbellamy Feb 27 '16 at 02:13
  • @rbellamy I see. Looks weird! :) I guess the best thing you can do in that case is writing something on your own.. (or modify existing prettifiers) – hek2mgl Feb 27 '16 at 10:30