Merge 2 XML into one

Discussion in 'XML' started by fj, Sep 15, 2008.

  1. fj

    fj Guest

    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
    fj, Sep 15, 2008
    #1
    1. Advertising

  2. 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: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>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Sep 16, 2008
    #2
    1. Advertising

  3. fj

    fj Guest

    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
    fj, Sep 16, 2008
    #3
  4. 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
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:param name="paramName"/>

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


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Sep 17, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loveNUNO
    Replies:
    2
    Views:
    898
    loveNUNO
    Nov 20, 2003
  2. Jacinle Young
    Replies:
    3
    Views:
    440
    Jacinle Young
    Jun 28, 2004
  3. Kjetil Finsrud

    Merge two different xml files into one

    Kjetil Finsrud, Nov 23, 2004, in forum: XML
    Replies:
    0
    Views:
    1,176
    Kjetil Finsrud
    Nov 23, 2004
  4. could ildg
    Replies:
    7
    Views:
    2,309
    Grant Edwards
    Apr 5, 2005
  5. Gregory Ewing
    Replies:
    0
    Views:
    248
    Gregory Ewing
    May 11, 2011
Loading...

Share This Page