This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
 
Page 26 of 28

 

Previous Page Table Of Contents Next Page

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:
XML by Example
Benoit Marchal
List Price: $24.99
Our Price: $17.49

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

Xml Design and Implementation
Paul Spencer
List Price: $399.99
Our Price: $31.99

XML: A Managers Guide
Kevin Dick
List Price: $34.95
Our Price: $29.95

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.

  1. 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.
  2. Create your default template
  3. Create a for-each loop for going through each book
  4. Format the XSL to display the correct result
    1. You need to hyperlink the name of the book.  For doing the hyperlink look at the xsl:template section

What to look out for:

  1. 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.
  2. This is a procedural example, therefore we only need to have the default template and do all our coding in this template.
  3. We need to have the book hyperlinked, therefore we need to use the xsl:attribute syntax
  4. 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. 

Page 26 of 28

 

Previous Page Table Of Contents Next Page
 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP