Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Merge 2 XML into one

Reply
Thread Tools

Merge 2 XML into one

 
 
fj
Guest
Posts: n/a
 
      09-15-2008
Hi,
I have two XML representing a master-detail relationship and I want to
'join' them into a single XML with all fields from both files. For example
One XML file (Order.XML) looks like
<Orders>
<Order>
<productID>1234</productID>
...other order elements...
</Order>
</Orders>

Another XML(Products) looks like
<Products>
<Product productID='1234'>
<Name>...
<Category>...
....
</Product>
<Product>
....
</Product>
</Products>

I want to get a final XML look like

One XML file looks like
<Orders>
<Order>
<Product productID='1234'>
<Name>...
<Category>...
....
</Product>
...other order elements...
</Order>
</Orders>

How can I do the join using a XSLT? Any links/suggestion will be helpful.

-FJ


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-16-2008
fj wrote:
> Hi,
> I have two XML representing a master-detail relationship and I want to
> 'join' them into a single XML with all fields from both files. For example
> One XML file (Order.XML) looks like
> <Orders>
> <Order>
> <productID>1234</productID>
> ..other order elements...
> </Order>
> </Orders>
>
> Another XML(Products) looks like
> <Products>
> <Product productID='1234'>
> <Name>...
> <Category>...
> ...
> </Product>
> <Product>
> ...
> </Product>
> </Products>
>
> I want to get a final XML look like
>
> One XML file looks like
> <Orders>
> <Order>
> <Product productID='1234'>
> <Name>...
> <Category>...
> ...
> </Product>
> ..other order elements...
> </Order>
> </Orders>
>
> How can I do the join using a XSLT? Any links/suggestion will be helpful.


That is easy, just use the XSLT document function to load the second
XML, then process and copy the elements.
So assuming the orders.xml is

<Orders>
<Order>
<productID>1234</productID>
<foo>bar</foo>
</Order>
</Orders>

and the products.xml is

<Products>
<Product productID='1234'>
<Name>P 1234</Name>
<Category>Cat 0815</Category>
</Product>
</Products>

then this stylesheet

<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="products"
select="document('test2008091602.xml')/Products/Product"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Order">
<xsl:copy>
<xsl:apply-templates select="$products[@productID =
current()/productID]"/>
<xsl:apply-templates select="*[not(self:roductID)]"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

creates the output

<Orders>
<Order>
<Product productID="1234">
<Name>P 1234</Name>
<Category>Cat 0815</Category>
</Product>
<foo>bar</foo>
</Order>
</Orders>
--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
fj
Guest
Posts: n/a
 
      09-16-2008
Thanks Martin.
I am still get stuck in one thing... I am using the xslt in my code. So
these two source xml documents will be represented as variables in my code,
such as XmlDocument in .net.

How can I pull in the second xml into the xslt template?

-fj


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      09-17-2008
fj wrote:

> I am still get stuck in one thing... I am using the xslt in my code. So
> these two source xml documents will be represented as variables in my code,
> such as XmlDocument in .net.
>
> How can I pull in the second xml into the xslt template?


Use an XsltArgumentList and call
XsltArgumentList xsltArgs = new XsltArgumentList();
xsltArgs.AddParam("paramName", "", yourXmlDocument)
on it, the pass that XsltArgumentList to the Transform method (as the
second argument usually).
In your stylesheet you need
<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslaram name="paramName"/>

<xsl:variable name="products"
select="$paramName/Products/Product"/>


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
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
Merge multiple source directories into one package with distutils? Gregory Ewing Python 0 05-11-2011 05:35 AM
How to merge two sides into one Rik DVD Video 1 01-26-2008 01:16 PM
How to merge two binary files into one? could ildg Python 7 04-05-2005 01:40 PM
Merge two different xml files into one Kjetil Finsrud XML 0 11-23-2004 09:20 AM
XML transfrom: merge 2 or more different XML file into one Jacinle Young XML 3 06-28-2004 01:20 AM



Advertisments