RE: [xsl] Grouping with keys

Subject: RE: [xsl] Grouping with keys
From: "Rick Quatro" <rick@xxxxxxxxxxxxxx>
Date: Thu, 24 Oct 2013 14:50:38 -0400
Hi Ted,

Thanks for the reply. Here is a simplified example:

<body>
  <li>one</li>
  <li>two</li>
  <p>paragraph</p>
  <li>three</li>
  <li>four</li>
  <p>paragraph</p>
  <li>five</li>
  <li>six</li>
</body>

should become:

<body>
  <ol>
    <li>one</li>
    <li>two</li>
  </ol>
  <p>paragraph</p>
  <ol>
    <li>three</li>
    <li>four</li>
  </ol>
  <p>paragraph</p>
  <ol>
    <li>five</li>
    <li>six</li>
  </ol>
</body>

Rick

-----Original Message-----
From: G. T. Stresen-Reuter [mailto:tedmasterweb@xxxxxxxxx] 
Sent: Thursday, October 24, 2013 2:05 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Grouping with keys

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