ASP.NET XML/XSL Transforms(转载www.aspalliance.com)
2024-07-10 12:58:10
供稿:网友
transforming an xml document using xsl/t and outputting the results to the browser is a fairly simple task in asp.net. the following user control demonstrates the ease with which this can be accomplished. this user control has a parameter for the xml source(xmlsource), and a parameter for the xsl source(xslsource). when placing this user control on a page, simply specify both values (using relative paths, since they are server.mappath'ed within the user control) and you're done! the transformed result will be output to response.output and sent to the user's browser. you can use this to create a two line aspx file that simply uses this user control to render its output. by using output and/or fragment caching, you can ensure that the cpu load required to transform the xml is minimized.
<%@ control language="c#" %>
<%@ import namespace="system.xml" %>
<%@ import namespace="system.xml.xsl" %>
<%@ import namespace="system.xml.xpath" %>
<script runat="server" language="c#">
public string xmlsource, xslsource;
void page_load(){
xmldocument docxml = new xmldocument();
docxml.load(server.mappath(xmlsource));
xsltransform docxsl = new xsltransform();
docxsl.load(server.mappath(xslsource));
docxsl.transform(docxml,null,response.output);
// chapter.text = docxml.transformnode(docxsl);
}
</script>
an example of a page using this user control, output caching for 1 minute:
<%@page%>
<%@ register tagprefix="authors" tagname="chapters" src="/controls/chapters.ascx"%>
<%@ outputcache duration="60" varybyparam="none" %>
<authors:chapters id="chapters" runat="server"
xmlsource="chapter.xml" xslsource="chapter.xsl" />
to test this out, you can use the following two files:
chapter.xml
<chapter>
<!-- chapter name -->
<name>chapter name</name>
<!-- author -->
<author>
<name>steven smith</name>
<email>[email protected]</email>
<website>http://aspalliance.com/stevesmith/</website>
</author>
<!-- examples from chapter -->
<examples>
<example>
<!-- the number from the book, e.g. 2.3 -->
<reference>2.1</reference>
<!-- link to working example -->
<demo>
<url>hellovb.asp</url>
<link_text>hellovb.asp</link_text>
</demo>
<!-- link to source code -->
<source>
<url>hellovb.txt</url>
<link_text>hellovb.asp source</link_text>
</source>
<!-- description of the example -->
<description>hello world using classic asp.</description>
</example>
</examples>
</chapter>
chapter.xsl
<?xml version="1.0" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">
<xsl:output method="xml" />
<xsl:template match="/">
<p>
<xsl:apply-templates select="chapter" />
</p>
</xsl:template>
<xsl:template match="/chapter" >
<b><xsl:value-of select="name"/></b>
<br/>
by <xsl:apply-templates select="author" />
<br/><br/>
<xsl:apply-templates select="examples" />
</xsl:template>
<xsl:template match="author" >
<i><xsl:value-of select="name"/></i>
</xsl:template>
<xsl:template match="examples" >
<table bgcolor="#000033">
<tr bgcolor="#ccccff">
<th>reference</th>
<th>demo</th>
<th>source</th>
<th>description</th>
</tr>
<xsl:apply-templates select="example" />
</table>
</xsl:template>
<xsl:template match="example" >
<tr bgcolor="#dddddd">
<td><xsl:value-of select="reference"/></td>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="demo/url"/>
</xsl:attribute>
<xsl:value-of select="demo/link_text"/>
</a>
</td>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="source/url"/>
</xsl:attribute>
<xsl:value-of select="source/link_text"/>
</a>
</td>
<td><xsl:value-of select="description"/></td>
</tr>
</xsl:template>
</xsl:transform>
,欢迎访问网页设计爱好者web开发。