XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
V GSubject: Merge two xml files
Author: V G
Date: 07 Oct 2004 03:07 AM
Hi,
How can I merge two xml files with xsl and java?

I have e.g.

first.xml

<person>
<name>
<first>John</first>
<middle>Smith</middle>
</name>
<name>
<first>Rebeka</first>
<middle>Oil</middle>
</name>
</person>

second.xml

<person>
<name>
<first>John</first>
<middle>Bush</middle>
</name>
<name>
<first>Jordan</first>
<middle>Bach</middle>
</name>
</person>

result.xml

<person>
<name>
<first>John</first>
<middle>Bush</middle>
</name>
<name>
<first>Rebeka</first>
<middle>Oil</middle>
</name>
<name>
<first>Jordan</first>
<middle>Bach</middle>
</name>
</person>

I use the following java code:

try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("D:/Projects/merging_xml_files/merge.xsl"));
transformer.setParameter("second-xml-url", "D:/Projects/merging_xml_files/second.xml");
FileWriter mergedXMLWriter = new FileWriter("D:/Projects/merging_xml_files/result.xml");
transformer.transform(new StreamSource("D:/Projects/merging_xml_files/first.xml"), new StreamResult(mergedXMLWriter));
mergedXMLWriter.close();
} catch (Exception e) {
e.printStackTrace();
}

But it seems that I can not construct correctly my xsl file.
Please help with this issue.
Thanks in advance.

Postnext
Ivan PedruzziSubject: Merge two xml files
Author: Ivan Pedruzzi
Date: 07 Oct 2004 11:46 AM

sure, try something like that

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>

<xsl:template match="/">

<person>
<xsl:apply-templates select="person/name"/> <!-- first.xml is the main input -->
<xsl:apply-templates select="document('second.xml')/person/name"/>
</person>

</xsl:template>

<xsl:template match="name">
<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

Postnext
V GSubject: Merge two xml files
Author: V G
Date: 12 Oct 2004 10:39 AM
Hi

I did the same at first but this code just copy first.xml and then the second.xml
My logic is that I want to overwrite the elements from first.xml if they appear in the second.xml

e.g.
in the first.xml the middle name of the John is Smith but in the secon.xml it is Bush
in the result (as I described) John must be with Bush as its middle name

Posttop
Ivan PedruzziSubject: Merge two xml files
Author: Ivan Pedruzzi
Date: 12 Oct 2004 03:26 PM
I see now...

Try the following

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:key name="name_by_first" match="person/name" use="first"/>

<xsl:template match="/">
<person>
<xsl:for-each select="person/name">
<xsl:variable name="first" select="first"/>
<xsl:variable name="name" select="."/>
<xsl:for-each select="document('second.xml')">

<!-- check if "name/first" is present in the second.xml -->
<xsl:variable name="name_from_second" select="key('name_by_first', $first)"/>
<xsl:choose>
<xsl:when test="count($name_from_second) &gt; 0"> <!-- it is present then use the second definiton -->
<xsl:apply-templates select="$name_from_second"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>

<!-- loop on second.xml to see if we have additional "name" element to add -->
<xsl:for-each select="document('second.xml')/person/name">
<xsl:variable name="first" select="first"/>
<xsl:variable name="name" select="."/>

<!-- check if "name/first" is present in the first.xml -->
<xsl:for-each select="document('first.xml')">
<xsl:if test="count(key('name_by_first', $first))=0"> <!-- first is not present then add it -->
<xsl:apply-templates select="$name"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</person>
</xsl:template>

<xsl:template match="name">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.