Re: [xsl] Newbie - XML and XSLT samples to produce XHTML Files please

Subject: Re: [xsl] Newbie - XML and XSLT samples to produce XHTML Files please
From: tom.kirkpatrick@xxxxxxxxxxxx
Date: Thu, 27 Jan 2005 13:35:07 +0000
thanks.

heres the xsl page which displays an article
(some templates are stored in other files).

It's currently very very, VERY messy, and needs sorting out badly!
(it has evolved from something very basic, into something
a lot more complicated and I havn't had the time to try to be
tidy while I code).

I use AxKit to apply the transformations to the pages.
It works as a kind of pipeline, which you can configure
in your apache configuration file.

If your thinking of running an xml/xsl based site, you
should definatly check out AxKit, although the site seems to
be down right now.



<?xml version = "1.0"?>

<!--
        Virus Bulletin Stylesheet
 
                ** VB Article **
                Tom Kirkpatrick - webmaster@xxxxxxxxxxxx
                Last Edited: 01-Nov 04
-->

<xsl:stylesheet
        xmlns:xsl = "http://www.w3.org/1999/XSL/Transform";
        version = "1.0">

        <xsl:output method = "xml" indent = "yes"/>
 
        <xsl:template match = "article">
                <xsl:param name="dissabled"></xsl:param>
                <xsl:param name="embeded"></xsl:param>
 
                        <!-- Set the magazine type -->
                        <xsl:variable name="magazine">
                                <xsl:choose>
                                        <xsl:when 
test="@magazine"><xsl:value-of select = "@magazine" /></xsl:when>
 <xsl:otherwise>magazine</xsl:otherwise>
                                </xsl:choose>
                        </xsl:variable>
 

                        <!-- Get the edition detais -->
                        <xsl:variable name = "edition_month" select = 
"substring(@edition, 5, 6)" />
                        <xsl:variable name = "edition_year" select = 
"substring(@edition, 1, 4)" />
 
                        <!-- Get the date details -->
                        <xsl:variable name = "day"      select = 
"substring(@date, 7, 8)" />
                        <xsl:variable name = "month"    select = 
"substring(@date, 5, 6)" />
                        <xsl:variable name = "year"     select = 
"substring(@date, 1, 4)" />
 
                        <!-- Get the name of the category -->
                        <xsl:variable name = "displaycategory">
                                <xsl:if test = "@type">
                                        <xsl:value-of select = 
"document(concat('/',$magazine,'/articles/index.xml'))/articles/category[@name 
= /article/@type]/@displayname" />
                                </xsl:if>
                                <xsl:if test = "@category">
                                        <xsl:value-of select = 
"document(concat('/',$magazine,'/articles/index.xml'))/articles/category[@name 
= /article/@category]/@displayname" />
                                </xsl:if>
                        </xsl:variable>
 
                        <!-- Sidebar modules -->
                        <sidebars>
                                <sidebar type = "rating" />
                                <sidebar type = "opinions" />
                                <sidebar type = "articlearchive" magazine 
= "{$magazine}" />
                        </sidebars>

                <!-- Header bits for search engine optimisation -->
 
                        <!-- page title -->
                        <xsl:element name="pagetitle">
                        <xsl:attribute name="name">
                                         <xsl:value-of select = 
"$displaycategory" /> - <xsl:value-of select = '@title' />
 
                                         <xsl:choose>
                                                        <xsl:when test = 
"keywords/keyword">
                                                                (
 <xsl:for-each select = 'keywords/keyword'>
 <xsl:if test = "position() &lt; 3">
 <xsl:value-of select = '.' />
 <xsl:if test = "position() != 2">, </xsl:if>
 </xsl:if>
 </xsl:for-each>
                                                                )
                                                        </xsl:when>
                                                </xsl:choose>
                        </xsl:attribute>
                        </xsl:element>

                        <!-- meta description information -->
                        <pagedescription name = "{@title} - An in depth 
article ({$displaycategory}) provided by independent anti-virus advisors, 
Virus Bulletin" />
 
                                          <!-- for each keyword, add a 
keyword -->
                                          <xsl:for-each select = 
'keywords/keyword'>
                                                  <xsl:element 
name="keyword">
                                                          <xsl:attribute 
name="name"><xsl:value-of select = '.' /></xsl:attribute>
                                                  </xsl:element>
                                          </xsl:for-each>
 
                        <!-- Breadcrumb elements -->
                        <levels>
                                <level depth = "1" name = "{$magazine}" 
location = "{$magazine}" />
                                <level depth = "2" name = "Issues" 
location = "issues" />
                                <level depth = "3" name = 
"{document('/global/autodate.xml')/date/month[@position = 
$edition_month]/@name} {$edition_year}" location = 
"{$edition_year}/{$edition_month}" />
                                <level depth = "4" name = "{@title}" 
location = "{@filename}" />
                        </levels>

                        <!-- Page heading (h1) -->
                        <heading><xsl:value-of select = "$displaycategory" 
/>: <xsl:value-of select = "@title" /></heading>
 
                        <!-- Article head -->
                        <section>
 
 
                        <!-- do embeded virus overview info -->
                        <xsl:if test = "not($embeded = 'true')">
                        <xsl:if test = "(//@type = 'analysis') or 
(//@category = 'analysis')">
                        <xsl:for-each select = 
"//related_viruses/related_virus">
                        <xsl:variable name = "filename" select = 
"@filename" />
                        <xsl:if test = 
"not(document('/resources/viruses/index.xml')/viruses/virus[@filename = 
$filename]/@dissabled)">
                        <xsl:variable name = "virusinfo" select = 
"document(concat('/resources/viruses/',@filename))"/>
                                <rawhtml>
                                                <div class="alpha-shadow 
right"> 
                                                        <div>
                                                                <div style 
= "border:1px solid #708E7E; margin:0px; padding:0px">
                                                                <h2 style 
="background-color:#708E7E; color:white; text-align:center; margin:0px; 
padding:2px;">
 <xsl:value-of select = "$virusinfo//@name" />
                                                                </h2>
                                                                <table 
style = "padding:0px 3px 0px 3px; margin:0px; width:200px;">
 <xsl:if test = "$virusinfo//@type">
 <tr>
        <th class = "leftContent">Type:</th>
        <td><xsl:value-of select = "$virusinfo//@type" /></td>
 </tr>
 </xsl:if>

 <xsl:if test = "$virusinfo//@exploits">
 <tr>
        <th class = "leftContent">Exploits:</th>
        <td><xsl:value-of select = "$virusinfo//@exploits" /></td>
 </tr>
 </xsl:if>
 
 <xsl:if test = "$virusinfo//@infects">
 <tr>
        <th class = "leftContent">Infects:</th>
        <td><xsl:value-of select = "$virusinfo//@infects" /></td>
 </tr>
 </xsl:if>

 <xsl:if test = "$virusinfo//@spreads">
 <tr>
        <th class = "leftContent">Spreads:</th>
        <td><xsl:value-of select = "$virusinfo//@spreads" /></td>
 </tr>
 </xsl:if>
 
 <xsl:if test = "$virusinfo//@payload">
 <tr>
        <th class = "leftContent">Payload:</th>
        <td><xsl:value-of select = "$virusinfo//@payload" /></td>
 </tr>
 </xsl:if>
 
 <xsl:if test = "$virusinfo//@removal">
 <tr>
        <th class = "leftContent">Removal:</th>
        <td><xsl:value-of select = "$virusinfo//@removal" /></td>
 </tr>
 </xsl:if>
 
 <xsl:if test = "$virusinfo//@patch">
 <tr>
        <th class = "leftContent">Patch:</th>
        <td><xsl:value-of select = "$virusinfo//@patch" /></td>
 </tr>
 </xsl:if>
                                                                </table>
                                                                <p style = 
"text-align:right; padding:0px 3px 4px 3px; margin:0px;">
                                                                        <a 
href = "/resources/viruses/{@filename}">
 more info &gt;
 </a>
                                                                </p>
                                                                </div>
                                                        </div>
                                                </div>
                                </rawhtml>
                        </xsl:if>
                        </xsl:for-each>
                        </xsl:if>
                        </xsl:if>
 
                        <!-- do author info -->
                        <xsl:for-each select = "authors/author[@id] | 
author[@id]">
                                <xsl:variable name = "id" select = "@id" 
/>
                                <!-- check author exists in database -->
                                <xsl:if test = 
"document('/global/authors.xml')/authors/author[@id = @id]">
 
                                        <!-- if we have an image... -->
                                                <xsl:if test = 
"document('/global/authors.xml')/authors/author[@id = $id]/image">
                                                  <!-- do image -->
                                                          <rawhtml>
                                                                  <div 
class="right rightContent" style="clear:both;">
 <table padding="0" margin="0" width="135">
        <tr><td>
                <div class="alpha-shadow right"> 
                        <div>
                        <img 
src="{document('/global/authors.xml')/authors/author[@id = $id]/image}" 
alt="VB{/issue/@edition}" title="VB{/issue/@edition}" />
                        </div>
                </div>
        </td></tr>
        <tr><td style = "line-height:12px; padding-right:5px;" class = 
"rightContent">
 <b><xsl:value-of select = 
"document('/global/authors.xml')/authors/author[@id = $id]/title" />
 <xsl:text> </xsl:text><xsl:value-of select = 
"document('/global/authors.xml')/authors/author[@id = $id]/firstname" />
 <xsl:text> </xsl:text><xsl:value-of select = 
"document('/global/authors.xml')/authors/author[@id = $id]/lastname" />
 </b>
 <xsl:if test = "document('/global/authors.xml')/authors/author[@id = 
$id]/company/name">
        <br />
        <xsl:value-of select = 
"document('/global/authors.xml')/authors/author[@id = $id]/company/name" 
/>
 </xsl:if>
 <xsl:if test = "document('/global/authors.xml')/authors/author[@id = 
$id]/company/country">
        , <xsl:value-of select = 
"document('/global/authors.xml')/authors/author[@id = 
$id]/company/country" />
 </xsl:if>
 </td></tr>
 </table>
 </div>
                                                          </rawhtml>
                                                </xsl:if>
                                </xsl:if>
                        </xsl:for-each>


 
                <!-- Show when it was published -->
                        <xsl:if test = "@edition">
                                <rawhtml>
                                <b>Published in Virus Bulletin: </b>
                                <a href = 
"/magazine/issues/{$edition_year}/{$edition_month}.xml">
                                        <xsl:value-of select = 
"document('/global/autodate.xml')/date/month[@position = 
$edition_month]/@name" />
                                        <xsl:text> 
</xsl:text><xsl:value-of select = "$edition_year" />
                                </a>
                                <br />
                                </rawhtml>
                        </xsl:if> 

                <!-- if there is no image display author in text at the 
top-->
                        <xsl:for-each select = "authors/author[@id] | 
author[@id]">
                                <xsl:variable name = "id" select = "@id" 
/>
                                <!-- check author exists in database -->
                                <xsl:if test = 
"document('/global/authors.xml')/authors/author[@id = @id]">
                                        <xsl:if test = 
"not(document('/global/authors.xml')/authors/author[@id = $id]/image)">
                                                <rawhtml>
                                                        <xsl:if test = 
"position() = 1"><b>By: </b></xsl:if>
                                                        <xsl:value-of 
select = "document('/global/authors.xml')/authors/author[@id = $id]/title" 
/>
                                                        <xsl:text> 
</xsl:text><xsl:value-of select = 
"document('/global/authors.xml')/authors/author[@id = $id]/firstname" />
                                                        <xsl:text> 
</xsl:text><xsl:value-of select = 
"document('/global/authors.xml')/authors/author[@id = $id]/lastname" />
                                                        - 
                                                        <xsl:if test = 
"document('/global/authors.xml')/authors/author[@id = $id]/company/name">
 <xsl:value-of select = 
"document('/global/authors.xml')/authors/author[@id = $id]/company/name" 
/>
                                                        </xsl:if>
                                                        <xsl:if test = 
"document('/global/authors.xml')/authors/author[@id = 
$id]/company/country">,
                                                        <xsl:value-of 
select = "document('/global/authors.xml')/authors/author[@id = 
$id]/company/country" />
                                                        </xsl:if>
                                                        <br />
                                                </rawhtml>
                                        </xsl:if>
                                </xsl:if>
                        </xsl:for-each>
 
                <xsl:for-each select = "authors/author[not(@id)] | 
author[not(@id)]"> 
                                <!-- support for legacy author entries -->
                                        <rawhtml>
                                                        <xsl:if test = 
"position() = 1"><b>By: </b></xsl:if>
                                                <xsl:value-of select = 
"@name" />
                                                <xsl:if test = "@company 
or @country">
                                                        - <xsl:value-of 
select = "@company" />, <xsl:value-of select = "@country" />
                                                </xsl:if>
                                                <br />
                                        </rawhtml>
                </xsl:for-each>
 
        </section>
 
                <!-- Article body -->
                <xsl:choose>
                        <xsl:when test = "$dissabled">
                                <section>
                                        [The latest articles are only 
available to <link location = 
"/perlbin/subscribe.cgi:SSL">subscribers</link>]
                                </section>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:choose>
                                        <xsl:when test = "body">
                                                <xsl:for-each select = 
"body">
 <xsl:apply-templates />
                                                </xsl:for-each>
                                        </xsl:when>
                                        <xsl:otherwise>
                                                <xsl:apply-templates />
                                        </xsl:otherwise>
                                </xsl:choose>
                        </xsl:otherwise>
                </xsl:choose>

        </xsl:template>
 
</xsl:stylesheet>




Julian Voelcker <asp@xxxxxxx>
27/01/2005 11:47
Please respond to xsl-list

 
        To:        xsl-list@xxxxxxxxxxxxxxxxxxxxxx
        cc: 
        Subject:        Re: [xsl] Newbie - XML and XSLT samples to produce XHTML Files please


Hi Tom,

> www.virusbtn.com

Nice site.

> This is the site I am currently working on. The entire site is driven by
> XML/XSL

Any chance of letting me look at the XML/XSL to generate one of the pages
to help me on my way........ Pretty please :-)
--
Cheers,

Julian Voelcker
United Kingdom




-- 
Tom David Kirkpatrick
Virus Bulletin Web Developer, Virus Bulletin

Tel: +44 1235 555139
Web: www.virusbtn.com

Current Thread