[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: indent XML doc based on spaces?

Subject: Re: indent XML doc based on spaces?
From: Eric van der Vlist <vdv@xxxxxxxxxxxx>
Date: Thu, 05 Oct 2000 09:18:34 +0200
xsl copy of indent
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.