Pretty Printing XML Attributes


April 01, 2006

Bill poses an interesting question on his blog.

Why is element-based XML easier for humans to read than attribute-based XML?

IE:

<person>
    <firstName>Bill</firstName>
    <lastName>Higgins</lastName>
    <emailAddress>bhiggins@us.ibm.com</emailAddress>
    <city>Durham</city>
    <state>NC</state>
</person>

is easier to read than:

<person firstName="Bill" lastName="Higgins" emailAddress="bhiggins@us.ibm.com" city="Durham" state="NC"/>

I think the answer is that we never learned good formating conventions for long lists of XML attributes. In such cases, most people are content to let the attributes run horizontally off the side of the page (sucks).

If we did the same thing with element-based XML, it would be hard to read as well:

<person><firstName>Bill</firstName><lastName>Higgins</lastName><emailAddress>bhiggins@us.ibm.com</emailAddress><city>Durham</city><state>NC</state></person>

I use the following convention for long attribute lists, which I've kind of borrowed from Lisp (all roads lead to Lisp):

<person firstName="Josh"
        lastName="Staiger"
        emailAddress="joshstaiger@gmail.com"
        city="Durham"
        state="NC" />

Much easier on the eyes, huh?

My algorithm goes something like this:

Write the first attribute on the same line as the tag name. Each consecutive attribute gets its own line and is indented to the level of the first attribute. Write the closing angle bracket on the same line as the last attribute.

I even wrote a SAX-based Python script which slurps XML and pretty prints it in this manner. I can dig it up and post if anyone is interested.