Merge 2 XML into one

F

fj

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
 
M

Martin Honnen

fj said:
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
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput 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::productID)]"/>
</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>
 
F

fj

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
 
M

Martin Honnen

fj said:
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
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:param name="paramName"/>

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

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top