Re: [xsl] Thorny select: getting surrounding years

Subject: Re: [xsl] Thorny select: getting surrounding years
From: Liam R E Quin <liam@xxxxxx>
Date: Thu, 21 Oct 2010 18:48:38 -0400
On Thu, 2010-10-21 at 10:12 -1000, Brooks Talley wrote:
> I've been wresting with this, and my brain is melting. I need to get a
> list of the surrounding years for an item -- zero to two years before,
> the item itself, zero to two years after. There may be gaps in the
> sequence, in which case I still need two items before and after (if
> available).
> 
> Sample xml:
> 
> <root>
>    <item>
>       <id>12345</id>
>       <year>2005</year>
>    </item>
>    <otherYears>
>      <item>
>         <id>56789</id>
>         <year>2001<year>
>      </item>

So e.g. in a template to match /root/item, you might want,
../otherYears/item[ (year - $year) * (year - $year) &lt;= 4 ]
to get the 2 years before or after. However, they might not be there,
so maybe really it's something like,
  <xsl:for-each select="../otherYears/item[id = ../../item/id]">
    <xsl:apply-templates
        select="preceding-sibling::item[position() &lt;= 2]" />
    <xsl:apply-templates select="." />
    <xsl:apply-templates
        select="following-sibling::item[position() &lt;= 2]" />
  </xsl:for-each>

Liam

-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org www.advogato.org

Current Thread