locked
How to compare 2 XML using XSLT RRS feed

  • Question

  • User-893002196 posted

    Hi All,

    Anyone have idea how to implement comparison between 2 XML using XSLT? Can share the link or sample skeleton please?

     

    Thank you.

     

    Regards,
    Micheale

    Sunday, March 31, 2013 11:36 PM

Answers

  • User1630798415 posted

    have you gone through this URL? if not please check, this is related to your query

    http://www.example8.com/category/view/id/16760

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 1, 2013 12:08 AM
  • User1630798415 posted

    another way, you can import XML into datatable and then compare it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 3, 2013 3:58 AM
  • User-782344923 posted

    comparison between 2 XML using XSLT? Can share the link or sample skeleton please?

    Hi micnie,

    Visit here to fix it

    http://stackoverflow.com/questions/6047505/how-to-compare-and-merge-two-xml-using-xslt

    Cheers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 3, 2013 4:24 AM

All replies

  • User1630798415 posted

    have you gone through this URL? if not please check, this is related to your query

    http://www.example8.com/category/view/id/16760

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 1, 2013 12:08 AM
  • User-893002196 posted

    Hi,

     

    My problem if i have 2 XML file, and need comparison using .xslt. It's possible to be done? And can anyone share with me the link?

    I have done it using C# LINQ method, but my boss asking me to used .XSLT. I am cracking my head. Looking around in internet for a week plus but still can't find any resources.

     

    Example

    a.xml

    <data>
        <opp>
                 <RefID>1<RefID>
                 <name>Test</name>
         </opp>
        <opp>             
                   <RefID>2<RefID>              
                   <name>Test2</name>
         </opp>
        <opp>                            
                      <RefID>3<RefID>                              
                      <name>Test3</name>     
        </opp>
    </data>

     

    b.xml

    <data>    
    <opp>             
         <RefID>1<RefID>             
         <name>Testing 01</name>     
    </opp>   
    <opp>                            
          <RefID>2<RefID>                              
          <name>Test2</name>     
    </opp> 
    <opp>                            
           <RefID>4<RefID>                              
            <name>Testing 04</name>     
    </opp>    
    <opp>                                              
            <RefID>5<RefID>                                                
            <name>Testing 05</name>         
    </opp>
    </data>

     

     

    Output Should Returned using .xslt compare:-
    <data>     
    <opp>             
              <RefID>1<RefID>              
              <name>Test | Testing 01</name>
              <remark>Modified</remark>    
    </opp>     
    <opp>                                              
                <RefID>3<RefID>                                                
                <Remark>Deleted</Remark>         
    </opp>
    <opp>                                    
                 <RefID>4<RefID>                                       
                 <Remark>New Inserted</Remark>     
    </opp>    
    <opp>                                                      
                 <RefID>5<RefID>                                                        
                 <Remark>New Inserted</Remark>        
    </opp>
    </data>

    Please advise.

     

    Thank you.

     

    Regards,

    Micheale

    Monday, April 1, 2013 2:28 AM
  • User-893002196 posted

    Hi All,

     

    I get the sample of .xsl file for comparison between 2 xml:-

    1.   3.xsl

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:exslt="http://exslt.org/common" version="1.0">
    
      <!-- Replace // with / everywhere if we're only interested
       	in immediate children of /RootElement. -->
    
      <xsl:variable name="docA" select="document('3a.xml')" />
      <xsl:variable name="docB" select="document('3b.xml')"/>
    
      <!-- This produces a whole nother copy of both docs!
           So, is the performance cost worth it?? -->
    
      <xsl:variable name="sortedNodesA">
        <!-- produce a sorted, flattened RTF of A's nodes -->
        <xsl:for-each select="$docA/Root/Element//*">
          <xsl:sort select="name()" />
          <xsl:copy-of select="." />
        </xsl:for-each>
      </xsl:variable>
    
      <xsl:variable name="sortedNodesB">
        <!-- produce a sorted, flattened RTF of B's nodes -->
        <xsl:for-each select="$docB/Root/Element//*">
          <xsl:sort select="name()" />
          <xsl:copy-of select="." />
        </xsl:for-each>
      </xsl:variable>
    
      <xsl:template match="/">
        <xsl:call-template name="recurse">
          <xsl:with-param name="nodesA"
            select="exslt:node-set($sortedNodesA)/*" />
          <xsl:with-param name="nodesB"
            select="exslt:node-set($sortedNodesB)/*" />
        </xsl:call-template>
      </xsl:template>
     
      <xsl:template name="recurse">
        <xsl:param name="nodesA" />
        <xsl:param name="nodesB" />
        <xsl:if test="$nodesA | $nodesB">
          <xsl:variable name="nameA" select="name($nodesA[1])" />
          <xsl:variable name="nameB" select="name($nodesB[1])" />
          <xsl:variable name="compar">
            <xsl:call-template name="compare-names">
              <xsl:with-param name="a" select="$nodesA[1]" />
              <xsl:with-param name="b" select="$nodesB[1]" />
            </xsl:call-template>
          </xsl:variable>
    
          <xsl:choose>
            <xsl:when test="0 > $compar"> <!-- $nodesA[1] is alph. first -->
              <p><xsl:value-of select="$nameA" /> is only in doc A.</p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA[position()>1]" />
                <xsl:with-param name="nodesB" select="$nodesB" />
              </xsl:call-template>
            </xsl:when>
    
            <xsl:when test="$compar > 0"> <!-- $nodesB[1] is alph. first -->
              <p><xsl:value-of select="$nameB" /> is only in doc B.</p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA" />
                <xsl:with-param name="nodesB" select="$nodesB[position()>1]" />
              </xsl:call-template>
            </xsl:when>
    
            <xsl:otherwise>
              <p><xsl:value-of select="$nameB" /> is in both documents.
                <!-- Do I need string(text(...))? -->
                <xsl:if
                  test="string($nodesA[1]/text()) != string($nodesB[1]/text())">
                  But their contents differ:
                  '<xsl:value-of select="$nodesA[1]/text()" />' !=
                  '<xsl:value-of select="$nodesB[1]/text()" />'.
                </xsl:if>
              </p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA[position()>1]" />
                <xsl:with-param name="nodesB" select="$nodesB[position()>1]" />
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:template>
    
      <xsl:template name="compare-names">
        <!-- Output -1, 0, or 1 as name of node A sorts before, equal to,
             or after name of node B. -->
        <xsl:param name="a" />
        <xsl:param name="b" />
        <xsl:choose>
          <xsl:when test="name($a) = name($b)"> 0 </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="$a|$b">
              <xsl:sort select="name()" />
              <xsl:if test="position() = 1">
                <xsl:choose>
                  <xsl:when test="name(.) = name($a)"> -1 </xsl:when>
                  <xsl:otherwise> 1 </xsl:otherwise>
                </xsl:choose>
              </xsl:if>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    </xsl:stylesheet>

    But,

    How can i display the result??

     

    I try to do:-

    2.   3a.xml

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type='text/xsl' href='2.xsl'?>
    <Root>
    <Element>
          <Element2 name="Attribute1">
                <Element3>
                      <Element4>Smittys</Element4>
    				  <Element4>John</Element4>
                </Element3>
          </Element2>
          <Element2 name="Attribute2">
                <Element3>
                      <Element4>Cleveland Clinic</Element4>
                      <Element4>Parma Community</Element4>
                </Element3>
          </Element2>
    </Element>
    <Element>
          <Element2 name="Attribute2">
                <Element3>
                      <Element4>Cleveland Clinic</Element4>
                      <Element4>Berea Community</Element4>
                </Element3>
          </Element2> 
    	  <Element2 name="Attribute3">
                <Element3>
                      <Element4>Joel</Element4>
                </Element3>
          </Element2>
          
    </Element>
    </Root>
    

    3.    3b.xml

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type='text/xsl' href='2.xsl'?>
    <Root>
    <Element>
          <Element2 name="Attribute1">
                <Element3>
                      <Element4>Smittys</Element4>
    				  <Element4>John</Element4>
                </Element3>
          </Element2>
          <Element2 name="Attribute2">
                <Element3>
                      <Element4>Cleveland Clinic</Element4>
                      <Element4>Parma Community</Element4>
                </Element3>
          </Element2>
    </Element>
    <Element>
          <Element2 name="Attribute1">
                <Element3>
                      <Element4>Dopplemeyer</Element4>
                </Element3>
          </Element2>
          <Element2 name="Attribute2">
                <Element3>
                      <Element4>Cleveland Clinic</Element4>
                      <Element4>Berea Community</Element4>
                </Element3>
          </Element2> 
    </Element>
    </Root>
    

    Page to be running in IE:-

    4.   test.xml

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type='text/xsl' href='3.xsl'?>


    The code above didn't return me any result. Can someone tell me what is wrong above code?

    Or

     Refer: http://www.factsandpeople.com/facts-mainmenu-5/20-xslt/59-difference-file-between-two-xml-files

    I try above method but still can't get it run in .html

    Please advise.

     

    Thank you.

     

    Regards,

    Micheale




     

    Tuesday, April 2, 2013 12:31 AM
  • User1630798415 posted

    another way, you can import XML into datatable and then compare it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 3, 2013 3:58 AM
  • User-782344923 posted

    comparison between 2 XML using XSLT? Can share the link or sample skeleton please?

    Hi micnie,

    Visit here to fix it

    http://stackoverflow.com/questions/6047505/how-to-compare-and-merge-two-xml-using-xslt

    Cheers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 3, 2013 4:24 AM