views:

497

answers:

4

I am trying to use XSLT variables and not having much success, hopefully I'm just doing something dumb.

I have the following code snippet:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xalan="http://xml.apache.org/xslt"
  version="1.0">

    <xsl:template match="/">
      <xsl:variable name="config" select="query/@config"></xsl:variable>

so I expect there to be a variable 'config' set to the value of the 'config' attribute of my top-level element 'query'.

I then try to use the variable later in my stylesheet, for example:

<a href="localhost/test?go">
    {$config}
</a>

but the output I see in my output HTML document is:

<a href="localhost/test?go">
    {$config}
</a>

so the value has not been substituted as I would have expected.

I think this is pretty much the simplest case there could be, so I'm doing domething stupid! Please help, thanks!


UPDATE thanks to all who responded, I misunderstood the different contexts of whether I was working in an attribute or outside. Sorted my problem out nicely!

If I could I would accept two answers, the one I have, and @Aaron Digulla's, which explained the attributes thing.

+3  A: 

Use <xsl:value-of select="$config"/> instead of {$config}.

Welbog
Beat me to it ;)
Kezzer
@Kezzer: That's why they pay me the big bucks <('_'<)
Welbog
+1  A: 

Because you need to use <xsl:value-of select="config"/> ?

Kezzer
If config is a variable, we must use $config, no?
bortzmeyer
+7  A: 

In your stylesheet you must use:

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

instead of:

{$config}
Laurent
+3  A: 

{$config} only works in attributes of XSLT elements. (Note: dollar inside the brace because the complete XPath expression must be surrounded by braces)

Aaron Digulla
Why would the $ need to be in front of the opening brace? Never seen that.
0xA3
@divo: because that's the syntax.
Welbog
Nope, it's not ;) See http://www.w3.org/TR/xslt#attribute-value-templates:
0xA3
(continued) "In an attribute value that is interpreted as an attribute value template [...] an expression can be used by surrounding the expression with curly braces ({})."
0xA3
Well, you showed me. That's what I get for trusting an answer on SO, I guess. I'll never make that mistake again!
Welbog
I corrected the misleading answer.
0xA3
Holy *)%*)
Aaron Digulla
@divo: I just read the link and ... I didn't understand it :P I mean, it's English and all but ... they don't really care whether anyone can get what they say, do they? ;)
Aaron Digulla