Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The following example does something very similar to this: it displays a scene from a play, adding at the start of the page a list of the characters who appear in this scene:
Example: Using Modes
This example uses a mode to create a list of characters appearing in a scene of a play.
Source
The source file,
scene.xml
, contains a scene from a play (specifically, Act I Scene 1 of Shakespeare's
Othello
—marked up in XML by Jon Bosak).
It starts like this:
etc.
Stylesheet
The stylesheet
scene.xsl
is designed to display this scene in HTML. This is how it starts:
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
version=“2.0”>
Cast:
mode=“cast-list”/>
The template rule shown above matches the
speakers
to be a sequence containing all the distinct
The template rule then calls
cast-list
(a nice side effect is that they will be listed in order of appearance). Finally, it calls
*
) except
The stylesheet carries on as follows:
This template rule defines how the
cast-list
mode. The sequence constructor has the effect of outputting the speaker's name, followed by a comma if this is not the last speaker in the list.
Finally, the remaining template rules define how each element should be output, when processed in default mode. Note that there are two different rules for
STAGEDIR
, depending on where it appears:
There is potentially a simpler solution to this requirement: the cast list can be constructed using the expression
string-join(distinct-values(//SPEAKER), ’, ’)
. However, using
distinct-values()
is undefined.