[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSLT Comparison of Two Trees

Subject: Re: XSLT Comparison of Two Trees
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Tue, 08 Sep 2009 20:35:33 +0200
Re:  XSLT Comparison of Two Trees
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.