This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: XML file compare problem


Hi Kumar,

> The problem I am facing is the xalan difference function is not
> giving correct result. For testing purpose I passed as parameter the
> same file as the input file to the stylesheet i.e. I compared
> "FileA" with "FileA" but still none of the nodes were matching
> between two instances of the same document source.

When an XML file is read in to an XSLT processor, each node in the
tree that it creates is given a unique IDs. The xalan:difference()
and xalan:intersection() functions compare the unique addresses of
nodes to tell if the nodes themselves are the same node.  (Other
node-set comparison functions and techniques work in the same way.)

So if you read in two separate documents, and use xalan:intersection()
with any node sets on them, you will never get any nodes returned
because none of the nodes in the first document are considered the
same as any of the nodes in the second document - they are given
different unique IDs.  The same holds when you compare the document
passed as the source of the transformation and a document you access
through the document() function - the XSLT processor has no way of
knowing that these are the same document, so it addresses the nodes
differently (I think).

If you try:

<xsl:variable name="file1"
  select="document('file:///ms/user/s/shailend/playingzone/xml-java/compare1.xml',/)/*" />
<xsl:variable name="file2"
  select="document('file:///ms/user/s/shailend/playingzone/xml-java/compare1.xml',/)/*" />

instead, then you *will* get some intersection because the nodes from
the two document() calls *will* get given the same unique IDs.

So you can't use those functions for the kind of comparison that you
want to do, between nodes in different documents. Probably you want to
compare the value of the col with a particular Index in a particular
row in one file with the value of the col with the same Index in the
same positioned row in the other file. To get you on the right track
with that, have a look at the following:

  <xsl:variable name="file2"
                select="document('compare1.xml')/MSCIData" />
  <xsl:for-each select="/MSCIData/row">
     <xsl:variable name="pos" select="position()" />
     <xsl:for-each select="col">
        <xsl:variable name="ind" select="@Index" />
        <xsl:variable name="col1" select="." />
        <xsl:variable name="value1" select="$col1/@Value" />
        <xsl:variable name="col2"
                      select="$file2/row[$pos]/col[@Index = $ind]" />
        <xsl:variable name="value2" select="$col2/@Value" />
        <xsl:choose>
           <xsl:when test="$value1 = $value2">
              Same: "<xsl:copy-of select="$col1" />"
           </xsl:when>
           <xsl:otherwise>
              Difference: "<xsl:copy-of select="$col2" />" vs.
                          "<xsl:copy-of select="$col2" />"
           </xsl:otherwise>
        </xsl:choose>
     </xsl:for-each>
  </xsl:for-each>

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]