xsl-list
[Top] [All Lists]

[xsl] Re: Merge Two Files

2009-07-04 18:28:34

Perfect.  Thanks to both of you.

brad


----- Original Message ----
From: Brad Felix <bfelix25(_at_)yahoo(_dot_)com>
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Sent: Friday, July 3, 2009 10:44:58 AM
Subject: Merge Two Files

I've searched and hacked a bit and am a little stuck, and sense the answer is 
simple.  I'm trying to merge two files, matched by ID.  As follows:

file 1:

<?xml version="1.0"?>
<chapter>
<title>Some Title</title>
<section id="sec1">
<title>My Title</title>
<para id="1">Some content</para>
<para id="2">Some content</para>
<para id="3">Some content</para>
<para id="4">Some content</para>
<para id="5">Some content</para>
<para id="6">Some content</para>
<para id="7">Some content</para>
<para id="8">Some content</para>
</section>
</chapter>

file 2:

<?xml version="1.0" encoding="UTF-8" ?>
<personalnotes>
<note id="1">Here is a sample 1 annotation</note>
<note id="2">Here is a sample 2 annotation</note>
<note id="3">Here is a sample 3 annotation</note>
<note id="4">Here is a sample 4 annotation</note>
</personalnotes>


File 1 provides the "master" and the result of the merge should look like:

<?xml version="1.0"?>
<chapter>
<title>Business Ethics and Social Responsibility</title>
<section id="sec1">
<title>My Title</title>
<para id="1">Some content</para>
<para role="note" id="1">Here is a sample 1 annotation</para>
<para id="2">Some content</para>
<para role="note" id="2">Here is a sample 2 annotation</para>
<para id="3">Some content</para>
<para role="note" id="3">Here is a sample 3 annotation</para>
<para id="4">Some content</para>
<para role="note" id="4">Here is a sample 4 annotation</para>
<para id="5">Some content</para>
<para id="6">Some content</para>
<para id="7">Some content</para>
<para id="8">Some content</para>
</section>
</chapter>


I'm working with this as a starting point but cannot seem to get it right (this 
just basically copies file1 to the output):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:output method="xml"/>
<!-- load the merge file -->
<xsl:variable name="file1" select="/" />
<xsl:variable name="file2" select="document('annotations.xml')"/>
<xsl:variable name="file2IDs" select="$file2//@id" />

       <xsl:template match="/">
     <xsl:call-template name="copyNode"/>
</xsl:template>

<xsl:template name="copyNode">

<xsl:for-each select="child::*">
<xsl:variable name="elemName" select="name()"/>
<xsl:choose>
<xsl:when test="$file1//*[(_at_)id = $file2IDs]">
     <xsl:copy-of select="$file1/*[name()=$elemName]"/>
     <xsl:copy-of select="$file2/*[name()=$elemName]"/>
       </xsl:when>
       <xsl:otherwise>
     <xsl:copy-of select="$file1/*[name()=$elemName]"/>
              </xsl:otherwise>
      </xsl:choose>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>



Any thoughts appreciated!

Thanks
brad

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>
  • [xsl] Re: Merge Two Files, Brad Felix <=