RU beehive logo ITEC dept promo banner
ITEC 325
2013spring
ibarland

homelectsexamshws
D2Lbreeze (snow day)

lect12b-ch02
XSLT
value-of, for-each

Originally based on XML Visual Quickstart Guide by Kevin Howard Goldberg, and notes therefrom by Jack Davis (jcdavis@radford.edu).

ch02 - XSLT basics

The next step in learning to use XML is understanding how to format these documents. The details for formatting XML documents was originally in a specification called XSLT, which stands for eXtensible Style Language Transformations. Chapters 2-4 explain how to use XSL to transform XML documents. The end result might be another XML document, or an HTML document (most common). You can transform an XML document into practically any document type.

Transforming an XML document means taking in one XML tree and producing another — a function from XML trees to XML trees. For example, consider the input

<my_children>
	<child>
		<name>Logan</name>
		<gender>M</gender>
		<age>7</age>
	</child>
	<child>
		<name>Rebecca</name>
		<gender>F</gender>
		<age>3</age>
	</child>
	<child>
		<name>Lee</name>
		<gender>F</gender>
		<age>2</age>
	</child>
</my_children>
An XSLT program might take this and return a new XML tree:
<html>
  <body>
    <h3>Some Kids</h3>
    <ul>
      <li>Logan, who is 7</li>
      <li>Rebecca, who is 3</li>
      <li>Lee, who is 2</li>
    </ul>
  </body>
</html>

Rather than write such a function in Java or a general-purpose programming language, XSLT is a domain-specific language, specifically invented to make these tree-transformations easy to write. XSLT can be used to reorder the ouput according to user established criteria, display only portions of the document, and more.

Overview

Building an XSL Style Sheet


1 Remember, one of the XML requirements is that there is exactly one top-level tag.      

2 The reason for the lack of xsl:else is because it's a bit unclear where the else tag should be placed — conceptually it belongs beside the if tag, but requiring side-by-side tags is unlike any other instruction. Putting the else inside the if makes more sense, but even then the condition attributes now seem placed oddly. A more appropriate version might have an if tag with exactly two children: “then” and “else”. … hey now we've arrived back at what xsl:choose does, except we're limited to exactly-two! I guess they designed the choice tag correctly after all!      

3 Perhaps they should use an English term beside “if”; perhaps when or unless?      

homelectsexamshws
D2Lbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2013.Apr.29 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme