如何使用xslt比较两个xml文件?
专家你好 我有 2 个 XML 文件,例如:
<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>
hello experts
I have 2 XML files, for ex:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 XSLT 将两个 XML 文件转换为两个纯文本文件,其中仅包含您要比较的信息。
然后只需
diff
这两个纯文本列表即可。在与
diff
比较之前,不要忘记对列表进行排序
。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 withdiff
.您只需要这个 XPath 表达式:
作为证明,这个样式表:
输出:
You want just this XPath expression:
As proof, this stylesheet:
Output: