欢迎光临
我们一直在努力

ASP.NET XML/XSL Transforms(转载www.aspalliance.com)-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

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.mappathed within the user control) and youre done! the transformed result will be output to response.output and sent to the users 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>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.NET XML/XSL Transforms(转载www.aspalliance.com)-.NET教程,Asp.Net开发
分享到: 更多 (0)