How do you use a variable in xsl when trying to select a node? *

Question

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.

Example:

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

Answer

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" />
< br > via < a class="StackLink" href=" http://stackoverflow.com/questions/5374/" >How do you use a variable in xsl when trying to select a node?< /a>
Share on Google Plus

About Cinema Guy

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment