Linux 如何使用xslt比较两个xml文件?
各位专家好 我有2个XML文件,例如:Linux 如何使用xslt比较两个xml文件?,linux,xslt,Linux,Xslt,各位专家好 我有2个XML文件,例如: <college> <student> <name>amit</name> <file>/abc/kk/final.c</file> <rollno>22</rollno> <function>a()</function> </student>
<college>
<student>
<name>amit</name>
<file>/abc/kk/final.c</file>
<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>
第二个XML文件
<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>
我想比较这两个XML文件,这样我们就可以得到那些不常见的节点的输出。由于我是xslt新手,请为我提供解决方案。
我正在使用:
<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>
使用XSLT将两个XML文件转换为两个纯文本文件,其中只包含要比较的信息 然后将两个纯文本列表进行区分
在与差异进行比较之前,不要忘记对列表进行排序。您只需要以下XPath表达式:
document('1.xml')/college/student[
starts-with(file, '/abc/kk')
][
not(
function = document('2.xml')/college/student/function
)
]
作为证明,此样式表:
<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>
输出:
<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>
您需要具有不同字符串值的节点。关于这件事在这里有很多疑问。有些甚至发布了输入样本!这是一个错误问题的示例—必须提供两个XML文档。我投票赞成这个问题。请编辑它并显著改进!谢谢你的回复…但是我可以在不转换成文本的情况下对.xsl文件执行此操作吗。我在xsl文件中为每个循环使用两个for循环,然后使用if检查条件,但没有得到结果..plz建议。