Subject: Re: [xsl] Merging elements From: Nic Gibson <nicg@xxxxxxxxxx> Date: Fri, 28 Oct 2011 10:14:54 +0200 |
Hi Wendell. After trying it with sibling recursion I actually ended up doing something virtually identical to your solution :) Sibling recursion worked but some of the predicates I had to express to handle edge cases got painful. It was easier to write a custom grouping function. I'm always impressed with the way our minds work - you step away from a problem, ask someone else and the act of expressing the problem to someone else allows you to think of a solution. nic On 28 Oct 2011, at 00:48, Wendell Piez wrote: > Hi, > > Sibling recursion would work for this, but so would this, I think. Plus it might (or might not) be easier to maintain: > > <xsl:template match="p"> > <p> > <xsl:call-template name="merge-spans"/> > </p> > </xsl:template> > > <xsl:template name="merge-spans"> > <xsl:for-each-group select="node()" group-adjacent="m:class(.)"> > <xsl:choose> > <xsl:when test="string(current-grouping-key())"> > <span class="{current-grouping-key()}"> > <xsl:apply-templates > select="current-group()" mode="merged"/> > </span> > </xsl:when> > <xsl:otherwise> > <xsl:apply-templates select="current-group()"/> > </xsl:otherwise> > </xsl:choose> > </xsl:for-each-group> > </xsl:template> > > <xsl:function name="m:class" as="xs:string"> > <!-- returns a class value for a span, or the class value of an > immediately preceding span for whitespace-only text, > comment or PI nodes --> > <xsl:param name="s" as="node()"/> > <xsl:sequence select="($s/self::span/@class, > $s/(self::text()[not(normalize-space())] | > self::comment() | > self::processing-instruction() )/ > preceding-sibling::*[1][self::span]/@class, > '')[1]"/> > </xsl:function> > > <xsl:template match="node()" mode="merged"> > <xsl:apply-templates select="."/> > </xsl:template> > > <xsl:template match="span[@class]" mode="merged"> > <xsl:apply-templates/> > </xsl:template> > > Cheers, > Wendell > > On 10/27/2011 4:13 AM, Nic Gibson wrote: >> <p><span class="strong-emphasis">Fome</span> <span class="strong-emphasis">Zero</span> >> <span class="strong">: la stratigie du Brisil pour risoudre les problhmes de l'insicuriti alimentaire et de la faim </span></p> >> >> And also: >> >> <p><span class="strong">C</span><span class="strong">ontexte</span></p> >> >> I need to merge those spans with identical class attribute values along with the whitespace to get something like: >> >> <p><span class="strong-emphasis">Fome Zero</span> >> <span class="strong">: la stratigie du Brisil pour risoudre les problhmes de l'insicuriti alimentaire et de la faim </span></p> >> >> And: >> >> <p><span class="strong">Contexte</span></p> >> > > -- > ====================================================================== > Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx > Mulberry Technologies, Inc. http://www.mulberrytech.com > 17 West Jefferson Street Direct Phone: 301/315-9635 > Suite 207 Phone: 301/315-9631 > Rockville, MD 20850 Fax: 301/315-8285 > ---------------------------------------------------------------------- > Mulberry Technologies: A Consultancy Specializing in SGML and XML > ====================================================================== > -- Corbas Consulting Digital Publishing Consultancy and Training http://www.corbas.co.uk, +44 (0)7718 906817/+44 (0)1273 930765
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Merging elements, Wendell Piez | Thread | Re: [xsl] Merging elements, Tony Graham |
Re: [xsl] Merging elements, Wendell Piez | Date | Re: [xsl] Merging elements, Tony Graham |
Month |