Using XSL to compare two XML files - Need help please!

F

Frank

Hi, is there a way to use XSL to compare two XML files to verify if a
"record"
in an XML file has changed of parent in another XML file ?

I am trying to implement a template in an XSL stylesheet that would be able
to compare the two files below
and indicate if a client has changed of category (ex.: from "corp" to
"prvt").

Any help would be gladly appreciated, I am desparatly trying to find a
solution to this.

In the above example, client number 0067 has changed
from "corp" (in file1.xml) to "prvt" (in file2.xml).

file1.xml:
-----------

<root>
<clients cat="corp">
<client>
<number>0098</number>
<lastname>Smith</lastname>
<frstname>John</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>[email protected]</email>
</client>
</clients>
<clients cat="prvt">
<client>
<number>0076</number>
<lastname>Lavigne</lastname>
<frstname>Avril</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0055</number>
<lastname>Donnely</lastname>
<frstname>Al</frstname>
<email>[email protected]</email>
</client>
</clients>
</root>

file2.xml:
-----------

<root>
<clients cat="corp">
<client>
<number>0098</number>
<lastname>Smith</lastname>
<frstname>John</frstname>
<email>[email protected]</email>
</client>
</clients>
<clients cat="prvt">
<client>
<number>0076</number>
<lastname>Lavigne</lastname>
<frstname>Avril</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0055</number>
<lastname>Donnely</lastname>
<frstname>Al</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>[email protected]</email>
</client>
</clients>
</root>

THANKS!
 
D

Dimitre Novatchev

A simple way to do this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="/*/file1/*/*/client">
<xsl:if test="not(../@cat = /*/file2/*/*/client[number =
current()/number]/../@cat)">
<xsl:copy-of select=". | /*/file2/*/*/client[number =
current()/number]" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

when this transformation is applied on this source.xml (I combined the two
files in one for convenience -- you'll have to use document()):

<files>
<file1>
<root>
<clients cat="corp">
<client>
<number>0098</number>
<lastname>Smith</lastname>
<frstname>John</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>[email protected]</email>
</client>
</clients>
<clients cat="prvt">
<client>
<number>0076</number>
<lastname>Lavigne</lastname>
<frstname>Avril</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0055</number>
<lastname>Donnely</lastname>
<frstname>Al</frstname>
<email>[email protected]</email>
</client>
</clients>
</root>
</file1>
<file2>
<root>
<clients cat="corp">
<client>
<number>0098</number>
<lastname>Smith</lastname>
<frstname>John</frstname>
<email>[email protected]</email>
</client>
</clients>
<clients cat="prvt">
<client>
<number>0076</number>
<lastname>Lavigne</lastname>
<frstname>Avril</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0055</number>
<lastname>Donnely</lastname>
<frstname>Al</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>[email protected]</email>
</client>
</clients>
</root>
</file2>
</files>


the wanted result is produced:

<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>[email protected]</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>[email protected]</email>
</client>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top