I have few titles which I am getting by having for loop around some xml(It can be n no. of titles). I want to display them horizontally in 3 columns but vertically in alphabetical order:
If I have 3 titles, I am representing them with just alphabet(I can get the count of no. of titles.
A B C -----count(3)
4 titles:
A C D -----count(4)
B
5 Titles:
A C E -----count(5)
B D
7 Titles:
A D F -----count(7)
B E G
C
I am using xsl 1.0 and right now I have it like
<div class="navigation">
<ul>
<xsl:foreach select="/Custom/Alphabet/titles">
<li>
<xsl:value-of select="." />
</li>
</xsl:foreach>
</ul>
</div>
Excellent question!
I. Here is an XSLT 2.0 solution (65 lines, can be converted to XSLT 1.0 almost mechanically):
when this transformation is applied on the provided (most complex) 7-items case:
the wanted, correct result is produced:
I have verified that the expected, correct result is produced for every
N = 1 to 7.Explanation:
We are building the required table recursively on the number of items in the input sequence (
pN):The base of the recursion is for any
$pNnot greater than$pK(the required number of columns). In this basic case the table has a single row.In the general case
$pN > $pK; then we build the leftmost column$vCol-1and, recursively, a smaller table with the rest of the items and new number of required columns:$pK -1.In case 2. above, we finally merge the column and the sub-table to produce the resulting table.
II. Equivalent XSLT 2.0 solution, writenn in a “more XSLT 2.0 style” (60 lines):
III. XSLT 1.0 solution (75 lines)
This is the first XSLT 2.0 solution (above), translated almost mechanically to XSLT 1.0:
IV. Finally, a pure, generative (non-recursive) XSLT 1.0 solution:
when applied on the same XML document (above), the wanted, correct result is produced: