Re: Keys across multilple input files

Subject: Re: Keys across multilple input files
From: Jeni Tennison <Jeni.Tennison@xxxxxxxxxxxxxxxx>
Date: Tue, 06 Jun 2000 14:50:09 +0100
Ann Marie,

Thank you for asking this question.  My understanding of how keys and
documents interact has been much expanded in trying to answer it.  I have
managed to put together something that will probably work, though obviously
I've simplified a few things rather than use the full complexity of what I
know of what you're trying to do.  I'm sure you can fill in the gaps.

You say that you have a file named filelist.xml that holds the names of the
XML class files.  I'm going to assume it looks something like:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
  <doc href="test1.xml" />
  <doc href="test2.xml" />
  <doc href="test3.xml" />

I'm also going to assume that it's the 'input file' for when you run the
stylesheet.  If it isn't, you can always refer to it using

On to the stylesheet.  First, we set up a variable that holds the contents
of all those documents:

<xsl:variable name="documents" select="document(/documents/doc/@href)" />

Note that the way the document() function works, it actually goes and gets
*all* those documents, not just the first one.  Don't ask me why because I
can't follow the definition of document(), but it works.  Which is handy.

Next I define the key in the normal way:

<xsl:key name="classes" match="class" use="@name" />

Now, for lack of a better thing to do, I'm going to work through these
classes one at a time according to the order the documents have been given
in and the order the classes have been given in within those documents.
You probably have some more sophisticated way of ordering your output.
Slot it in here.

<xsl:template match="/">
  <xsl:for-each select="$documents">
    <xsl:apply-templates select="/classes/class" />

For each of the classes, I'm going to have a bit of information about the
class, and then generate the hierarchy that you (used to) want.  You
definitely have a more sophisticated output for each class.  Slot it in here.

<xsl:template match="class">
  <h3><xsl:value-of select="@name" /></h3>
  <xsl:apply-templates select="." mode="hierarchy" />

And finally, the bit where we use the key() function to get the superclass
node to build the hierarchy.  Note that we have to define a variable for
the name of the superclass outside the xsl:for-each.  The key() function
works in exactly the same way as normal, but the xsl:for-each defines the
documents that the key is used within.  You definitely have a more
sophisticated output for the formatting and linking of the hierarchy.  Slot
it in here.

<xsl:template match="class" mode="hierarchy">
  <xsl:variable name="superclass" select="@superclass" />
  <xsl:for-each select="$documents">
    <xsl:apply-templates select="key('classes', $superclass)"
      mode="hierarchy" />
  +- <xsl:value-of select="@name" />

As I said, I don't understand exactly why or how this works, and will be
glad to see anyone explain the technical ins and outs.  But it gives the
desired output in SAXON, which I guess is all that matters in the end.

I hope that helps,


Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email

 XSL-List info and archive:

Current Thread