XSLT to generate SVG tutorial: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 41: Line 41:
# Generate an HTML5 file with inline SVG. All modern browsers can display that.
# Generate an HTML5 file with inline SVG. All modern browsers can display that.


=== Example level 0 ===


We firstly will show how to produce some SVG graphic from the contents of a most simple XML document. We then show how to do this for [[HTML5]].
Take the following XML file:
<source lang="XML">
<?xml version="1.0" ?>
<?xml-stylesheet href="zero.xsl" type="text/xsl" ?>
<thing>
  <height>50</height>
  <width>100</width>
</thing>
</source>
We can imagine rendering this as a nice red rectangle. There we have to write a template that will extract contents of ''height'' and ''width'' and then use this to define the dimensions of the SVG rectangle. Absolute beginners should notice that the names of these XML tags do not matter, we also could have called these elements ''a'' and ''b''...
Below is the XSLT code. As you can see, it is fairly straight forward. With respect to XSLT to HTML transforms, there are two differences:
* We should declare the SVG namespace in the ''xsl:stylesheet'' root element on top
* We should configure the output produced, else your browser may not want to display the SVG code.
<source lang="XML">
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2000/svg"
>
  <xsl:output
      method="xml"
      indent="yes"
      standalone="no"
      doctype-public="-//W3C//DTD SVG 1.1//EN"
      doctype-system="http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"
      media-type="image/svg" />
 
  <xsl:template match="thing">
    <svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" >
      <rect x="10" y="10" width="{width}"
    height="{height}" fill="red" stroke="black"/> 
    </svg>
  </xsl:template>
 
</xsl:stylesheet>
</source>
Now let us examine the template that deals with ''thing''. Since we do not use a template for the root /''/''), we firstly must generate the svg tag.
<source lang="XML">
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" >
  .....
</svg>
</source>
Next, we simply have to define an SVG rectangle and substitute the values for width and height by the values that we extract from the XML.
''{width}'' and ''{height}'' will be substituted by text found within &lt;width&gt; and &lt;height&gt; tags in the XML document.
Live Code (may include slight variations):
* [http://tecfa.unige.ch/guides/svg/ex/html5-xslt/zero.xml zero.xml]
* [http://tecfa.unige.ch/guides/svg/ex/html5-xslt/zero.xsl zero.xsl]


== Links ==
== Links ==


* [http://www.w3.org/People/maxf/papers/2002-07-SVGOpen/svgopen.html Using XSLT and SVG together: a survey of case studies] by Max Froumentin, Vincent Hardy, W3C, W3C/Sun Microsystems Inc. (2004). This is a somewhat outdated text, but I took some ideas from the introduction.
* [http://www.w3.org/People/maxf/papers/2002-07-SVGOpen/svgopen.html Using XSLT and SVG together: a survey of case studies] by Max Froumentin, Vincent Hardy, W3C, W3C/Sun Microsystems Inc. (2004). This is a somewhat outdated text, but I took some ideas from the introduction.

Revision as of 17:11, 20 March 2013

<pageby nominor="false" comments="false"/>

This article or section is currently under construction

In principle, someone is working on it and there should be a better version in a not so distant future.
If you want to modify this page, please discuss it with the person working on it (see the "history")

Introduction

This tutorial will teach you how to generate SVG graphics from XML data. We will produce both pure SVG and HTML5 embedded SVG.

Learning goals

  • Understand the purpose of XSLT, i.e. be able to think of XSLT as a translation language.
  • Do simple transformations from XML to HTML
  • Be able to use simple XPath expressions (tag and attribute names) in template selectors and for element and attribute extraction.
Prerequisites
Next steps

My first SVG from XML

Generating SVG with XSLT is really easy if you master some basic XSLT programming as explained in the XSLT Tutorial - Basics. Of course, you also should have some SVG basics.

At the time of writing, you can serve SVG in three different ways on the web:

  1. Embedded SVG in XHTML 1. This requires that you serve XHTML as XML. If you don't understand what that means, skip this option.
  2. Generate a pure SVG file. All modern browsers can display that.
  3. Generate an HTML5 file with inline SVG. All modern browsers can display that.

Example level 0

We firstly will show how to produce some SVG graphic from the contents of a most simple XML document. We then show how to do this for HTML5.

Take the following XML file:

<?xml version="1.0" ?>
<?xml-stylesheet href="zero.xsl" type="text/xsl" ?>
<thing>
  <height>50</height>
  <width>100</width>
</thing>

We can imagine rendering this as a nice red rectangle. There we have to write a template that will extract contents of height and width and then use this to define the dimensions of the SVG rectangle. Absolute beginners should notice that the names of these XML tags do not matter, we also could have called these elements a and b...

Below is the XSLT code. As you can see, it is fairly straight forward. With respect to XSLT to HTML transforms, there are two differences:

  • We should declare the SVG namespace in the xsl:stylesheet root element on top
  • We should configure the output produced, else your browser may not want to display the SVG code.
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" 
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns="http://www.w3.org/2000/svg"
		>
  <xsl:output
      method="xml"
      indent="yes"
      standalone="no"
      doctype-public="-//W3C//DTD SVG 1.1//EN"
      doctype-system="http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"
      media-type="image/svg" />
  
  <xsl:template match="thing">
    <svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" >
      <rect x="10" y="10" width="{width}" 
	    height="{height}" fill="red" stroke="black"/>  
    </svg>
  </xsl:template>
  
</xsl:stylesheet>

Now let us examine the template that deals with thing. Since we do not use a template for the root //), we firstly must generate the svg tag.

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" >
   .....
</svg>

Next, we simply have to define an SVG rectangle and substitute the values for width and height by the values that we extract from the XML. {width} and {height} will be substituted by text found within <width> and <height> tags in the XML document.

Live Code (may include slight variations):

Links