This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Generic xsl template to convert xml data into html table
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: [xsl] Generic xsl template to convert xml data into html table
- From: "Ahmed" <ahmed at bodhtree dot com>
- Date: Sun, 24 Jun 2001 00:25:02 +0530
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi all,
I want to write a generic xsl template which can convert my xml into html
table (with table heading as the element name)
My xml is as follows:
<root>
<row>
<id>1</id>
<name>Mr. X</name>
<city>Shangri-La</city>
<row>
<row>
<id>2/id>
<name>Mr. Y</name>
<city>Erehwon</city>
<phone>1234567890</phone>
<row>
<row>
<id>3</id>
<name>Mr. Z</name>
<city>Shangri-La</city>
<fax>9876543210</fax>
<row>
</root>
I want the output as follows
<table>
<tr>
<th>id</th>
<th>name></th>
<th>city</th>
<th>phone</th>
<th>fax</th>
</tr>
<tr>
<td>1</td>
<td>Mr. X</td>
<td>Shangri-La</td>
<td> - </td>
<td> - </td>
<tr>
<tr>
<td>2</td>
<td>Mr. Y</td>
<td>Erehwon</td>
<td>1234567890</td>
<td> - </td>
<tr>
<tr>
<td>3</td>
<td>Mr. Z</td>
<td>Shangri-La</td>
<td> - </td>
<td>9876543210</td>
<tr>
</table>
I tried it this way. It works for xml which has similar elements in the
row element.
Thanks in advance
My xsl is here.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xt = "http://www.jclark.com/xt"
Version = "1.0"
extension-element-prefixes="xt" >
<xsl:template match="/" >
<html>
<head>
<title>Test</title>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0"
marginheight="0">
<hr/>
<table border="1" cellspacing="1" cellpadding="1">
<tr>
<xsl:apply-templates match="root/row" mode="wrap"/>
</tr>
<xsl:apply-templates match="root/row"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="root/row" mode="wrap">
<xsl:if test="position() = 1">
<xsl:for-each select="./node()">
<th><xsl:value-of select="name()"/></th>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="root/row">
<xsl:for-each select=".">
<tr>
<xsl:for-each select="./node()">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list