Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2006, 06:25 PM
Registered User
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DWalker
Default Merging two XML documents with XSL

Can someone help me with the following problem, please?

I'm attempting to merge two XML documents with similar structures. I would like to use an XSL transformation to accomplish this.

The first document looks like this:

<RECORDS>
   <DETAIL INDEX="1">
      <FIELD01>record 1 data</FIELD01>
      <FIELD02>record 1 data</FIELD02>
   </DETAIL>
   <DETAIL INDEX="3">
      <FIELD01>record 3 data</FIELD01>
      <FIELD02>record 3 data</FIELD02>
   </DETAIL>
   <DETAIL INDEX="999">
      <FIELD01>record 999 data</FIELD01>
      <FIELD02>record 999 data</FIELD02>
   </DETAIL>
</RECORDS>

The second file looks like this:

<RECORDS>
   <RECORD INDEX="3">
      <FIELD03>record 3 data</FIELD03>
      <FIELD04>record 3 data</FIELD04>
      <FIELD05>record 3 data</FIELD05>
   </RECORD>
   <RECORD INDEX="999">
      <FIELD03>record 999 data</FIELD03>
      <FIELD04>record 999 data</FIELD04>
      <FIELD05>record 999 data</FIELD05>
   </RECORD>
   <RECORD INDEX="1">
      <FIELD03>record 1 data</FIELD03>
      <FIELD04>record 1 data</FIELD04>
      <FIELD05>record 1 data</FIELD05>
   </RECORD>
</RECORDS>

and the output should look like this:

<RECORDS>
   <DETAIL INDEX="1">
      <FIELD01>record 1 data</FIELD01>
      <FIELD02>record 1 data</FIELD02>
      <FIELD03>record 1 data</FIELD03>
      <FIELD04>record 1 data</FIELD04>
      <FIELD05>record 1 data</FIELD05>
   </DETAIL>
   <DETAIL INDEX="3">
      <FIELD01>record 3 data</FIELD01>
      <FIELD02>record 3 data</FIELD02>
      <FIELD03>record 3 data</FIELD03>
      <FIELD04>record 3 data</FIELD04>
      <FIELD05>record 3 data</FIELD05>
   </DETAIL>
   <DETAIL INDEX="999">
      <FIELD01>record 999 data</FIELD01>
      <FIELD02>record 999 data</FIELD02>
      <FIELD03>record 999 data</FIELD03>
      <FIELD04>record 999 data</FIELD04>
      <FIELD05>record 999 data</FIELD05>
   </DETAIL>
</RECORDS>

The merge must be performed by matching the values in the Index attribute of both files. Notice that in the second file, the index values are out of order. The index values in both files can be non-sequential.

Both files with have the same number of records with a maximum of 50,000 records per file.

Any help will be greatly appreciated.

David Walker
Memphis, TN
 
Old February 3rd, 2006, 06:58 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If every index value appears each file, then this isn't too difficult. Keys make it much more efficient:

<xsl:variable name="doc1" select="/">
<xsl:variable name="doc2" select="document('doc2.xml')"/>

<xsl:key name="k" match="DETAIL" use="@INDEX"/>

<xsl:template match="/">
  <RECORDS>
  <xsl:apply-templates select="RECORDS/DETAIL"/>
  </RECORDS>
</xsl:template>

<xsl:template match="DETAIL">
  <DETAIL>
   <xsl:copy-of select="@*|*"/>
   <xsl:copy-of select="key('k', @INDEX, $doc2)/*"/>
  </DETAIL>
</xsl:template>

The 3-argument form of key() is XSLT 2.0 only, on 1.0 you have to write the circumlocution

   <xsl:variable name="this" select="."/>
   <xsl:for-each select="$doc2">
     <xsl:copy-of select="key('k', $this/@INDEX)/*"/>
   </xsl:for-each>

because key() only works when the node you want is in the same document as the context node.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
merging 2 xml with xslt gbordel XSLT 7 April 24th, 2008 05:27 AM
Merging XML Help Please sam666 XML 7 March 8th, 2006 02:50 AM
Merging 2 Xml docs nkuar XML 3 December 8th, 2005 05:22 AM
Merging 2 documents, joining on a unique ID remote XSLT 1 March 8th, 2005 03:23 PM
merging 2 xml documents espider4u XSLT 0 August 31st, 2004 11:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.