Blog
Jan 05, 2009

Comparing two XML Documents


XML
Posted: Oct 21, 2005

In migrating to the XML Registy and Repository for the Education Community we needed to make sure that the data in the registry accurately reflected all of the elements in the static PESC Core Main schema. In the XML Registry there are now almost 1,000 data elements. This is way too hard to manually compare 1,000 elements against a static schema.

I thought it would be an interesting little trick to use the data within the files themselves to build a document showing the similarities and differences. Hey, both source documents are XML (actually XSD) so XSLT is the logical choice for me to do this.

There are a few key points necessary to make this work:

  1. The XSL document() function allows you to read in another separate file from the main one being processed.
  2. When displaying the XML Schema snippets to a web page I need to encode everything so that the browser will display it correctly. I found an XML to HTML Verbatim Formatter with Syntax Highlighting to do the job for me.

Start the XSL by declaring the XML Schema namespace and also importing the XML Verbatim stylesheet.

<xsl:transform
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:xs=”http://www.w3.org/2001/XMLSchema”
version=”2.0″>
<xsl:import href=”xmlverbatim.xsl”/>

The main loop of the xsl looks like this (simplified a bit to remove other html cruft):

<xsl :template match=”/”>
<html>
<body>
<table border=”1″>
<tr><th>Core Component Name</th><th>CoreMain</th><th>Registry Value</th></tr>
<xsl :apply-templates select=”/*/xs:complexType” />
</table>
</body>
</html>

Now match on the xs:complexType element to print the matched element and the element from the other document.

<xsl:template match=”xs:complexType”>
<xsl:variable name=”elementName” select=”@name”/>
<tr><td>
<xsl:value-of select=”$elementName”/>
</td><td>
<xsl:apply-templates select=”.” mode=”xmlverb”/>
</td><td>
<xsl:variable name=”ad” select=”document(’registry/aid-disbursement.xsd’)”/>
<xsl:apply-templates select=”$ad//*[@name=$elementName]” mode=”xmlverb”/>
</td></tr>
</xsl:template>

Voila! Using a bit of CSS there in the html I can get nice color coded XML and display the old document and the new document side by side in an HTML page. This was really a lot simpler that I expected it to be.

Print
All content copyright 2006 The Bornholtz Group  •  Bornholtz.com blog