XML Transformations (XSLT) 2.0 – Part II

XML Foundations (INFO 242)

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

This work is licensed under a CC
Attribution 3.0 Unported License

Abstract

Many of the new features of XSLT 2.0 have their roots in XPath 2.0 and the underlying new data model of sequences. But some features of XSLT 2.0 really are part of the language itself, such as support for user-defined functions, and the ability to group items and then iterate over these groups. In addition, XSLT now can be used as a typed programming language, which consumes and produces typed trees instead of just well-formed XML trees.

Outline (User-Defined Functions)

  1. User-Defined Functions [2]
  2. Grouping [3]
  3. Typed Programming [3]
    1. Typed Nodes [1]
    2. Typed Trees [1]
  4. Conclusions [1]

Reusable Code in XPaths

Function Definition and Call

 <xsl:function name="myns:format-date">
  <xsl:param name="date"/>
  <xsl:value-of select="if ( $date castable as xs:date ) then format-date(xs:date($date), '[MNn] [D], [Y]') else if ( $date castable as xs:gYearMonth ) then format-date(xs:date(concat($date, '-01')), '[MNn] [Y]') else $date"/>
 </xsl:function>
     <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>

Outline (Grouping)

  1. User-Defined Functions [2]
  2. Grouping [3]
  3. Typed Programming [3]
    1. Typed Nodes [1]
    2. Typed Trees [1]
  4. Conclusions [1]

Aggregating Items

Muenchian Grouping

<records>
 <contact id="0001">
  <forename>John</forename>
  <surname>Smith</surname>
 </contact>
 <contact id="0002">
  <forename>Amy</forename>
  <surname>Jones</surname>
 </contact>
 <contact id="0003">
  <forename>Jimmy</forename>
  <surname>Jones</surname>
 </contact>
 <xsl:key name="contacts-by-surname" match="contact" use="surname"/>
 <xsl:template match="records">
  <ul>
   <xsl:for-each select="contact[count(. | key('contacts-by-surname', surname)[1]) = 1]">
    <xsl:sort select="surname"/>
    <li><xsl:value-of select="surname"/>
     <ul>
      <xsl:for-each select="key('contacts-by-surname', surname)">
       <xsl:sort select="forename"/>
       <li><xsl:value-of select="forename"/></li>
      </xsl:for-each>
     </ul>
    </li>
   </xsl:for-each>
  </ul>
 </xsl:template>

Grouping and Iterating over Groups

  <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 (Typed Programming)

  1. User-Defined Functions [2]
  2. Grouping [3]
  3. Typed Programming [3]
    1. Typed Nodes [1]
    2. Typed Trees [1]
  4. Conclusions [1]

XDM and XPath 2.0 Support Types

Outline (Typed Nodes)

  1. User-Defined Functions [2]
  2. Grouping [3]
  3. Typed Programming [3]
    1. Typed Nodes [1]
    2. Typed Trees [1]
  4. Conclusions [1]

XSDL Simple Types

Outline (Typed Trees)

  1. User-Defined Functions [2]
  2. Grouping [3]
  3. Typed Programming [3]
    1. Typed Nodes [1]
    2. Typed Trees [1]
  4. Conclusions [1]

XSDL Complex Types

Outline (Conclusions)

  1. User-Defined Functions [2]
  2. Grouping [3]
  3. Typed Programming [3]
    1. Typed Nodes [1]
    2. Typed Trees [1]
  4. Conclusions [1]

Consider Upgrading