Controlling whitespaces exactly is really hard in XSLT. Mostly it's recommended to not use indent="yes" but to add an extra XSLT giving you more control about it.

The theory (this makes me think about when I wrote my last XSLT ...): By default whitespaces from the input XML are preserved, whitespaces in the XSLT are stripped - that's why you probably see different behavior for these both XML structures. You can change the XSLT behavior by setting xsl:strip-space [1].

When you do <xsl:apply-templates/> in the XSLT you tell the XSLT processor to process all child nodes of the currently processed node (same as <xsl:apply-templates select="node()"/>) - including the whitespace-only text nodes. By default (or more exact a so called built-in template) text nodes are copied to the output. If you change it to <xsl:apply-templates select="*"/> only the elements are processed, text nodes will be ignored and not copied to the output. That's what I'm usually doing.

Now what indent="yes" exactly does is nearly unpredictable and XSLT processor specific. The XSLT spec says about it [2]:

If the indent attribute has the value yes, then the xml output method *may* output whitespace in addition to the whitespace in the result tree (possibly based on whitespace stripped from either the source document or the stylesheet) in order to indent the result nicely;

You see this is very unspecific. That's why mostly all whitespaces are stripped and a pretty-formatting XSLT is added at the end of a pipeline.

Hope this helps,

Joerg

[1] http://www.w3.org/TR/xslt#strip
[2] http://www.w3.org/TR/xslt#section-XML-Output-Method

On 16.08.2007 0:04 Uhr, Lincoln Mitchell wrote:
I have only added this additional HTML to my XSL file:
...
        <p>my para</p>
        <div>
                <div>
                <div></div>
            </div>
        </div>
...

The result is:
...
<p>my para</p>
<div>
<div>
<div />
</div>
</div>
...
Which has no indents. The only indents I get are the indents on the HTML
tags that where generated via a template and have content from the XML file.


Here is my XML and XSL:
XML:
...
<?xml version="1.0" encoding="UTF-8"?>
<page>
 <title>Hello</title>
 <content>
  <para>This is my first Cocoon page!</para>
 </content>
</page>
...

XSL:
...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xhtml" indent="yes" encoding="UTF-8"/>
  <xsl:template match="/">
   <html>
    <head>
     <title>
      <xsl:value-of select="page/title"/>
     </title>
     <link href="/styles/main.css" type="text/css" rel="stylesheet"/>
    </head>
    <body><xsl:apply-templates/>
        <p>my para</p>
        <div>
                <div>
                <div></div>
            </div>
        </div>
    </body>
    </html>
  </xsl:template>
  <xsl:template match="title"><h1><xsl:apply-templates/></h1></xsl:template>
  <xsl:template match="content"><xsl:apply-templates
select="para"/></xsl:template>
  <xsl:template match="para"><p><xsl:apply-templates/></p></xsl:template>
</xsl:transform>
...

-----Original Message-----
From: Joerg Heinicke [mailto:[EMAIL PROTECTED]
Sent: Thursday, 16 August 2007 11:31 AM
To: [email protected]
Subject: Re: output indent yes?

On 15.08.2007 22:37 Uhr, Lincoln Mitchell wrote:

That's works for content from the hello.XML file but doesn't indent my
html
in the xsl file. Ie:
Sorry, Lincoln. I don't exactly understand what works and what not. What
do you mean with "content from the hello.XML" and "html in the xsl
file"? What have you changed?

Joerg

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to