How to merge XML (with XSLT?)

V

Victor

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
 
D

Derek Harmon

Victor said:
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:eek:utput 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=Ohfss2C#[email protected]&oe=UTF-8&output=gplain


Hope this has been of help,


Derek Harmon
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top