XML Transformations (XSLT) – Part II

XML Foundations (INFO 242)

Erik Wilde, UC Berkeley School of Information
2007-09-27
Creative Commons License

This work is licensed under a CC
Attribution 3.0 Unported License

Abstract

XSLT processes documents by matching nodes in the document tree to templates, which then are executed to process these nodes. This process of matching and executing templates is the core of XSLT's processing model. XSLT has built-in templates which complement the user-supplied templates, so that the XSLT processor always finds a template to execute. Templates can conflict, and it is then necessary to resolve this conflict by finding the best match of all matching templates. This conflict resolution process also is a very important component of the XSLT processing model.

XSLT Programming

Outline (XSLT Processing Model)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]

Input and Output

xslt-model.png

Outline (Templates)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]

Templates as Building Blocks

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
 <xsl:template match="/ | *">
  <xsl:text>( Element: </xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:apply-templates select="@* | *"/>
  <xsl:text>)</xsl:text>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:text>Attribute: </xsl:text>
  <xsl:value-of select="local-name()"/>
 </xsl:template>

Template Selection

Patterns

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"/>

Outline (Built-In Templates)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]

XSLT Default Behavior

Root and Elements

 <xsl:template match="* | /">
  <xsl:apply-templates/>
 </xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

</xsl:stylesheet>

Text and Attributes

 <xsl:template match="text() | @*">
  <xsl:value-of select="."/>
 </xsl:template>

Processing Instructions and Comments

 <xsl:template match="processing-instruction() | comment()"/>

Outline (Conflict Resolution)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]

Template Selection

Template Selection

  1. All templates with a match attribute
  2. All templates with the same mode
    • part of the apply-templates instruction selecting the node
  3. The Pattern must match
  4. If more than one template matches, order by import precedence
    • the import tree of the stylesheet is considered (this includes the built-in rules)
  5. If more than one template matches, order by priority
    • this sorts rules according to the specificity
  6. Execute resulting rule
    • if still more than one, signal error or execute last in stylesheet

Import Precedence

xslt-import-precedence.png

Priorities

Different Conflicts

<?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:apply-templates select="descendant::a"/>
 </xsl:template>
 <xsl:template match="*"/>
 <xsl:template match="a"/>
 <xsl:template match="b/a"/>
 <xsl:template match="c/b/a"/>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<c>
 <b>
  <a/>
 </b>
</c>

Resolution Process

Pattern Priority Resolution Step Manual
Adjustment
1 2 3 4 5
Built-in: text() | @*
Built-in: * | /
* -0.5
a 0.0
b/a 0.25
c/b/a 0.25 priority="1"

Adjusting Priorities

Outline (Conclusions)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]

Document-Driven Transformations