XML Transformations (XSLT) 2.0 – Part II

XML Foundations [./]
Fall 2011 — INFO 242 (CCN 42596)

Erik Wilde, UC Berkeley School of Information
2011-11-10

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) 2.0 – Part II

Contents

E. Wilde: XML Transformations (XSLT) 2.0 – Part II

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



User-Defined Functions

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]
User-Defined Functions E. Wilde: XML Transformations (XSLT) 2.0 – Part II

(4) Reusable Code in XPaths



User-Defined Functions E. Wilde: XML Transformations (XSLT) 2.0 – Part II

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


Grouping

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]
Grouping E. Wilde: XML Transformations (XSLT) 2.0 – Part II

(7) Aggregating Items



Grouping E. Wilde: XML Transformations (XSLT) 2.0 – Part II

(8) 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 E. Wilde: XML Transformations (XSLT) 2.0 – Part II

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


Typed Programming

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]
Typed Programming E. Wilde: XML Transformations (XSLT) 2.0 – Part II

(11) XDM and XPath 2.0 Support Types



Typed Nodes

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]
Typed Nodes E. Wilde: XML Transformations (XSLT) 2.0 – Part II

(13) XSD Simple Types

  • Simple Types [XSD – Part I; Simple Types (1)] are used to type non-markup values
    • mostly atomic types which describe the content of an element or attribute
    • union types and list types describe alternative or repeatable types
  • Working with simple types is supported in any XSLT 2.0 processor
    • the basic XSD datatypes are part of XDM [http://dret.net/lectures/xml-fall08/xdm] and always available
      xs:date( if ( $i castable as xs:date ) then $i else '2000-01-01' )
  • Working with user-defined simple types is a different story
    • Simple Type Restriction [XSD – Part I; Simple Type Restriction (1)] is used to derive new simple types
    • users can build their own specialized type library
    • such a library is a schema and requires a schema-aware XSLT 2.0 processor


Typed Trees

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]
Typed Trees E. Wilde: XML Transformations (XSLT) 2.0 – Part II

(15) XSD Complex Types

  • Complex Types [XSD – Part II; Complex Types (1)] are used to type markup structures
    • they are always associated with element nodes
    • they describe the content model and allowed attributes for that type
  • Working with complex types very much depends on the schema design
    • if the schema tries to reflect the model in the types, it may be useful
    • if the schema has been generated by some tool, types are not very relevant
  • A more interesting idea would be to actually inspect a type
    • upon finding a type, find out what attributes are optional but not required
    • this would require access not only to the type name, but to the type definition
    • this is a very interesting research issue, but not yet possible in XSLT 2.0
    • the Schema Component XML Syntax (SCX) [http://dret.net/netdret/publications#wil07e] is an attempt to solve this problem


Conclusions

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]
Conclusions E. Wilde: XML Transformations (XSLT) 2.0 – Part II

(17) Consider Upgrading



2011-11-10 XML Foundations [./]
Fall 2011 — INFO 242 (CCN 42596)