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"?>
<languages>
<language code="BPN">
<name>Ba Pai</name>
<pop>62,000</pop>
</language>
<language code="DJK">
<name>Aukan</name>
<pop>21,500</pop>
</language>
<language code="TRQ">
<name>Trique</name>
<pop>23,000</pop>
</language>
</languages>
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?
"sum(/languages/language/pop)"
(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="/">
<html>
<head>
<title>Languages</title>
<style type="text/css">
td { margin-left: 30; margin-right: 30 }
</style>
</head>
<body>
<h2>Languages</h2>
<table border="1">
<tr>
<th>Name</th>
<th>SIL code</th>
<th>Speaker population</th>
<th>Link to Ethnologue
entry</th>
</tr>
<xsl:apply-templates
select="languages/language" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="language">
<tr>
<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(_dot_)asp?code={(_at_)code}">Click
here</a></td>
</tr>
</xsl:template>
</xsl:stylesheet>
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
etc.
More on the XPath data model tomorrow.
Lars
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list