Reputation: 765
I have to find the common nodes in 2 XMLs. I have written an XSL after looking some examples online to get the intersection of 2, but is giving error while merging when used with Java.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="no" indent="yes"/>
<xsl:param name="V9_XML_PATH" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="schedule">
<xsl:variable name="match" select="/schedule/scheduleItem[measurements/measurement=document($V9_XML_PATH)/schedule/scheduleItem/measurements/measurement]"/>
<xsl:choose>
<xsl:when test="$match">
<xsl:copy-of select="$match"/>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
My 2 XMLs are:
XML1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schedule>
<scheduleItem scheduleId="1" startDate="2013-01-01" stopDate="2037-12-31">
<measurements>
<measurement>ADM010000</measurement>
</measurements>
<measPeriods>
<period day="10" duration="10" hour="10" interval="45" minutes="0"/>
</measPeriods>
</scheduleItem>
<scheduleItem scheduleId="2" startDate="2013-01-01" stopDate="2037-12-31">
<measurements>
<measurement>ADM020000</measurement>
</measurements>
<measPeriods>
<period day="0" duration="0" hour="0" interval="15" minutes="0"/>
</measPeriods>
</scheduleItem>
</schedule>
XML2
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schedule>
<scheduleItem scheduleId="1" startDate="2013-01-01" stopDate="2037-12-31">
<measurements>
<measurement>ADM010000</measurement>
</measurements>
<measPeriods>
<period day="0" duration="0" hour="0" interval="15" minutes="0"/>
</measPeriods>
</scheduleItem>
<scheduleItem scheduleId="2" startDate="2013-01-01" stopDate="2037-12-31">
<measurements>
<measurement>ADM030000</measurement>
</measurements>
<measPeriods>
<period day="0" duration="0" hour="0" interval="15" minutes="0"/>
</measPeriods>
</scheduleItem>
</schedule>
Expected Output:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schedule>
<scheduleItem scheduleId="1" startDate="2013-01-01" stopDate="2037-12-31">
<measurements>
<measurement>ADM010000</measurement>
</measurements>
<measPeriods>
<period day="10" duration="10" hour="10" interval="45" minutes="0"/>
</measPeriods>
</scheduleItem>
</schedule>
Here V9_XML_PATH="XML2" which is passed as parameter to XSL, and if any tag matched in XML1 and XML2, the record from XML1 I have to take. Please notice period
tag in expected output.
I don't know what's the problem, it's not working in Java. Please help me to find the issue.
Upvotes: 0
Views: 136
Reputation: 117018
I don't know what's the problem
There are several problems with your second template:
First, you're matching on schedule
when you should be matching on scheduleItem
.
Next, the way you define the match
variable makes no sense: you want to know if the other document contains a scheduleItem
whose measurement
value is equal to the measurement
value of the current scheduleItem
.
Finally, there's no measSchedule
node in your XML2 document.
Try instead:
<xsl:template match="scheduleItem">
<xsl:variable name="match" select="document($V9_XML_PATH)/schedule/scheduleItem[measurements/measurement=current()/measurements/measurement]"/>
<xsl:if test="$match">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
Upvotes: 1