Re: [xsl] xpath key problems

Subject: Re: [xsl] xpath key problems
From: Piet van Oostrum <piet@xxxxxxxxxxxxxx>
Date: Sun, 21 Nov 2010 16:46:15 -0400
Birnbaum, David J wrote:

 > Dear XSLT List,
 > 
 > I'd be grateful for advice about a problem I'm having with the use of
 > keys.
......
 >     <xsl:key name="msline-pb" match="hm281 | hm280 | hm282"
 >         use="preceding::pb[parent::*/name() eq name()][1]/@folio"/>
......
 >             <pointer>
 >                 <xsl:value-of select="key('msline-pb',.)"/>
 >             </pointer>

You have your key backwards. It finds the manuscript entries (hm281 | hm280 | hm282) and it uses the foloi numbers to find them. Instead you want to find folio numbers and give it a manuscript entry to find it. The key() call more or less tries this.

As the key values will be atomized you must create a unique atomized value for each manuscript entry. You can do this with generate-id(). The key must only give manuscript entries which have a pb child, otherwise we can't get a folio out of it. Oh yes, we also have to include "ourselves" in the key. With this I get the following:

    <xsl:key name="msline-pb" match="hm281[pb] | hm280[pb] | hm282[pb]" use="(generate-id(.),following::*[name() eq current()/name()]/generate-id(.))"/>

I.e. all the following hm* entries with the same name are used as keys to find the entry with the pb child (and we use generate-id() to create the search value). It would be better to stop the key values at the next entry which has a pb child but this is left as an exercise for the reader :)

To retrieve the folio number:

<xsl:value-of select="key('msline-pb',generate-id(.))[position()=last()]/pb/@folio"/>

Because we don't stop at the next pb entry we have to get the last of the retrieved hm* entries and obtain its folio.

It would probably also be faster to split the key in 3 keys, one for each hm* kind. Actually I am curious how much speed-up this key gives, if any.
-- 
Piet van Oostrum
Cochabamba. URL: http://pietvanoostrum.com/
Nu Fair Trade woonartikelen op http://www.zylja.com

Current Thread