XSL Free Tutorial

Web based School

Creating the Result Tree


Next Next


Creating the Result Tree

There are several more elements that can be used in creating the result tree:

<xsl:attribute-set>

Names a collection of attributes for use as a set

<xsl:comment>

Creates an XML comment (<!-- like this -->) in the result tree

<xsl:processing-instruction>

Creates an XML processing instruction (<?pitarget like="this"?>) in the result tree

Keys

The ID/IDREF mechanism for locating elements in XML documents has been generalized in XSL to the notion of keys.

  • ID/IDREF is only useful when:

    • The document has declarations that identify the ID and IDREF attributes and

    • The processor is capable of processing the declarations

  • Using select expressions (XPath) to locate elements may be inefficient

Declaring keys gives the stylesheet processor an indication of what elements should be cached for fast access.

Key Example

Example 27. keys.xsl

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:key name="bibkey" match="//biblio/bib" use="@abbrev"/>

<xsl:template match="doc">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="biblio">
  <!-- suppressed -->
</xsl:template>

<xsl:template match="para">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="bibref">
  <xsl:variable name="bib" select="key('bibkey',string(.))"/>
  <xsl:choose>
    <xsl:when test="$bib">
      <i><xsl:apply-templates select="$bib"/></i>
    </xsl:when>
    <xsl:otherwise>
      <b>NO BIB FOR <xsl:apply-templates/></b>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="bib">
  <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>



Example 28. keys.xml

<doc>
<para>See <bibref>xyzzy</bibref>.</para>
<biblio>
<bib abbrev="xyzzy">The Great Grue</bib>
<bib abbrev="abcde">That Alphabet Song</bib>
</biblio>
</doc>


Example 29. keys.html

<?xml version="1.0" encoding="utf-8" ?>
See <i>The Great Grue</i>.


Extending XSLT

  • Extension Functions

  • Extension Elements

  • <xsl:fallback>

Extension Examples

These examples are from James' XT distribution:

Example 30. ext-func.xsl

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:date="http://www.jclark.com/xt/java/java.util.Date">

<xsl:template match="/">
  <html>
    <xsl:if test="function-available('date:to-string')
     and function-available('date:new')">
      <p><xsl:value-of select="date:to-string(date:new())"/></p>
    </xsl:if>
  </html>
</xsl:template>

</xsl:stylesheet>


Example 31. ext-elem.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		version="1.0"
                xmlns:xt="http://www.jclark.com/xt"
                extension-element-prefixes="xt">

<xsl:output method="text"/>

<xsl:template match="file">
  <xt:document href="{@name}" method="xml">
    <xsl:fallback>This should not happen.</xsl:fallback>
    <xsl:apply-templates mode="copy"/>
  </xt:document>
  <xsl:text>Created file </xsl:text>
  <xsl:value-of select="@name"/>
  <xsl:text>&#xA;</xsl:text>
</xsl:template>

<xsl:template mode="copy" match="*|@*">
  <xsl:copy>
    <xsl:apply-templates select="@*" mode="copy"/>
    <xsl:apply-templates select="node()" mode="copy"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Nested list example

<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="doc">
  <fo:block>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="p">
  <fo:block>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="list">
  <fo:list-block space-before="10pt"
                 provisional-distance-between-starts="3pc"
                 provisional-label-separation="1pc">
    <xsl:apply-templates/>
  </fo:list-block>
</xsl:template>

<xsl:template match="list//list">
  <fo:list-block space-before="8pt"
                 provisional-distance-between-starts="3pc"
                 provisional-label-separation="1pc"
                 start-indent="3pc">
    <xsl:apply-templates/>
  </fo:list-block>
</xsl:template>

<xsl:template match="list/item">
  <fo:list-item space-before="6pt">
    <fo:list-item-label>
      <fo:block>
        <xsl:number count="item" format="1."/>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body>
      <fo:block>
        <xsl:apply-templates/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<xsl:template match="list//list/item" priority="1">
  <fo:list-item space-before="6pt">
    <fo:list-item-label>
      <fo:block>
        <xsl:number count="item" format="a."/>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body>
      <fo:block>
        <xsl:apply-templates/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<xsl:template match="list//list//list/item" priority="2">
  <fo:list-item space-before="6pt">
    <fo:list-item-label>
      <fo:block>
        <xsl:number count="item" format="i."/>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body>
      <fo:block>
        <xsl:apply-templates/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

</xsl:stylesheet>

Other formatting objects

  • graphic

  • rule

  • float, footnote

  • simple-link

  • multi-switch, multi-case, multi-toggle





Next Next