This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Grouping question


Hello,

I'm not Jeni, but here is the XSLT 1.0 solution with Muenchian Method:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output indent="yes"/>

<xsl:param name="cols" select="2"/>

<xsl:key name="n" match="n" use="@color"/>

<xsl:template match="data">
     <xsl:apply-templates select="n[count(. | key('n', @color)[1]) = 1]" 
mode="table"/>
</xsl:template>

<xsl:template match="n" mode="table">
     <table border="1">
         <tr><td colspan="{$cols}"><xsl:value-of select="@color"/></td></tr>
         <xsl:apply-templates select="key('n', @color)[position() mod 
$cols = 1]" mode="row"/>
     </table>
</xsl:template>

<xsl:template match="n" mode="row">
     <tr>
         <xsl:apply-templates select=". | following-sibling::n[@color = 
current()/@color][position() &lt; $cols]" mode="cell"/>
     </tr>
</xsl:template>

<xsl:template match="n" mode="cell">
     <td><xsl:value-of select="."/></td>
</xsl:template>

</xsl:stylesheet>

Regards,

Joerg

bryan.s.schnabel@exgate.tek.com schrieb:
> While we wait for Jeni to give us the well-thought-out XSLT 1.0 Meunchian
> method, here's some fun to consider with XSLT 2.0.
> 
> Assuming your input is well formed (not </color>):
> <data>
>   <n color="R">1</n>
>   <n color="R">2</n>
>   <n color="R">3</n>
>   <n color="Y">4</n>
>   <n color="Y">5</n>
>   <n color="Y">6</n>
>   <n color="W">7</n>
>   <n color="W">8</n>
>   <n color="W">9</n>
> </data>
> 
> Something like this would work:
> 
> <xsl:template match="data">
>  <tables>
>   <xsl:for-each-group select="n" group-by="@color">
>     <xsl:sort select="." />
>      <table>
>       <xsl:for-each select="current-group()">
>        <xsl:if test="position() mod 2">
>         <xsl:variable name="color" select="@color" />
>         <tr>
>          <td>
>           <xsl:value-of select="." />
>          </td>
>          <xsl:if test="following-sibling::n[@color=$color]">
>           <td>
>            <xsl:value-of select="following-sibling::n[@color=$color]" />
>           </td>
>          </xsl:if>
>         </tr>
>        </xsl:if>
>       </xsl:for-each>
>      </table>
>   </xsl:for-each-group>
>  </tables>
> </xsl:template>
> 
> Bryan
> 
> -----Original Message-----
> From: Spectron International, Inc. [mailto:spectron@coqui.net]
> Sent: Tuesday, May 21, 2002 7:05 AM
> To: XSL-List@lists.mulberrytech.com
> Subject: [xsl] Grouping question
> 
> 
> I have the following XML:
> 
> <data>
>   <n color="R">1</color>
>   <n color="R">2</color>
>   <n color="R">3</color>
>   <n color="Y">4</color>
>   <n color="Y">5</color>
>   <n color="Y">6</color>
>   <n color="W">7</color>
>   <n color="W">8</color>
>   <n color="W">9</color>
> </data>
> 
> and I want to create 3 tables (one for each color) with 2 columns each:
> 
> <table>
>   <tr><td>1</td><td>2</td></tr>
>   <tr><td>3</td></tr>
> </table>
> <table>
>   <tr><td>4</td><td>5</td></tr>
>   <tr><td>6</td></tr>
> </table>
> 
> <table>
>   <tr><td>7</td><td>8</td></tr>
>   <tr><td>9</td></tr>
> </table>
> 
> I have checked the grouping section in http://www.dpawson.co.uk/xsl/ but
> none of the samples do what I need. How can I do that?
> 
> Faw
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]