parsing variable in value-of

Here should go questions about transforming XML with XSLT and FOP.
phunkk
Posts: 3
Joined: Thu Nov 02, 2006 4:37 pm

parsing variable in value-of

Post 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!
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post 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
phunkk
Posts: 3
Joined: Thu Nov 02, 2006 4:37 pm

Post 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:
phunkk
Posts: 3
Joined: Thu Nov 02, 2006 4:37 pm

Post 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!
Post Reply