p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   How to combine stuff from two XML-files? (http://p2p.wrox.com/showthread.php?t=44035)

Lemminkäinen June 15th, 2006 06:52 AM

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.

mhkay June 15th, 2006 07:29 AM

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

Lemminkäinen June 15th, 2006 07:40 AM

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.


mhkay June 15th, 2006 07:53 AM

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

Lemminkäinen June 15th, 2006 08:03 AM

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



All times are GMT -4. The time now is 03:01 PM.

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