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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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>

   
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.