This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: indent XML doc based on spaces?
Joshua,
> Has anyone built an XSLT transform that indents an XML file based on spaces
> (e.g. 3 spaces per level)? I'm just confusing myself here. The closest I
> have come is modifying the identity transform to be as included below:
Try the following code. It differs only slightly from yours, yet indents both
starting and closing tags and has no limits on element nesting depth.
Regards,
Nikolai
===========================
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:param name="indent-increment" select="' '" />
<xsl:template match="*">
<xsl:param name="indent" select="'
'"/>
<xsl:value-of select="$indent"/>
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates>
<xsl:with-param name="indent"
select="concat($indent, $indent-increment)"/>
</xsl:apply-templates>
<xsl:value-of select="$indent"/>
</xsl:copy>
</xsl:template>
<xsl:template match="comment()|processing-instruction()">
<xsl:copy />
</xsl:template>
<!-- WARNING: this is dangerous. Handle with care -->
<xsl:template match="text()[normalize-space(.)='']"/>
</xsl:stylesheet>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list