XSLT: how can proper XML indentation levels be preserved?

XSLT: how can proper XML indentation levels be preserved?

Post by Neil Zanel » Tue, 08 Jul 2003 12:40:27



Hello,

I would like to know whether it is possible to get an XSLT processor to output
tags with the proper indentation. For instance, given the following standard
example I get the output given below:

$ cat hello.xml
<?xml version="1.0"?>
<greeting>
  Hello, World!
</greeting>

$ cat hello.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" />
  <xsl:template match="/">
    <xsl:apply-templates select="greeting" />
  </xsl:template>
  <xsl:template match="greeting">
    <html>
      <body>
        <h1>
          <xsl:value-of select="." />
        </h1>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

$ xsltproc hello.xsl hello.xml
<html><body><h1>
  Hello, World!
</h1></body></html>

... but this is my first time running an XSLT processor and I was expecting:

<html>
  <body>
    <h1>
      Hello, World!
    </h1>
  </body>
</html>

Is there anything I can do about this to fix it?

Thanks,

Neil

 
 
 

XSLT: how can proper XML indentation levels be preserved?

Post by Dimitre Novatche » Tue, 08 Jul 2003 13:32:30


In case you want to preserve indentation, add the xml:space="preserve" to
the top element of your stylesheet (the xsl:stylesheet element).

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


Quote:> Hello,

> I would like to know whether it is possible to get an XSLT processor to
output
> tags with the proper indentation. For instance, given the following
standard
> example I get the output given below:

> $ cat hello.xml
> <?xml version="1.0"?>
> <greeting>
>   Hello, World!
> </greeting>

> $ cat hello.xsl
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
>   <xsl:output method="html" />
>   <xsl:template match="/">
>     <xsl:apply-templates select="greeting" />
>   </xsl:template>
>   <xsl:template match="greeting">
>     <html>
>       <body>
>         <h1>
>           <xsl:value-of select="." />
>         </h1>
>       </body>
>     </html>
>   </xsl:template>
> </xsl:stylesheet>

> $ xsltproc hello.xsl hello.xml
> <html><body><h1>
>   Hello, World!
> </h1></body></html>

> ... but this is my first time running an XSLT processor and I was
expecting:

> <html>
>   <body>
>     <h1>
>       Hello, World!
>     </h1>
>   </body>
> </html>

> Is there anything I can do about this to fix it?

> Thanks,

> Neil


 
 
 

XSLT: how can proper XML indentation levels be preserved?

Post by Neil Zanel » Wed, 09 Jul 2003 03:25:31


Not quite. With your suggestion I end up with the following output.
This not only has the wrong spacing but there are also lots of redundant
blank lines which i do not want in the output. Any ideas on how to fix the
given style sheet so that the output looks exactly in the original posting
but without modifying the original input XML file?

Thanks,

Neil

--- begin output --------------------

    <html>
      <body>
        <h1>

  Hello, World!

        </h1>
      </body>
    </html>

--- end output ----------------------


> In case you want to preserve indentation, add the xml:space="preserve" to
> the top element of your stylesheet (the xsl:stylesheet element).

> =====
> Cheers,

> Dimitre Novatchev.
> http://fxsl.sourceforge.net/ -- the home of FXSL



> > Hello,

> > I would like to know whether it is possible to get an XSLT processor to
>  output
> > tags with the proper indentation. For instance, given the following
>  standard
> > example I get the output given below:

> > $ cat hello.xml
> > <?xml version="1.0"?>
> > <greeting>
> >   Hello, World!
> > </greeting>

> > $ cat hello.xsl
> > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>  version="1.0">
> >   <xsl:output method="html" />
> >   <xsl:template match="/">
> >     <xsl:apply-templates select="greeting" />
> >   </xsl:template>
> >   <xsl:template match="greeting">
> >     <html>
> >       <body>
> >         <h1>
> >           <xsl:value-of select="." />
> >         </h1>
> >       </body>
> >     </html>
> >   </xsl:template>
> > </xsl:stylesheet>

> > $ xsltproc hello.xsl hello.xml
> > <html><body><h1>
> >   Hello, World!
> > </h1></body></html>

> > ... but this is my first time running an XSLT processor and I was
>  expecting:

> > <html>
> >   <body>
> >     <h1>
> >       Hello, World!
> >     </h1>
> >   </body>
> > </html>

> > Is there anything I can do about this to fix it?

> > Thanks,

> > Neil

 
 
 

XSLT: how can proper XML indentation levels be preserved?

Post by Dimitre Novatche » Wed, 09 Jul 2003 04:01:16


I am really sorry I cannot help. There is no such problem using other XSLT
processors -- e.g. MSXML or Saxon.

With my version of xsltproc, if I use:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output method="html" />
  <xsl:template match="/">
    <xsl:apply-templates select="greeting" />
  </xsl:template>
  <xsl:template match="greeting">
    <html xml:space="preserve">
      <body>
        <h1>
          <xsl:value-of select="." />
        </h1>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

the result is:

<html space="preserve">
      <body>
        <h1>

  Hello, World!

        </h1>
      </body>
    </html>

As we see, xsltProc "loses" a namespace...

With MSXML4  the result is correct:

<html xml:space="preserve">
      <body>
        <h1>

  Hello, World!

        </h1>
      </body>
    </html>

Now, if you wonder why the text node child of "h1" is on three lines, this
is exactly what you have specified. Remember the source xml document:

<greeting>
  Hello, World!
</greeting>

The text node contains exactly two linefeeds.

This instruction in your template, which matches "greeting":

          <xsl:value-of select="." />

copies the string value of the current node "greetings", which in this case
id its only text node child.

If you do not wish the linefeeds, then use:

          <xsl:value-of select="translate(., '
', '')" />

Then the result will be:

<html xml:space="preserve">
      <body>
        <h1>
            Hello, World!
        </h1>
      </body>
    </html>

If you still do not like this result, you may sacrifice the readability of
your XSLT code by not indenting properly the literal result elements:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output method="html" />
  <xsl:template match="/">
    <xsl:apply-templates select="greeting" />
  </xsl:template>
  <xsl:template match="greeting">
<html xml:space="preserve">
  <body>
    <h1>
      <xsl:value-of select="translate(., '
', '')" />
    </h1>
  </body>
</html>
  </xsl:template>
</xsl:stylesheet>

Now you get:

<html xml:space="preserve">
  <body>
    <h1>
        Hello, World!
    </h1>
  </body>
</html>

Certainly, this exercise is quite useless:

   1. No browser needs pretty-printed html source.

   2. You can view the html result (as long as it is well-formed xml) with a
tool like IE and it will be displayed in a nice, prettyprinted form.

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


> Not quite. With your suggestion I end up with the following output.
> This not only has the wrong spacing but there are also lots of redundant
> blank lines which i do not want in the output. Any ideas on how to fix the
> given style sheet so that the output looks exactly in the original posting
> but without modifying the original input XML file?

> Thanks,

> Neil

> --- begin output --------------------

>     <html>
>       <body>
>         <h1>

>   Hello, World!

>         </h1>
>       </body>
>     </html>

> --- end output ----------------------




- Show quoted text -

> > In case you want to preserve indentation, add the xml:space="preserve"
to
> > the top element of your stylesheet (the xsl:stylesheet element).

> > =====
> > Cheers,

> > Dimitre Novatchev.
> > http://fxsl.sourceforge.net/ -- the home of FXSL



> > > Hello,

> > > I would like to know whether it is possible to get an XSLT processor
to
> >  output
> > > tags with the proper indentation. For instance, given the following
> >  standard
> > > example I get the output given below:

> > > $ cat hello.xml
> > > <?xml version="1.0"?>
> > > <greeting>
> > >   Hello, World!
> > > </greeting>

> > > $ cat hello.xsl
> > > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> >  version="1.0">
> > >   <xsl:output method="html" />
> > >   <xsl:template match="/">
> > >     <xsl:apply-templates select="greeting" />
> > >   </xsl:template>
> > >   <xsl:template match="greeting">
> > >     <html>
> > >       <body>
> > >         <h1>
> > >           <xsl:value-of select="." />
> > >         </h1>
> > >       </body>
> > >     </html>
> > >   </xsl:template>
> > > </xsl:stylesheet>

> > > $ xsltproc hello.xsl hello.xml
> > > <html><body><h1>
> > >   Hello, World!
> > > </h1></body></html>

> > > ... but this is my first time running an XSLT processor and I was
> >  expecting:

> > > <html>
> > >   <body>
> > >     <h1>
> > >       Hello, World!
> > >     </h1>
> > >   </body>
> > > </html>

> > > Is there anything I can do about this to fix it?

> > > Thanks,

> > > Neil

 
 
 

1. what is the proper syntax to include a html file in a xslt script?

Hello.

I currently have a xslt file as follows:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" indent="no"/>

<xsl:template match="/">

<!-- #include virtual ="/html/blank.htm" -->

<table style="font-size: 8pt" width="100%" border="5" cellpadding="2"
 cellspacing="0">
 <tr style="background-color:#000000;color:#FFFFFF">
  <th>Name</th>
  <th>Phone</th>
</tr>
<xsl:apply-templates select="//call"/>
</table>
</xsl:template>
<xsl:template match="call">
<tr>
<td>
<xsl:value-of select="cal_name"/>
</td>
<td>
<xsl:value-of select="cal_mph"/>
</td>
</tr>

</xsl:template>
</xsl:stylesheet>

I would like to include a html file into this code so that it would be the
background upon which this table is displayed.
I tried the aforementioned include and I do not get an error. It is just
ignored.
Anyone have any ideas where I went wrong? Should I place this table on a
html page and then call it?

If this is not the correct place to place this include could you please show
me where you would typically place this type of include?

Thank you in advance.

Chuck

2. Windows media player 7

3. Using DOM Level 2, but servlet engine finds DOM Level 1

4. Matrix routines

5. High level SOAPClient vs low level SOAPSerializer

6. \makeindex interferes with \pagestyle{myheadings}

7. Preserving space problem XML-XSLT-XML

8. SOAP Client No Longer works in 2003

9. XSLT: preserving white-space

10. preserving line breaks and HTML tags in XSLT

11. preserve whitespace from xsl during xslt transformation

12. Q XSLT: How to preserve elements?

13. Preserving XHTML entities through an XSLT transformation