XSL Tutorial
Worksheet 9: Amazon books
In this exercise we want to output Amazon XML books from an XML
file
For this example we are using the books.xml file
<books>
<book>
<name
link="http://www.amazon.com/exec/obidos/ASIN/0789722429/vbxml">XML
by Example</name>
<author>Benoit
Marchal</author>
<listprice>24.99</listprice>
<price>17.49</price>
<review>4.5</review>
<publish>QUE</publish>
</book>
In which we want to output the following HTML:
Professional XML Mark Birbeck, Michael Kay, stev Livingstone, Stephen F. Mohr, Didier Martin, Dino Esposito, Steven Livingston, Brian Loesgen, Nikola Ozu, Mark Seabourne |
List Price:
$49.99
Our Price: $34.99
|
Xml in Action William J. Pardi |
List Price:
$39.99
Our Price: $31.99
|
Here's what to do:
This is an example of using procedural programming in XSLT.
What I mean by that is we are going to use an xsl:for-each loop to
find each new book. We could use rule-base programming, but that
we will do in Worksheet 10. There solutions will show both so
that you can see them both, but for simplicity as a beginner, try the
following example using a for-each loop.
- In the Worksheet_9 directory, open the books.xsl
file - where a basic XSLT file has been created. Just as a hint I
normally open my HTML editing tool, like Frontpage and design how I want
my HTML to look from my XML source. Then take this HTML and wrap
it up in an xsl:template and basically do a 'paint-by-numbers', where I
insert the XSLT where I need it, which is what I've done in this XSLT
file.
- Create your default template
- Create a for-each loop for going through each
book
- Format the XSL to display the correct result
- You need to hyperlink the name of the book. For doing
the hyperlink look at the xsl:template
section
What to look out for:
- Firstly each new book I've inserted into a
separate table as I have an horizontal like under each table. This
makes the recursion a little simpler. The horizontal line is a bit
of a trick, as you would normally want to insert <hr>, which is
not a valid XML syntax.
- This is a procedural example, therefore we only
need to have the default template and do all our coding in this
template.
-
We need to have the book hyperlinked, therefore we need to use the
xsl:attribute syntax
-
Be careful when trying to get the value of the link attribute into
the above hyperlink. What context is the link attribute in,
the <book> element (which is the context you'll be looping
through) or the <name> context?
Have fun!
A: Procedural Solution
A: Rule-based Solution
Aw! I want more of a challenge:
If you want more a challenge, then here goes.
In your Worksheet_9 directory there are graphics (gifs) that
represent the review of a book. You'll notice that they are in
the same format as the <review> tag, that is
<review>4-5</review> is the same format as
star4-5.gif.
- Under 'Our Price', put add the gif that
represents the review.
- If there is no review then insert the word 'New'
For this challenge, you do not need to write a whole lot of <xsl:when>
statements for the review. Work on concatenating a string so
that the output of the HTML is correct. Just a note: Your
Transforming tool might not display the graphic, but your HTML syntax
is correct. This is because it is expecting the full path for
the graphic and it cannot find it, because your Transforming Tool is
running in another directory compared to where your XML/XSL files
are. This is only a problem with using one of these tools.
It doesn't affect the ASP code later on.
|