RE: [xsl] Positional grouping with exceptions

Subject: RE: [xsl] Positional grouping with exceptions
From: "Fredrik Geers" <fredrik@xxxxxxxxxx>
Date: Fri, 22 Dec 2006 09:11:13 +0100
Michael Kay, Andrew Welch, thank you for your help.

However, Micheal Kay's solution isn't working, Saxon reports that you
cannot start with self:: in a group-starting-with attribute. (Axis in
pattern must be child or attribute).

The solution of Andrew Welch seemed to work better, until I discovered
that when I have a node containing a childnode and some text, only the
childnode remains in the resulting document.
I've tried to modify the stylesheet, but no such luck.

--
Fredrik Geers

> -----Original Message-----
> Date: Wed, 20 Dec 2006 15:11:12 -0000
> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> From: "Michael Kay" <mike@xxxxxxxxxxxx>
> Subject: RE: [xsl] Positional grouping with exceptions
> Message-ID: <01c701c72449$161cb0a0$6401a8c0@turtle>
>
> Try
>
> <xsl:template match="book">
>   <book>
>     <xsl:for-each-group select="*"
>       group-starting-with="self::a[not(child::text())] |
>
self::a[not(preceding-sibling::*[1][self::a])] |
>                            *[not(self::a)]">
>       <xsl:choose>
>         <xsl:when test="self::a[not(child::text())]">
>           <a1><xsl:copy-of select="remove(current-group(),1)"/></a1>
>         </xsl:when>
>         <xsl:when test="self::a">
>           <a1><xsl:copy-of select="current-group()"/></a1>
>         </xsl:when>
>         <xsl:otherwise>
>           <xsl:copy-of select="current-group()"/>
>
> Not tested.
>
> Michael Kay
> http://www.saxonica.com/

> -----Original Message-----
> Date: Wed, 20 Dec 2006 15:28:15 +0000
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
> Subject: Re: [xsl] Positional grouping with exceptions
> Message-ID:
<74a894af0612200728o791232f5t915f97e8b0faf43@xxxxxxxxxxxxxx>
>
> I think you need a modified identity transform for this one.  The
> following stylesheet walks the tree along the following-sibling axis,
> allowing you to group <a>'s with text as you come across them:
>
> <xsl:stylesheet version="2.0"
> 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
> <xsl:template match="node()">
> 	<xsl:copy>
> 		<xsl:copy-of select="@*"/>
> 		<xsl:apply-templates select="node()[1]"/>
> 	</xsl:copy>
> 	<xsl:apply-templates select="following-sibling::*[1]"/>
> </xsl:template>
>
> <xsl:template match="a[text()]">
> 	<al>
> 		<xsl:apply-templates select="." mode="fill"/>
> 	</al>
> 	<xsl:apply-templates
select="following-sibling::*[not(self::a[text()])][1]"/>
> </xsl:template>
>
> <xsl:template match="a[text()]" mode="fill">
> 	<xsl:copy-of select="."/>
> 	<xsl:apply-templates
> select="following-sibling::*[1][self::a[text()]]" mode="fill"/>
> </xsl:template>
>
> </xsl:stylesheet>
>
> The output generated is:
>
> <book>
> 	<header>title</header>
> 	<al>
> 		<a>text</a>
> 		<a>text</a>
> 	</al>
> 	<otherelement>title</otherelement>
> 	<al>
> 		<a>text</a>
> 		<a>text</a>
> 	</al>
> 	<a/>
> 	<al>
> 		<a>text</a>
> 	</al>
> </book>
>
> Notice the <a/> which wasnt present in your example - easy to suppress
> if that really is the case.
>
> By the way, Saxon 8.8 is available now.
>
> cheers
> andrew

Current Thread