XML Transformations (XSLT) 2.0 – Part I

XML Foundations (INFO 242)

Erik Wilde, UC Berkeley School of Information
2007-10-16
Creative Commons License

This work is licensed under a CC
Attribution 3.0 Unported License

Abstract

While XML Transformations (XSLT) 1.0 has become a successful programming language widely used for transforming XML documents, its limitations sometimes make it difficult to use XSLT in a good way. An important reason for many of the limitations is the fact that XSLT 1.0 has been designed as a client-side language. Building on XSLT 1.0 and XPath 2.0, XML Transformations (XSLT) 2.0 improves the language in a variety of ways.

XSLT 1.0 Restrictions

XSLT 2.0 Improvements

Outline (Multiple Result Documents)

  1. Multiple Result Documents [7]
    1. Creating Identifiers [5]
  2. Text Processing [9]
    1. Accessing Text Documents [2]
    2. Transforming Text [6]
  3. Conclusions [1]

One XML, Many HTML

Generating HTML Pages

  <xsl:for-each select="//reference">
   <xsl:result-document method="xhtml" href="reference/{@name}.html">
    <html>
     <head>
      <title><xsl:value-of select="title"/></title>
      <link rel="stylesheet" type="text/css" href="../reference.css"/>
     </head>
     <body>
      <div class="navigation">
       <a href="{ if ( position() ne 1 ) then preceding-sibling::reference[1]/@name else following-sibling::reference[last()]/@name }.html">Previous</a> |
       <a href="../reference-list.html">Index</a> |
       <a href="{ if ( position() ne last() ) then following-sibling::reference[1]/@name else preceding-sibling::reference[last()]/@name }.html">Next</a>
      </div>
      <h2><xsl:value-of select="title"/></h2>
      <xsl:apply-templates select="names[@type='author']/*"/>
      <h4><a href="../reference-list.html#year{substring(date/@value, 1, 4)}"><xsl:value-of select="myns:format-date(date/@value)"/></a></h4>
      <xsl:if test="abstract">
       <div class="abstract"><xsl:copy-of select="abstract/richtext/*"/></div>
      </xsl:if>
     </body>
    </html>
   </xsl:result-document>
  </xsl:for-each>

Outline (Creating Identifiers)

  1. Multiple Result Documents [7]
    1. Creating Identifiers [5]
  2. Text Processing [9]
    1. Accessing Text Documents [2]
    2. Transforming Text [6]
  3. Conclusions [1]

Navigable Hypertext

Reuse Existing Identifiers

Generate Content-Based Identifiers

Generate Random Identifiers

Using Existing Identifiers

  <xsl:result-document method="xhtml" href="reference-list.html">
   <html>
    <head>
     <title>Reference List</title>
     <link rel="stylesheet" type="text/css" href="reference.css"/>
    </head>
    <body>
     <h2>Reference List</h2>
     <xsl:for-each-group select="//reference" group-by="substring(date/@value, 1, 4)">
      <xsl:sort select="current-grouping-key()"/>
      <h5 id="year{current-grouping-key()}"><xsl:value-of select="current-grouping-key()"/></h5>
      <ul>
       <xsl:for-each select="current-group()">
        <xsl:sort select="title"/>
        <li><a href="reference/{@name}.html"><xsl:value-of select="title"/></a></li>
       </xsl:for-each>
      </ul>
     </xsl:for-each-group>
    </body>
   </html>
  </xsl:result-document>

Outline (Text Processing)

  1. Multiple Result Documents [7]
    1. Creating Identifiers [5]
  2. Text Processing [9]
    1. Accessing Text Documents [2]
    2. Transforming Text [6]
  3. Conclusions [1]

Text Processing in XSLT 1.0

Outline (Accessing Text Documents)

  1. Multiple Result Documents [7]
    1. Creating Identifiers [5]
  2. Text Processing [9]
    1. Accessing Text Documents [2]
    2. Transforming Text [6]
  3. Conclusions [1]

Non-XML in an XML World

Comma-Separated Values (CSV)

Outline (Transforming Text)

  1. Multiple Result Documents [7]
    1. Creating Identifiers [5]
  2. Text Processing [9]
    1. Accessing Text Documents [2]
    2. Transforming Text [6]
  3. Conclusions [1]

Regular Expressions

Matching & Replacing

Tokenizing

Analyzing Strings

Replacing Characters with Elements

<xsl:analyze-string select="abstract" regex="\n">
  <xsl:matching-substring>
    <br/>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <xsl:value-of select="."/>
  </xsl:non-matching-substring>
</xsl:analyze-string>

Replacing Character Markup

<xsl:analyze-string select="body" regex="\[(.*?)\]">
  <xsl:matching-substring>
    <cite><xsl:value-of select="regex-group(1)"/></cite>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <xsl:value-of select="."/>
  </xsl:non-matching-substring>
</xsl:analyze-string>

Outline (Conclusions)

  1. Multiple Result Documents [7]
    1. Creating Identifiers [5]
  2. Text Processing [9]
    1. Accessing Text Documents [2]
    2. Transforming Text [6]
  3. Conclusions [1]

A Better XSLT