Outputting value of xsl:variable with xsl:value-of problem
Asked Answered
B

1

7

I think I may have a misunderstanding of <xsl:variable\> and <xsl:value-of\> so perhaps someone can correct me!

I'm attempting to tweak some hardcoded banners to be a bit cleaner so I thought it would be a good idea to create an <xsl:variable> containing the banner link and image code, then use <xml:value-of> at the various places where the banner is needed. For example:

<!-- Global variable in my xslt file. There are a bunch of these... -->
<xsl:variable name="banner1">
    <a href="http://www.link.com/" title="Title" target="_blank">
        <img width="120" height="506" src="/images/banners/image.gif" alt="alt" />
    </a>
</xsl:variable>

<!-- Then when used: -->
<xsl:when test="blah'">
    <xsl:value-of select="$banner1"/>
</xsl:when>

But this isn't producing the output I expect. The images path etc is valid, but this just spits out nothing at all. Any text added before or after the <a> tag appears correctly, but nothing between the <a> tags Themselves.

What have I misunderstood about <xsl:variable> and how could I do this better (other than doing it "properly" and pulling adverts from a database etc. which I'd prefer...).

Barone answered 8/3, 2010 at 13:28 Comment(0)
S
7

The value you are selecting with xsl:value-of is the string value of the variable.

You want <xsl:copy-of select='$banner1' /> to copy the result tree fragment.

Seaplane answered 8/3, 2010 at 13:31 Comment(1)
Thankyou muchly! Pretty new to xsl and I wasn't seeing what I should have been doing differently. Cheers!Barone

© 2022 - 2024 — McMap. All rights reserved.