Page 1 of 1

parsing variable in value-of

Posted: Thu Nov 02, 2006 4:45 pm
by phunkk
Hi,

I'm having problems trying to output the value of a variable. I've tried a LOT of different ways to solve it but i just seem to get back at this annoying problem.. It's hard to explain but imma give a try :)

This is the situation: i have a for-each loop over several xml-records. Each loop puts a string in a variable called $displayName. For each loop i want the script to return a value from the xml-document; the location of this value depends on the $displayName.

For instance, for a certain point in the loop the variable $displayName = 'companyName'. I want to call //HEADER/companyName, so i write <xsl:value-of select="//HEADER/$displayName"/> . XSL doesnt like it when i do that.

Is there a way to get around this? Thanks in advance!

Posted: Thu Nov 02, 2006 4:54 pm
by george
<xsl:value-of select="//HEADER/*[name()=$displayName]"/>

Note that the above works when the input document is in no namespace. If your input is in one or more namespaces then you can check local-name instead of name and namespace-uri to check the namespace.

Best Regards,
George

Posted: Thu Nov 02, 2006 5:25 pm
by phunkk
Wow, i never hoped to get a response this fast :D Imma go try this. I do have to admit i can see i have a lot to learn :roll:

Posted: Thu Nov 02, 2006 5:42 pm
by phunkk
george wrote:<xsl:value-of select="//HEADER/*[name()=$displayName]"/>

Note that the above works when the input document is in no namespace. If your input is in one or more namespaces then you can check local-name instead of name and namespace-uri to check the namespace.

Best Regards,
George
I'm sorry if im trying something really stupid :oops:, but it wont display anything, whether i try name or local-name. Gonna do some readin' bout the namespaces cause i dont know what those are..

The code i'm using is this:

Code: Select all

<xsl:choose>
<xsl:when test="//HEADER/*[name()=$displayName] != ''">
<xsl:value-of select="//HEADER/*[name()=$displayName]"/>
</xsl:when>
<xsl:otherwise>
pwn3d
</xsl:otherwise>
</xsl:choose>
Is this what you had in mind? Thanks again!