Reputation: 909
I was looking for some XSLT stylesheet that can handle correct indenting of my xml document and found really nice one at http://www.printk.net/~bds/indent.html.
Hope author won't blame me for such quotation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>
<xsl:param name="indent-increment" select="' '"/>
<xsl:template name="newline">
<xsl:text disable-output-escaping="yes">
</xsl:text>
</xsl:template>
<xsl:template match="comment() | processing-instruction()">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:copy />
</xsl:template>
<xsl:template match="text()">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="text()[normalize-space(.)='']"/>
<xsl:template match="*">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:choose>
<xsl:when test="count(child::*) > 0">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="*|text()">
<xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
</xsl:apply-templates>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
It does almost all I wanted except one nasty thing: it makes strange 8 spaces indent of the root element of my document (but not XML declaration). Result looks like that piece of markup:
<?xml version="1.0" encoding="UTF-8"?>
<database>
<books>
<book id="0">
<ISBN value="0123456789"/>
<title>Some book title Language</title>
<hardcover value="false"/>
<price value="40.46"/>
<in_stock value="100"/>
<annotation>Some annotation</annotation>
</book>
</books>
</database>`
I'm rather new to XSLT technology and spent quite a lot of time trying to understand how to fix it, but so far not succeeded. I'm using standard javax.xml.transform.Transformer class to do XSLT transormation. Do you have any ideas why this happens?
Upvotes: 3
Views: 3420
Reputation: 243459
You don't need any special XSLT code to do indenting.
Use just the standard identity template, with <xsl:output indent="yes"/>
:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on any XML document (such as this):
<database> <books>
<book id="0">
<ISBN value="0123456789"/>
<title>Some book title Language</title>
<hardcover value="false"/>
<price value="40.46"/>
<in_stock value="100"/>
<annotation>Some annotation</annotation>
</book></books> </database>
it outputs the same XML document but indented:
<database>
<books>
<book id="0">
<ISBN value="0123456789"/>
<title>Some book title Language</title>
<hardcover value="false"/>
<price value="40.46"/>
<in_stock value="100"/>
<annotation>Some annotation</annotation>
</book>
</books>
</database>
Upvotes: 6