• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Change element name to child element text value

I have the following incoming XML document which I need to transform using XSL to another XML document.

<TRANSACTION>
  <EVENTS>
        <EVENT_TYPE>ASSR</EVENT_TYPE>  
        <REF_NUMBER>F04000178</REF_NUMBER>
        <COMPLETED_DATE>15-Apr-2005 00:00:00</COMPLETED_DATE>
    </EVENTS>
  <EVENTS>
      <REF_NUMBER>F04000178</REF_NUMBER>
      <EVENT_TYPE>AUDIT</EVENT_TYPE>
      <COMPLETED_DATE>15-Apr-2005 00:00:00</COMPLETED_DATE>
  </EVENTS>
</TRANSACTION>

The resulting XML Document needs to look like this:
<TRANSACTION>
  <ASSR>
        <ACCOUNT>F04000178</ACCOUNT>
        <DATE>15-Apr-2005 00:00:00</DATE>
    </ASSR>
  <AUDIT>
      <ACCOUNT>F04000178</ACCOUNT>
      <DATE>15-Apr-2005 00:00:00</DATE>
  </AUDIT>
</TRANSACTION>

I can get everything transformed correctly with the exception of the EVENT_TYPE element.  I need to replace it's parent element name with it's text node value.  No hardcoding of the parent element name (EVENTS) can be used since the name of the parent element can change.


Thanks.

0
trudyhlittle
Asked:
trudyhlittle
1 Solution
 
b1xml2Commented:
xslt
====
<?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" version="1.0" />
<xsl:template match="@* | node()">
<xsl:copy><xsl:apply-templates select="@* | node()" /></xsl:copy>
</xsl:template>

<xsl:template match="EVENT_TYPE" />

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

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

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

 
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now