Re: [xsl] Merge two xml files into one ?

Subject: Re: [xsl] Merge two xml files into one ?
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 11 Feb 2008 17:15:31 +0000
On 11/02/2008, Kerry, Richard <richard.kerry@xxxxxxxxxxx> wrote:
>
>
> Can I use XSL to merge the contents of two xml files into one ?
>
> I have :
> File1.xml :
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE device SYSTEM "device.dtd">
> <device name="Test">
>     <param name="CommsStatus" slot="2" />
>     <param name="DriverStatus" slot="3" />
>     <param name="LastDeviceError" slot="4" />
>     <param name="AlarmCount" slot="5" />
>     <param name="QuietMode" slot="6" />
> </device>
>
> File2.xml:
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE device SYSTEM "device.dtd">
> <device>
>     <param name="CommsStatus" slot="5" />
>     <param name="DriverStatus" slot="6"  />
>     <param name="LastDeviceError" slot="7"  />
>     <param name="AlarmCount" slot="8"  />
>     <param name="QuietMode" slot="9"  />
> </device>
>
>
> I want:
>
> File3.xml:
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE device
>   SYSTEM "device.dtd">
> <device name="Test">
>     <param name="CommsStatus" slot="2" />
>     <param name="DriverStatus" slot="3" />
>     <param name="LastDeviceError" slot="4" />
>     <param name="AlarmCount" slot="5" />
>     <param name="QuietMode" slot="6" />
>     <param name="CommsStatus" slot="5" />
>     <param name="DriverStatus" slot="6"  />
>     <param name="LastDeviceError" slot="7"  />
>     <param name="AlarmCount" slot="8"  />
>     <param name="QuietMode" slot="9"  />
> </device>
>
> Note that I don't care about the order of slot numbers, the contents of
> the second file just gets added at the end.
> Also that I will assume the same dtd and outermost element for both
> files.

Use the indentity template (see the other post a few minutes ago) with:

<xsl:template match="device">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    <xsl:copy-of select="document('file:/c:/path/to/file2.xml')/device/param"/>
  </xsl:copy>
</xsl:template>

This assumes you are processing file1.xml and want to include
file2.xml - you'd probably want to separate out the path to a
parameter.

cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread