使用.NET 类库进行XML文档的XSLT样式转换_Server端转换(适用于所有浏览器)
2007年04月10日 星期二 上午 11:18
1.XML文件
Data.xml
<?xml version="1.0" encoding="ISO8859-1"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY> USA </COUNTRY> <COMPANY> Columbia </COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> </CATALOG>
2.XSLT文件
Style.xsl
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <table border="2" bgcolor="yellow"> <tr> <th>Title</th> <th>Artist</th> <th>Company</th> </tr> <xsl:for-each select="CATALOG/CD"> <tr> <td> <xsl:value-of select="TITLE"/> </td> <td> <xsl:value-of select="ARTIST"/> </td> <td> <xsl:value-of select="COMPANY"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> 3 aspx文件
Example.aspx
<%@ Page Language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System" %> <%@ Import namespace="System.Xml.Xsl"%> <%@ Import namespace="System.Xml.XPath"%>
<script runat="server" language="C#"> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(Server.MapPath("Style.xsl")); XPathDocument xpathDocument = new XPathDocument(Server.MapPath("Data.xml")); xslt.Transform(xpathDocument, null, Response.Output);
} } </script>
|