Posts Tagged ‘XSLT’

XML indentation in XSLT

August 23, 2008 1 comment

This my first XSLT script. I usually use DOM (either Java or Python API) for such things but I wanted to give XSLT a try.

<?xml version="1.0" encoding="ISO-8859-15"?>

<xsl:stylesheet xmlns:xsl="" version="1.0">
  <xsl:output method="xml"/>

  <xsl:template match="@*">

  <xsl:template match="text()">
    <xsl:value-of select="normalize-space(.)" />

  <xsl:template match="*">
    <xsl:param name="indent" select="''"/>
    <xsl:value-of select="$indent" />
      <xsl:apply-templates select="@*|*|text()">
        <xsl:with-param name="indent" select="concat($indent, '  ')"/>
    <xsl:if test="count(../*)>0 and ../*[last()]=.">
      <xsl:value-of select="substring($indent,3)" />

Tags: ,