Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Merge 2 XML into one >Thread Next - Re: Merge 2 XML into one Re: Merge 2 XML into oneTo: NULL Date: 9/16/2008 12:59:00 PM 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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output 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> -- Martin Honnen http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
