Created
October 17, 2011 20:31
-
-
Save greystate/1293712 to your computer and use it in GitHub Desktop.
Grouping a set of nodes into sets of [n] elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<!-- | |
Grouping a set of nodes into sets of [n] elements | |
--> | |
<xsl:stylesheet | |
version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
> | |
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> | |
<xsl:variable name="nodes" select="/numbers/number" /> | |
<xsl:variable name="groupSize" select="5" /> | |
<xsl:template match="/"> | |
<!-- Apply the "group" mode template to the first element of every group --> | |
<xsl:apply-templates select="$nodes[(position() mod $groupSize) = 1]" mode="group" /> | |
</xsl:template> | |
<!-- Template for 1st item in every group --> | |
<xsl:template match="*" mode="group"> | |
<ul> | |
<!-- Apply the "item" mode template to all elements in the group --> | |
<xsl:apply-templates select=". | following-sibling::*[position() < $groupSize]" mode="item" /> | |
</ul> | |
</xsl:template> | |
<!-- Template for every item --> | |
<xsl:template match="*" mode="item"> | |
<li> | |
<xsl:value-of select="." /> | |
</li> | |
</xsl:template> | |
</xsl:stylesheet> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- Dummy data for testing --> | |
<numbers> | |
<number>1</number> | |
<number>2</number> | |
<number>3</number> | |
<number>4</number> | |
<number>5</number> | |
<number>6</number> | |
<number>7</number> | |
<number>8</number> | |
<number>9</number> | |
<number>10</number> | |
<number>11</number> | |
<number>12</number> | |
</numbers> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment