XML Transformations (XSLT) – Part IV

XML Foundations (INFO 242)

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

This work is licensed under a CC
Attribution 3.0 Unported License

Abstract

Advanced XSLT processing includes better control of the input and output documents, which can be finely controlled in terms of how whitespace is treated. Another interesting feature of XSLT are keys, which allow shorthand notations for frequently used access paths to nodes, and provide XSLT processors with more information for performance optimizations. Instructions for creating all possible kinds of nodes in the output tree make it possible to write code which generates element or attribute names based on runtime evaluations.

Outline (Controlling Documents)

  1. Controlling Documents [6]
    1. Input Documents [3]
    2. Output Documents [2]
  2. Keys [6]
  3. Generating Result Nodes [2]
  4. Modularizing Stylesheets [2]
  5. Conclusions [1]

XSLT Processing Model

Outline (Input Documents)

  1. Controlling Documents [6]
    1. Input Documents [3]
    2. Output Documents [2]
  2. Keys [6]
  3. Generating Result Nodes [2]
  4. Modularizing Stylesheets [2]
  5. Conclusions [1]

Opening Documents

<?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>me: </xsl:text>
  <xsl:value-of select="count(document('document.xsl')/descendant::*)"/>
  <xsl:text>&#xa;http://dret.net/netdret/publications: </xsl:text>
  <xsl:value-of select="count(document('http://dret.net/netdret/publications')/descendant::*)"/>
 </xsl:template>
</xsl:stylesheet>

Whitespace in Documents

Controlling Whitespace

 <xsl:strip-space elements="*"/>
 <xsl:preserve-space elements="xsl:text"/>
 <xsl:template match="/">
  <xsl:text>xsl:text is used for outputting text.</xsl:text>
  <xsl:text> </xsl:text>
  <xsl:text>it also is the only element where whitespace nodes in the stylesheet are significant</xsl:text>
  <xsl:text>&#xa;</xsl:text>
  <xsl:value-of select="count(//text())"/>
 </xsl:template>

Outline (Output Documents)

  1. Controlling Documents [6]
    1. Input Documents [3]
    2. Output Documents [2]
  2. Keys [6]
  3. Generating Result Nodes [2]
  4. Modularizing Stylesheets [2]
  5. Conclusions [1]

Serialization

Multiple Output Documents

Outline (Keys)

  1. Controlling Documents [6]
    1. Input Documents [3]
    2. Output Documents [2]
  2. Keys [6]
  3. Generating Result Nodes [2]
  4. Modularizing Stylesheets [2]
  5. Conclusions [1]

Document Access

Declaring and Using Keys

XML and XSLT for using a Key

<people>
 <entry id="dret" country="de">
  <name>
   <given>Erik</given>
   <given>Thomas</given>
   <sur>Wilde</sur>
  </name>
  <email>dret@berkeley.edu</email>
  <affiliation country="us">iSchool/UCB</affiliation>
  <phone location="office" type="voice">+1-510-6432253</phone>
  <phone location="office" type="fax">+1-510-6425814</phone>
 <xsl:key name="givenNameKey" match="name" use="given"/>
 <xsl:key name="affiliationKey" match="affiliation" use="."/>
 <xsl:key name="countryKey" match="entry | affiliation" use="@country"/>

XSLT Key Structure

givenNameKey
Node Value
[1] Erik Thomas Wilde Erik
[1] Erik Thomas Wilde Thomas
[2] Thomas Plagemann Thomas
[3] Bob Glushko Bob
countryKey
Node Value
[1a] Erik Thomas Wilde de
[1b] iSchool/UCB us
[2a] Thomas Plagemann de
[2b] IFI/UIO no
[3a] Bob Glushko us
[3b] iSchool/UCB us

Using Keys

Node Set Intersection

$a[count(. | $b) = count($b)]: Find all nodes in $a where the cardinality of $b does not change when adding this node to it. This means the node must be in $b, and it is in $a to start with.

xpath-intersection.png

Outline (Generating Result Nodes)

  1. Controlling Documents [6]
    1. Input Documents [3]
    2. Output Documents [2]
  2. Keys [6]
  3. Generating Result Nodes [2]
  4. Modularizing Stylesheets [2]
  5. Conclusions [1]

Literal Result Elements

Producing Nodes Explicitly

 <xsl:template match="*">
  <xsl:element name="{translate(local-name(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}">
   <xsl:apply-templates select="node() | @*"/>
  </xsl:element>
 </xsl:template>
 <xsl:template match="@*">
  <xsl:attribute name="{translate(local-name(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}">
   <xsl:value-of select="."/>
  </xsl:attribute>
 </xsl:template>

Outline (Modularizing Stylesheets)

  1. Controlling Documents [6]
    1. Input Documents [3]
    2. Output Documents [2]
  2. Keys [6]
  3. Generating Result Nodes [2]
  4. Modularizing Stylesheets [2]
  5. Conclusions [1]

Including and Importing

Import Precedence

xslt-import-precedence.png

Outline (Conclusions)

  1. Controlling Documents [6]
    1. Input Documents [3]
    2. Output Documents [2]
  2. Keys [6]
  3. Generating Result Nodes [2]
  4. Modularizing Stylesheets [2]
  5. Conclusions [1]

XSLT in Practice