Adding line breaks between attributes
Asked Answered
M

1

9

I have an XML document which after sending it through my XSLT no longer has line breaks before the XML attributes. So for example

<myoutertag one="a"
            two="b"
            three="c">
    <myinnertag four="d"
                five="e"/>
</myoutertag>

would become

<myoutertag one="a" two="b" three="c">
    <myinnertag four="d" five="e"/>
</myoutertag>

This is of course perfectly valid XML but it's more difficult to read, especially if there are many long attribute values. From what I've read, XSLT is not able to preserve these line breaks as the XSLT processor is not passed such unimportant information.

So, what I'm looking for now is a command line based pretty printer (usable in Linux) which ideally would only change the document in that it adds line breaks between the attributes. Whether it adds one before the first attribute or not is pretty much irrelevant to me, just as long as it's more easily readable.

What I've tried unsuccessfully so far:

I'm using the input file

<?xml version="1.0" encoding="UTF-8"?>

<myoutertag one="a" two="b" three="c">
    <myinnertag four="d" five="e"/>
</myoutertag>

xmllint --format

I tried both xmllint --format test.xml and cat test.xml | xmllint --format - with the same result:

<?xml version="1.0" encoding="UTF-8"?>
<myoutertag one="a" two="b" three="c">
  <myinnertag four="d" five="e"/>
</myoutertag>

So, the changes are:

  • the line break after the xml declination is gone
  • the indentation of <myinnertag> was reduced from four spaces to two spaces

I want neither of those changes. This is using libxml version 20706.

xml_pp -s

I tried the styles none, nsgmls, nice, indented, record and record_c. The only one that comes close is nsgmls which will add line breaks, but the result looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<myoutertag
one="a"
two="b"
three="c"
><myinnertag
four="d"
five="e"
/></myoutertag>

So, no indentation and weird line breaking.

xmlstarlet

The output of xmlstarter fo test.xml is the same as with xmllint. I also tried finding something like xmlstarter -ed -P --insert "//@*" -t text -n "" -v "\\n" test.xml but that resulted in a glibc pointer error. Not surprising I guess, as I'm trying to add text in between attributes.

tidy

This is the closest I've gotten so far. Running the command tidy -quiet -xml -indent -wrap 1 test.xml gives me:

<?xml version="1.0"
encoding="UTF-8"?>
<myoutertag one="a"
two="b"
three="c">

  <myinnertag four="d"
  five="e"/>
</myoutertag>

So, if I could get it to indent some more before those attributes in new lines that would basically solve my problem (I think).

Any further suggestions?

Marcellus answered 22/8, 2014 at 12:25 Comment(11)
Does the top answer to this StackOverflow question meet your requirements? #16091369Hairstyle
@Hairstyle No, xmllint doesn't add those line breaks, I tried that before I asked here.Marcellus
How about this? pauldeden.com/2009/01/pretty-printing-xml-in-ubuntu-on.htmlHairstyle
@Hairstyle As far as I can see, xml_pp only offers predefined styles, none of which would change only this one thing. Or am I missing something?Marcellus
I agree with the initial comment from @DWRoelands. I ran cat file.xml | xmllint --format - and obtained exactly what you want.Flabby
@Flabby That's odd, I didn't... I'll try again later, not at the computer right now. I believe it changed other things though, such as removing empty lines, which I don't want.Marcellus
@Emilen I checked again and it doesn't work for me. Check the edited question for more information.Marcellus
AFAIK, whitespace between the attributes isn't part of the XML data model in any commonly used representation. There's nothing in the XML spec that requires it to be passed through to the application.Krypton
@Krypton Agreed. That is why I'm looking for a tool that will add them again after the document has been parsed by my XSLT processor. They will probably not be in exactly the right places but adding a line break for every new attribute should be close enough.Marcellus
I forgot to finish my thought, which was that for the reasons outlined above you're unlikely to find a tool that already does it. I think your best bet is to write a SAX or StAX outputter (or rather, to take one that exists and override the attribute writing method).Krypton
@Krypton That would be a solution, yes. I have however just found a solution using an existing tool.Marcellus
M
10

OK, I've found a solution. The tool I used is called HTML Tidy (well, actually I used jTidy, a port of HTML Tidy to Java which therefore is portable). The tool offers many options for configuration; the one I was looking for is called indent-attributes: true. In fact, my whole configuration file is:

add-xml-decl: true
drop-empty-paras: false
fix-backslash: false
fix-bad-comments: false
fix-uri: false
input-xml: true
join-styles: false
literal-attributes: true
lower-literals: false
output-xml: true
preserve-entities: true
quote-ampersand: false
quote-marks: false
quote-nbsp: false

indent: auto
indent-attributes: true
indent-spaces: 4
tab-size: 4
vertical-space: true
wrap: 150

char-encoding: utf8
input-encoding: utf8
newline: CRLF
output-encoding: utf8

quiet: true

The meanings of those options are explained in the Tidy manual (or the man page if you install it on a Linux system), I mostly cared about that middle block where I can set the indentation settings.

I can now call the tool using the command java -jar jtidy-r938.jar -config tidy.config test.xml and the output will be

<?xml
  version="1.0"
  encoding="UTF-8"?>
<myoutertag
 one="a"
 two="b"
 three="c">
    <myinnertag
     four="d"
     five="e" />
</myoutertag>

Now I'm happy. :-)

Marcellus answered 26/8, 2014 at 9:33 Comment(1)
I compared jTidy (latest git version) and tidy, and found that the later has more options by now. most importantly (for my needs), it has the --sort-attributes option.Stauffer

© 2022 - 2024 — McMap. All rights reserved.