Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: XSLT 1.0 -- grouping items from a list of items of variable length

From: jhendrich@----------.------
To: 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



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent