> developer > dnu > courses > Web Services page 12
Web Services
April 2003
DeveloperNet University Course
Reader Rating    from ratings rate this article
View an eBook Version of this course - LARGE FILE! Send this page to a friend

Exercise 2: Write a Simple XSLT Stylesheet

In this exercise, you will create an XSLT stylesheet for the XML file you just created in order to display the XML document with HTML in a browser.

  1. Open the XML document you created in the previous exercise and add the following line to the document underneath the XML declaration.


<?xml-stylesheet type="text/xsl" href="sheet.xsl"?>

  1. Open a new text file in your text editor.

  2. Add the XML declaration line to your file.


<?xml version="1.0" encoding="ISO-8859-1"?>

  1. Add the stylesheet declaration to your document.


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">

  1. Type the statement to match the template for the root element.


<xsl:template match="/">

  1. Type the HTML tags you want to use to display the elements in the HTML document.

    1. Type <html>

    2. Type <body>

    3. Type <h2>Jeff Fischer's Java Book Collection</h2>

    4. Type <table border = "1">

    5. Type <tr bgcolor = "ffffde">

    6. Type <th>Title</th>

    7. Type <th>Author</th>

    8. Type <th>Topic</th>

    9. Type <th>ISBN</th>

    10. Type </tr>

  2. Use an XSLT for-each statement to choose the element you want to display.

    1. Type <xsl:for-each select="catalog/book">

    2. Type <tr>

    3. Type <td><xsl:value-of select="title"/></td>

    4. Type <td><xsl:value-of select="author" /></td>

    5. Type <td><xsl:value-of select="topic" /></td>

    6. Type <td><xsl:value-of select="isbn" /></td>

    7. Type </tr>

    8. Type </xsl:for-each>

  3. Close the remaining tags.

    1. Type </table>

    2. Type </body>

    3. Type </html>

    4. Type </xsl:template>

    5. Type </xsl:stylesheet>

  4. Save the file as style.xsl.

  5. You should now try to open the XML document and it should open in your browser and display the data of the XML file in an HTML table.

Previous Contents Next
download sample file