SVG, Unicode, and XSLT - Part 3 of Chapter 7 from Perl Graphics Programming (4/5) | WebReference

SVG, Unicode, and XSLT - Part 3 of Chapter 7 from Perl Graphics Programming (4/5)

To page 1To page 2To page 3current pageTo page 5
[previous] [next]

Perl Graphics Programming, Chapter 7: Creating SVG with Perl

Our example uses only three XSLT elements to transform the slide description format into SVG:

<template>
A template element is a block of XML that is passed on to the output tree when a piece of input text is matched. The match attribute allows you to match a tag name; every tag of the matched type has this template applied to it. For example, the following template:

<xsl:template match="slide">
    <foo>Bar</foo>
</xsl:template>
indiscriminately replaces each <slide> element (and anything contained within it) with the given <foo> tag.

<apply-templates>
This element allows you to recursively apply the same set of XSLT templates to elements or content wrapped up in other elements.

<value-of>
This element retrieves the value of an element or attribute, whose value replaces the <value-of> tag. To retrieve an attribute from the tag that has been matched with the <template> tag, use the syntax:

<xsl:template match="tag">
    <xsl:value-of select="@attribute"/>
</xsl:template>

Additionally, you can retrieve the content of any element with the tag {element}, where element is the first tag that matches that name. This is used to extract the image URL in the following example.

The XSLT script in Example 7-4 uses these basic XSLT commands to convert the source XML into a valid SVG document.

Example 7-4: An XSLT transform

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="slide">
  <svg width="600" height="400"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink">
   <text x="130" y="48" style="font-size:24;fill:#000000">
   <xsl:value-of select="@title"/>
   </text>
   <image x="20" y="20" width="100" height="100"
             xlink:href="{image}"/> 
   <text x="150" y="60" style="font-size:14;fill:#000000">
    <xsl:apply-templates/>
   </text>
  </svg>
</xsl:template>
   
<xsl:template match="textline">
   <tspan x="130" dy="20" font-size="16">
       <xsl:apply-templates/>
   </tspan> 
</xsl:template>
   
<xsl:template match="bulletlist">
   <xsl:apply-templates/> 
</xsl:template>
   
<xsl:template match="bullet">
   <tspan x="150" dy="16"> 
       &#x2022; <xsl:apply-templates/>
   </tspan> 
</xsl:template>
   
<xsl:template match="image">
</xsl:template>
   
</xsl:stylesheet>

To page 1To page 2To page 3current pageTo page 5
[previous] [next]

Created: February 26, 2003
Revised: February 26, 2003

URL: http://webreference.com/programming/perl/chap7/3/4.html