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>

Edited 6 Years Ago by me655321: n/a

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.

This question has already been answered. Start a new discussion instead.