[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Generic xsl template to convert xml data into html tab

Subject: [xsl] Generic xsl template to convert xml data into html table
From: "Ahmed" <ahmed@xxxxxxxxxxxx>
Date: Sun, 24 Jun 2001 00:25:02 +0530
generic xsl
	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   "&#160;">

]>

<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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.