1 定义Test.xml
<?xml version="1.0"?>
<sales xmlns:HTML="http://www.w3.org/profiles/XHTML-transitional">
<product id='Travel'>
<revenue>60</revenue>
<growth>8</growth>
</product>
<product id='Communications'>
<revenue>4.5</revenue>
<growth>-78</growth>
</product>
<product id='Munitions'>
<revenue>4</revenue>
<growth>-3</growth>
</product>
</sales>
2 定义Test.xsl
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<table width="210" border="1pt" cellspacing="0" cellpadding="0" bordercolor="#dddddd" style="border-collapse:collapse;">
<tr>
<th align="left">Product <br/>Category</th><th>Revenue</th><th>Growth</th>
</tr>
<xsl:for-each select='sales/product'>
<tr>
<td class="Normal" width="100">
<i><xsl:value-of select='@id' /></i>
</td>
<td class="Normal" width="100">
<CENTER><xsl:value-of select='revenue'/></CENTER>
</td>
<td class="Normal">
<xsl:if test='growth < 0'>
<xsl:attribute name='style'>
<xsl:text>color:red</xsl:text>
</xsl:attribute>
</xsl:if>
<CENTER><xsl:value-of select='growth'/></CENTER>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
3 aspx页面
<body>
<form id="form1" runat="server">
<asp:Xml ID="Xml1" runat="server"></asp:Xml>
</form>
</body>
4 aspx.cs页面
protected void Page_Load(object sender, EventArgs e)
{
Xml1.DocumentSource = "Test.xml";
Xml1.TransformSource = "Test.xsl";
}
5 输入页页
Product Category | Revenue | Growth |
---|---|---|
Travel |
|
|
Communications |
|
|
Munitions |
|
|
6 输入页页的html代码
<?xml version="1.0" encoding="gb2312"?><table width="210" border="1pt" cellspacing="0" cellpadding="0" bordercolor="#dddddd" style="border-collapse:collapse;"><tr><th align="left">Product <br />Category</th><th>Revenue</th><th>Growth</th></tr><tr><td class="Normal" width="100"><i>Travel</i></td><td class="Normal" width="100"><CENTER>60</CENTER></td><td class="Normal"><CENTER>8</CENTER></td></tr><tr><td class="Normal" width="100"><i>Communications</i></td><td class="Normal" width="100"><CENTER>4.5</CENTER></td><td class="Normal" style="color:red"><CENTER>-78</CENTER></td></tr><tr><td class="Normal" width="100"><i>Munitions</i></td><td class="Normal" width="100"><CENTER>4</CENTER></td><td class="Normal" style="color:red"><CENTER>-3</CENTER></td></tr></table>