4

I have the following XML:

<?xml version="1.0"?>
<products>
    <product at1="a"
             at2="b"
             at3="c">
    </product>
</products>

and the following XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="@*|node()">
            <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

In theory, the xslt should leave the input xml unchanged. However, the output I get after processing is:

<?xml version="1.0"?>
<products>
    <product at1="a" at2="b" at3="c">
    </product>
</products>

Is there I way I can prevent the transformer from reformatting the spacing between the attributes. I understand that both the input and output xml are functionally equivalent but I would like to preserve the attribute per line format for human-readability purposes. If it matters, I'm using ubuntu's xsltproc to do this transformation:

xsltproc -o test2.xml test.xslt test.xml
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76
  • Very similar: http://stackoverflow.com/questions/4617993/how-to-preserve-whitespace-within-an-elements-opening-tag-in-xslt – mzjn Jan 13 '12 at 16:41
  • http://stackoverflow.com/questions/1265255/putting-each-attribute-on-a-new-line-during-xml-serialization – Mads Hansen Jan 13 '12 at 16:51

1 Answers1

5

No, not with standard XML/XSLT tools.

That information is not part of the XML infoset and will be lost when the XML is read by the XML parser. Consequently, can't be preserved in the output.

You will need to modify the output with something else to apply that sort of formatting.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Before I go and write my own xml parser, can you recommend any tools or libraries that might be able to read xml while preserving all whitespace? – Alex Spurling Jan 13 '12 at 18:14
  • 1
    @AlexSpurling I also have a similar issue to yours. However, why not only executing a script (e.g. python) to format the indentation as you want to after the first pass with XSLT? It might be way faster than rewrite the whole XML Parser :) Hope to help! – ForceMagic Apr 11 '12 at 22:22