Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How to merge XML (with XSLT?)

Reply
Thread Tools

How to merge XML (with XSLT?)

 
 
Victor
Guest
Posts: n/a
 
      02-20-2004
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
 
Reply With Quote
 
 
 
 
Derek Harmon
Guest
Posts: n/a
 
      02-21-2004
"Victor" <> wrote in message news: 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 xmlnssl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Receives the file name of the document containing the employee to add.
-->
<xslaram name="employeeUrl" />

<!-- Your sample excludes an XML declaration, and is indented, so this tells
the XSLT processor to emit the same way.
-->
<xslutput 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML transfrom: merge 2 or more different XML file into one Jacinle Young XML 3 06-28-2004 01:20 AM
Compare & Merge XML documents Michael Ransburg Java 0 02-16-2004 02:23 PM
merge two xml files based on common key Luke Airig XML 1 12-16-2003 07:36 PM
merge of 2 xml files John Huntjens XML 0 09-14-2003 03:06 PM
Good way to selectively merge XML docs? Jim Bancroft XML 0 06-26-2003 08:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57