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

Re: remove 'invisable' white spaces

Subject: Re: remove 'invisable' white spaces
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 2 Apr 2001 17:59:35 +0100
javascript remove white spaces
Hi Kristof,

> tried several elements and functions with goal to remove invisable
> white spaces, but don't succeeded.

Ahh... your problem is here:

> <script language="Javascript">
>         <xsl:for-each select="Information/Body/Block">
>                 label<xsl:value-of select="position()"/> = new
> Label('<xsl:apply-templates select="."/>')
>         </xsl:for-each>
> </script>

When an XML document (like the XSLT stylesheet) is parsed and made
into a nice tree, all the text nodes that have any non-whitespace
characters in them are kept. As it's an XSLT stylesheet, then all the
whitespace-only text nodes aside from those in xsl:text are ignored
automatically.

So in the above, the node tree looks like (with \n = newline):

+- (element) script
   +- (element) xsl:for-each
      +- (text) "\n                label"
      +- (element) xsl:value-of
      +- (text) " = new Label('"
      +- (element) xsl:apply-templates
      +- (text) "')\n        "

You don't want all that whitespace before 'label' and after the
closing bracket.  To get rid of it, you can enclose the text that you
want to output in xsl:text elements:

   <script language="Javascript">
      <xsl:for-each select="Information/Body/Block">
         <xsl:text>label</xsl:text>
         <xsl:value-of select="position()"/> = new Label('<xsl:apply-templates select="."/>
         <xsl:text>')</xsl:text>
      </xsl:for-each>
   </script>

This makes the tree look like:

+- (element) script
   +- (element) xsl:for-each
      +- (element) xsl:text
      |  +- (text) "label"
      +- (element) xsl:value-of
      +- (text) " = new Label('"
      +- (element) xsl:apply-templates
      +- (element) xsl:text
         +- (text) "')"

Or you could use the Allouche method, which is to place an empty
xsl:text element at the beginning of the significant text -
essentially this turns the whitespace that you're not interested in
into a whitespace-only text node, which is automatically stripped from
the tree:

   <script language="Javascript">
      <xsl:for-each select="Information/Body/Block">
         <xsl:text />label<xsl:value-of select="position()"/> = new Label('<xsl:apply-templates select="."/>')<xsl:text />
      </xsl:for-each>
   </script>

This results in a tree that looks like:

+- (element) script
   +- (element) xsl:for-each
      +- (element) xsl:text
      +- (text) "label"
      +- (element) xsl:value-of
      +- (text) " = new Label('"
      +- (element) xsl:apply-templates
      +- (text) "')"
      +- (element) xsl:text

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

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.