Comparing XML Files

This is just a quick note on comparing XML files.

Often it is useful to compare the contents of, for example, two configuration files, with the case and ordering of those files ignored. This can be done easily in Cygwin/Bash, using the following script. Something similar can probably be done in MS DOS.

#!/bin/bash

### copy the original file without copying any file attributes
cat filename.xml > filename.sorted.0.xml

### convert the file's contents to lower case
tr '[:upper:]' '[:lower:]' < filename.sorted.0.xml > filename.sorted.1.xml

### sort the XML elements and attributes via XSLT
xsltproc sort.xsl filename.sorted.1.xml > filename.sorted.2.xml

### apply pretty indentation to the XML
xmllint --format filename.sorted.2.xml > filename.sorted.xml

### delete the temporary files
rm filename.sorted.[0-9].xml

The stylesheet <sort.xsl> above might be something like this:

<?xml version="1.0" ?>

<xsl:stylesheet
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		version="1.0">
	<!-- you may need other namespaces declared in here -->

<xsl:template match="/">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="*">
	<xsl:element name="{name()}">
		<xsl:for-each select="@*">
			<xsl:sort select="name()"/>
			<xsl:attribute name="{name()}">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:for-each>
		<xsl:apply-templates>
			<xsl:sort select="name()"/>
			<xsl:sort select="@type" />
			<xsl:sort select="@name" />
			<!-- you may wish to adjust these sorting rules -->
		</xsl:apply-templates>
	</xsl:element>
</xsl:template>

<!-- you may wish to also handle text() nodes -->
<!-- you may wish to handle certain elements in specialised templates -->

</xsl:stylesheet>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: