This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: remove 'invisable' 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]