This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: remove 'invisable' white spaces
- To: Kristof Bernaert <kbernaert at 1eEurope dot be>
- Subject: Re: [xsl] remove 'invisable' white spaces
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Mon, 2 Apr 2001 17:59:35 +0100
- CC: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Organization: Jeni Tennison Consulting Ltd
- References: <4B67D93B3C8CD311AE81009027D5E55EA014D2@DAVINCI>
- Reply-To: xsl-list at lists dot mulberrytech dot com
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