Re: [xsl] Count Ancestors Up To But Not Including a Given Type

Subject: Re: [xsl] Count Ancestors Up To But Not Including a Given Type
From: Eliot Kimber <ekimber@xxxxxxxxxxxx>
Date: Tue, 05 Feb 2008 09:09:41 -0600
David Carlisle wrote:

<xsl:stylesheet version="2.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
		xmlns:xs="http://www.w3.org/2001/XMLSchema";
		xmlns:f="data:,f"
		>


<xsl:function name="f:same" as="element()*"> <xsl:param name="n" as="element()"/> <xsl:sequence select="$n,$n/../..[node-name(.)=node-name($n)]/f:same(.)"/> </xsl:function>

<xsl:template match="y">
:<xsl:value-of select="count(f:same(../..)/name())"/>
</xsl:template>

These are very helpful examples--I find the select="x,f(x)" construct quite elegant (and familiar now that I remember my old lisp/DSSSL training).


But I realize I didn't give the full story, which is that the number of ancestors between a given list item and its parent list is variable, in that DITA allows both:

<ol><li><ol><li>

and

<ol><li><p><ol><li>

So a simple "../.." won't be reliable. (Although one could argue that the intervening <p> should restart the list sequence--I'm not sure the DITA 1.1 spec is clear on that point.)

In thinking about it more I don't think there's a way to generalize this to a query that is not DTD-specific since you have to know which intervening elements to ignore and which to respect.

Thanks,

Eliot

--
Eliot Kimber
Senior Solutions Architect
"Bringing Strategy, Content, and Technology Together"
Main: 610.631.6770
www.reallysi.com
www.rsuitecms.com

Current Thread