Next page Previous page Start of chapter End of chapter

A final example

XSLT contains much more. It defines 37 elements divided in 3 categories:

In fact, XSLT is a Turing-complete functional programming language. However, the elements introduced so far lay the foundation for all more advanced XSLT processing.

Some other useful XSLT elements are the following:

  1. the xsl:if instruction element. It contains a template that is instantiated if and only if the XPath expression contained in its test attribute is true;
  2. the xsl:choose and xsl:when instruction elements. The xsl:choose element contains one or more xsl:when elements, each of which has a test condition. The contents are output for the first xsl:when element whose test results to be true. If no test condition is true, then the xsl:otherwise element, if present, is used. If xsl:otherwise is not included, then no output is produced;
  3. the xsl:for-each element. It iterates over the nodes identified by its select attribute and applies templates to each one;
  4. the xsl:sort instruction element. It appears as a child of xsl:apply-templates or xsl:for-each element. It changes the order in which the templates are applied from document order to the order specified by its select attribute;
  5. the attribute value template. It is an XPath expression enclosed in curly braces that is placed in an attribute value. The XSLT processor replaces the attribute value template with the value of the XPath expression;
  6. the xsl:text instruction element. It creates a text node and puts it in the output document. There are similar instructions for all kinds of XML nodes;
  7. the copy-of instruction element. It inserts whatever is identified by the select attribute into the output document. In particular, all the content of the identified nodes is copied, including the markup;
  8. the xsl:output top-level element. This element helps determine the exact formatting of the resulting document. In particular, it allows to introduce the DTD declaration in the output document.

We conclude with two stylesheet examples that illustrate the above elements. The first one translates an XML bibliography into an XHTML document. The XML bibliography may include XHTML elements in the abstract field of the bibliography items and it uses namespaces to distingush the bibliography application from the XHTML one. Here are a DTD for the bibliography application and a sample XML document containing a short bibliography. The XSLT document follows:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:b="http://www.dimi.uniud.it/~francesc/bib"
                xmlns="http://www.w3.org/1999/xhtml">
  
  <xsl:output method="xml" 
              doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
              doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" 
              indent="yes" 
              media-type="application/xml"/>
  
  <xsl:template match="/">
    <html>
      <head>
        <title>A small bibliography</title>
      </head>
      <body>
        <h1>A small bibliography</h1>
        
        <xsl:if test="b:bibliography/b:article">
          <h2>Articles</h2>
          <ol>
            <xsl:apply-templates select="b:bibliography/b:article">                  
              <xsl:sort select="b:year" order="descending"/>
            </xsl:apply-templates>
          </ol>
        </xsl:if>
        
        <xsl:if test="b:bibliography/b:book">
          <h2>Books</h2>
          <ol>
            <xsl:apply-templates select="b:bibliography/b:book">                  
              <xsl:sort select="b:year" order="descending"/>
            </xsl:apply-templates>
          </ol>
        </xsl:if>
        
      </body>
    </html>
  </xsl:template>
    
  <xsl:template match="b:bibliography/b:article">
    <li><a name="{@b:key}"/>
      <xsl:apply-templates select="b:author">
        <xsl:sort select="b:surname"></xsl:sort>
      </xsl:apply-templates>
      <b><xsl:apply-templates select="b:title"/></b>.
      <xsl:apply-templates select="b:journal"/>,
      <xsl:apply-templates select="b:year"/>.
      <xsl:if test="b:cite">
        References: <xsl:apply-templates select="b:cite"/>
      </xsl:if>
      <xsl:if test="b:abstract">
        <br></br>
        Abstract: 
        <xsl:for-each select="b:abstract/node()">
          <xsl:copy-of select="."/>
        </xsl:for-each>
      </xsl:if>
    </li>
  </xsl:template>
  
  <xsl:template match="b:bibliography/b:book">
    <li><a name="{@b:key}"/>
      <xsl:apply-templates select="b:author">
        <xsl:sort select="b:surname"></xsl:sort>
      </xsl:apply-templates>
      <b><xsl:apply-templates select="b:title"/></b>,
      <xsl:apply-templates select="b:year"/>.
      <xsl:apply-templates select="b:publisher"/>.
      <xsl:apply-templates select="@b:isbn"/>.
      <xsl:if test="b:cite">
        References: <xsl:apply-templates select="b:cite"/>
      </xsl:if>
    </li>
  </xsl:template>
  
  <xsl:template match="b:author">
    <xsl:apply-templates select="b:name"/>
    <xsl:apply-templates select="b:surname"/> 
    <xsl:if test="position() != last()">, </xsl:if>  
    <xsl:if test="position() = last()">. </xsl:if>  
  </xsl:template>

  <xsl:template match="b:name">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  
  <xsl:template match="b:surname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>

  <xsl:template match="b:cite">
    <a href="#{@b:item}">
      <xsl:apply-templates select="@b:item"/>
    </a>
  </xsl:template>

</xsl:stylesheet>

Here is the XHTML output document, and here is the XML input document extended with the stylesheet instruction (they should appear the same if your browser supports XSLT).

As another example, this XSLT document converts an XML bibliography in DBLP format into a BibTeX document:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes"/>
  
  <xsl:template match="/">
    <xsl:for-each select="dblp/*">
      @<xsl:value-of select="name(.)"/>{<xsl:value-of select="@key"/>,
      author = {<xsl:apply-templates select="author"/>},
      <xsl:for-each select="*[name(.) != 'author']">
        <xsl:value-of select="name(.)"/> = {<xsl:value-of select="."/>},
      </xsl:for-each>}
    </xsl:for-each>
  </xsl:template>
  
  
  <xsl:template match="dblp/*/author[position() < last()]">
    <xsl:value-of select="."/> and 
  </xsl:template>
  
</xsl:stylesheet>

Notice how we have used the XPath name() function to retrieve the name of an element. Moreover, the output document is not XML, hence the XML declaration has been omitted. If we apply this stylesheet to our short DBLP bibliography we obtain the following BibTeX instance:

@inproceedings{M4M05,
   author = {M. Franceschet and E. Zimuel},
   title = {Modal logic and navigational XPath: an experimental comparison},
   booktitle = {Workshop Methods for Modalities},
   pages = {156-172},
   year = {2005},
   url = {http://www.sci.unich.it/~francesc/pubs/m4m05.pdf},
}

@article{JLLI05,
   author = {M. Franceschet and B. ten Cate},
   title = {Guarded fragments with constants},
   journal = {Journal of Logic, Language and Information},
   volume = {14},
   number = {3},
   pages = {281-288},
   year = {2005},
   url = {http://www.sci.unich.it/~francesc/pubs/jlli05.pdf},
}
Next page Previous page Start of chapter End of chapter
Caffè XML - Massimo Franceschet