新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:ASP.NET XML/XSL Transforms(转载www.aspalliance.com)

ASP.NET XML/XSL Transforms(转载www.aspalliance.com)

新客网 XKER.COM 2003-07-12 来源: 收藏本文
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>ssmith@aspalliance.com</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>
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐