Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 15th, 2006, 06:52 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
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.
Reply With Quote
  #2 (permalink)  
Old June 15th, 2006, 07:29 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
  #3 (permalink)  
Old June 15th, 2006, 07:40 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
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.

Reply With Quote
  #4 (permalink)  
Old June 15th, 2006, 07:53 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
  #5 (permalink)  
Old June 15th, 2006, 08:03 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:52 PM.


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