selecting the value of the node based on the value of parameter

S

sp

i have an xml file

<catalog>
<cd>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
<cd>
<title>When a man loves a woman</title>
<artist>Percy Sledge</artist>
<country>USA</country>
<company>Atlantic</company>
<price>8.70</price>
<year>1987</year>
</cd>
<cd>
<title>Black angel</title>
<artist>Savage Rose</artist>
<country>EU</country>
<company>Mega</company>
<price>10.90</price>
<year>1995</year>
</cd>
<cd>
<title>1999 Grammy Nominees</title>
<artist>Many</artist>
<country>USA</country>
<company>Grammy</company>
<price>10.20</price>
<year>1999</year>
</cd>
</catalog>

i need to get the individual elements value under the cd tag

and my xsl looks like

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="mycount" />
<xsl:param name="myelement" />
<xsl:template match="/">
<xsl:for-each select="/catalog/cd">
<xsl:if test="position()=$mycount>
<xsl:value-of select="$myelement" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

and when i pass the name of the elements like "title" or "artist" or
"country" etc.,,,

my output is

the string value of the tag i passed (i.e, if i pass "title" output is
"title")

how is that i can get the value of the title like Romanza which is at
position 1

can anybody help me

-praveen
 
G

George Bina

Hi Praveen,

Replace your template with
<xsl:template match="/">
<xsl:value-of
select="/catalog/cd[position()=$mycount]/*[name()=$myelement]"/>
</xsl:template>
and you should get the expected result.

Best Regards,
George
 
J

Joe Kesselman

<xsl:value-of select="$myelement" />

If your processor support the EXSLT extension library, you can use the
"dynamic XPath" support to execute computed XPaths, but basic XSLT does
not support this. I'd suggest finding another way to express it, such as
testing the value of the param and switching between several different
value-of's which explicitly retrieve the requested value.
 
J

Joseph Kesselman

George said:
Replace your template with
<xsl:template match="/">
<xsl:value-of
select="/catalog/cd[position()=$mycount]/*[name()=$myelement]"/>
</xsl:template>
and you should get the expected result.

That solution will work, but is not namespace-sensitive. If you want to
correctly process namespaced documents, you'll need to test localname
and namespace uri rather than name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top