473,498 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to merge XML (with XSLT?)

Hi, this might sound silly but I cannot figure out how to incorporate
some XML of employees into some XML of a company to give new XML
containing them all.

For example, this is the company now
<COMPANY>
<EMPLOYEE currentEmployee="true">
<NAME>Victor</NAME>
<AGE>37</AGE>
</EMPLOYEE>
</COMPANY>

And this is other XML that I want to add to the company
<EMPLOYEE currentEmployee="true">
<NAME>Thomas</NAME>
<AGE>39</AGE>
</EMPLOYEE>

<EMPLOYEE currentEmployee="false">
<NAME>Violeta</NAME>
<AGE>34</AGE>
</EMPLOYEE>

And the XML I want to generate is
<COMPANY>

<EMPLOYEE currentEmployee="true">
<NAME>Victor</NAME>
<AGE>37</AGE>
</EMPLOYEE>

<EMPLOYEE currentEmployee="true">
<NAME>Thomas</NAME>
<AGE>39</AGE>
</EMPLOYEE>

<EMPLOYEE currentEmployee="false">
<NAME>Violeta</NAME>
<AGE>34</AGE>
</EMPLOYEE>

</COMPANY>

I think I should be using XSLT but just cannot figure out the syntax.
Could someone help me out with what the XSLT should look like for this
example (if it is XSLT I should be using)?

Thank you again everyone for your help.
Regards
Victor
Jul 20 '05 #1
1 8664
"Victor" <jo**********@hotmail.com> wrote in message news:52*************************@posting.google.co m...
Hi, this might sound silly but I cannot figure out how to incorporate
some XML of employees into some XML of a company to give new XML
containing them all. : : And this is other XML that I want to add to the company : : <EMPLOYEE currentEmployee="true">
<NAME>Thomas</NAME>
<AGE>39</AGE>
</EMPLOYEE>

<EMPLOYEE currentEmployee="false">
<NAME>Violeta</NAME>
<AGE>34</AGE>
</EMPLOYEE>
Given each of these EMPLOYEE elements are in their own XML instance
document,

- - - merger.xsl
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Receives the file name of the document containing the employee to add.
-->
<xsl:param name="employeeUrl" />

<!-- Your sample excludes an XML declaration, and is indented, so this tells
the XSLT processor to emit the same way.
-->
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<!-- Copies verbatim any elements or attributes not explicitly
matched by another template rule.
-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<!-- Matches the source XML document's root node. This template
rule re-emits the source XML but appends the document element
of the XML instance document passed into the employeeUrl before
emitting the closing COMPANY tag.
-->
<xsl:template match="/">
<COMPANY>
<xsl:apply-templates select="COMPANY/EMPLOYEE" />
<xsl:if test="$employeeUrl">
<xsl:apply-templates select="document($employeeUrl)/EMPLOYEE" />
</xsl:if>
</COMPANY>
</xsl:template>

</xsl:stylesheet>
- - -

On the other hand, if all of the EMPLOYEE elements are in one XML instance
document, then they must be contained in a document element, i.e.,

<EMPLOYEES>
<EMPLOYEE> <!-- . . . --> </EMPLOYEE>
<EMPLOYEE> <!-- . . . --> </EMPLOYEE>
</EMPLOYEES>

because an XML instance document can have only one top-level document
element. If this is the case, then the above XSLT transform requires a minor
revision.

: : I think I should be using XSLT but just cannot figure out the syntax.
Could someone help me out with what the XSLT should look like for this
example (if it is XSLT I should be using)?


Sure, see the above. It can be done in XSLT, but as always, there are 33
ways to skin an armadillo (if for some reason, you want to skin an armadillo).

If you want to see a solution written in VB.NET to a very similar problem, that
does not involve any XSLT whatsoever, please see my reply in the following
newsgroup thread (watch for line-wrap in the hyperlink),

http://groups.google.com/groups?selm...&output=gplain
Hope this has been of help,
Derek Harmon
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4171
by: Jacinle Young | last post by:
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...
2
10177
by: MAF | last post by:
Is there a quick way to merge two XML files example A <TestNode> <Data>123</Data> </TestNode> <TestNode> <Data>123</Data> <Data>222</Data>
3
5364
by: Richard Fritzler | last post by:
I was given the task of designing a complete web based document prep system. In simplest terms (using a msword explanation) create a database of merge fields, and a library of templates. Allow the...
8
1842
by: ilikesluts | last post by:
Hi Group, I'm new to XML, here is my question: Would it be possible to write an algorithm that takes in two XML documents with the only condition being that they have the same root element? ...
1
1050
by: volunteer | last post by:
SIMPLY VERSION OF THE QUESTION: XML_TO_COPY.XML <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="xsl_that_copies.xsl"?> <fruits date="20060621"> <fruit name="orange"...
4
1390
by: volunteer | last post by:
SIMPLE VERSION OF THE QUESTION: XML_TO_COPY.XML <?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="xsl_that_tries_to_copy_but_does_not_work.xsl"?> <fruits...
7
3368
by: Meelis Lilbok | last post by:
Hi is for synchronizing two xml files any fast solution? Lets say i have 2 xml files 1.xml and 2.xml 1.xml contianes <test> <t id="1">Hello</t> <t id="2">World</t> <t id="3">Good bye!</td>
1
3951
by: Andy Fish | last post by:
hi, I am looking for a library (i.e. not a standalone GUI program) that can do diff and merge of HTML or XML, preferably in C# or at least that can be called from C# anyone know of such a...
1
1537
by: Muhammad Nasir Waqar | last post by:
Hi, I have an XSLT which merges 2 XML Documents provided as input parameters. I want to use that XSLT in my dot net code(C#). Please help me that how can I handle this situation. -- Kind...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.