3.3 Writing the Stylesheet

     

So far, you have three XML documents that contain three very different, but randomly overlapping, grammars. (The species and name elements appear in different roles in the two main content documents.) Your goal is to make this information available on the Web to HTML browsers. You want to reach the widest possible audience, and that means maintaining the lowest possible expectations of the requesting client's capabilities. That is, you cannot rely on everyone who wants to read your pages having a thoroughly modern browser capable of doing appropriate client-side transformations to your XML documents via CSS or XSLT. You must deliver basic HTML if you expect your data to be widely accessible.

With this in mind, you need a way to transform the disparate data structures contained in each of your XML documents into the unified grammar of simple HTML.That's where AxKit's transformational languages and stylesheets enter the picture. AxKit offers many ways to transform XML data. (We will examine the merits of many of these in later chapters.) In this example, we examine how you can transform your cryptozoology documents into HTML using two of the more popular transformation languages: XSLT and XPathScript.

I will save the examination of the lower-level details of these languages for later. At this point, it suffices to understand that both XSLT and XPathScript offer a declarative syntax that provides a way to create new documents by applying transformations to all or some of the elements, attributes, and other content that an existing XML document contains.

3.3.1 Using XSLT

Rather than taking small steps through the XSLT stylesheet, I present it here in one block to give you an idea of what a full, working stylesheet looks like. (See Example 3-4.) Do not worry if much of it seems foreign; we will look at the syntactic elements of XSLT in more detail in Chapter 5.

As you read through the stylesheet, keep in mind that:

  • An XSLT stylesheet itself is an XML document.

  • Transformation rules are applied based on the properties of the various parts of the source XML document (element and names , relationships between elements, etc.).

  • Template rules are created to match all elements of the grammars found in your XML documents, so the same stylesheet can be used to transform both the list of species and the list of sightings.

Example 3-4. cryptozoo.xsl
 <?xml version="1.0"?> <xsl:stylesheet    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    version="1.0"   > <xsl:output       method="html" encoding="ISO-8859-1" /> <xsl:template match="/"> <html>   <head><title>Cryptozoology Pages</title>   <link rel="stylesheet" type="text/css" href="crypto.css"/>   </head>   <body>   <div class="header">   <h1>My Cryptozoology Pages</h1>   </div>   <div class="nav">     <xsl:apply-templates select="document('nav.xml', /)/*"/>   </div>   <div class="content">     <xsl:apply-templates/>   </div>   </body>  </html> </xsl:template> <!-- top-level templates --> <xsl:template match="animals">   <p>     Today's rural legend is tomorrow's newly      discovered species.   </p>   <xsl:apply-templates/> </xsl:template> <xsl:template match="sightings">   <p>     Here is a list of sightings.   </p>   <xsl:apply-templates/> </xsl:template> <xsl:template match="animals/species"> <div class="species">   <xsl:apply-templates/> </div> </xsl:template> <xsl:template match="sighting"> <div class="sighting">   <xsl:apply-templates/> </div> </xsl:template> <xsl:template match="species/name"> <h2>   <xsl:apply-templates/> </h2> </xsl:template> <xsl:template match="species/habitat">   <p>     <i>Habitat:</i>     <xsl:text> </xsl:text>     <xsl:value-of select="."/>   </p>   <xsl:apply-templates select="*"/> </xsl:template> <xsl:template match="sighting/locationsighting/speciessighting/date">   <div class="subheading">     <i><xsl:value-of select="name( )"/>:</i>     <xsl:text> </xsl:text>     <xsl:value-of select="."/>   </div>   <xsl:apply-templates select="*"/> </xsl:template>  <xsl:template match="witnesses">    <div>      <i>witnesses:</i>      <xsl:text> </xsl:text>      <xsl:for-each select="name">        <xsl;value-of select="." />        <xsl:if test="position( ) != last( )">, </xsl:if>      </xsl:for-each>    </div> </xsl:template> <xsl:template match="witnesses/name">   <xsl:text> </xsl:text>   <xsl:value-of select="."/> </xsl:template> <xsl:template match="parap"> <p>   <xsl:apply-templates/> </p> </xsl:template> <xsl:template match="italic"> <i>   <xsl:apply-templates/> </i> </xsl:template> <xsl:template match="a">   <xsl:copy-of select="."/><br/> </xsl:template> </xsl:stylesheet> 

3.3.2 Using XPathScript

Compare the previous XSLT stylesheet, which transforms your cryptozoology documents into HTML, with the one below, written in XPathScript. I will leave the syntactic details for Chapter 6; however, as you read through the stylesheet, note that most moving parts are written in Perl and separated from the larger template document using <% and %> as delimiters.

 <% # declarative templates $t->{'p'}{pre} = '<p>'; $t->{'p'}{post} = '</p>'; $t->{'para'}{pre} = '<p>'; $t->{'para'}{post} = '</p>'; $t->{'animals'}{pre} = qq   <p>     Today's rural legend is tomorrow's newly     discovered species.   </p> ; $t->{'sightings'}{pre} = qq   <p>     Here is a list of sightings.   </p> ; $t->{'sighting'}{pre} = '<div class="sighting">'; $t->{'sighting'}{post} = '</div>'; $t->{'habitat'}{pre} =  '<p><i>Habitat:</i> '; $t->{'habitat'}{post} =  '</p>'; $t->{'location'}{pre} = '<div class="subheading"><i>location: </i>'; $t->{'location'}{post} = '</div>'; $t->{'date'}{pre} = '<div class="subheading"><i>date: </i>'; $t->{'date'}{post} = '</div>'; $t->{'witnesses'}{pre} = '<div><i>witnesses: </i>'; $t->{'witnesses'}{post} = '</div>'; $t->{'a'}{showtag} = 1; # testcode templates for like-named elements $t->{'name'}{testcode} = sub {   my ($node, $t) = @_;   if (findnodes('parent::species', $node)) {       $t->{pre} = '<h2>';       $t->{post} = '</h2>';   }   return 1; }; $t->{'species'}{testcode} = sub {   my ($node, $t) = @_;   if (findnodes('parent::animals', $node)) {      $t->{pre} = '<div class="species">';      $t->{post} = '</div>';   }   else {     $t->{pre} = '<div class="subheading"><i>species: </i>';     $t->{post} = '</div>';   }   return 1; }; %> <html>   <head><title>Cryptozoology Pages</title></head>   <link rel="stylesheet" type="text/css" media="screen" href="crypto.css"/>   <body>   <div class="header">   <h1>My Cryptozoology Pages</h1>   </div>   <div class="nav">     <%= apply_templates( "document('nav.xml')" ) %>   </div>   <div class="content">     <%= apply_templates( ) %>   </div>   </body> </html> 

Do not be overwhelmed. Remember that most sites typically use either XSLT or XPathScript and only rarely both, so you need not try to take in both at once. These duplicated examples only intend to show that with AxKit, as with Perl, there is always more than one way to do it. You are free to choose the tools and techniques that suit you best.



XML Publishing with AxKit
XML Publishing with Axkit
ISBN: 0596002165
EAN: 2147483647
Year: 2003
Pages: 109
Authors: Kip Hampton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net