I have added some comments in my logic(shown below) to
help in understanding..
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" />
<xsl:param name="group-size" select="3" />
<xsl:template match="/Parent">
<!-- calculate the no of "node" elements, which are
left as a fraction; which are to be displayed in the
last group -->
<xsl:variable name="n" select="count(node) -
(floor((count(node) div $group-size)) * $group-size)"
/>
<xsl:for-each select="node">
<!-- determine group boundary; this if test stops
at the last "node" element of the group -->
<xsl:if test="(position() mod $group-size) = 0">
group <xsl:value-of select="floor(position()
div $group-size)" /><xsl:text> - </xsl:text>
<!-- display group members -->
<xsl:for-each select=". |
preceding-sibling::node[position() <= ($group-size
- 1)]">
<xsl:value-of select="." /><xsl:if
test="(position() mod $group-size) !=
0"><xsl:text>,</xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:if>
<!-- this if test processes the last group; whose
number of group members will be less than the
group-size -->
<xsl:if test="((position() = last()) and
((position() mod $group-size) != 0))">
group <xsl:value-of select="floor(position()
div $group-size) + 1" /><xsl:text> - </xsl:text>
<xsl:for-each select=". |
preceding-sibling::node[position() < $n]">
<xsl:value-of select="." /><xsl:if
test="position() !=
last()"><xsl:text>,</xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I would be happy to see any discussion on this post..
Regards,
Mukul
PS: I have posted it as a bug on Xalan site.
--- Mukul Gandhi <mukul_gandhi(_at_)yahoo(_dot_)com> wrote:
Hello,
I was solving a problem posted by a user at
microsoft.public.xsl newsgroup. I seem to have been
hit by a Xalan bug(Xalan-J 2.6.0), which I wish to
confirm with members here..
The problem posted was -
Subject: Grouping by two or any number
--------------------------------------
i have the following xml
<Parent>
<node>a</node>
<node>s</node>
<node>d</node>
<node>f</node>
<node>g</node>
<node>h</node>
<node>j</node>
<node>k</node>
<node>l</node>
</Parent>
Need to print in following format (into groups of
two)
group 1 - a,s
group 2 - d,f
group 3 - g,h
group 4 - j,k
group 5 - l
I wrote the following stylesheet -
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" />
<xsl:param name="group-size" select="3" />
<xsl:template match="/Parent">
<xsl:variable name="n" select="count(node) -
(floor((count(node) div $group-size)) *
$group-size)"
/>
<xsl:for-each select="node">
<xsl:if test="(position() mod $group-size) =
0">
group <xsl:value-of select="floor(position()
div $group-size)" /><xsl:text> - </xsl:text>
<xsl:for-each select=". |
preceding-sibling::node[position() <=
($group-size
- 1)]">
<xsl:value-of select="." /><xsl:if
test="(position() mod $group-size) !=
0"><xsl:text>,</xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:if test="((position() = last()) and
((position() mod $group-size) != 0))">
group <xsl:value-of select="floor(position()
div $group-size) + 1" /><xsl:text> - </xsl:text>
<xsl:for-each select=". |
preceding-sibling::node[position() < $n]">
<xsl:value-of select="." /><xsl:if
test="position() !=
last()"><xsl:text>,</xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I invoke Xalan like this:
java org.apache.xalan.xslt.Process -in file.xml -xsl
file.xsl -PARAM group-size 2
The output is fine for -PARAM group-size values of
1,2,3 & 4. But for -PARAM group-size values of 5 and
above, the ouput is not coming as expected. For
e.g.,
for -PARAM group-size 5, the output received is -
group 1 - a,s,d,f,g
group 2 - h,
I tested the same XSL with Saxon-SA 8.1.1 and MSXSL
4,
and both produce correct result for all the cases.
Both Saxon and MSXSL produce the following output
for
group-size=5 -
group 1 - a,s,d,f,g
group 2 - h,j,k,l
Does this look like a Xalan bug?
Regards,
Mukul
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--