Let\'s say I have this XML node:
- ...
-
- ...
-
- ...
-
-
I. XSLT 1.0 solution:
Here is probably one of the shortest possible solutions that notably doesn't require explicit recursion:
when this transformation is applied on the following XML document:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
the wanted, correct result is produced:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Explanation:
The wanted number of cells per row is specified in the external/global parameter $pNumCols.
Templates are applied only on such children of the top element, whose position is the start of a new row -- they are generated by the expression $k * $pNumCols +1, where $k can be any integer.
The template that processing each row-starting item creates a row (tr element) and within it applies templates in a special mode "copy" for the $pNumCols starting with itself.
The template matching an item in mode "copy" simply creates a cell (td element) and outputs inside it the string value of the item element being matched.
II. XSLT 2.0 solution:
applied on the same XML document as before, this transformation produces the same, correct result.
Explanation:
The item elements where each group contains the elements that must be represented in one row.
The standard XPath 2.0 operator idiv is used for this purpose.
The XSLT 2.0 function current-group() contains all items that must be presented in the current row.