I am trying to transform 1 XML document to another using XSLT. I want to call the value from my source XML to the element in my result XML as the value of an attribute.

Source code-

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>

Want output as-
<root>
<singer name=”Bob Dylan”>
<!--this name is the value of "artist"-->
<title>Empire Burlesque</title>
</singer>
<singer name=”Bonnie Tyler”/>
<title>Hide your heart</title>
</singer>
</root>

I don’t even know whether it is possible or not using XSLT. Please advise.

I'm not an expert in XSLT but I think you can do that with something like this:

<singer>
	<xsl:attribute name="name">
		<xsl:value-of select="artist"/> 
	</xsl:attribute>
	<title><xsl:value-of select="title"/></title>
</singer>

Thanks for the help me655321. I am new to XSLT and getting stuck in things like this.
It's working for me, however, my need is bit different (My apology as i should have mentioned it earlier).

i need to use a namespace in the resultant XML.
<root>
<singer ab:name=”Bob Dylan”>
<!--need to show "ab:name"-->
<title>Empire Burlesque</title>
</singer>
<singer ab:name=”Bonnie Tyler”/>
<title>Hide your heart</title>
</singer>
</root>


I tried the same thing with this "ab:name" and might be thats the reason it was not working, because when i try it without ":" it works.

<singer>
<xsl:attribute name="ab:name"> <!-- this one is not working>
<xsl:value-of select="@artist"/> </xsl:attribute>
<title><xsl:value-of select="title"/> </title>
</singer>
<singer>
<xsl:attribute name="abname"> <!-- this one is working>
<xsl:value-of select="@artist"/>
</xsl:attribute>
<title><xsl:value-of select="title"/></title>
</singer>


Please advise.

I got solution for it. thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.