hello experts I have 2 XML files, for ex:
<college>
<student>
<name>amit</name>
<file>/abc/kk/final.c</fi开发者_运维问答le>
<rollno>22</rollno>
<function>a()</function>
</student>
<student>
<name>sumit</name>
<file>/abc/kk/up.h</file>
<rollno>23</rollno>
<function>b()</function>
</student>
<student>
<name>nikhil</name>
<file>/xyz/up.cpp</file>
<rollno>24</rollno>
<function>c()</function>
</student>
<student>
<name>bharat</name>
<file>/abc/kk/down.h</file>
<rollno>25</rollno>
<function>d()</function>
</student>
<student>
<name>ajay</name>
<file>/simple/st.h</file>
<rollno>27</rollno>
<function>e()</function>
</student>
</college>
2nd XML file
<college>
<student>
<name>amit</name>
<file>/abc/kk/final.c</file>
<function>a()</function>
</student>
<student>
<name>nikhil</name>
<file>/xyz/up.cpp</file>
<function>c()</function>
</student>
<student>
<name>ajay</name>
<file>/simple/st.h</file>
<function>e()</function>
</student>
</college>
I want to compare the two XML files such that, we will get the output of those nodes which are not common. As I am new to xslt please provide me the solution. I am using:
<xsl:for-each select="document('1.xml')/college/student[
starts-with(file, '/abc/kk')
]">
<xsl:for-each select="document('2.xml')/college/student">
<xsl:if test="string(document('1.xml')/college/student/function)
!= string(document('2.xml')/college/student/function)">
<tr>
<td>
<xsl:value-of
select="document('1.xml')/college/student/name"/>
</td>
<td>
<xsl:value-of
select="document('1.xml')/college/student/file"/>
</td>
<td>
<xsl:value-of
select="document('1.xml')/college/student/function"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
Use XSLT to transform the two XML files into two plain text files that contain just the informations you want to compare.
Then just diff
the two plain text lists.
Don't forget to sort
the lists before comparing with diff
.
You want just this XPath expression:
document('1.xml')/college/student[
starts-with(file, '/abc/kk')
][
not(
function = document('2.xml')/college/student/function
)
]
As proof, this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table>
<xsl:apply-templates
select="college/student[
starts-with(file, '/abc/kk')
][
not(
function=document('2.xml')/college/student/function
)
]"/>
</table>
</xsl:template>
<xsl:template match="student">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="student/*">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>
Output:
<table>
<tr>
<td>sumit</td>
<td>/abc/kk/up.h</td>
<td>23</td>
<td>b()</td>
</tr>
<tr>
<td>bharat</td>
<td>/abc/kk/down.h</td>
<td>25</td>
<td>d()</td>
</tr>
</table>
精彩评论