如何使用xslt比较和合并两个xml [英] how to compare and merge two xml using xslt

查看:65
本文介绍了如何使用xslt比较和合并两个xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个xml,然后将它们合并.例如:

I want to compare two xmls and then merge them. For example:

myFile1.xml

myFile1.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<data>
    <title>Title1</title>
    <description>Description1</description>
    <myid>1</myid>
</data>
<data>
    <title>Title2</title>
    <description>Description2</description>
    <myid>2</myid>
</data>
</catalog>

myFile2.xml

myFile2.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<data>
    <title>Title1</title>
    <description>Description1</description>
    <author>Author1</author>
    <date>12/34/5678</date>
    <myid>1</myid>
</data>
<data>
    <author>Author2</author>
    <date>87/65/4321</date>
    <myid>2</myid>
</data>
</catalog>

所需的输出:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<data>
    <title>Title1</title>
    <description>Description1</description>
    <myid>1</myid>
    <author>Author1</author>
    <date>12/34/5678</date>
</data>
<data>
    <title>Title2</title>
    <description>Description2</description>
    <myid>2</myid>
    <author>Author2</author>
    <date>87/65/4321</date>
</data>
</catalog>

我有一个代码,但是没有按照要求的输出执行.

I have a code but, it doesnot perform as per the required output.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:variable name="compare" select="'myFile1.xml'"/>
<xsl:variable name="with" select="'myFile2.xml'"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:variable name="info1" select="document($compare)/catalog/data[myid=current()/myid]/."/>
        <xsl:variable name="info2" select="document($with)/catalog/data[myid=current()/myid]/."/>
        <xsl:for-each select="$info1/*">
            <xsl:variable name="check1" select="name(current())"/>
            <!--xsl:text>Current node1 : </xsl:text><xsl:value-of select="$check1"/-->
            <xsl:for-each select="$info2/*">
                <xsl:variable name="check2" select="name(current())"/>
                <!--xsl:text>Current node2 : </xsl:text><xsl:value-of select="$check2"/-->
                <xsl:if test="$check1!=$check2">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
</xsl:transform>

请帮助!

推荐答案

此解决方案完全没有循环或键.我只使用document()加载了一个文档,而我使用了另一个文档作为源.简而言之,源文档中缺少一个元素,该元素将在已加载的元素上使用.此解决方案是您使用较少的更多元素的解决方案.请参阅底部以了解更一般的内容.

This solution is totally free of loops or keys. I've loaded only one document using document(), while I use the other one as source. Briefly, an element missing in the source document, it is taken on the loaded one. More elements you have less usable is this solution. See bottom for a more general one.

XSLT 1.0 已在 Saxon-HE 9.2.1.1J

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="catalog2" select="document('source_test2.xml')/catalog"/>

    <xsl:template match="catalog">
        <catalog>
            <xsl:apply-templates select="data"/>
        </catalog>
    </xsl:template>

    <xsl:template match="data">
        <xsl:variable name="data2" select="$catalog2/data[myid=current()/myid]/."/>
        <data>
            <xsl:choose>
                <xsl:when test="title">
                    <xsl:copy-of select="title"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$data2/title"/>
                </xsl:otherwise>
            </xsl:choose>

            <xsl:choose>
                <xsl:when test="description">
                    <xsl:copy-of select="description"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$data2/description"/>
                </xsl:otherwise>
            </xsl:choose>

            <xsl:copy-of select="myid"/>

            <xsl:choose>
                <xsl:when test="author">
                    <xsl:copy-of select="author"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$data2/author"/>
                </xsl:otherwise>
            </xsl:choose>

            <xsl:choose>
                <xsl:when test="date">
                    <xsl:copy-of select="date"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$data2/date"/>
                </xsl:otherwise>
            </xsl:choose>

        </data>
    </xsl:template>

</xsl:stylesheet>


这里遵循一个更通用的解决方案.方法是相同的.对于每个data,将myFile2中存在但myFile1中缺少的元素添加到结果树中,反之亦然.


Here follows a more general solution. The approach is the same. For each data, an element present in myFile2 and missing in myFile1 is added to the result tree, and vice-versa.

XSLT 1.0 已在 Saxon-B 9.0.0.4J

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="catalog2" select="document('myFile2.xml')/catalog"/>

    <xsl:template match="catalog">
        <catalog>
            <xsl:apply-templates select="data"/>
        </catalog>
    </xsl:template>

    <xsl:template match="data">
        <xsl:variable name="data1" select="."/>
        <xsl:variable name="data2" select="$catalog2/data[myid=current()/myid]/."/>
        <data>
            <xsl:copy-of select="$data1/*"/>
            <xsl:for-each select="$data2/*">
                <xsl:variable name="element2" select="name(.)"/>
                <xsl:if test="count($data1/*[name()=$element2])=0">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </data>
    </xsl:template>

</xsl:stylesheet>

这篇关于如何使用xslt比较和合并两个xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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