Reputation: 43560
I'd link to transform XML with attributes like the 'name' attribute in the following:
<books>
<book name="TheBumperBookOfXMLProgramming"/>
<book name="XsltForDummies"/>
</books>
into elements called what was in the name attribute:
<books>
<TheBumperBookOfXMLProgramming/>
<XsltForDummies/>
</books>
using XSLT. Any ideas?
Upvotes: 3
Views: 894
Reputation: 13536
<xsl:template match="book">
<xsl:element name="{@name}">
<xsl:copy-of select="@*[name()!='name'] />
</xsl:element>
</xsl:template>
this also copies over any properties on <book>
not named 'name'
<book name="XsltForDummies" id="12" />
will turn into
<XsltForDummies id="12 />
Upvotes: 3
Reputation: 176169
You can create elements by name using xsl:element
:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<books>
<xsl:apply-templates />
</books>
</xsl:template>
<xsl:template match="book">
<xsl:element name="{@name}" />
</xsl:template>
</xsl:stylesheet>
Upvotes: 4