[XOM-interest] Comparing two semantically equal XML files
Marcelo Schneider
marcelo.schneider at digitro.com.br
Fri Oct 10 10:43:28 EDT 2008
Just found a small bug in my test case - now different attribute order
and different <a /> / <a></a> pass. My fault.
Order element isn't really significant in my use-case, anyway I just
sorted them alphabetically before sending assuring they are always in
the same order - not really the best solution, but probably the fastest
one.
About whitespace, just found
http://lists.ibiblio.org/pipermail/xom-interest/2005-December/002760.html
and NormalizingFactory (on the examples). These appear to work just as
expected, so I (hopefully) won't need something as robust as Saxonica.
Thanks a lot for the help!
--
Marcelo Frantz Schneider
SIC - TCO - Tecnologia em Engenharia do Conhecimento DÍGITRO
TECNOLOGIA
Michael Kay escreveu:
> Your XML examples aren't well-formed, which makes it difficult to help you
> as we can't run your test cases.
>
> Whitespace is significant: if you want to eliminate it from the comparison,
> you'll have to do this by hand (e.g. by a transformation applied before
> canonicalizing).
>
> Attribute order, and the difference between <a></a> and <a/>, should not be
> significant.
>
> Order of elements is significant.
>
> You might like to look at the saxon:deep-equal() function as an alternative
> approach.
>
> http://www.saxonica.com/documentation/extensions/functions/deepequal.html
>
> It takes a set of flags to define what features of the XML you consider
> significant or insignificant, and there is also an option to get
> explanations if the documents are deemed non-equal (though that doesn't seem
> to be needed for your application). It can be called from XPath and is also
> available directly from Java as the static method
> net.sf.saxon.functions.DeepEqual.deepEquals().
>
> Michael Kay
> http://www.saxonica.com/
>
>
>> -----Original Message-----
>> From: xom-interest-bounces at lists.ibiblio.org
>> [mailto:xom-interest-bounces at lists.ibiblio.org] On Behalf Of
>> Marcelo Schneider
>> Sent: 10 October 2008 12:59
>> To: xom-interest at lists.ibiblio.org
>> Subject: [XOM-interest] Comparing two semantically equal XML files
>>
>> Good morning list.
>>
>> In our company we currently store some graphs in XML form:
>> the client can create a graph in a Java applet, that is sent
>> to the server and stored as a file. Currently, all the graph
>> <-> XML transformation is done using JDom.
>>
>> What I need to do is, when a user opens an already existing
>> graph and tries to save it, compare the new XML sent with the
>> previously existing XML in the server, saving only if they
>> are different (so changing dates are correct).
>>
>> Googling around for a while I found out that a possible way
>> to do that without worrying with XML structure was to use
>> Canonicalization, and that XOM could possibly help me with
>> that. Is this assumption correct?
>> Here is what I'm currently doing:
>>
>> | public boolean areSemanticallyEqual(String oldXML, String newXML) {
>> | Builder builder = new Builder();
>> | Document oldDoc = builder.build(oldXML);
>> | Document newDoc = builder.build(newXML);
>> | // here canonicalize the old xml
>> | ByteArrayOutputStream oldStream = new ByteArrayOutputStream();
>> | Canonicalizer outputter = new Canonicalizer(oldStream , false);
>> | outputter.write(oldDoc);
>> | // here canonicalize the new xml
>> | ByteArrayOutputStream newStream = new ByteArrayOutputStream();
>> | Canonicalizer outputter = new Canonicalizer(newStream, false);
>> | outputter.write(newDoc);
>> | return Arrays.equals(oldStream.toByteArray(),
>> newStream.toByteArray());
>> | }
>>
>> However, the above code returns false when I compare XMLs
>> like these (they all start with "<?xml version="1.0"
>> encoding="UTF-8"?>"):
>>
>> xml1: "<tag1><tag2 id="1" name="a">Some
>> Text</tag2></tag1><other tag />"
>> xml2: "<tag1>\n\t<tag2 id="1" name="a">Some
>> Text</tag2>\r</tag1><other tag />" (with new lines, tabs and
>> carriage returns)
>> xml3: "<tag1><tag2 id="1" name="a">Some Text</tag2></tag1><other
>> tag></other tag>" (other valid xml representation)
>> xml4: "<tag1><tag2 name="a" id="1">Some
>> Text</tag2></tag1><other tag />"
>> (attributes order reversed)
>> xml5: "<other tag /><tag1><tag2 id="1" name="a">Some
>> Text</tag2></tag1>"
>> (yet another thing that looks the same, but I'm probably
>> wrong assuming this should count as equal)
>>
>> I would expect the return to be always true, except for
>> (maybe?) xml1 > xml5. Is this correct? Any hints are highly
>> appreciated (:
>>
>> --
>> Marcelo Frantz Schneider
>> SIC - TCO - Tecnologia em Engenharia do Conhecimento DÍGITRO
>> TECNOLOGIA
>>
>>
>>
>> --
>> Esta mensagem foi verificada pelo sistema de antivírus da Dígitro e
>> acredita-se estar livre de perigo.
>>
>> _______________________________________________
>> XOM-interest mailing list
>> XOM-interest at lists.ibiblio.org
>> http://lists.ibiblio.org/mailman/listinfo/xom-interest
--
Esta mensagem foi verificada pelo sistema de antivírus da Dígitro e
acredita-se estar livre de perigo.
More information about the XOM-interest
mailing list