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

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

With XSLT 2.0, it is no longer necessary to use a secondary document for these purposes, because the data can be held in a tree-valued variable in the stylesheet and accessed directly. However, it may in some cases be more convenient to maintain the data in a separate file (for example, it makes it easier to generate the data periodically from a database), and in any case you may still want to write stylesheets that work with XSLT 1.0 processors, especially if you want the transformation to happen client-side. So I'll show the XSLT 1.0 technique first and then show how the same problem can be tackled in XSLT 2.0.

XSLT allows data such as look-up tables to appear within any top-level stylesheet element, provided it belongs to a non-default namespace.

Example: A Look-Up Table in the Stylesheet

This example uses data in a look-up table to expand abbreviations of book categories. Two techniques are shown: in the first example the look-up table is held in the stylesheet; in the second example it is held in a separate XML document.

Source

This is the
booklist.xml
file we saw earlier:



     Number, the Language of Science

     Danzig



     The Young Visiters

     Daisy Ashford



     When We Were Very Young

     A. A. Milne



     Design Patterns

     Erich Gamma

     Richard Helm

     Ralph Johnson

     John Vlissides



Stylesheet

The stylesheet is
list-categories.xsl
. It processes each of the

elements in the source file and, for each one, finds the

element in the stylesheet whose
code
attribute matches the
category
attribute of the

. Note the use of
current()
to refer to the current book; it would be wrong to use
.
here, because
.
refers to the context node, which is the

element being tested.

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

 version=“1.0”

 xmlns:book=“http://ns.wrox.com/books.uri”

 exclude-result-prefixes=“book”

>


  

    

       

       

Category: 

                      select=“document(”)/*/book:category

                      [@code=current()/@category]/@desc”/>

       


    

  






Output

The output of this stylesheet is as follows:


   

      Number, the Language of Science

      

Category: Science


      The Young Visiters

      

Category: Children's Fiction


      When We Were Very Young

      

Category: Children's Fiction


      Design Patterns

      

Category: Computing


   


XSLT 2.0 Stylesheet

Now, let's modify this stylesheet to take advantage of XSLT 2.0 facilities. It's renamed
list-categories2-0.xsl
. It isn't a big change; the lines that are different are shown with a shaded background.

 

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

 version=“2.0”

 >

 

   

     

        

        

Category: 

                       select=“$categories[@code=current()/@category]/@desc”/>

        


     

   

 

  

  

  

  

  

 

Supplying an Explicit Base URI

This section discusses what happens when the second argument to the
document()
function is supplied. In this instance, instead of using the containing node or the stylesheet as the base for resolving a relative reference, the base URI of the node supplied as the second argument is used. In other words, if a node in
href
contains a relative reference such as
data.xml
, the system will look for the file
data.xml
in the directory containing the XML document from which the node in
$base
was derived.

The value of the second argument must be a single node. For example, the call
document (@href, /)
will use the root node of the source document as the base URI, even if the element containing the
href
attribute was found in an external entity with a different URI.

Other books

Secrets of a Perfect Night by Stephanie Laurens, Victoria Alexander, Rachel Gibson
Phantom Riders MC - Hawk by Tory Richards
A Pocket Full of Seeds by Marilyn Sachs
The Dragon's Son by Margaret Weis
Last Surgeon by Michael Palmer
Back in Service by Rosanna Challis
A Ghost of Brother Johnathan's by Elizabeth Eagan-Cox
Call Me Mrs. Miracle by Debbie Macomber