Next: Conclusion Up: XSL Previous: XSL-FO

XSLT and Xpath

Although the preferred language for formatting XML documents is XSL-FO, XML data is likely not encoded is XSL-FO format. The W3C created XSLT and Xpath for converting your XML documents into XSL-FO form. Unlike XSL-FO, XSLT has several implementations including popular products such as Internet Explorer 5 from Microsoft. Interestingly, Internet Explorer uses XSLT for converting XML documents into HTML and not into XSL-FO. This is possible because XSLT is a generic transformation language for converting one XML document into another XML document. It is not specific to XSL-FO.

Again, to see the power of XSLT and Xpath, it is useful to look at an example. Consider a simple grocery list in XML:


<grocery-list> 
<item>Apple</item>  
<item>Kraft Dinner</item> 
<item>Coke</item> 
<nonitem><date>January 31, 2001</date></nonitem> 
</grocery-list>

Suppose we want to encode this as a simple HTML list for viewing like the figure below


<ul>
<li>Apple</li>
<li>Kraft Dinner</li>
<li>Coke</li>
</ul>

In order to use XSLT to transform this document, it is necessary to create an XSL document describing the transformations to be made. If this document were called html.xsl, then it's possible to reference this stylesheet from the XML document with a stylesheet reference placed at the top of th XML document.


<?xml version="1.0"?>
<?xml-stylesheet type= "text/xsl" href= "html.xsl"?>

<grocery-list> 
<item>Apple</item>  
<item>Kraft Dinner</item> 
<item>Coke</item> 
<nonitem><date>January 31, 2001</date></nonitem> 
</grocery-list>

Although XSLT is capable of a wide variety of programming constructs such as queries, loops, variables, and expressions, this example will show how the XSLT template system is used for transformation XML documents. XSLT templates model your XML document as a tree structure. Below is a diagram demonstrating how the above XML documents is modeled as a tree using standard XPath practices.

\epsfig{file=list1.eps, width=3in, height=2.5in}

The following is html.xsl which describes two templates for manipulating the XML tree. Each template describes a node in the tree that it should "match" before that template is used. Once a match is found, the instructions within that template are applied. The "matches" are described using Xpath and resemble commands for navigating directory trees.


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

<xsl:template match="/grocery-list">
  <ul>
    <xsl:apply-templates select= "item"/>
  </ul>
</xsl:template>

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

</xsl:stylesheet>

When the XSLT stylesheet is run, it starts at the root of the document tree and iterates through the list of templates finding one that "matches" the tree. The matching template is for "/grocery-list." That template is activated and the commands inside are run. A <ul> tag is inserted, and the <xsl:apply-templates/> command is run. This command takes subtrees of the current tree and applies templates to these subtrees. In this case, the command states that all <item> subtrees should be taken and have templates applied to them. XSLT will then take each item subtree in turn and apply templates to them.

\epsfig{file=list2.eps, width=3in, height=3in}

Once in these item subtrees, XSLT will again iterate through the template list to find an appropriate template to apply. In this case, the item template matches, <li> tags are inserted, and templates are reapplied to the children of the <item> subtree. The children of the subtree is just text, and when templates are applied to text, they are simply copied into the new document.

\epsfig{file=list3.eps, width=3in, height=3in}

The resulting document is the html desired.

As you can see, XSLT is potentially extremely powerful. It is extremely flexible and widely implemented. Unfortunately, with its power comes complexity, and it is not clear how many people will be capable of programming in XSLT. If programming in XSLT proves to be too complicated, people will instead resort to automatic program generators for XSLT, but XSLT may not easily adapt itself to automatic generation. A potential new problem on the horizon is that XML-query is developing their own language for query and it may end up competing with XSLT and cause confusion among developers.


Next: Conclusion Up: XSL Previous: XSL-FO

2001-05-11