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 June 15th, 2006, 06:52 AM
Registered User
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to combine stuff from two XML-files?

Hiya people,

I have the following problem and was wondering if you enlightened people could help me with it. I have two XML-files with different information about the same topics. I want to merge all the info from these two files into one file.

An example of what I mean:
Code:
I have

file1.xml

<thingy name="epsilon">
  <color>red</color>
</thingy>
<thingy name="gamma">
  <color>blue</color>
<thingy>


and in another file

file2.xml

<thingy name="epsilon">
  <number>seven</number>
</thingy>
<thingy name="gamma">
  <number>four</number>
<thingy>

And I want

file3.xml

<thingy name="epsilon">
  <color>red</color>
  <number>seven</number>
</thingy>
<thingy name="gamma">
  <color>blue</color>
  <number>four</number>
<thingy>
Is this even doable with XSLT? I've been wrestling with this for over four hours now and am completely stumped.

Thanks for reading!

Edit: Oh yeah, the entries aren't in the same order in both files.
 
Old June 15th, 2006, 07:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's easier in 2.0 than in 1.0. In 2.0:

<xsl:for-each-group
  select="(doc('file1.xml),doc('file2.xml'))/*/thingy"
  group-by="@name">
  <thingy>
    <xsl:copy-of select="current-group()/*"/>
  </thingy>
</xsl:for-each-group>

In 1.0 it's tricky because most of the standard grouping techniques (see http://www.jenitennison.com/xslt/grouping) only work on a single document. You could merge the documents first with one stylesheet, then do the grouping in another.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 15th, 2006, 07:40 AM
Registered User
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A huge thanks for the speedy reply. I'm working with 2.0 so that is not a problem. There is one more complication, though - the names aren't in the same place (I made an oversight in my simplification for the example).

This is closer to the truth:

file1.xml

<thingy1 name="epsilon">
  <color>red</color>
</thingy1>
<thingy1 name="gamma">
  <color>blue</color>
<thingy1>


and in another file

file2.xml

<thingy2>
  <name>epsilon</name>
  <number>seven</number>
</thingy2>
<thingy2>
  <name>gamma</name>
  <number>four</number>
<thingy>

And I want

file3.xml

<thingy name="epsilon">
  <color>red</color>
  <number>seven</number>
</thingy>
<thingy name="gamma">
  <color>blue</color>
  <number>four</number>
<thingy>


I've never used for-each-group before, so pardon my inexperience. But great big thanks for the first reply, now I at least know that it is doable with XSLT which in and of itself is a good thing for motivation.

 
Old June 15th, 2006, 07:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In this particular case you can just use (name|@name) as your grouping key. If it gets more complex it's still doable, for example you could write a grouping key as (if (root() is $doc1) then XXX else YYY) where $doc1 is one of the document nodes.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 15th, 2006, 08:03 AM
Registered User
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah yes, I see it now. Very nice. Thank you so much, you've been immensely helpful.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine rtf files with random access isnoend07 Pro VB 6 2 March 31st, 2007 01:22 PM
Problem to create an xml file from two xml files saurabh_inblore XSLT 1 April 12th, 2006 02:58 AM
combine the value of XML node pravind XSLT 3 March 15th, 2006 07:58 AM
combine the value of XML node pravind XSLT 0 March 15th, 2006 07:42 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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