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

Re: group by number

Subject: Re: group by number
From: Frederic Laurent <fl@xxxxxxxxxxxxx>
Date: Fri, 28 Nov 2003 15:38:47 +0100
xsl group by
Andreas Lindahl wrote:

> Hi
> 
> I have an xml file which looks pretty much like this:
> 
<snip/>

> Each group node should contain three item nodes.
> 
> How should I do this with XSLT?

use the position function...
I set the root node to result


<xsl:template match="/">
<result>
  <xsl:apply-templates select="//item" mode="test"/>
</result>
</xsl:template>

<xsl:template match="item" mode="test">
  <xsl:if test="position() mod 3 = 1">
    <group id="{ceiling(position() div 3)}">
      <xsl:apply-templates select="."/>
      <xsl:apply-templates select="following-sibling::item[1]"/>
      <xsl:apply-templates select="following-sibling::item[2]"/>
   </group>
  </xsl:if>
</xsl:template>


<xsl:template match="item">
  <xsl:copy><xsl:apply-templates select="*|@*|text()"/></xsl:copy>
</xsl:template>

----

java org.apache.xalan.xslt.Process -IN items.xml -XSL items.xsl
<?xml version="1.0" encoding="UTF-8"?>
<result>
<group id="1">
<item>1</item>
<item>2</item>
<item>3</item>
</group>
<group id="2">
<item>4</item>
<item>5</item>
<item>6</item>
</group>
<group id="3">
<item>7</item>
<item>8</item>
<item>9</item>
</group>
</result>


HTH

cheers

-- 

XPath free testing software :  http://lantern.sourceforge.net
Frédéric Laurent                     http://www.opikanoba.org

 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.