This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: indent XML doc based on spaces?


You need to add a text node with a line feed at the end of each element
and .

This can be done modifying your template like this:

<xsl:template match="*">
   <xsl:variable name="varspaces"
select="substring($spaces,1,count(ancestor::node()) *$indent)"/>
   <xsl:value-of select="$varspaces" />
   <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
   <xsl:value-of select="concat('&#x0A;', $varspaces)" />
   </xsl:copy>
</xsl:template>

Doing so, you are still sensitive to spaces and line feeds available in
your source document which can damage your pretty looking...

You may also want to display CDATA elements within a single line.

The following template does it quite well:

<xsl:template match="*">
   <xsl:variable name="varspaces"
select="substring($spaces,1,count(ancestor::node()) *$indent)"/>
   <xsl:value-of select="$varspaces" />
   <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:choose>
	<xsl:when test="*">
          <xsl:apply-templates />
   	  <xsl:value-of select="concat('&#x0A;   ', $varspaces)" />
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="normalize-space()"/>
	</xsl:otherwise>
      </xsl:choose>
   </xsl:copy>
</xsl:template>

Hope this helps.

Eric

Joshua Allen wrote:
> 
> 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

-- 
------------------------------------------------------------------------
Eric van der Vlist       Dyomedea                    http://dyomedea.com
http://xmlfr.org         http://4xt.org              http://ducotede.com
------------------------------------------------------------------------


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]