This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Keys across multilple input files


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"?>
<documents>
  <doc href="test1.xml" />
  <doc href="test2.xml" />
  <doc href="test3.xml" />
</documents>

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
document('filelist.xml').

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" />
  </xsl:for-each>
</xsl:template>

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" />
</xsl:template>

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:for-each>
  +- <xsl:value-of select="@name" />
</xsl:template>

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,

Jeni

Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 • Fax 0115 9061304 • Email
jeni.tennison@epistemics.co.uk



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]