indent XML doc based on spaces?

Subject: indent XML doc based on spaces?
From: Joshua Allen <joshuaa@xxxxxxxxxxxxx>
Date: Wed, 4 Oct 2000 22:55:17 -0700
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:

Problem is, this indents only opening tags, not closing tags, and doesn't
remove any indents that might already be in the source document.

P.S.  The reason I'm doing this is so I can include a file (pretty.xslt)
with my XSLT test tool and allow pretty (indented but not different infoset)
formatting of the text.  I could write some code to do it, but if it can be
done as an external XSLT file it makes things easier to customize.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" />
<xsl:variable name="indent" select="3" />
<xsl:variable name="spaces" select="'                 '" />

<xsl:template match="*">
   <xsl:value-of select="substring($spaces,1,count(ancestor::node()) *
$indent)" />
   <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
   </xsl:copy>
</xsl:template>

<xsl:template match="comment()|processing-instruction()">
   <xsl:copy />
</xsl:template>

</xsl:stylesheet>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread