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.


### 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" ?>

	<!-- you may need other namespaces declared in here -->

<xsl:template match="/">

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

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


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: