I need to find a way to join two XML files when they have a matching node. From what I gather this could be accomplished with many different languages… is there a PHP or an AJAX way to do this? From other posts on SO I see XSLT solutions.. that I dont really get. Is this the best/preferred method? If so, know of any helpful XSLT tutorials?
For example XML-1 is like :
<FOO>
</A>
</B>
</C>
</D>
</FOO>
and XML-2 :
<FOO>
</B>
</E>
</FOO>
What would be the best approach for checking where <B>==<B> then add <E>
Update
Well I cant get this to work with my hypothetical example and thought I would update with what I am really doing to see if anyone can help me figure this. I have tried the methods from below and others I have found on SO with no luck.
The real schema is like :
file1.xml
<?xml version="1.0"?>
<DATA>
<ITEM>
<PRODUCT_TYPE>simple</PRODUCT_TYPE>
<STYLE_COLOR>1524740007</STYLE_COLOR>
<SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
<CLASS_NAME>FOOTWEAR</CLASS_NAME>
<STATUS>Disabled</STATUS>
</ITEM>
...
</DATA>
file2.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl" ?>
<DATA>
<ITEM>
<STYLE_COLOR>1524740007</STYLE_COLOR>
<NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
</ITEM>
....
</DATA>
What I need to figure out is to have a new XML file generated that would merge these nodes with identical SYTLE_COLOR and look like:
<DATA>
<ITEM>
<PRODUCT_TYPE>simple</PRODUCT_TYPE>
<STYLE_COLOR>1524740007</STYLE_COLOR>
<SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
<CLASS_NAME>FOOTWEAR</CLASS_NAME>
<NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
<STATUS>Disabled</STATUS>
</ITEM>
I tried creating a merge.xsl that looks like :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" />
<xsl:output indent="yes"/>
<xsl:variable name="with" select="'file-2.xml'" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="scene">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<xsl:variable name="info" select="document($with)/DATA/ITEM[STYLE_COLOR=current()/STYLE_COLOR]/." />
<xsl:for-each select="$info/*">
<xsl:if test="name()!='STYLE_COLOR'">
<xsl:copy-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:transform>
I also tried a merge like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:variable name="input2" select="document('file-2.xml')/DATA/ITEM"/>
<xsl:template match="STYLE_COLOR">
<xsl:copy>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="$input2/*[name()=name(current())]">
<xsl:copy-of select="$input2/*"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Neither of these methods are working.. sorry XSLT is very new to me so I am not sure what I am doing and would really appreciate some hand holding on this one.
This is the original transform slightly modified to adapt the new requirements. The merge is performed by checking against file2.xml elements. For the current ITEM in file1, a children ITEM in file2 will be merged only if not present in the file1.
[XSLT 1.0]
Applied on this
input1.xml:and
input2.xmlto merge, produces:produces:
Notice that:
input1.xmlis copied in output only if has a match ininput2.xml