 |
|
|
 |
| Web Services |
 |
| April 2003 |
 |
| DeveloperNet University Course |
 |
 |
| Reader Rating |
 |
|
 |
from ratings |
 |
|
 |
|
|
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.
- 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"?>
- Open a new text file in your text editor.
- Add the XML declaration line to your file.
<?xml version="1.0" encoding="ISO-8859-1"?>
- Add the stylesheet declaration to your document.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
- Type the statement to match the template for the root element.
<xsl:template match="/">
- Type the HTML tags you want to use to display the elements in the HTML document.
- Type <html>
- Type <body>
- Type <h2>Jeff Fischer's Java Book Collection</h2>
- Type <table border = "1">
- Type <tr bgcolor = "ffffde">
- Type <th>Title</th>
- Type <th>Author</th>
- Type <th>Topic</th>
- Type <th>ISBN</th>
- Type </tr>
- Use an XSLT for-each statement to choose the element you want to display.
- Type <xsl:for-each select="catalog/book">
- Type <tr>
- Type <td><xsl:value-of select="title"/></td>
- Type <td><xsl:value-of select="author" /></td>
- Type <td><xsl:value-of select="topic" /></td>
- Type <td><xsl:value-of select="isbn" /></td>
- Type </tr>
- Type </xsl:for-each>
- Close the remaining tags.
- Type </table>
- Type </body>
- Type </html>
- Type </xsl:template>
- Type </xsl:stylesheet>
- Save the file as style.xsl.
- 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.
|