Multiple Input XML documents to produce one output XML

S

SV

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
 
M

Mukul Gandhi

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
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top