XSLT Introduction Example

 

- Info:

- This example shows how to convert XML file into HTML file which uses table to display data from XML file.

- If you have XML file which contains only data, then if you want to display this data in browser you need to add HTML

tags which will tell the browser how certain data should be displayed.

 

- Procedure:

- Create C:\inetpub\wwwroot\test.xml and test.xsl

- http://localhost/test.xml

- Browser goes to test.xsl and it uses it's commands and data from test.xml to create output file which will be shown.

 

test.xml

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

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

<catalog>

<cd>

<title>Empire Burlesque</title>

<artist>Bob Dylan</artist>

<price>10.90</price>

</cd>

</catalog>

test.xsl

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th align="left">Title</th>

<th align="left">Artist</th>

</tr>

<xsl:for-each select="catalog/cd">

<tr>

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

<td><xsl:value-of select="artist"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

 

My CD Collection

Title

Artist

Empire Burlesque

Bob Dylan

 

 
result in browser