如何使用xslt比较两个xml文件?

发布于 2024-10-30 22:14:33 字数 2824 浏览 0 评论 0原文

专家你好 我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秉烛思 2024-11-06 22:14:33

使用 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 with diff.

嘿嘿嘿 2024-11-06 22:14:33

您只需要这个 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文