views:

79

answers:

1

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...).

+5  A: 

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.

Lachlan Roche
Thankyou muchly! Pretty new to xsl and I wasn't seeing what I should have been doing differently. Cheers!
xan