XSLT: How to compare two xml files and how to get distinct value from it

走远了吗. 提交于 2020-01-17 04:13:11

问题


I have two xml files, i just to get distinct chapter and that needs to displayed on the output file.

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

and

Another XML[using doc function to import inside the 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>

Now, here, the distinct values is <chunk>13 Example of distint chaptrr fruits; miscellaneous grains</chunk>.

How can i get this through XSLT code, any sample xslt code please?

This is my code, It does not work as of now.

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


I believe this will do what you asked for:

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>

Result:

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

To make this bi-directional - i.e to return what is in essence the difference between the two sets - try:

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>


来源:https://stackoverflow.com/questions/28628901/xslt-how-to-compare-two-xml-files-and-how-to-get-distinct-value-from-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!