Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
If you wanted to be clever you could handle the
,
,
, . . . ,
select=“translate($this, ‘12345678’, ‘23456789’)”/>
group-starting-with=“*[name()=$next]”>
Using group-ending-with
The
group-ending-with
option complements
group-starting-with
by matching the last item in a group instead of the first. This requirement is far less common, but it does arise. The classical example for it is where a large document has been broken up, for transmission reasons, into small arbitrary chunks, and the last chunk carries some distinguishing characteristic such as the absence of an attribute saying
continued=“yes”
. To reconstitute the documents from the sequence of chunks,
group-ending-with
is the answer:
I have also found
group-ending-with
useful on occasions when writing an up-conversion to XML from formats such as Excel spreadsheets, where the last line of a group can be detected by the presence of a word such as
TOTAL
or
SUBTOTAL
in a particular column.
Arranging Data in Tables
Arranging data in tables is a common requirement when generating HTML pages, and the
towns.xml
,
towns-by-rows.xsl
, and
towns-by-columns.xsl
.
If you need to arrange data in rows, like this:
the simplest approach is this, where
$cols
is the number of columns required:
group-adjacent=“(position()-1) idiv $cols”>
If the data needs to be sorted first, use the
group-adjacent=“(position()-1) idiv $cols”>
…
The
If you need to generate empty table cells to fill up the last row, one convenient way is to add them to the sequence before you start:
select=“(count(towns) idiv $cols)*$cols + $cols - count(towns)”/>
if ($gaps = $cols) then () else for $i in 1 to $gaps return ‘ ’”/>
…
If you want to arrange the data in columns, like this:
then it is probably simplest to use
group-by
. The grouping key (the things that the towns in a particular row have in common) is the value of
position() mod 3
where 3 is the number of rows, which you can calculate as
count($cells) idiv $cols
):