Multiple Input XML documents to produce one output XML

Discussion in 'XML' started by SV, Aug 16, 2005.

  1. SV

    SV Guest

    I have two input XML files. One lists all the expected elements
    (default.xml) with default values and the other is the actual data
    (data.xml). For any elements that are missing in data.xml, I want to
    copy them from all.xml and put it in the resulting OUTPUT document

    default.xml
    <CLOTHES>
    <JEANS>unknown/JEANS>
    <SHIRTS>unknown</SHIRTS>
    <TOPS>missing</TOPS>
    <SWIMSUITS>missing</SWIMSUITS>
    </CLOTHES>

    data.xml
    <CLOTHES>
    <JEANS>xxx</JEANS>
    <SHIRTS>yyy</SHIRTS>
    <TOPS>zzz</TOPS>
    </CLOTHES>

    OUTPUT should look like this:
    <CLOTHES>
    <JEANS>xxx</JEANS>
    <SHIRTS>yyy</SHIRTS>
    <TOPS>zzz</TOPS>
    <SWIMSUITS>missing</SWIMSUITS>
    </CLOTHES>

    How do i do this?

    Thanks,
    SV
     
    SV, Aug 16, 2005
    #1
    1. Advertising

  2. SV

    Mukul Gandhi Guest

    Please try this XSLT stylesheet.

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:common="http://exslt.org/common"
    exclude-result-prefixes="common"
    version="1.0">

    <xsl:eek:utput method="xml" indent="yes" />

    <xsl:variable name="doc1" select="/" />
    <xsl:variable name="doc2" select="document('default.xml')" />

    <xsl:template match="/">
    <CLOTHES>
    <xsl:variable name="rtf">
    <!-- iterate 1st document -->
    <xsl:for-each select="$doc1/CLOTHES/*">
    <xsl:variable name="elem" select="." />
    <xsl:variable name="flag">
    <xsl:for-each select="$doc2/CLOTHES/*">
    <xsl:if test="name($elem) = name()">
    1
    </xsl:if>
    </xsl:for-each>
    </xsl:variable>
    <xsl:if test="not(contains($flag, '1'))">
    <xsl:copy-of select="$elem" />
    </xsl:if>
    </xsl:for-each>

    <!-- iterate 2nd document -->
    <xsl:for-each select="$doc2/CLOTHES/*">
    <xsl:variable name="elem" select="." />
    <xsl:variable name="flag">
    <xsl:for-each select="$doc1/CLOTHES/*">
    <xsl:if test="name($elem) = name()">
    1
    </xsl:if>
    </xsl:for-each>
    </xsl:variable>
    <xsl:if test="not(contains($flag, '1'))">
    <xsl:copy-of select="$elem" />
    </xsl:if>
    </xsl:for-each>
    </xsl:variable>

    <xsl:copy-of select="$doc1/CLOTHES/*" />
    <xsl:copy-of select="common:node-set($rtf)/*" />
    </CLOTHES>
    </xsl:template>

    </xsl:stylesheet>

    Hope this helps,

    Regards,
    Mukul
     
    Mukul Gandhi, Aug 16, 2005
    #2
    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. Tim Rogers

    Having One Page Produce Multiple Views

    Tim Rogers, Nov 7, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    579
    George Ter-Saakov
    Nov 10, 2003
  2. loveNUNO
    Replies:
    2
    Views:
    967
    loveNUNO
    Nov 20, 2003
  3. wyattroerb
    Replies:
    0
    Views:
    273
    wyattroerb
    May 2, 2006
  4. Replies:
    5
    Views:
    491
    Luc The Perverse
    Oct 27, 2006
  5. Replies:
    4
    Views:
    790
    Dimitre Novatchev
    Nov 24, 2006
Loading...

Share This Page