Tuesday, 15 July 2014

How do I transform an XML element name into an attribute value using XSLT? -



How do I transform an XML element name into an attribute value using XSLT? -

given xml snippet:

<transactions> <tran id="1"> <e8> <datestamp>2012-05-17t15:16:57z</datestamp> </e8> </tran> </transactions>

how transform element <e8> <event type="e8"> using xslt?

edit: expected output:

<transactions> <tran id="1"> <event type="e8"> <datestamp>2012-05-17t15:16:57z</datestamp> </event> </tran> </transactions>

use:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <xsl:template match="tran/*"> <event type="{name()}"> <xsl:value-of select="."/> </event> </xsl:template> </xsl:stylesheet>

output:

<transactions> <tran id="1"> <event type="e8">2012-05-17t15:16:57z</event> </tran> </transactions>

xml xslt xpath

No comments:

Post a Comment