In XML, carriage return, linefeed, tab, and the spacebar are the whitespace characters. By default, XSLT preserves whitespaces. White-space in the stylesheet is ignored as long as it occurs between XML elements only. For example, the following XSL template:
<xsl:template match="foo">
foo
</xsl:template>
will produce the following results from the processor's point of view.
"\n··foo\n"
To avoid the unwanted whitespaces, you should try the following XSL template
<xsl:template match="foo">
<xsl:text>foo</xsl:text>
</xsl:template>
Also you are applying the following template
<xsl:apply-templates />
It applies to "white-space-only" nodes as well because the default XSLT rule for text nodes says "copy them to the output". For instance:
<xml>
<data> value </data>
</xml>
contains three text nodes:
"\n··"
(right after <xml>
)
"·value·"
- "
\n"
(right before </xml>
)
To avoid that #1 and #3 sneak into the output (which is the most common reason for unwanted spaces), you can override the default rule for text nodes by declaring an empty template:
<xsl:template match="text()" />
All text nodes are now muted and text output must be created explicitly:
<xsl:value-of select="data" />
To remove white-space from a value, you could use the normalize-space()
XSLT function:
<xsl:value-of select="normalize-space(data)" />
But careful, since the function normalizes any white-space found in the string, e.g. "·value··1·"
would become "value·1"
.
Additionally you can use the <xsl:strip-space>
and <xsl:preserve-space>
elements. By default, XSLT templates have <xsl:preserve-space> set, which will keep whitespace in your output. You can add <xsl:strip-space elements="*"> to tell it to where to delete whitespace.