使用XSL Transform查找两个XML之间的差异 [英] Find differences between two XMLs using XSL Transform

查看:68
本文介绍了使用XSL Transform查找两个XML之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,我正在编写转换文件。

I have two files and I am working on writing the transform

我希望转换文件返回修改了详细信息的人员的姓名(地址,年龄)以及在文件1中不存在但在文件2中存在的人员以及在文件1中存在但在文件2中不存在的人员的名称。

I would like the transform to return the names of the people whose details have been modified (address,age) and the names of the people who do not exist in file 1 but present in file 2 as well as people who exist in file 1 but do not exist in file 2.

我尝试了以下操作,但只能进行比较,找不到任何添加或删除内容

I have tried the following but it can only compare, it cannot find any addition or deletion

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:param name="path-to-file2" select="'file2.xml'"/>

<xsl:template match="/row">
<xsl:if test="address != document($path-to-file2)/rowset/row/address">
    <xsl:value-of select="name"/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

文件1:

<rowset>
<row>
    <name>kathy</name>
    <address>Cameron blvd</address>
    <age>12</age>
</row>
<row>
    <name>james</name>
    <address>superior blvd</address>
    <age>15</age>
</row>
</rowset>

文件2:

<rowset>
<row>
    <name>kathy</name>
    <address>Cameron blvd Las Vegas</address>
    <age>14</age>
</row>
<row>
    <name>james</name>
    <address>superior blvd</address>
    <age>15</age>
</row>
<row>
    <name>Henry</name>
    <address>Huron blvd</address>
    <age>18</age>
</row>
</rowset>

在上述情况下,我希望转换返回:

In above scenario I would like the transform to return:


亨利·凯西

Kathy, Henry


推荐答案

首先,如果您定义了两个变量来保存对每个文件中行的引用,这可能会有所帮助

Firstly, it may help if you defined two variables to hold the references to the rows in each file

<xsl:variable name="file1Rows" select="/rowset/row" />
<xsl:variable name="file2Rows" select="document($path-to-file2)/rowset/row" />

然后,要添加行(在文件2中),请执行此操作

Then, to get rows added (in file 2), do this

<xsl:apply-templates select="$file2Rows[not(name = $file1Rows/name)]" />

要删除,只需将其反转即可。

To get deletions, just reverse this...

<xsl:apply-templates select="$file1Rows[not(name = $file2Rows/name)]" />

获取更改,需要一些额外的工作,但是可以这样做...

Getting changes, takes a bit of extra work, but can be done like this...

<xsl:for-each select="$file1Rows">
    <xsl:apply-templates select="$file2Rows[name = current()/name and (address != current()/address or age != current()/age)]" />
</xsl:for-each>

将其全部放入即可得到...

Putting this altogether gives this...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>

<xsl:param name="path-to-file2" select="'file2.xml'"/>

<xsl:variable name="file1Rows" select="/rowset/row" />
<xsl:variable name="file2Rows" select="document($path-to-file2)/rowset/row" />

<xsl:template match="/*">
    <xsl:apply-templates select="file1/rowset" />
</xsl:template>

<xsl:template match="rowset">
    <additions>
        <xsl:apply-templates select="$file2Rows[not(name = $file1Rows/name)]" />
    </additions>
    <deletions>
        <xsl:apply-templates select="$file1Rows[not(name = $file2Rows/name)]" />
    </deletions>
    <changes>
    <xsl:for-each select="$file1Rows">
        <xsl:apply-templates select="$file2Rows[name = current()/name and (address != current()/address or age != current()/age)]" />
    </xsl:for-each>
    </changes>
</xsl:template>

<xsl:template match="row">
    <xsl:copy-of select="name" />
</xsl:template>

</xsl:stylesheet>

这篇关于使用XSL Transform查找两个XML之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆