XML Transformations (XSLT) – Part I

XML Foundations (INFO 242)

Erik Wilde, UC Berkeley School of Information
2007-09-25
Creative Commons License

This work is licensed under a CC
Attribution 3.0 Unported License

Abstract

Because XML can be used to represent any vocabulary (often defined by some schema), the question is how these different vocabularies can be processed and maybe transformed into something else. This something else may be another XML vocabulary (a common requirement in B2B scenarios), or it may be HTML (a common scenario for Web publishing). Using XSL Transformations (XSLT), mapping tasks can be implemented easily. XSLT leverages XPath's expressive power in a rather simple programming language, the programs are often called stylesheets. For easy tasks, XSLT mappings can be specified without much real programming going on, by simply specifying how components of the source markup are mapped to components of the target markup.

XPath and XSLT

  <xsl:for-each select=".//a">
   <xsl:if test="not(@title)">
    <xsl:text>This link has no title!</xsl:text>
   </xsl:if>
  </xsl:for-each>

  <xsl:for-each select=".//a[not(@title)]">
   <xsl:text>This link has no title!</xsl:text>
  </xsl:for-each>

XSLT Syntax

xml-technology-syntaxes.png

XSLT Executive Summary

XSLT as a Programming Language

Outline (Simple Examples)

  1. Simple Examples [7]
  2. XSLT Instructions [5]
  3. Conclusions [1]

My First XSLT

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

</xsl:stylesheet>

Why does it Work?

My Second XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/ | *">
  <xsl:text>( Element: </xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:apply-templates select="@* | *"/>
  <xsl:text>)</xsl:text>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:text>Attribute: </xsl:text>
  <xsl:value-of select="local-name()"/>
 </xsl:template>

</xsl:stylesheet>

How does it Work?

My Third XSLT

 <xsl:output method="html"/>
 <xsl:template match="/">
  <html>
   <head>
    <title>Address List</title>
   </head>
   <body>
    <table>
     <xsl:for-each select="/people/entry">
      <tr>
       <td><xsl:value-of select="name/pre[1]"/></td>
       <td><xsl:value-of select="name/sur"/></td>
       <td><a href="mailto:{email}"><xsl:value-of select="email"/></a></td>
      </tr>
     </xsl:for-each>
    </table>
   </body>
  </html>
 </xsl:template>

How Mappings Work

Hello World in XSLT

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

 <xsl:template match="/">
  <xsl:text>Hello World!</xsl:text>
 </xsl:template>

</xsl:stylesheet>

Outline (XSLT Instructions)

  1. Simple Examples [7]
  2. XSLT Instructions [5]
  3. Conclusions [1]

XSLT is RISC

Iterations

     <xsl:for-each select="/people/entry">
      <tr>
       <td><xsl:value-of select="name/pre[1]"/></td>
       <td><xsl:value-of select="name/sur"/></td>
       <td><a href="mailto:{email}"><xsl:value-of select="email"/></a></td>
      </tr>
     </xsl:for-each>

Conditional Instructions

My Third XSLT (II)

    <table>
     <xsl:for-each select="/people/entry">
      <tr>
       <td><xsl:value-of select="name/pre[1]"/></td>
       <td><xsl:value-of select="name/sur"/></td>
       <td>
        <xsl:if test="email">
         <a href="mailto:{email}"><xsl:value-of select="email"/></a>
        </xsl:if>
       </td>
      </tr>
     </xsl:for-each>
    </table>

My Third XSLT (III)

       <td>
        <xsl:for-each select="phone">
         <xsl:text>☎ (</xsl:text>
         <xsl:choose>
          <xsl:when test="@location = 'office'">Office</xsl:when>
          <xsl:when test="@location = 'home'">Home</xsl:when>
         </xsl:choose>
         <xsl:text> </xsl:text>
         <xsl:choose>
          <xsl:when test="@type = 'voice'">phone</xsl:when>
          <xsl:when test="@type = 'fax'">fax</xsl:when>
         </xsl:choose>
         <xsl:text>): </xsl:text>
         <xsl:value-of select="."/>
         <xsl:if test="position() ne last()">
          <br/>
         </xsl:if>
        </xsl:for-each>
       </td>

Outline (Conclusions)

  1. Simple Examples [7]
  2. XSLT Instructions [5]
  3. Conclusions [1]

XSLT is Simple