Let's say I have this XML node:
<items>
<item>...<item>
<item>...<item>
<item>...<item>
<item>...<item>
<item>...<item>
...
</items>
where there are N item
nodes.
Now I would like to transform it into an HTML table with 4 columns. (e.g. if N=12, there are 3 complete rows, and if N=27, there are 7 rows, the last having 3 cells)
How could I go about doing this?
My gut call is to do it this way, where {{something}}
is what I don't know how to implement:
<xsl:template match="items">
<table>
<xsl:call-template name="partition-items">
<xsl:with-param name="skip" select="0" />
</xsl:call-template>
</table>
</xsl:template>
<xsl:template name="partition-items">
<xsl:param name="skip" />
{{ if # of items in current node > $skip,
output a row,
and call partition-items($skip+4)
}}
<xsl:template />
The pieces I don't know how to implement, are
- how to make a predicate for testing the # of
item
elements in the current node - how to get the Nth
item
element in the current node
Update from comments
How to pad the last row with empty
<td />
elements so that each row contains exactly the wanted cells?
With for-each-group you can get a more elegant solution:
That's my working solution.
As you didn't provide a desired output, this particular one may be uncomplete for your needs.
Test input:
Output:
Do note: you can pass columns number dynamically.
Additional requirements and edit.
It can be applied to the previous sample or to this concise XML:
Result will be:
Do note:
item
elements than number of columns is.If there won't ever be less elements than number of columns, you can just apply to the
item
elements with the same predicate and a differentmode
.And last edit. With a counted loop.
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:
the wanted, correct result is produced:
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 theitem
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
<xsl:for-each-group>
instruction is used to select the different groups ofitem
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.Just for style, this XSLT 1.0 stylesheet:
With @Flack's answer input, output: