频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

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

来源: 作者: 出处:巧巧读书 2006-10-15 进入讨论组

  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">
                    URL查看 http://www.qqread.com/aspdotnet/n222322.html 更多文章 更多内容请看.NET移动与嵌入式技术.NET开发手册XML详解专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章