Converting and rename attribute values to elements

Here should go questions about transforming XML with XSLT and FOP.
Oubaas
Posts: 1
Joined: Mon Sep 17, 2007 1:42 am

Converting and rename attribute values to elements

Post by Oubaas »

How would I convert and rename the attribute "key" value to a corresponding element using xslt?

<record>
<properties>
<property key="Category">Male</property>
<property key="Product">Shoe</property>
<property key="Color">Green</property>
<property key="Region">Europe</property>
<property key="Date">11/11/2008</property>
<property key="ProductType">Running</property>
</properties>
</record>

to

<record>
<properties>
<cat>Male</cat>
<prd>Shoe</prd>
<col>Green</col>
<rgn>Europe</rgn>
<dat>11/11/2008</dat>
<pdt>Running</pdt>
</properties>
</record>

thanks
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

There are many possible solutions, one way to do it is like below

Code: Select all


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<record>
<properties>
<xsl:apply-templates/>
</properties>
</record>
</xsl:template>

<xsl:template match="property[@key='Category']">
<cat><xsl:value-of select="."/></cat>
</xsl:template>

<xsl:template match="property[@key='Product']">
<prd><xsl:value-of select="."/></prd>
</xsl:template>

<xsl:template match="property[@key='Color']">
<col><xsl:value-of select="."/></col>
</xsl:template>

<xsl:template match="property[@key='Region']">
<rgn><xsl:value-of select="."/></rgn>
</xsl:template>

<xsl:template match="property[@key='Date']">
<dat><xsl:value-of select="."/></dat>
</xsl:template>

<xsl:template match="property[@key='ProductType']">
<pdt><xsl:value-of select="."/></pdt>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George
George Cristian Bina
Post Reply