1

I have XSLT like:

<xsl:variable name="letter">
  <xsl:value-of select="@display_value"/>
</xsl:variable>

and somewhere I use:

<xsl:value-of select="$letter"/>  

But this is adding extra space. I'd like to use xsl:text, but it doesn't take a variable.

Do you have suggestions, please, on how to get rid of the space?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
asahun
  • 195
  • 3
  • 16

1 Answers1

1

It'd be better if you showed us sample input XML and output XML. Without that, one might guess that @display_value in the input XML has more white space than you'd like. You could use normalize-space() when you define letter:

<xsl:variable name="letter">
  <xsl:value-of select="normalize-space(@display_value)"/>
</xsl:variable>

or when you use it:

<xsl:value-of select="normalize-space($letter)"/>

Note, however, that normalize-space() is not the same as trim in other languages in that it will reduce repetitive whitespace internal to a string, not just on its ends.

See also "How to Trim in XSLT".


Update per new comment from OP:

Another possibility (apparently the reason in this case) is that the context into which $letter is output has significant white space that one is mistakenly attributing to $letter itself. Changing this:

[
<xsl:value-of select="$letter"/>
]

to this:

<xsl:text>[</xsl:text>
<xsl:value-of select="$letter"/>
<xsl:text>]</xsl:text>

helped resolve the problem.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks, apparently the issue wasn't the on the variable. I have braces [ and ] surrounding . I put the braces inside xsl:text, like [ and ]. This fixed it – asahun Nov 15 '13 at 19:14
  • Ah, ok. I've edited the answer to reflect this possibility for posterity's sake. Thanks for the follow-up. – kjhughes Nov 15 '13 at 19:30