tylik
tylik

Reputation: 1068

convert <xsl:variable> string value , to the value which I can select with <xsl:value-of select>

I have a variable from which I need to dynamically generate nodes

<xsl:template match="banner_discount_1 | banner_discount_2 | banner_discount_3"> 
    <xsl:variable name="link">banner_discount_<xsl:value-of select="substring-after(name(.) ,'banner_discount_')" />_link</xsl:variable>

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

<xsl:value-of> selects the string, but I want to be able to select the node which name matches the name of a variable. In my case the node looks something like this:

<banner_discount_1_link />
<banner_discount_2_link />
...

Here is the xml I'm using

<banner_discount_1> 12 </banner_discount_1>
<banner_discount_2> 21 </banner_discount_2>
<banner_discount_3> 32 </banner_discount_3>

<banner_discount_1_link> link1 </banner_discount_1_link>
<banner_discount_2_link> link2 </banner_discount_2_link>
<banner_discount_3_link> link3 </banner_discount_3_link> 

Upvotes: 1

Views: 2328

Answers (2)

harpo
harpo

Reputation: 43168

@MartinHonnen is on the right track, but you need to set the selection context as well.

Since you're in a template that's selecting the banner_discount_ nodes, that is your context. From your XML sample, it looks like the nodes you want to select are siblings, so this should work:

<xsl:value-of select="../*[local-name() = $link]"/>

It is preferable to target the nodes directly, but if they could be anywhere in the document, then you may resort to

<xsl:value-of select="//*[local-name() = $link]"/>

This is a last resort because it is potentially O(n) with respect to the number of nodes in the document.

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167491

Use <xsl:value-of select="*[local-name() = $link]"/>. If that does not help then consider to show a sample of the XML.

Upvotes: 1

Related Questions