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
4.85Mb 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

Beauty Is a Wound by Eka Kurniawan, Annie Tucker
Murder Team by Chris Ryan
Hero Duty by Jenny Schwartz
99 Days by Katie Cotugno
The Things We Keep by Sally Hepworth
One Bird's Choice by Iain Reid
The Sherbrooke Bride by Catherine Coulter
Black Otter Bay by Vincent Wyckoff