BaconJuice
BaconJuice

Reputation: 3779

Setting selected attr value to XSL variable

Hey guys I'm trying to set a value from an attribute to a variable in XSL and I'm having a bit of an issue.

XSL Code is as such

<xsl:template match="/"> 

<xsl:call-template name="GETSTOCK">
<xsl:with-param name="ARTICLES" select="/Result/Response/STOCK/QUOTE[ @symbol = 'MSFT' ]" />
</xsl:call-template>

</xsl:template>
<xsl:template name="GETSTOCK">
<xsl:param name="data" />

<xsl:for-each select="$data">
<xsl:variable name="NAME" select="@name" />

<p>{$NAME}</p>

</xsl:for-each>

</xsl:template>

And here is what my XML looks like.

<Response>
    <STOCK>
        <QUOTE symbol="MSFT" name="Microsoft CA"/>
        <QUOTE symbol="MSFT" name="Microsoft US"/>
        <QUOTE symbol="MSFT" name="Apple CA"/>
        <QUOTE symbol="MSFT" name="Apple US"/>
        <QUOTE symbol="MSFT" name="Apple AU"/>
    </STOCK>
</Response>

Currently the output looks something like this.

 {$NAME}
 {$NAME}

Which basically means its grabbing the two nodes, just not the value. How do I get the value of the attribute name and populate it in the variable?

Thank you in advance for your help!

Upvotes: 0

Views: 75

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167726

With XSLT 1.0 and 2.0 you need to use

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

to output the value of the variable.

With XSLT 1.0/2.0 the {$NAME} only works in attribute values; in XSLT 3.0 it works if you ask for it, see http://www.w3.org/TR/xslt-30/#dt-text-value-template.

Upvotes: 3

JLRishe
JLRishe

Reputation: 101768

Martin's answer is absolutely correct, but if what you're showing is actually the code you're using, then there's really no reason to use a variable at all. Just use xsl:value-of directly on the attribute:

<xsl:for-each select="$data">
    <p>
        <xsl:value-of select="@name" />
    </p>
</xsl:for-each>

Upvotes: 1

Related Questions