XSLT:如何比较两个xml文件以及如何从中获取不同的值

时间:2015-02-20 12:21:44

标签: xml xslt

我有两个xml文件,我只想获得不同的章节,并且需要在输出文件中显示。

源XML

<xml>
<chapter>1 Live animals.</chapter>
<chapter>2 Meat and edible meat offal.</chapter>
<chapter>3 Fish and crustaceans, molluscs and other aquatic invertebrates.</chapter>
<chapter>4 Dairy produce; birds eggs; natural honey; edible products of animal origin, not elsewhere specified or included.</chapter>
<chapter>5 Products of animal origin, not elsewhere specified or included.</chapter>
<chapter>6 Live trees and other plants; bulbs, roots and the like; cut flowers and ornamental foliage.</chapter>
<chapter>7 Edible vegetables and certain roots and tubers.</chapter>
<chapter>8 Edible fruit and nuts; peel of citrus fruit or melons.</chapter>
<chapter>9 Coffee, tea mat, and spices.</chapter>
<chapter>10 Cereals.</chapter>
<chapter>11 Products of the milling industry; malt; starches; inulin; wheat gluten.</chapter>
<chapter>12 Oil seeds and oleaginous fruits; miscellaneous grains, seeds and fruit; industrial or medicinal plants; straw and fodder.</chapter>
</xml>

另一个XML [使用doc函数在xslt中导入]

<main>
<chunk>1 Live animals.</chunk>
<chunk>2 Meat and edible meat offal.</chunk>
<chunk>3 Fish and crustaceans, molluscs and other aquatic invertebrates.</chunk>
<chunk>4 Dairy produce; birds eggs; natural honey; edible products of animal origin, not elsewhere specified or included.</chunk>
<chunk>5 Products of animal origin, not elsewhere specified or included.</chunk>
<chunk>6 Live trees and other plants; bulbs, roots and the like; cut flowers and ornamental foliage.</chunk>
<chunk>7 Edible vegetables and certain roots and tubers.</chunk>
<chunk>8 Edible fruit and nuts; peel of citrus fruit or melons.</chunk>
<chunk>9 Coffee, tea mat, and spices.</chunk>
<chunk>10 Cereals.</chunk>
<chunk>11 Products of the milling industry; malt; starches; inulin; wheat gluten.</chunk>
<chunk>12 Oil seeds and oleaginous fruits; miscellaneous grains, seeds and fruit; industrial or medicinal plants; straw and fodder.</chunk>
<chunk>13 Example of distint chaptrr fruits; miscellaneous grains</chunk>
</main>

现在,此处的不同值为<chunk>13 Example of distint chaptrr fruits; miscellaneous grains</chunk>

我如何通过XSLT代码获取此信息,请使用任何示例xslt代码?

这是我的代码,它现在不起作用。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml">
    <xsl:output method="xml" encoding="UTF-8"/>

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

    <xsl:for-each select="//chunk">
        <xsl:variable name="thisvalue" select="."/>
        <xsl:for-each select="document('Main2.xml')/xml/chapter">
                         <xsl:choose>
                     <xsl:when test=". = $thisvalue">

                     </xsl:when>
                     <xsl:otherwise>
                     <distint><xsl:value-of select="."/></distint>
                     </xsl:otherwise>
                     </xsl:choose>

        </xsl:for-each>
    </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>   

1 个答案:

答案 0 :(得分:1)

我相信这会做你所要求的:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="doc2" select="document('file2.xml')"/>

<xsl:template match="/">    
    <xsl:variable name="chapters" select="xml/chapter" />
    <root>
        <xsl:for-each select="$doc2/main/chunk[not(. = $chapters)]">
            <distinct><xsl:value-of select="."/></distinct>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

<强>结果

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <distinct>13 Example of distint chaptrr fruits; miscellaneous grains</distinct>
</root>

为了使这个双向 - 即返回两组之间的差异 - 尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="doc2" select="document('file2.xml')"/>

<xsl:template match="/">    
    <xsl:variable name="chapters" select="xml/chapter" />
    <xsl:variable name="chunks" select="$doc2/main/chunk"/>
    <root>
        <xsl:for-each select="$chunks[not(. = $chapters)] | $chapters[not(. = $chunks)]">
            <distinct><xsl:value-of select="."/></distinct>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>
相关问题