XML Transformations (XSLT) – Part I

XML Foundations [./]
Fall 2013 — INFO 242 (CCN 41613)

Erik Wilde, UC Berkeley School of Information
2013-10-21

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents E. Wilde: XML Transformations (XSLT) – Part I

Contents

E. Wilde: XML Transformations (XSLT) – Part I

(2) 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.



E. Wilde: XML Transformations (XSLT) – Part I

(3) 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>


E. Wilde: XML Transformations (XSLT) – Part I

(4) XSLT Executive Summary



E. Wilde: XML Transformations (XSLT) – Part I

(5) XSLT as a Programming Language



E. Wilde: XML Transformations (XSLT) – Part I

(6) XSLT Processing Model

xslt-model.png

Simple Examples

Outline (Simple Examples)

  1. Simple Examples [5]
  2. XSLT Instructions [5]
  3. Templates [6]
  4. Conclusions [1]
Simple Examples E. Wilde: XML Transformations (XSLT) – Part I

(8) 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>


Simple Examples E. Wilde: XML Transformations (XSLT) – Part I

(9) Why does it Work?



Simple Examples E. Wilde: XML Transformations (XSLT) – Part I

(10) My Second 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/given[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>


Simple Examples E. Wilde: XML Transformations (XSLT) – Part I

(11) How Mappings Work



Simple Examples E. Wilde: XML Transformations (XSLT) – Part I

(12) 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>


XSLT Instructions

Outline (XSLT Instructions)

  1. Simple Examples [5]
  2. XSLT Instructions [5]
  3. Templates [6]
  4. Conclusions [1]
XSLT Instructions E. Wilde: XML Transformations (XSLT) – Part I

(14) XSLT is Simple



XSLT Instructions E. Wilde: XML Transformations (XSLT) – Part I

(15) Iterations

     <xsl:for-each select="/people/entry">
      <tr>
       <td><xsl:value-of select="name/given[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>


XSLT Instructions E. Wilde: XML Transformations (XSLT) – Part I

(16) Conditional Instructions



XSLT Instructions E. Wilde: XML Transformations (XSLT) – Part I

(17) My Second XSLT (II)

    <table>
     <xsl:for-each select="/people/entry">
      <tr>
       <td><xsl:value-of select="name/given[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>


XSLT Instructions E. Wilde: XML Transformations (XSLT) – Part I

(18) My Second 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>


Templates

Outline (Templates)

  1. Simple Examples [5]
  2. XSLT Instructions [5]
  3. Templates [6]
  4. Conclusions [1]
Templates E. Wilde: XML Transformations (XSLT) – Part I

(20) XSLT Processing Model

xslt-model.png

Templates E. Wilde: XML Transformations (XSLT) – Part I

(21) Templates as Building Blocks



Templates E. Wilde: XML Transformations (XSLT) – Part I

(22) Basic Mechanics

  1. The source node list contains only the root node
  2. The result tree is created by inserting the result from processing a node from the source node list
  3. Processing typically puts more nodes on the source node list
  4. The process is repeated until the source node list is empty
  <html>
   <head>
    <title>Address List</title>
   </head>
   <body>
    <table>
     <xsl:for-each select="/people/entry">
      <tr>
       <td><xsl:value-of select="name/given[1]"/></td>
       <td><xsl:value-of select="name/sur"/></td>
       <td><a href="mailto:{email}"><xsl:value-of select="email"/></a></td>


Templates E. Wilde: XML Transformations (XSLT) – Part I

(23) Template Selection



Templates E. Wilde: XML Transformations (XSLT) – Part I

(24) Patterns



Templates E. Wilde: XML Transformations (XSLT) – Part I

(25) Pattern-Based Processing

    <ul>
     <xsl:for-each select="/people/entry">
      <li>
       <xsl:value-of select="concat(name/given[1], ' ', name/sur, ': ')"/>
       <xsl:apply-templates select="*[local-name() != 'name']"/>
      </li>
     </xsl:for-each>
    </ul>
   </body>
  </html>
 </xsl:template>
 <xsl:template match="email">
  <a href="mailto:{.}" title="{.}">✉</a>
  <xsl:text> </xsl:text>
 </xsl:template>
 <xsl:template match="phone">
  <a href="tel:{.}" title="{@location}/{@type}:{.}">☎</a>
  <xsl:text> </xsl:text>
 </xsl:template>
 <xsl:template match="affiliation"/>


Conclusions

Outline (Conclusions)

  1. Simple Examples [5]
  2. XSLT Instructions [5]
  3. Templates [6]
  4. Conclusions [1]
Conclusions E. Wilde: XML Transformations (XSLT) – Part I

(27) Document-Driven Transformations



2013-10-21 XML Foundations [./]
Fall 2013 — INFO 242 (CCN 41613)