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?
file.xml | xmllint --format -
and obtained exactly what you want. – Flabby