Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 07:10 AM
Frank
Guest
 
Posts: n/a
Default Using XSL to compare two XML files - Need help please!

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>1@1.1</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>2@2.2</email>
</client>
</clients>
<clients cat="prvt">
<client>
<number>0076</number>
<lastname>Lavigne</lastname>
<frstname>Avril</frstname>
<email>3@3.3</email>
</client>
<client>
<number>0055</number>
<lastname>Donnely</lastname>
<frstname>Al</frstname>
<email>4@4.4</email>
</client>
</clients>
</root>

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

<root>
<clients cat="corp">
<client>
<number>0098</number>
<lastname>Smith</lastname>
<frstname>John</frstname>
<email>1@1.1</email>
</client>
</clients>
<clients cat="prvt">
<client>
<number>0076</number>
<lastname>Lavigne</lastname>
<frstname>Avril</frstname>
<email>3@3.3</email>
</client>
<client>
<number>0055</number>
<lastname>Donnely</lastname>
<frstname>Al</frstname>
<email>4@4.4</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>2@2.2</email>
</client>
</clients>
</root>

THANKS!
  #2  
Old July 20th, 2005, 07:10 AM
Dimitre Novatchev
Guest
 
Posts: n/a
Default Re: Using XSL to compare two XML files - Need help please!

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>1@1.1</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>2@2.2</email>
</client>
</clients>
<clients cat="prvt">
<client>
<number>0076</number>
<lastname>Lavigne</lastname>
<frstname>Avril</frstname>
<email>3@3.3</email>
</client>
<client>
<number>0055</number>
<lastname>Donnely</lastname>
<frstname>Al</frstname>
<email>4@4.4</email>
</client>
</clients>
</root>
</file1>
<file2>
<root>
<clients cat="corp">
<client>
<number>0098</number>
<lastname>Smith</lastname>
<frstname>John</frstname>
<email>1@1.1</email>
</client>
</clients>
<clients cat="prvt">
<client>
<number>0076</number>
<lastname>Lavigne</lastname>
<frstname>Avril</frstname>
<email>3@3.3</email>
</client>
<client>
<number>0055</number>
<lastname>Donnely</lastname>
<frstname>Al</frstname>
<email>4@4.4</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>2@2.2</email>
</client>
</clients>
</root>
</file2>
</files>


the wanted result is produced:

<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>2@2.2</email>
</client>
<client>
<number>0067</number>
<lastname>Carter</lastname>
<frstname>Gary</frstname>
<email>2@2.2</email>
</client>


=====
Cheers,

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

"Frank" <francoisplourde@yahoo.com> wrote in message
news:236d7175.0309111653.312e6bab@posting.google.c om...[color=blue]
> 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[/color]
able[color=blue]
> 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>1@1.1</email>
> </client>
> <client>
> <number>0067</number>
> <lastname>Carter</lastname>
> <frstname>Gary</frstname>
> <email>2@2.2</email>
> </client>
> </clients>
> <clients cat="prvt">
> <client>
> <number>0076</number>
> <lastname>Lavigne</lastname>
> <frstname>Avril</frstname>
> <email>3@3.3</email>
> </client>
> <client>
> <number>0055</number>
> <lastname>Donnely</lastname>
> <frstname>Al</frstname>
> <email>4@4.4</email>
> </client>
> </clients>
> </root>
>
> file2.xml:
> -----------
>
> <root>
> <clients cat="corp">
> <client>
> <number>0098</number>
> <lastname>Smith</lastname>
> <frstname>John</frstname>
> <email>1@1.1</email>
> </client>
> </clients>
> <clients cat="prvt">
> <client>
> <number>0076</number>
> <lastname>Lavigne</lastname>
> <frstname>Avril</frstname>
> <email>3@3.3</email>
> </client>
> <client>
> <number>0055</number>
> <lastname>Donnely</lastname>
> <frstname>Al</frstname>
> <email>4@4.4</email>
> </client>
> <client>
> <number>0067</number>
> <lastname>Carter</lastname>
> <frstname>Gary</frstname>
> <email>2@2.2</email>
> </client>
> </clients>
> </root>
>
> THANKS![/color]


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles