tags:

views:

264

answers:

2

I would have thought this would be an easy one to Google, but I've been unsucessful.

I want to assign a variable the value out of an attribute (easy so far) then use that variable to select another node based on the value of that attribute.

ex:

<xsl:variable name="myId" select="@id" />
<xsl value-of select="//Root/Some/Other/Path/Where[@id='{@myId}']/@Name />

That does not work. If I replace the {@myId} with the value that is in the variable then it does find the right node, but doign it this way produces nothing. I'm sure I'm missing something, or perhaps there is a different way to do it.

The context is that there is related data under different top-level nodes that share the same id value so I need to get the related nodes in my template.

+1  A: 

Ok, I finally figured it out. Silly problem really, I simply needed to leave out the quotes and the braces. One of those times when I thought that I'd already tried that. :D Oh, and I mistyped @myId in the first example, the code was actually $myId.

<xsl:variable name="myId" select="@id" />
<xsl value-of select="//Root/Some/Other/Path/Where[@id=$myId']/@Name />
palehorse
A: 

You seem to have got confused with use of a variable (which is just $variable) and Attribute Value Templates, which allow you to put any XPath expression in some attributes, e.g.

<newElement Id="{@Id}"/>

They can obviously be combined, so you can include a variable in an Attribute Value Template, such as:

<newElement Id="{$myId}"/>
samjudson