Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 27th, 2009, 08:10 AM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default compare 2 xml files

Hi,

I'm trying to compare 2 xml files and if they are different want to output the nodenames and values where they differ.
I managed to compare then using the deep-equal() function but i don't know how to output the differences. Please help
 
Old March 27th, 2009, 08:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Saxon has a variant of the deep-equal() function that outputs such information (though not necessarily in the format you want). See

http://www.saxonica.com/documentatio...deepequal.html
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 27th, 2009, 12:24 PM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks Micheal,
I don't quite understand the function ( what does collation means here )
Could you please give an example
 
Old March 29th, 2009, 08:34 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The collation is specified in terms of a URI where the XSLT processor you use defines how to URI looks exactly. For Saxon 9 you can find an explanation here: http://www.saxonica.com/documentatio...collation.html
However you can simply use (), the empty sequence for that argument and the default collation is used which I think is the Unicode codepoint collation.
To give you an example using saxon:deep-equals, assume the XML input is as follows:
Code:
<root>
  <foo>bar</foo>
  <foo>BAR</foo>
</root>
and we want to compare root/foo[1] and root/foo[2] according to the English language as used in the US, one time case-sensitive, one time case-insensitive, then we can use
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:saxon="http://saxon.sf.net/"
  exclude-result-prefixes="xs saxon">
  
  <xsl:output method="text"/>
  
  <xsl:template match="/">
    <xsl:value-of select="
      saxon:deep-equal(root/foo[1], root/foo[2], 'http://saxon.sf.net/collation?lang=en-US', '?'),
      saxon:deep-equal(root/foo[1], root/foo[2], 'http://saxon.sf.net/collation?lang=en-US;ignore-case=yes', '?')"
      separator="
"/>
  </xsl:template>

</xsl:stylesheet>
The output (tested with Saxon 9.1.0.5 Java) is as follows:

Warning: deep-equal(): text() values differ ("/root/foo[1]/text()[1], /root/foo[2]/text()[1]: bar", "BAR")
Warning: deep-equal(): nodes at position 1 differ
false
true
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 30th, 2009, 06:17 AM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks for the information Martin

I want to do the comparison in datapower and it doesn not suppory saxon extensions. Are there any other ways to do is without using extensions ?
 
Old March 30th, 2009, 07:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Datapower doesn't even support XSLT 2.0 deep-equal()!

You've got various options:

* write it yourself

* get a product like DeltaXML (but Saxon is cheaper)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 30th, 2009, 07:26 AM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default

My only option is to write my own code
Could you please give me some idea on where to start..
thanks
 
Old March 30th, 2009, 06:38 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>My only option is to write my own code
>Could you please give me some idea on where to start..

Try writing a recursive template diag-deep-equal() as follows:

compare two nodes using deep-equal().

if equal, exit with result true.

if not equal, examine the children. If the number of children of the two nodes is different, output an xsl:message to say so and return false. Otherwise compare the child nodes one-by-one, using a recursive call on your function diag-deep-equal(). When you hit the first pair that are not equal (the function returns false), output an xsl:message to say which elements you found, and return false.

Use a supporting function to output the path to a node to construct diagnostics, which you can obtain by walking up the ancestor axis.

[I know that this is well beyond the level of anything you've written before. Please don't come back asking me to implement it for you. You asked for advice on where to start, and I've given you that. Now it's your job to finish.]
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 31st, 2009, 04:04 AM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks for the suggestion





Similar Threads
Thread Thread Starter Forum Replies Last Post
compare 2 files and output third files mtnguye6 VBScript 0 April 14th, 2008 11:03 AM
Source on Java to compare XML files like ExamXML alapick XML 1 July 28th, 2006 04:05 AM
Compare two xml files using xslt sudha XSLT 0 March 10th, 2006 01:04 AM
Is there any tool available to compare 2 xml files parekh_bhakti ASP.NET 1.0 and 1.1 Professional 1 October 28th, 2005 12:59 PM
compare two xml files attributes, elements k_s_ashok XML 1 June 9th, 2004 02:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.