Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Example 2: Precedence of Template Rules
In this example I shall define a complete stylesheet
standard-style.xsl
to display poems in HTML, and then override one of its rules in an importing stylesheet. The files required are all in the subdirectory
import
in the download file for this chapter.
Source
This example works with the poem that we used in Chapter 1. In the download file it's available as
poem.xml
. It starts like this:
etc.
Stylesheet A
Here is
standard-style.xsl
:
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
Output A
When you run this stylesheet, the output starts like this (indented for clarity):
Song
And suddenly the wind comes soft,
And Spring is here again;
And the hawthorn quickens with buds of green
And my heart with buds of pain.
Stylesheet B
Now we want to create a variant of this in which the lines of the poem are numbered. This will act as the principal stylesheet when you want this form of output. Here it is in
numbered-style.xsl
:
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
Note the use of the character reference
to output a nonbreaking space. In HTML this is normally done by writing
. You can use this entity reference in the stylesheet if you like (it's simply a symbolic name for the Unicode character
XA0
), but only if you declare it as an entity in the DTD. It's usually simpler just to use the numeric character reference.
Output B
This time the output starts like this. Again, the precise format depends on the processor (for example, some processors may output
, or
instead of
), but it should look the same when displayed in the browser: