Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - XSLT 1.0 -- grouping items from a list of items of variable length [Thread Next] RE: XSLT 1.0 -- grouping items from a list of items of variable lengthTo: NULL Date: 12/1/2006 10:58:00 AM > Hi, > > I want to transform a list of items that can be of variable length into > groups of 6 items each, with the last group possibly having less than 6. > > For example, the source list might have 40 items. I therefore would want to > achieve 6 groups of 6 items plus a 7th group of 4 items. > > Specifically, given a list of images, I want to create n pages with 6 images > each. > > Question: Is there a way to do this, or is this a limitation of XSLT? > > For reference, you can see how I had originally intended to do this...except > that it doesn't work due to needing to mismatch start and end tags. > > Example source xml: > > <Images> > <Image>23.01.20 hrs __[0000033].bmp</Image> > <Image>23.01.16 hrs __[0000034].bmp</Image> > <Image>01.45.16 hrs __[0000035].bmp</Image> > <Image>01.45.20 hrs __[0000039].bmp</Image> > <Image>01.45.20 hrs __[0000043].bmp</Image> > <Image>01.45.16 hrs __[0000044].bmp</Image> > <Image>02.45.16 hrs __[0000053].bmp</Image> > <Image>02.45.20 hrs __[0000063].bmp</Image> > <Image>03.45.20 hrs __[0000072].bmp</Image> > <Image>03.45.16 hrs __[0000077].bmp</Image> > <Image>04.45.20 hrs __[0000044].bmp</Image> > <Image>04.45.16 hrs __[0000045].bmp</Image> > <Image>23.57.21 hrs __[0000005].jpeg</Image> > <Image>18.34.27 hrs __[0000006].jpeg</Image> > <Image>21.59.27 hrs __[0000006].jpeg</Image> > <Image>22.57.21 hrs __[0000105].jpeg</Image> > </Images> > > XSLT: > > <xsl:template match="Images"> > > <!-- insert a page break --> > <p style="page-break-before: always;"> > > <!-- display is 6-up per page --> > <table class="n6up"> > <xsl:for-each select="Image"> > <xsl:if test="position() mod 6 = 0"> > </table> > </p> > <p style="page-break-before: always;"> > <table class="n6up"> > </xsl:if> > <xsl:if test="position() mod 2 = 1"> > <tr> > <td class="n6up"> > <xsl:element name="img"> > <xsl:attribute name="class"><xsl:value-of > select="'nUpImage'"/></xsl:attribute> > <xsl:attribute name="src"><xsl:value-of > select="string(.)"/></xsl:attribute> > <xsl:attribute name="alt"><xsl:value-of > select="string(.)"/></xsl:attribute> > </xsl:element> > </td> > </xsl:if> > <xsl:if test="position() mod 2 = 0"> > <td class="n6up"> > <xsl:element name="img"> > <xsl:attribute name="class"><xsl:value-of > select="'nUpImage'"/></xsl:attribute> > <xsl:attribute name="src"><xsl:value-of > select="string(.)"/></xsl:attribute> > <xsl:attribute name="alt"><xsl:value-of > select="string(.)"/></xsl:attribute> > </xsl:element> > </td> > </tr> > </xsl:if> > </xsl:for-each> > </table> > </p> > </xsl:template> > On second glance, there's at least one bug in the above XSLT and it's hard to follow, so please disregard it. Here's the pseudocode: start a new page before start table for ( each Image item ) if position mod 6 is 0 add td item end row end table end page start a new page before start table else if position mod 2 is 1 start a new row add td item else if position mod 2 is 0 add td item end row end table end page | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
