Jacinto Benavente
Jacinto Benavente

Reputation: 1

XSLT rename element with attribute value

I'm new with xslt. and im trying to rename the element name of my .xml with the attribute name, and also remove the atttribute.

This is a sample of the XML i want to transform:

<configdata>  
 <element xsi:type="AAA">
  <attributes>
     <att1>0</att1>
     <att2>1</att2>
     <att3>25</att3>
  </attributes>
 </element>
 <element xsi:type="BBB">
  <attributes>
     <att4>23</att4>
     <att5>44</att5>
     <att6>12</att6>
  </attributes>
 </element>
</configdata>

desired output:

<configdata> 
 <AAA>
  <attributes>
     <att1>0</att1>
     <att2>1</att2>
     <att3>25</att3>
  </attributes>
 </AAA>
 <BBB>
  <attributes>
     <att4>23</att4>
     <att5>44</att5>
     <att6>12</att6>
  </attributes>
 </BBB>
</configdata>

The xml has hundreds of elements(AAA,BBB,CCC,DDD...) so, any general solution would be great.

I've tried with the following xslt code but in the output i keep the input xml with no change at all.

<?xml version="1.0"?>
<xsl:stylesheet 
version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="element">
 <xsl:element name="{@xsi:type}">
  <xsl:value-of select="."/>
 </xsl:element>
</xsl:template>

</xsl:stylesheet>

I would appreciate any help. Thanks

Upvotes: 0

Views: 958

Answers (1)

Tomalak
Tomalak

Reputation: 338386

<xsl:template match="element">
 <xsl:element name="{@xsi:type}">
  <xsl:apply-templates select="@*|node()"/>
 </xsl:element>
</xsl:template>

You want to process all children of <element>. However, <xsl:value-of select="."/> does not do that. All it does is outputting the text content of an element.

Upvotes: 0

Related Questions