Meaningless Drivel is fun!*
The moose likes XML and Related Technologies and the fly likes Dynamically set the xsl:value-of select through a xsl:variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Dynamically set the xsl:value-of select through a xsl:variable" Watch "Dynamically set the xsl:value-of select through a xsl:variable" New topic
Author

Dynamically set the xsl:value-of select through a xsl:variable

Monty Guppy
Ranch Hand

Joined: Sep 15, 2001
Posts: 49
Here is what I am trying to do:
My XML:
<benefits>
<MEDICAL_ID>7123456</ID>
<MEDICAL_Description>HMO</Description>
<DENTAL_ID>2163939</ID>
<DENTAL_Description>PPO</Description>
</benefits>
I want to on demand be able to obtain only one of the above two 'benefits'. (Below I want to get only MEDICAL_ID value).
I am doing this by populating a variable called VAR_BEN_TYPE1, and subequently want to get the value by calling the variable (there are reasons for doing this way which would take a lot of sentences to type):
<xsl:variable name="VAR_BEN_TYPE1">
<xsl:value-of select="[concat('MEDICAL','_ID')]"/>
</xsl:variable>
<xsl:value-of select="$VAR_BEN_TYPE1"/>
The Output however comes out to be the literal value of the variable ("MEDICAL_ID") instead of what I want which is the value of MEDICAL_ID(=7123456).
Can you please help me to determine how to dynamically set the <xsl:value-of select> to a variable which is actually a XML tag reference?
Thanks
Prateek Gupta
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I think you'll need to use <xsl:choose>,<xsl:when>, and <xsltherwise>. Yes, I know it's incredibly verbose....


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Monty Guppy
Ranch Hand

Joined: Sep 15, 2001
Posts: 49
Ron,
<xsl:choose>.... would work well with a couple of options as I have shown in my XML above. But the reality is that there are about 30 benefits to choose from, which would make this extremely, extremely verbose.
Let me know if you can think of anything else.
Thanks.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
name() function should do it.


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
1) No, name() will not work!
Copied from http://www.w3.org/TR/xpath
Function: string name(node-set?)
2) Error msg from xalan by using naming
(Location of error unknown)XSLT Error (org.apache.xpath.XPathException): Can not convert #RTREEFRAG to a NodeList!
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
I am using someone else's computer right now and I do not have access to documentation to check or Xalan to test but it should be something like
<xsl:value-of select="*[name()=$VAR_BEN_TYPE1]"/>
Monty Guppy
Ranch Hand

Joined: Sep 15, 2001
Posts: 49
It works!!! Thanks a bunch Mapraputa for your help with the code.
 
Don't get me started about those stupid light bulbs.
 
subject: Dynamically set the xsl:value-of select through a xsl:variable
 
Similar Threads
Not getting my variable value
What are my options ??? - Urgent help needed
how can get just name@javaranch.com without tags in email application??
get value of xml element at runtime in XSL
xsl variable and xsl:if