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

Re: IDENT

Subject: Re: IDENT
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 5 Feb 2001 16:13:49 +0000
xsl ident
Hi Maribel,

> I'm transforming a HTML file to a text file with xsl,  and I want to
> obtain a plain text, without lines feeds.  How I can obtain it?

The short answer: wrap xsl:text elements around the actual text that
you want outputted.

The long answer: have a look at this template:

>   <xsl:template match="B" mode="special">
>     &lt;B&gt;
>     <xsl:value-of select="."/>
>     &lt;/B&gt;<xsl:text> </xsl:text>
>   </xsl:template>

When a DOM builder goes over that bit of XML, it creates a node tree
that looks like:

+- (element) xsl:template
   +- (text) [NL][SP][SP]<B>[NL][SP][SP]
   +- (element) xsl:value-of
   +- (text) [NL][SP][SP]</B>
   +- (element) xsl:text
   |  +- (text) [SP]
   +- (text) [NL]

All those new lines and spaces are because of the indenting in the
stylesheet. When the XSLT processor gets hold of this node tree, it
first strips out all white-space only text nodes that are in the tree,
unless they appear within a xsl:text element. So it gets rid of the
last text node to give:

+- (element) xsl:template
   +- (text) [NL][SP][SP]<B>[NL][SP][SP]
   +- (element) xsl:value-of
   +- (text) [NL][SP][SP]</B>
   +- (element) xsl:text
      +- (text) [SP]

Now, when the XSLT processor applies this template, any plain text
nodes that are found are copied directly to the result tree.  The
xsl:value-of adds the relevant value (e.g. 'x') and the xsl:text adds
whatever its content is.  You end up with a result tree that looks
like:

+- (text) [NL][SP][SP]<B>[NL][SP][SP]x[NL][SP][SP]</B>[SP]

which is why you get all the whitesapce that you do in your output -
it's copying over the whitespace that you've used to indent stuff in
your stylesheet.

The way around this is to cut out the whitespace that you don't want
using xsl:text elements to limit what whitespace is interpreted as
whitespace for inclusion in the result tree, and what is just used to
indent your stylesheet code.

If you use the template:

<xsl:template match="B" mode="special">
   <xsl:text> &lt;B&gt; </xsl:text>
   <xsl:value-of select="." />
   <xsl:text> &lt;/B&gt;</xsl:text>
</xsl:template>

Then the node tree that the DOM builder builds looks like:

+- (element) xsl:template
   +- (text) [NL][SP][SP]
   +- (element) xsl:text
   |  +- (text) [SP]<B>[SP]
   +- (text) [NL][SP][SP]
   +- (element) xsl:value-of
   +- (text) [NL][SP][SP]
   +- (element) xsl:text
   |  +- (text) [SP]</B>
   +- (text) [NL]

This time, a lot of whitespace disappears when the whitespace-only
nodes are cut out by the XSLT processor:

+- (element) xsl:template
   +- (element) xsl:text
   |  +- (text) [SP]<B>[SP]
   +- (element) xsl:value-of
   +- (element) xsl:text
      +- (text) [SP]</B>

And the output is simply:

+- (text) [SP]<B>[SP]x[SP]</B>

which I think is what you're after.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread
  • IDENT
    • Maribel - Mon, 5 Feb 2001 10:42:40 -0500 (EST)
      • Jeni Tennison - Mon, 5 Feb 2001 11:15:53 -0500 (EST) <=
      • Michael Kay - Mon, 5 Feb 2001 12:49:46 -0500 (EST)

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.