XSL Free Tutorial

Web based School

Stylesheet Examples


Next Next


Example 1. A Stylesheet

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

Example 2. A Transformation Sheet

<eg:transform xmlns:eg="http://www.w3.org/1999/XSL/Transform"
               version="1.0">
    ...
</eg:transform>

Example 3. Document as Stylesheet

<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<head>
<title>Silly Example</title>
</head>
<body>
<h1>Silly Example</h1>
<p>You'd probably use extension elements, or somthing
more interesting in real life: 3+4 is <xsl:value-of select="3+4"/>.
</p>
</body>
</html>


A Complete Example

This is a simple stylesheet that transforms source <para> and <emphasis> elements into HTML:

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

<xsl:template match="para">
  <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="emphasis">
  <i><xsl:apply-templates/></i>
</xsl:template>

</xsl:stylesheet>

With this stylesheet, the following XML document:

<?xml version='1.0'?>
<para>This is a <emphasis>test</emphasis>.</para>

Would be transformed into:

<?xml version="1.0" encoding="utf-8"?>
<p>This is a <i>test</i>.</p>

Note that this has been serialized as XML.


Understanding A Template

Most templates have the following form:

<xsl:template match="emphasis">
  <i><xsl:apply-templates/></i>
</xsl:template>
  • The whole <xsl:template> element is a template

  • The match pattern determines where this template applies

  • Literal result element(s) come from non-XSL namespace(s)

  • XSLT elements come from the XSL namespace


Match Patterns (Locating Elements)

One critical capability of a stylesheet language is to locate source elements to be styled. CSS, for example, does this with "selectors." FOSIs do it with "e-i-c's", elements in context. XSLT does it with "match patterns" defined by the XML Path Language (XPath) (http://www.w3.org/TR/xpath).

  • XPath has an extensible string-based syntax

  • It describes "location paths" between parts of a document or documents

  • One inspiration for XPath was the common "path/file" file system syntax

Two things to remember about XPath expressions:

  • Pattern matching occurs in a context; XPath expressions and XSLT elements can change the current context and consequently the nodes which match

  • XPath is inclusive or greedy, it addresses all matching elements. You must use predicates to refine the set of nodes selected.


Pattern Examples
para

Matches all <para> children in the current context

para/emphasis

Matches all <emphasis> elements that have a parent of <para>

/

Matches the root of the document

para//emphasis

Matches all <emphasis> elements that have an ancestor of <para>

section/para[1]

Matches the first <para> child of all the <section> children in the current context

//title

Matches all <title> elements anywhere in the document

.//title

Matches all <title> elements that are descendants of the current context


More Complex Patterns
section/*/note

Matches <note> elements that have <section> grandparents.

stockquote[@symbol]

Matches <stockquote> elements that have a "symbol" attribute

stockquote[@symbol="XXXX"]

Matches <stockquote> elements that have a "symbol" attribute with the value "XXXX"

emphasis|strong

Matches <emphasis> or <strong> elements





Next Next