tags:

views:

55

answers:

1

Good day to you,

I use Castor to map network infrastructure into Java classes. My XML file looks like this:

<SPECTRUM_Topology>
  <Topology>
    <!-- Device information -->
    <Device ip_dnsname="172.20.162.1" ... />
  </Topology>
  <Update>
    <Device ip_dnsname="172.20.162.1">
       <!-- Port information -->
       <Port ... />
       <Port ... />
    </Device>
  </Update>
</SPECTRUM_Topology>

I need the file to look like this:

<SPECTRUM_Topology>
  <Topology>
    <!-- Device information -->
    <Device ip_dnsname="172.20.162.1" ...>
       <!-- Port information -->
       <Port ... />
       <Port ... />
    </Device>
  </Topology>
</SPECTRUM_Topology>

Is there a way to do this via XSLT?

+2  A: 

Sure. Just create a specific template which matches the Device tag in Topology and inserts the Update/Device tag contents there.

This may get you started:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="xml" indent="yes"/>

 <xsl:key name="devices" match="/SPECTRUM_Topology/Update/Device" use="@ip_dnsname"/>

 <xsl:template match="Device">
  <xsl:copy>
   <xsl:apply-templates select="@*"/>
   <xsl:apply-templates select="key('devices', @ip_dnsname)/*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@* | node()">
  <xsl:if test="not(self::Update)">
   <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
   </xsl:copy>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>
Lucero
Thanks, I'll take a look on it.
Working perfect! Thanks :)