XML transfrom: merge 2 or more different XML file into one

J

Jacinle Young

Hi,

I need to merge XML from different sources to 1 XML file.
I am wondering if XSLT could do this job as usually it is used to perform
1 to 1 file transfrom

eg.
src1.xml
<?xml version="1.0" encoding="UTF-8"?>
<nickname>Fred</nickname>
<email>[email protected]</email>
<html>TRUE</html>
<firstname>Fred</firstname>
<lastname>Flintstone</lastname>

src2.xml
<?xml version="1.0" encoding="UTF-8"?>
<nickname>Fred</nickname>
<work>999-Quarry</work>
<home>999-BedrockLane</home>
<fax>888-Squawk</fax>
<pager>777-pager</pager>
<cell>555-cell</cell>

result.xml

<?xml version="1.0" encoding="UTF-8"?>
<addressbook>
<nickname>Fred</nickname>
<email>[email protected]</email>
<html>TRUE</html>
<firstname>Fred</firstname>
<lastname>Flintstone</lastname>
<work>999-Quarry</work>
<home>999-BedrockLane</home>
<fax>888-Squawk</fax>
<pager>777-pager</pager>
<cell>555-cell</cell>
</addressbook>


Jacinle
 
C

Cat

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I need to merge XML from different sources to 1 XML file.
I am wondering if XSLT could do this job as usually it is used to perform
1 to 1 file transfrom

eg.
src1.xml
<?xml version="1.0" encoding="UTF-8"?>
<nickname>Fred</nickname>
<email>[email protected]</email>
<html>TRUE</html>
<firstname>Fred</firstname>
<lastname>Flintstone</lastname>

src2.xml
<?xml version="1.0" encoding="UTF-8"?>
<nickname>Fred</nickname>
<work>999-Quarry</work>
<home>999-BedrockLane</home>
<fax>888-Squawk</fax>
<pager>777-pager</pager>
<cell>555-cell</cell>

result.xml

<?xml version="1.0" encoding="UTF-8"?>
<addressbook>
<nickname>Fred</nickname>
<email>[email protected]</email>
<html>TRUE</html>
<firstname>Fred</firstname>
<lastname>Flintstone</lastname>
<work>999-Quarry</work>
<home>999-BedrockLane</home>
<fax>888-Squawk</fax>
<pager>777-pager</pager>
<cell>555-cell</cell>
</addressbook>
I haven't done any work on a problem of that sort but I'd say XInclude would be
the solution.
http://www.w3.org/TR/xinclude/

You definitely can't do it in vanilla xslt. You could try some ugly programming
hacks but I'd go with XInclude.
- --
Cat

http://www.ratrobot.com/writing/charity/ What is charity?
Is it charitable to give people a blanket but not a job? In this
article I will explore exactly what charity is.
Fri Jun 25 22:34:29 UTC 2004
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA3Kh1KHRjYtwQ1QARAvI4AKDfQnELC0FGOTPC20dP68O5yNle8gCfX2G1
NTlH6Y9gJ8XsqvV+S0vUMpw=
=35Yk
-----END PGP SIGNATURE-----
 
C

Cat

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 23 Jun 2004 20:32:22 -0700, Jacinle Young wrote:
Sorry, there is a simple way to do it in vanilla xslt, and here it is.

=================== XSLT =======================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput indent="yes"/>

<xsl:template match="root">
<addressbook>
<xsl:copy-of select="child::*"/>
<xsl:copy-of select="document('test2.xml')/root/*[name(.)!='nickname']"/>
</addressbook>
</xsl:template>
</xsl:stylesheet>
================= TEST1.XML ===================
<?xml version="1.0" encoding="UTF-8"?>
<root>
<nickname>Fred</nickname>
<email>[email protected]</email>
<html>TRUE</html>
<firstname>Fred</firstname>
<lastname>Flintstone</lastname>
</root>
================ TEST2.XML ===================
<?xml version="1.0" encoding="UTF-8"?>
<root>
<nickname>Fred</nickname>
<work>999-Quarry</work>
<home>999-BedrockLane</home>
<fax>888-Squawk</fax>
<pager>777-pager</pager>
<cell>555-cell</cell>
</root>

================ OUTPUT ======================

<?xml version="1.0" encoding="UTF-8"?>
<addressbook>
<nickname>Fred</nickname>
<email>[email protected]</email>
<html>TRUE</html>
<firstname>Fred</firstname>
<lastname>Flintstone</lastname>
<work>999-Quarry</work>
<home>999-BedrockLane</home>
<fax>888-Squawk</fax>
<pager>777-pager</pager>
<cell>555-cell</cell>
</addressbook>


Quite a neat trick? And I wouldn't have know about it without your question
8^).
- --
Cat

http://www.ratrobot.com/java/menu/ Simple menu applet that lets you
set colors,fonts and use it with a tiled background. It is completely FREE
for any use.
Sun Jun 27 21:12:05 UTC 2004
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA3zglKHRjYtwQ1QARAtwMAKDacBKFEsI4lsCl/xLWqqb+Hy13mQCfT5iF
3qypgKEWF/Oop3EzNFIL5S8=
=sW2r
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top