Re: Compare data from different XML-files

Subject: Re: Compare data from different XML-files
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 13 Nov 2000 09:37:49 +0000

> I want to compare XML-data from two different XML-sources-files in my
> XSL-stylesheet, how do I do that? 

The function that you need is the document() function, which pulls in
information from a named XML file.  The way I would usually arrange
this is to have the input to the XSLT stylesheet be some XML that
describes the position of the two files that you want to compare:

--- input.xml ---
  <file href="file1.xml" />
  <file href="file2.xml" />

You can then access the two documents and put them into global
variables using:

<xsl:variable name="file1"
              select="document(/files/file[1]/@href, /)" />
<xsl:variable name="file2"
              select="document(/files/file[2]/@href, /)" />

[Note the second argument to document() ensures that the file names
are resolved relative to your input.xml document rather than the

An alternative way of reaching this state is to use one of the files
as the input to the stylesheet, and name the other as a parameter to
the stylesheet.  You can then use:

<xsl:param name="comparison-file" />
<xsl:variable name="file1" select="/" />
<xsl:variable name="file2" select="document($comparison-file, /)" />

When you have the two document trees as separate variables, you can
compare them however you want. For example, to test whether the names
of the document elements are the same you could use:

  name($file1/*) = name($file2/*)

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread