Jump to content

Using XSLT to convert a XML file to a table in HTML ???


nobitavn94

Recommended Posts

Hi all,Suppose we have a XML file describe products as follow:<?xml version="1.0" encoding="utf-8" ?><store> <product> <ImageUrl>product1.gif</ImageUrl> <Name>Screen</Name> <Price>12</Price> </product> <product> <ImageUrl>product2.gif</ImageUrl> <Name>Printer</Name> <Price>15</Price> </product> <product> <ImageUrl>product3.gif</ImageUrl> <Name>Printer</Name> <Price>20</Price> </product></store>If we want to show 3 products per a line (and suppose we have many lines) as follow:<table> <tr> <td> ...//product i info </td> <td> ...//product i+1 info </td> <td> ..//product i+2 info </td> </tr> <tr> <td> ...//product i+3 info </td> <td> ...//product i+4 info </td> <td> ..//product i+5 info </td> </tr>..... //product i+n info</table>How can we obtain this by using XSLT ?Thanks a lot !

Link to comment
Share on other sites

Thanks you very much !But all products are showed three times .I wonder if I made any mistakes ?.Here is my code :<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="store"><table bgcolor="#DDEEF7" WIDTH="100%" > <xsl:for-each select="product"> <tr> <xsl:for-each select=". | following-sibling :: product[position() < 3]"> <td> ....some code displaying product info here... </td> </xsl:for-each> </tr> </xsl:for-each></table></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Try this one:

<?xml version="1.0" encoding="windows-1251"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251"doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/><xsl:param name="group-size" select="3" /><xsl:template match="/store"><html xmlns="http://www.w3.org/1999/xhtml">	<head>		<title>Products</title>		</head>		<body> 			<table bgcolor="#DDEEF7" WIDTH="100%"> 				<xsl:apply-templates select="product[(position() mod $group-size) = 1]" /> 			</table> 		</body> 	</html> </xsl:template><xsl:template match="product"> 	<tr> 		<xsl:for-each select=". | following-sibling::product[position() < $group-size]"> 			<td>				<!--....some code displaying product info here...-->			</td> 		</xsl:for-each> 	</tr> </xsl:template></xsl:stylesheet>

It's how you should have adjusted this stylesheet in the first place. I mean, you only have one single template, but you need two.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...