下面的代碼是結合http://blog.csdn.net/cds27/archive/2006/05/08/712546.aspx 的一個例子。
也可以直接到http://cds.gameres.com/samples/SankOrange'sXslt_asp.rar 下載代碼。
該例子主要示範用我的xslt類,實現ASP中XML+XSL輸出HTML的方法,同時它和JS的例子略微不同的是輸出的方式不一樣。這種方式可以避免無法設置HTML中的charset帶來的編碼問題。
另外,這下面的XML和XSL,也可以作爲新手學習XML+XSL的參考代碼。
ASP:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%>
<script language="javascript" src="XsltClass.js" runat="server"></script>
<%
var xmlName=Server.MapPath("Main.xml");
var xslName=Server.MapPath("Main.xsl");
oXslt=new XsltClass(xmlName, xslName);
oXslt.output(Response);
//Response.Write(oXslt.output()); //假如這麼寫,頁面的charset就會被設置成utf-16.
%>
-----------------------------------------
Main.xsl:
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="gb2312" include-content-type="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Main">
<html>
<head>
<xsl:apply-templates select="Head"/>
</head>
<body><xsl:apply-templates select="Body"/></body>
</html>
</xsl:template>
<xsl:template match="Head">
<title><xsl:value-of select="Title"/></title>
</xsl:template>
<xsl:template match="Body">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Container">
<div id="Container">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="PageHead">
<h1><xsl:apply-templates/></h1>
</xsl:template>
<xsl:template match="Menu">
<ul id="Menu">
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="Item">
<li><a href="{Href/text()}"><xsl:value-of _fcksavedurl=""{Href/text()}"><xsl:value-of" _fcksavedurl=""{Href/text()}"><xsl:value-of" _fcksavedurl=""{Href/text()}"><xsl:value-of" select="Caption/text()"/></a></li>
</xsl:template>
<xsl:template match="PageFoot">
<p><xsl:apply-templates/></p>
</xsl:template>
</xsl:stylesheet>
-----------------------------------------
Main.xml:
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="main.xsl"?>
<Main>
<Head>
<Title>Sank orange 個人站點</Title>
</Head>
<Body>
<Container>
<PageHead>Sank orange 個人站點 </PageHead>
<Menu>
<Item>
<Href>index.asp</Href>
<Caption>首頁</Caption>
</Item>
<Item>
<Href>link1.asp</Href>
<Caption>鏈接1</Caption>
</Item>
<Item>
<Href>link2.asp</Href>
<Caption>鏈接2</Caption>
</Item>
</Menu>
<PageFoot><Copyright>Copyright 2006 Sank Orange,All rights reserved</Copyright></PageFoot>
</Container>
</Body>
</Main>