<?xml version="1.0"?>
<!DOCTYPE TEI.2 SYSTEM "teixlite.dtd"
[ 
 <!ATTLIST xref url CDATA #IMPLIED>
 <!ATTLIST figure file CDATA #IMPLIED>
 <!ATTLIST figure width CDATA #IMPLIED>
 <!ATTLIST figure height CDATA #IMPLIED>
 <!ENTITY pound "&#163;">
 <!ENTITY ldots "&#8230;">
]> 
<TEI.2>
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>Introduction to XSLT </title>
      </titleStmt>
      <publicationStmt>
        <p> </p>
      </publicationStmt>
      <sourceDesc>
        <p></p>
      </sourceDesc>
    </fileDesc>
    <revisionDesc>
      <list>
        <item><date>July 23rd 2001</date></item>
        <item><date>January 7th 2001</date></item>
        <item><date>October 15th 2000</date></item>
        <item><date>July 12th 2000</date></item>
        <item><date>June 24th 2000</date></item>
        <item><date>July 25th 1999</date></item>
      </list>
    </revisionDesc>
  </teiHeader>
  <text>
<front>
<titlePage>
<docTitle>
 <titlePart type="main">Introduction to XSLT</titlePart>
</docTitle>
<docAuthor>Sebastian Rahtz</docAuthor>
<docDate>July 2001</docDate>
</titlePage>
</front>
<body>
<div rend="slide">
<head>What is the XSL family?</head>
<p>
<list rend="pausing" type="unordered">
<item>XPATH: a language for expressing paths through XML trees </item>
<item>XSLT: a language for expressing transformation of XML </item>
<item>XSL FO: an XML vocabulary for describing formatted pages </item>
</list>
The XSLT language is
 <list>
   <item>Expressed in XML; uses namespaces to distinguish 
     output from instructions</item>
   <item>Purely functional</item>
   <item>Reads and writes XML trees</item>
   <item>Designed to generate XSL FO</item>
 </list>
</p>
</div>

<div rend="slide">
<head>How is XSLT used? (1)</head>
<p>
<list>
<item>With a command-line  program to transform XML (eg to HTML)
<list><item>Downside: no dynamic content, user sees HTML</item>
<item>Upside: no server overhead, understood by all clients</item></list>
</item>
<item>In a web server <emph>servlet</emph>, eg serving up HTML from XML
<list><item>Downside: user sees HTML, server overhead</item>
<item>Upside: understood by all clients, allows for dynamic changes</item></list>
</item>
</list>
</p>
</div>

<div rend="slide">
<head>How is XSLT used? (2)</head>
<p>
<list>
<item>In a web browser, displaying XML on the fly
<list><item>Downside: most clients do not understand it </item>
<item>Upside: user sees XML</item></list>
</item>
<item>Embedded in specialized program</item>
<item>As part of a chain of production processes, performing
arbitrary transformations</item>
</list>
</p>
</div>

<div rend="slide">
<head>What do you mean, `transformation'?</head>
<p>Take this
<eg><![CDATA[<recipe>
 <title>Pasta for beginners</title>
 <ingredients><item>Pasta</item>
     <item>Grated cheese</item>
 </ingredients>
 <cook>Cook the pasta and mix with the cheese</cook>
</recipe>]]></eg>
and make this
<eg><![CDATA[<html>
 <h1>Pasta for beginners</h1>
 <p>Ingredients: Pasta Grated cheese
 <p>Cook the pasta and mix with the cheese
</html>]]></eg>
</p>
</div>

<div rend="slide">
<head>How do you express that in XSL?</head>
<p>
<eg><![CDATA[<xsl:stylesheet 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
    version="1.0">
<xsl:template match="/recipe">
 <html> 
 <h1><xsl:value-of select="title"/></h1>
 <p>Ingredients:   <xsl:apply-templates 
     select = "ingredients/item"/> </p>
 <p><xsl:apply-templates select = "cook"/></p>
 </html>
</xsl:template>
</xsl:stylesheet>]]></eg>
</p>
</div>


<div rend="slide">
<head>XSL Resources</head>
<list type="gloss">
<label>Open source processors</label>
<item>XT, Saxon, Xalan, libxslt,  Sablotron, Transformiix; Perl/Python etc</item>
<label>Commercial (but free) processors</label>
<item>Oracle XML, uxsl</item>
<label>Web browsers</label>
<item>Internet Explorer 5 (upgrade), Mozilla (nearly)</item>
<label>FO processors</label>
<item>FOP, PassiveTeX, XEP, Unicorn</item>
</list>
</div>

<div rend="slide">
<head>How an XSLT processor works (1)</head>
<p>
<list>
<item>An XSLT stylesheet has rules for
transforming a <emph>source</emph> tree into a <emph>result</emph>
tree.</item>
<item>The transformation is achieved by associating patterns with
templates.  </item>
<item>A pattern is matched against elements in the source tree.</item>
<item>A template is instantiated to create part of the result tree,
which is separate from the source tree.
<!--, and whose structure can be
completely different from the structure of the source tree.-->
</item>
<item>In constructing the result tree, elements from the source
tree can be filtered and reordered, and arbitrary structure can be
added.</item>
 </list>
</p>
</div>

<div rend="slide">
<head>How an XSLT processor works (2)</head>
<p>
<list>
<item>When a template is instantiated, each instruction is executed
and replaced by the result tree fragment that it creates. Instructions
can select and process descendant source elements.</item>
<item>Note that elements are only processed when they have been
selected by the execution of an instruction.</item>
<item>The result tree is constructed by finding the template rule
for the root node and instantiating its template.
</item>
 </list>
</p>
</div>

<div rend="slide">
<head>How an XSLT processor works (3)</head>
<p>
<list>
<item>When finding the applicable template rule, more than one
template rule may have a pattern that matches a given element. However, only
one template rule will be applied. 
<!--A conflict resolution mechanism
exists--></item>
<item>XSLT makes use of the XPath expression language for selecting
elements for processing, for conditional processing and for generating
text.</item>
<item><emph>Extension mechanisms</emph> are defined for extending
the set of instruction elements used in templates and for extending
the set of functions used in XPath expressions.</item>
</list>
</p>
</div>

<div><head>Complete example: input</head>
<p>
<eg><![CDATA[<?xml version='1.0'?><cemetery><stone number="31">
  <person sex="m">
    <name>
      <fnm status="2">John</fnm><snm status="2">Keats</snm>
    </name>
    <born>
      <date><day>0</day><mon>0</mon><yr>0</yr></date>
    </born>
    <died> 
      <date><day>24</day><mon>2</mon><yr>1821</yr></date>
    </died>
    <age>0</age>
    <nat status="1" idref="GB" />
  </person>
  <inscrip face="f_S" cond="c_1" manner="m_IF" type="t_P"
  lang="l_EN">
    <l p="c"><i>This Grave</i></l>
    <l p="c"><i>contains all that was Mortal,</i></l>
    <l p="c"><i>of a</i></l>
    <l p="c">YOUNG ENGLISH POET,</l>
  </inscrip>
  <deco>
    <icon face="f_S" occs="1" type="it_B" key="i58"></icon>
  </deco>
</stone></cemetery>
]]></eg>
</p>
</div>

<div><head>Complete example: stylesheet</head>
<p>
<eg><![CDATA[<?xml version='1.0'?>
<xsl:stylesheet 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
    version="1.0">
<xsl:template match="/cemetery">
 <html> 
 <head> <title>Protestant Cemetery Catalogue </title> </head>
 <body>   <xsl:apply-templates/> </body>
 </html>
</xsl:template>

<xsl:template match="stone">
  <h1>Stone <xsl:value-of select="@number"/></h1>
  <ul>
    <xsl:apply-templates select="person"/>
  </ul>
</xsl:template>

<xsl:template match="person">
 <li><xsl:apply-templates select="name"/></li>
</xsl:template>
</xsl:stylesheet>]]></eg>
</p>
</div>


<div rend="slide"><head>XSLT constructs (1)</head>
<list>
<item>
<p>Template match against element<eg><![CDATA[<xsl:template match="person">
 <xsl:apply-templates/>
</xsl:template>]]></eg>
</p>
</item>
<item>
<p>Process all children
<eg><![CDATA[<xsl:apply-templates/>]]></eg>
</p>
</item>
<item>
<p>Process selected children<eg><![CDATA[<xsl:apply-templates select="foo/bar"/>]]></eg>
</p>
</item>
<item>
<p>Process children separately<eg><![CDATA[First:
<xsl:apply-templates select="foo"/>
Second:
<xsl:apply-templates select="bar"/>]]></eg>
</p>
</item>
</list>
</div>

<div rend="slide"><head>XSLT constructs (2)</head>
<list>
<item>Loop around a set of children<eg><![CDATA[<ol>
 <xsl:for-each select="item"/>
    <li><xsl:apply-templates/></li>
 </xsl:for-each>
</ol>]]></eg>
</item>
<item>Print an attribute<eg><![CDATA[ <xsl:value-of select="@number"/>]]></eg>
</item>
<item>Put a calculated value in 
an output attribute<eg><![CDATA[ <a href="#S{@number}">
    <xsl:value-of select="@number"/>
 </a>]]></eg>
</item>
<item>Put out literal text (including spaces)<eg><![CDATA[<xsl:apply-templates select="item"/>
<xsl:text> !</xsl:text>
<xsl:apply-templates select="bar"/>]]></eg>
</item>
</list>
</div>

<div rend="slide"><head>XSLT constructs (3)</head>
<list>
<item>Template match against 
 attributes<eg><![CDATA[<xsl:template match="person[@sex='M']">
 <xsl:apply-templates/>
</xsl:template>]]></eg>
</item>
<item>Simple test<eg><![CDATA[<xsl:if test="@sex='M'">
 <xsl:apply-templates/>
</xsl:if>]]></eg></item>
<item>Case statement<eg><![CDATA[ <xsl:choose>
  <xsl:when test="@sex='M'">Its a boy!</xsl:when>
  <xsl:when test="@sex='F'">Its a girl!</xsl:when>
  <xsl:otherwise>
   Error in data: sex attribute 
   has <xsl:value-of select="@sex"/>
  </xsl:otherwise>
 </xsl:choose>]]></eg></item>
</list>
</div>

<div rend="slide"><head>XSLT constructs (4)</head>
<list>
<item>Numbering. The default is to provide the sibling number, but you can 
also make it document-wide:
<eg><![CDATA[<xsl:template match="item">
 <xsl:number/>. <xsl:apply-templates/>
</xsl:template>

<xsl:template match="div3">
 <xsl:number level="multiple" count="div1|div2"/>. 
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="note">
 <xsl:number level="any"/>. <xsl:apply-templates/>
</xsl:template>]]></eg></item>
</list>
</div>

<div rend="slide"><head>XSLT constructs (5)</head>
<list>
<item>Sorting
<eg><![CDATA[<xsl:template match="/">
 <xsl:apply-templates select="name">
   <xsl:sort select="surname"/>
   <xsl:sort select="forename"/>
 </xsl:apply-templates>
</xsl:template>

<xsl:template match="/">
 <ul>
 <xsl:for-each select="name">
   <xsl-sort select="surname"/>
   <xsl-sort select="forename"/>
   <li><xsl:apply-templates/></li>
 </xsl:for-each>
 </ul>
</xsl:template>]]></eg></item>
</list>
</div>

<div rend="slide"><head>XSLT constructs (6)</head>
<list>
<item>Using built-in functions<eg><![CDATA[<xsl:apply-templates select="id(@target)"/>

<xsl:value-of select="count(item)"/>

<xsl:value-of select="sum(item)"/>

<xsl:value-of select="substring-after(item,':')"/>

<xsl:if test="count(item) > 6">
  ....
</xsl:if>]]></eg></item>
</list>
</div>

<div rend="slide"><head>XSLT constructs (7)</head>
<list>
<item>Using axes
<eg><![CDATA[<xsl:if test="not(preceding-sibling::item)">
 ....
</xsl:if>

<xsl:value-of select="count(descendant::footnote)"/>

<xsl:apply-templates
 select="ancestor::teiHeader//revisionDesc/list/item[1]/date"/>]]></eg>
</item>
</list>
</div>


<div rend="slide"><head>XPATH axes</head>
<list type="unordered">
<item>self                </item>
<item>attribute            (shorthand form: <code>@</code>)</item>
<item>child                (shorthand form: <code></code>)</item>
<item>descendant           (shorthand form: <code>//</code>)</item>
<item>descendant-or-self</item>
<item>ancestor</item>
<item>ancestor-or-self</item>
<item>namespace</item>
<item>following</item>
<item>preceding</item>
<item>following-sibling</item>
<item>preceding-sibling</item>
<item>parent               (shorthand form: <code>..</code>)</item>
</list>
</div>

<div rend="slide"><head>All children</head>
<p>
<eg><![CDATA[<xsl:template match="/">
 <xsl:apply-templates>
</xsl:template>]]></eg>
<figure rend="inline" height="4in" file="tree1"/>
</p>
</div>

<div rend="slide"><head>No children</head>
<p>
<eg><![CDATA[<xsl:template match="/">
</xsl:template>]]></eg>
<figure rend="inline" height="4in" file="tree2"/>
</p>
</div>
<div rend="slide"><head>Inscriptions</head>
<p>
<eg><![CDATA[<xsl:template match="/">
 <xsl:apply-templates 
     select = "cemetery/stone/inscrip"/>
</xsl:template>]]></eg>
<figure rend="inline" height="3in" file="tree3"/>
</p>
</div>
<div rend="slide"><head>Death dates of peope on stones</head>
<p>
<eg><![CDATA[<xsl:template match="/">
 <xsl:apply-templates 
      select = "cemetery/stone/person/died"/>
</xsl:template>]]></eg>
<figure rend="inline" height="3in" file="tree4"/>
</p>
</div>
<div rend="slide"><head>Person inside stone</head>
<p>
<eg><![CDATA[<xsl:template match="/">
 <xsl:apply-templates/>
</xsl:template>

<xsl:template match="stone">
 <xsl:apply-templates select = "person"/>
</xsl:template>]]></eg>
<figure rend="inline" height="3in" file="tree5"/>
</p>
</div>
<div rend="slide"><head>First person on each stone</head>
<p>
<eg><![CDATA[<xsl:template match="/">
 <xsl:apply-templates/>
</xsl:template>

<xsl:template match="stone">
 <xsl:apply-templates select = "person[1]"/>
</xsl:template>]]></eg>
<figure rend="inline" height="3in" file="tree6"/>
</p>
</div>
<div rend="slide"><head>Death dates of last person on first stone</head>
<p>
<eg><![CDATA[<xsl:template match="/">
 <xsl:apply-templates 
  select = 
   "cemetery/stone[1]/person[last()]/died"/>
</xsl:template>

<xsl:template match="died">
 <xsl:value-of select = "day"/>/
 <xsl:value-of select = "mon"/>/
 <xsl:value-of select = "yr"/>
 <xsl:apply-templates select = "parent::person/born"/>
</xsl:template>]]></eg>
<figure rend="inline" height="3in" file="tree7"/>
</p>
</div>
<div rend="slide"><head>Illustration of axes</head>
<p>
<figure rend="inline" height="4.5in" file="tree8"/>
</p>
</div>

<div rend="slide">
<head>Conclusions</head>
<list>
<item>XSLT is good at expressing typical documentation transformations</item>
<item>XPATH has expressive power sufficient for most eventualities</item>
<item>The range and number of implementations makes the standard secure</item>
<item>XSLT is already widely used at all levels</item>
<item>XSLT currently has problems with big documents</item>
</list>
</div>
</body>
</text>
</TEI.2>
