XML Transformations (XSLT) – Part II

XML Foundations [./]
Fall 2009 — INFO 242 (CCN 42575)

Erik Wilde, UC Berkeley School of Information
2009-09-29

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

Contents

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

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



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

(3) XSLT Programming



XSLT Processing Model

Outline (XSLT Processing Model)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]
XSLT Processing Model E. Wilde: XML Transformations (XSLT) – Part II

(5) Input and Output

xslt-model.png

Templates

Outline (Templates)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]
Templates E. Wilde: XML Transformations (XSLT) – Part II

(7) Templates as Building Blocks



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

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


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

(9) Template Selection



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

(10) Patterns



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

(11) 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"/>


Built-In Templates

Outline (Built-In Templates)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]
Built-In Templates E. Wilde: XML Transformations (XSLT) – Part II

(13) XSLT Default Behavior



Built-In Templates E. Wilde: XML Transformations (XSLT) – Part II

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


Built-In Templates E. Wilde: XML Transformations (XSLT) – Part II

(15) Text and Attributes

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


Built-In Templates E. Wilde: XML Transformations (XSLT) – Part II

(16) Processing Instructions and Comments

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


Conflict Resolution

Outline (Conflict Resolution)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]
Conflict Resolution E. Wilde: XML Transformations (XSLT) – Part II

(18) Template Selection



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

(19) Template Selection

  1. All templates with a match attribute
    • this excludes Named Templates [XML Transformations (XSLT) – Part III; Named Templates (1)]
  2. All templates with the same mode
    • part of the apply-templates instruction selecting the node
  3. The Pattern [Patterns (1)] 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


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

(20) Import Precedence

xslt-import-precedence.png

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

(21) Priorities



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

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


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

(23) 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"


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

(24) Adjusting Priorities



Conclusions

Outline (Conclusions)

  1. XSLT Processing Model [1]
  2. Templates [5]
  3. Built-In Templates [4]
  4. Conflict Resolution [7]
  5. Conclusions [1]
Conclusions E. Wilde: XML Transformations (XSLT) – Part II

(26) Document-Driven Transformations



2009-09-29 XML Foundations [./]
Fall 2009 — INFO 242 (CCN 42575)