Page 1 of 1

How to display a limited number of records using xsl?

Posted: Mon Jul 18, 2005 3:20 pm
by joshi_ameya
Hi Frnds,

I am new to xsl.

I have the following problem:

I have an XML file which has 100 records in it and this number can vary.

I want to write an XSL file which will display 10 records at a time in a table and inside a layer and rest all records it will keep in a table and that will be inside different hidden layers.

How do I go abt tht...my structure is pretty simple and there are no attributes to make it complicated. And I cant add / remove nodes / attributes from / to the xml.

Please guide me.

thanks :)

Posted: Fri Jul 22, 2005 2:24 pm
by Radu
Hi,

The main idea is to group the nodes in the document 10 at a time and then display each group in its own table.
The following stylesheet (version 2.0) when applied to the "personal.xml" file located in the Oxygen "samples" folder will output for each group a new HTML file. Each file has "Next", "Previous" links. If you want to do this with only one output file each group should be represented by a table.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<!-- Number of pages on which the result will be displayed -->
<xsl:param name="pages" select="3"/>
<xsl:template match="/">
<!-- Make groups of nodes for each page. -->
<xsl:for-each-group select="//person" group-by="floor((position() - 1) div $pages)">
<!-- The index of each page -->
<xsl:variable name="aa" select="position()"/>
<!-- Output each page in a new html file -->
<xsl:result-document href="{$aa}.html">
<html>
<body>
<xsl:element name="table">
<xsl:attribute name="border">2</xsl:attribute>
<tr>
<xsl:attribute name="color">#FFFFFF</xsl:attribute>
<xsl:attribute name="bgcolor">#336666</xsl:attribute>
<xsl:attribute name="align">center</xsl:attribute>
<td>
<font name="Arial" size="3">
<b>Name</b>
</font>
</td>
<td>
<font name="verdana" size="3">
<b>Email</b>
</font>
</td>
<td>
<font name="verdana" size="3">
<b>Link</b>
</font>
</td>
</tr>
<xsl:for-each select="current-group()">
<xsl:element name="tr">
<xsl:attribute name="align">center</xsl:attribute>
<xsl:element name="td">
<font name="verdana" size="3">
<xsl:attribute name="width">120</xsl:attribute>
<i>
<xsl:value-of select="name/family/text()"/>
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="name/given/text()"/>
</i>
</font>
</xsl:element>
<xsl:element name="td">
<xsl:attribute name="width">120</xsl:attribute>
<font name="verdana" size="3">
<xsl:value-of select="email/text()"/>
</font>
</xsl:element>
<xsl:element name="td">
<font color="black" name="verdana" size="3">
<xsl:value-of select="./link/@subordinates"/>
<xsl:value-of select="./link/@manager"/>
</font>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
<!-- Previous page link -->
<xsl:if test="$aa > 1">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="$aa - 1"/>
<xsl:text>.html</xsl:text>
</xsl:attribute>
<xsl:text>Previous Page</xsl:text>
</xsl:element>
</xsl:if>
<xsl:text> </xsl:text>
<!-- Link for each page -->
<xsl:for-each-group select="//person" group-by="floor((position() - 1) div $pages)">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="position()"/>
<xsl:text>.html</xsl:text>
</xsl:attribute>
<xsl:value-of select="position()"/>
<xsl:text> </xsl:text>
</xsl:element>
</xsl:for-each-group>
<xsl:text> </xsl:text>
<!-- Next page link -->
<xsl:if test="$aa < (floor(count(//person) div $pages))">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="$aa + 1"/>
<xsl:text>.html</xsl:text>
</xsl:attribute>
<xsl:text>Next Page</xsl:text>
</xsl:element>
</xsl:if>
</body>
</html>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
The transformation for this stylesheet needs to be done with a processor which supports version 2.0 (Saxon 8 for example)

Hope this helps,
Regards, Radu.