RE: [xsl] XSLT Comparison of Two Trees

Subject: RE: [xsl] XSLT Comparison of Two Trees
From: "Doug Franklin" <doug@xxxxxxxxxxxxxxxx>
Date: Tue, 08 Sep 2009 12:13:50 -0700
Mr. Honnen,

Thanks for your speedy reply.  I'm getting this error:

The XML page cannot be displayed

Cannot view XML input using XSL style sheet. Please correct the error
and then click the Refresh button, or try again later.


-----------------------------------------------------------------------------
---

The system cannot locate the resource specified.

Am I failing to pull both trees in?

Doug

-------- Original Message --------
Subject: Re: [xsl] XSLT Comparison of Two Trees
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Tue, September 08, 2009 2:35 pm
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx

Doug Franklin wrote:
> I wish to compare two (xml tree) documents, a previous document and it's
> current version. The previous tree looks like this: (the current tree
> appears below this previous tree, the xsl - below that.)
>
> If I could
>
> A. Create a report of zip codes that were added
> B. Create a report of zip codes that were deleted
> C. Create a report of zip codes that were moved
> D. Find the percentage difference of the <Total>####</Total>
> information, using this algorithm: (currentTotal -
> previousTotal)/previousTotal

Here is a stylesheet that outputs added, moved and deleted zip elements
for each city element:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">

<xsl:param name="pv" select="'previous.xml'"/>
<xsl:variable name="pdoc" select="document($pv)"/>
<xsl:variable name="cdoc" select="/"/>
<xsl:output indent="yes"/>

<xsl:template match="/">
<new-codes>
<xsl:for-each select="cFieldOfficeList/city">
<city name="{@name}">
<xsl:copy-of select="zip[not(@zipCode =
$pdoc/pFieldOfficeList/city/zip/@zipCode)]"/>
</city>
</xsl:for-each>
</new-codes>
<moved-codes>
<xsl:for-each select="cFieldOfficeList/city">
<city name="{@name}">
<xsl:copy-of select="zip[not(@zipCode = 'zipTotals') and
@zipCode = $pdoc/pFieldOfficeList/city[not(@name =
current()/@name)]/zip/@zipCode]"/>
</city>
</xsl:for-each>
</moved-codes>
<deleted-codes>
<xsl:for-each select="$pdoc/pFieldOfficeList/city">
<city name="{@name}">
<xsl:copy-of select="zip[not(@zipCode =
$cdoc/cFieldOfficeList/city/zip/@zipCode)]"/>
</city>
</xsl:for-each>
</deleted-codes>
</xsl:template>

</xsl:stylesheet>

If applied against the posted XML input (current version) as the primary

input document it outputs

<new-codes>
<city name="All areas"/>
<city name="Total, California"/>
<city name="Alhambra">
<zip zipCode="11111"><!-- This zip code was added to Alhambra -->
<Total>1000</Total>
</zip>
</city>
<city name="American River"/>
<city name="Anaheim"/>
<city name="Antioch">
<zip zipCode="22222"><!-- This zip code was added to Antioch -->
<Total>5790</Total>
</zip>
</city>
<city name="Auburn">
<zip zipCode="33333"><!-- This zip code was added to Auburn -->
<Total>5790</Total>
</zip>
</city>
<city name="Bakersfield"/>
<city name="Bakersfield, East Hills"/>
</new-codes>
<moved-codes>
<city name="All areas"/>
<city name="Total, California"/>
<city name="Alhambra"/>
<city name="American River"/>
<city name="Anaheim">
<zip zipCode="95610"><!-- zip code was moved from American River to
Anaheim -->
<Total>5750</Total>
</zip>
</city>
<city name="Antioch"/>
<city name="Auburn">
<zip zipCode="95628"><!-- This zip code was moved from American River
to Auburn -->
<Total>7325</Total>
</zip>
</city>
<city name="Bakersfield"/>
<city name="Bakersfield, East Hills"/>
</moved-codes>
<deleted-codes>
<city name="All areas"/>
<city name="Total, California"/>
<city name="Alhambra"/>
<city name="American River">
<zip zipCode="95621">
<Total>7135</Total>
</zip>
</city>
<city name="Anaheim"/>
<city name="Antioch">
<zip zipCode="94505">
<Total>1575</Total>
</zip>
</city>
<city name="Auburn"/>
<city name="Bakersfield"/>
<city name="Bakersfield, East Hills"/>
</deleted-codes>

which I think is the correct list of nodes, based on the comments in the

XML document.



--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/

Current Thread