0

I have two nodes with unknown structure, for example:

<EXAMPLE1>
    <A>text a</A>
    <B>
        <C>text c</C>
        <D>
            <E>text e</E>
            <A>text second A</A>
        </D>
    </B>
</EXAMPLE1>

<EXAMPLE2>
    <A>text a</A>
    <B>
        <C_DIFFERENT>text c</C_DIFFERENT>
        <D>
            <E>DIFFERENT TEXT</E>
            <A>text second A</A>
        </D>
    </B>
</EXAMPLE2>

They may be complete equals (having just different base names), but may be different (having another value or different set of tags).

And task is to check, are they equials or not.

I created this:

            <xsl:for-each select="//EXAMPLE1//*[not(*)]">
                <xsl:if test="not(//EXAMPLE2//*[not(*) and name() = name(current())]) or //EXAMPLE2//*[not(*) and name() = name(current())]/text() != current()/text()">

                <!-- PRINT MY STUFF --> 

                </xsl:if>
            </xsl:for-each>
            <xsl:for-each select="//EXAMPLE2//*[not(*)]">
                <xsl:if test="not(//EXAMPLE1//*[not(*) and name() = name(current())]) or //EXAMPLE1//*[not(*) and name() = name(current())]/text() != current()/text()">

                <!-- PRINT MY STUFF --> 

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

(I'm trying to move over all leaf nodes of EXAMPLE1, search same at EXAMPLE2 and check if same exist and if it has same value, and then do same for EXAMPLE2 and EXAMPLE1)

But it doesn't works, because, for example, there is node named that appears twice, and I can't compare first with first and second with second. And I have to "print my stuff" just if they are equials.

Also it looks like bicycle, and I hope XSLT 1.0 has something more useful and simple, then such constructions.

Please, help me find it.

Arkady
  • 2,084
  • 3
  • 27
  • 48
  • 1
    XSLT has not been made for this. This is not at all a trivial task and you will have an *extremely* hard time implementing such functionality in XSLT, and with very little benefit. Use a tool that has been made for the job. See the answers on this thread: http://stackoverflow.com/questions/2924350/comparing-xmldocument-for-equality-content-wise – Tomalak Oct 01 '14 at 16:58

1 Answers1

0

Calculate a hash for the first subtree (the simplest would probably be just to concatenate all element names and values and attribute name and values in the order you visit). Then using the same template calculate a hash for the second subtree and compare the hash values. (Note that it is not 100% correct since as per Xml rules the order of the attributes does not matter while in this case the hash will be different but I think it should be OK in your scenario)

Pawel
  • 31,342
  • 4
  • 73
  • 104
  • I also think about that way, but now have problem with implementing that. I tried tamplate that calls itself for that, with no success. – Arkady Oct 01 '14 at 17:02
  • 1
    Just don't do it within XSLT. Really. (Comparing the source code of two XML files is a very bad idea. Don't go there.) – Tomalak Oct 01 '14 at 17:04