theta
theta

Reputation: 25631

How to reference variable in xsl:text element?

I have a stylesheet where I would prefer to insert predefined string variable in xsl text element, but can't find any pointers while searching the web.

For example:

<xsl:variable name="var" select="node()/ref/text()"/>
...
<xsl:text>Some text where I want to append $var variable desperately</xsl:text>
...

I tried with $var, ($var), {$var}...

Upvotes: 13

Views: 25955

Answers (4)

Basheer AL-MOMANI
Basheer AL-MOMANI

Reputation: 15337

I did something like this to create a variable with string and another variable on it

<xsl:variable name="imgsrc">http://mirrors.creativecommons.org/presskit/buttons/<xsl:value-of select='$iconSize' />/<xsl:value-of select="substring-before($licenceTypeAndText, '|')" /></xsl:variable>

and this will produce a variable with a value like this

http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by.png

Upvotes: 0

Christopher Creutzig
Christopher Creutzig

Reputation: 8774

Some text where I want to include the value of <xsl:value-of select='$var'/>.

Note that <xsl:text> is only needed to control the behavior on whitespace. Most of the time, you can simply type text and just include this element when the result is not what you expected – I guess knowing the rules for when to use <xsl:text> doesn't hurt, however. :) (And it's not complicated: Use <xsl:text> if otherwise, your text nodes would only have white space or if they would include additional whitespace you want to avoid at either end.)

EDIT: Note that whitespace in variables doesn't need xsl:text for protection when used, that is only for the XSL parsing step:

<xsl:variable name="newline"><!-- can be global -->
<xsl:text>
</xsl:text>
</xsl:variable>

<xsl:when test="starts-with(., $newline)">
  ...
</xsl:when>

Here's some text<xsl:value-of select='$newline' />with formatting.

Upvotes: 15

Michael Kay
Michael Kay

Reputation: 163458

xsl:text is only used to output fixed text, and it cannot contain nested instructions. The instruction to output variable text is xsl:value-of.

Upvotes: 7

Neil
Neil

Reputation: 7437

Here's another way:

<xsl:value-of select="concat(
    'Some text where I want to append ', 
    $var, 
    ' variable desperately')" />

Although it's a bit ugly, this gives you full control of the white space between your text and the variable's text.

Upvotes: 8

Related Questions