XSLT:如何比较两个XML文件,以及如何从它那里得到不同的值(XSLT: How to compa

2019-11-03 15:53发布

我有两个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 [使用文档函数导入的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> <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>   

Answer 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>


文章来源: XSLT: How to compare two xml files and how to get distinct value from it
标签: xml xslt