Re: [xsl] Grouping with keys

Subject: Re: [xsl] Grouping with keys
From: "G. T. Stresen-Reuter" <tedmasterweb@xxxxxxxxx>
Date: Thu, 24 Oct 2013 19:05:29 +0100
On Oct 24, 2013, at 4:34 PM, Rick Quatro <rick@xxxxxxxxxxxxxx> wrote:

> <xsl:key name="orderedlist" match="xhtml:p[@class='listitem1' or
>
@class='listitem2'][preceding-sibling::*[1][self::xhtml:p[@class='listitem1'
> or @class='listitem2']]]"
>        use="generate-id(preceding-sibling::xhtml:p[@class='listitem1' or
>
@class='listitem2'][not(preceding-sibling::*[1][self::xhtml:p[@class='listit
> em1' or @class='listitem2']])])" />

Hi,

Leaving aside for the moment exactly what you're trying to do, try to keep in
mind that you specify the element you want to capture in the MATCH attribute.
The USE attribute, then, is an XPath from the MATCHed element to the node you
want to use as a key (the group identifier).

Also, if possible, please provide a simplified version of the problem. I'm
unable to process what you've sent and looking at it. I'm having trouble
identifying what you are trying to key off of.

That said, here's my best *UNTESTED* attempt at answering your query (and note
that there will be some issues with the START attribute in your OL element).

<xsl:key name="listitems" match="p[starts-with(@class, 'listitem')]"
use="preceding-sibling::p[starts-with(@class, 'listintro') or
starts-with(@class, 'caption')]" />

<xsl:template match="p[not(starts-with(@class, 'listitem'))]">
	<xsl:copy-of select="." />
	<xsl:variable name="currP" select="." />
	<xsl:variable name="Ppos" select="position()" />
	<xsl:if test="count(key('listitems', .)) &gt; 0">
		<xsl:element name="ol">
			<xsl:attribute name="start"><xsl:value-of select="$PPos"
/></xsl:attribute>
			<xsl:for-each select="key('listitems', $currP)">
				<xsl:element name="li">
					<xsl:value-of select="." />
				</xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:if>
</xsl:template>

Also note that you are going to have duplicate numbers in the rendered HTML as
the LI element inside an OL element will prepend the contents with a number
and your contents already contains a number.

Ted Stresen-Reuter

(Feels good to be back in the game, just wish it was a full time gig.)

Current Thread