XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (182 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
10.6Mb size Format: txt, pdf, ePub

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:



Rupert Brooke

1912

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.


etc.


Stylesheet A

Here is
standard-style.xsl
:

    xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>


  

    

      <xsl:value-of select=“//title”/>

    

    

      

    

  



    



   by 

      

   




   




   




Output A

When you run this stylesheet, the output starts like this (indented for clarity):


   

     

     Song

   

   

     by Rupert Brooke


     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:

Other books

Solomon's Grave by Keohane, Daniel G.
In the Darkness by Karin Fossum
Lawman's Pleasure (sWet) by Karland, Marteeka
Wings of War by John Wilson
The Forbidden Daughter by Shobhan Bantwal
Telling Tales by Charlotte Stein
Even Angels Fall by Fay Darbyshire
Victoria's Demon Lover by Bess, Alia
Wings of Love by Jeanette Skutinik
Payback by Kimberley Chambers