[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Grouping question [1.0 way]

Subject: RE: Grouping question [1.0 way]
From: Bryan Schnabel <bryan.schnabel@xxxxxxxxxxxxx>
Date: Tue, 21 May 2002 13:06:05 -0700 (PDT)
xsl for each color
Hmmm.  Well, this works with XSLT 1.0

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

<xsl:template match="/">
 <tables>
  <xsl:for-each
select="/data/n[generate-id()=generate-id(key('color-group',@color)[1])]">
   <xsl:sort select="." />
   <table>
    <xsl:for-each select="key('color-group',@color)">
     <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>
 </tables>
</xsl:template>

Bryan


--- bryan.s.schnabel@xxxxxxxxxxxxxx wrote:
> 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@xxxxxxxxx]
> Sent: Tuesday, May 21, 2002 7:05 AM
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  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>




__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com

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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.