Re: [xsl] Sorted node set and following-sibling axis

Subject: Re: [xsl] Sorted node set and following-sibling axis
From: "Nick Fitzsimons" <nick@xxxxxxxxxxxxxx>
Date: Thu, 11 Aug 2005 09:58:19 +0100 (BST)
Hi Jay,

Unfortunately, although your solution works for me in Saxon 8B, it fails
in Saxon 6.5.3, Xalan and MSXML 4, giving errors on the theme of "Cannot
convert a result tree fragment to a node-set". It seems that Saxon
supports some such conversion automatically (possibly a side-effect of
XSLT 2 support?), but as my client may switch XSLT processors at any time,
I'm having to stick to standard XSLT 1.0.

Thanks for your suggestion; if I can ever guarantee a supporting
processor, I'll bear it in mind. In the meantime, if you need something
similar to work cross-processor, the approach posted by Joris Gillis works
for me.

Regards,

Nick.

> Hi, Nick,
>
> How do you determine which values go in which divs? That is, what is the
> logic behind the grouping? It appears to two to a div, but it's hard to
> tell with a small data sample. If you do just want to a div element around
> every pair of values, that's pretty straightforward:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
>   <xsl:output method="xml" indent="yes"/>
>
>   <xsl:variable name="articles">
>     <xsl:for-each select="/articles/article">
>       <xsl:sort select="@priority" data-type="number" order="descending"/>
>       <p><xsl:value-of select="."/></p>
>     </xsl:for-each>
>   </xsl:variable>
>
>   <xsl:template match="articles">
>     <xsl:for-each select="$articles/p">
>       <xsl:if test="position() mod 2 = 1">
>         <div>
>           <xsl:copy-of select="."/>
>           <xsl:if test="following-sibling::*[1]">
>             <xsl:copy-of select="following-sibling::*[1]"/>
>           </xsl:if>
>         </div>
>       </xsl:if>
>     </xsl:for-each>
>   </xsl:template>
>
> </xsl:stylesheet>
>
> The notion is to first get a list of sorted p elements, which you can then
> stuff into whatever logic you need for div elements. In this case, I just
> grouped them as pairs (as your output showed). Speaking of output, here's
> what Saxon 8.5 gave me:
>
> <div>
>    <p>
>     first
>   </p>
>    <p>
>     second
>   </p>
> </div>
> <div>
>    <p>
>     third
>   </p>
>    <p>
>     last
>   </p>
> </div>
>
> Jay Bryant
>  Bryant Communication Services
> (presently consulting at Synergistic Solution Technologies)
>
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/

Current Thread