利用ASP.NET的XslCompiledTransform將xml檔透過xsl檔轉成Html格式

利用ASP.NET的XslCompiledTransform將xml檔透過xsl檔轉成Html格式

最近在小舖看到一篇xml檔利用xsl檔轉成html的討論

小弟對xsl不是很懂..去網路上找了一些相關資料...在此分享給大家呀..

由下圖可知xml檔加上xsl檔的結合,再經由一個轉換Engine就可以產生像Html格式的內容了...

XSLT Transforms

資料來源:http://dn.codegear.com/tw/article/27106

小弟用asp.net的XslCompiledTransform來實作這張圖的意思....

asp.net(c#)範例

vehicles.xml

01 <?xml version="1.0" encoding="utf-8" ?>
02 <vehicles>
03   <type>Cars</type>
04   <car>
05     <name>Commodore</name>
06     <manufacturer>Holden</manufacturer>
07     <price>38,000</price>
08   </car>
09   <car>
10     <name>Falcon</name>
11     <manufacturer>Ford</manufacturer>
12     <price>36,000</price>
13   </car>
14   <car>
15     <name>Verada</name>
16     <manufacturer>Mitsubishi</manufacturer>
17     <price>36,500</price>
18   </car>
19   <car>
20     <name>Avalon</name>
21     <manufacturer>Toyota</manufacturer>
22     <price>41,000</price>
23   </car>
24 </vehicles>


vehicles.xsl

01 <?xml version="1.0" encoding="utf-8"?>
02
03 <xsl:stylesheet version="1.0"
04 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
05
06   <xsl:template match="/">
07     <HTML>
08       <HEAD>
09         <TITLE>
10           <xsl:value-of select="vehicles/type"/>
11         </TITLE>
12       </HEAD>
13       <BODY>
14         <TABLE border="1">
15           <TR>
16             <TD>Name</TD>
17             <TD>Manufacturer</TD>
18             <TD>Price</TD>
19           </TR>
20           <xsl:for-each select="vehicles/car">
21             <TR>
22               <TD>
23                 <xsl:value-of select="name"/>
24               </TD>
25               <TD>
26                 <xsl:value-of select="manufacturer"/>
27               </TD>
28               <TD>
29                 <xsl:value-of select="price"/>
30               </TD>
31             </TR>
32           </xsl:for-each>
33         </TABLE>
34       </BODY>
35     </HTML>
36   </xsl:template>
37
38 </xsl:stylesheet>


XMLtoHtml.aspx

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="XMLtoHtml.aspx.cs" Inherits="xsl_XMLtoHtml" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06     <title>XMLtoHtml</title>
07 </head>
08 <body>
09     <form id="form1" runat="server">
10         <div>
11         </div>
12     </form>
13 </body>
14 </html>
15


XMLtoHtml.aspx.cs

01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Xml.Xsl;
12 using System.Xml;

13
14 public partial class xsl_XMLtoHtml : System.Web.UI.Page
15 {
16     protected override void Render(HtmlTextWriter writer)
17     {
18         XmlDocument xml = new XmlDocument();
19
20         xml.Load(Server.MapPath("~/xsl/vehicles.xml"));
21
22         XslCompiledTransform xsl = new XslCompiledTransform();
23
24         xsl.Load(Server.MapPath("~/xsl/vehicles.xsl"));
25
26         xsl.Transform(xml, null, writer);
27     }

28 }


執行結果:

檢視原始碼: