[xsl] questions/exercises for day 1

Subject: [xsl] questions/exercises for day 1
From: "Lars Huttar" <lars_huttar@xxxxxxx>
Date: Mon, 8 Dec 2003 21:46:20 -0600
Hi folks,
Here are some review questions and exercises. For the answers, use a nearby XML/XSL book
or the W3C Recommendations.
I don't guarantee that we've covered all the information necessary
to answer these questions.
The * indicates lower-priority questions (more optional).

Consider the XML document languages.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="lgtable.xsl" type="text/xsl"?>
	<language code="BPN">
		<name>Ba Pai</name>
	<language code="DJK">
	<language code="TRQ">

0. *For the artistic types, draw a representation of the
   node tree for this document.
1. What is the document element?
2. *What is the root element? (trick question)
3. *In the abstract tree representation of this document,
 how many children does the root node have?
4. How many children does the document element have?
5. How many children does the first "language" element have?
   How many nodes is the first "language" element the parent of?
6. What is the very first node in document order?
   What is the first element?
7. *What is the last node in document order?
   What is the last element?
8. If you wanted to use the language codes as id's, so that
   you could find the Trique data from the string "TRQ",
   what would you have to put in the DTD for languages.xml?
9. *What is the value of the processing-instruction node?
10. What stylesheet will be used if you open this document
   in a browser?
11. List the elements along the "following-sibling" axis starting
   from the first <name> element.
12. List the elements along the "following" axis starting
   from the first <name> element.
13. What is the value of the second <language> element?
14. What XPath expression would select all elements named "language"
   that are children of element(s) named "languages" that are
   children of the root node?
15. What would the following XPath expression produce?
  (which would give the same result as "sum(//pop)").

Now consider the stylesheet lgtable.xsl (slightly modified from
the one on the web site):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
	<xsl:output method="html" indent="yes" />
	<xsl:template match="/">
				<style type="text/css">
					td { margin-left: 30; margin-right: 30 }
				<table border="1">
						<th>SIL code</th>
						<th>Speaker population</th>
						<th>Link to Ethnologue entry</th>
					<xsl:apply-templates select="languages/language" />
	<xsl:template match="language">
			<td><xsl:value-of select="name" /></td>
			<td><xsl:value-of select="@code" /></td>
			<td><xsl:value-of select="pop" /></td>
			<td><a href="http://www.ethnologue.com/show_language.asp?code={@code}";>Click here</a></td>

1. What XSL instructions are used in this stylesheet?
   What literal result elements (name just a few)?
1a. What are the top-level elements?
2. What namespaces are used?
3. *Note that we can mix CSS styles with XSL styles (see the <style> element
   child of <head>).
4. Run this stylesheet on the given input document.
   View the results both as raw HTML markup, and in a browser.
4a. In what order are the two templates processed?
5. Change the stylesheet so that the "SIL code" column comes after the
   "Speaker population" column. Run it again to check your results.
6. *Twiddle the stylesheet around in other ways to make the output look more to your taste.
7. *Change the stylesheet to present just a bulleted list (<ul>) of language names,
   instead of a whole table.
8. If you were going to create a completely new stylesheet, which parts
   of the above stylesheet would you copy and paste into the new one?
9. *Can this composite stylesheet be written as a simple stylesheet?
10. *Browse through the sample stylesheets prod-*.xsl and try to follow what's going on.
   It may help to look at the sample output for each stylesheet, prod-exp.htm, prod-wml.wml

More on the XPath data model tomorrow.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread