Tra forming an XML document using XSL/T and outputting the results to the browser is a fairly simple task in A .NET. The following user control demo trates 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 ecify both values (using relative paths, since they are Server.Ma ath'ed within the user control) and you're done! The tra formed result will be output to Re o e.Output and sent to the user's browser. You can use this to create a two line A X file that simply uses this user control to render its output. By using Output and/or Fragment caching, you can e ure that the CPU load required to tra form the XML is minimized.
<%@ Control Language="c#" %>
<%@ Import Name ace=" ystem.Xml" %>
<%@ Import Name ace=" ystem.Xml.Xsl" %>
<%@ Import Name ace=" ystem.Xml.XPath" %>
< cript runat=" erver" language="c#" gt;
public string xmlSource, xslSource;
void Page_Load(){
XmlDocument docXml = new XmlDocument();
docXml.Load(Server.Ma ath(xmlSource));
XslTra form docXsl = new XslTra form();
docXsl.Load(Server.Ma ath(xslSource));
docXsl.Tra form(docXml,null,Re o e.Output);
// chapter.Text = docXml.Tra formNode(docXsl);
}
</script>
An example of a page using this user control, Output Caching for 1 minute:
<%@Page%>
<%@ Register TagPrefix="author quot; tagname="chapter quot; src=http://www.cndownz.com/article/5/368/389/2006/"/controls/chapters.ascx"%>
<%@ OutputCache Duration="60" VaryByParam=" one" %>
<authors:chapters id="chapter quot; runat=" erver"
xmlSource="chapter.xml" xslSource="chapter.xsl" />
To test this out, you can use the following two files:
chapter.xml
<chapter>
lt;!-- Chapter Name -->
lt ame>Chapter Name</name>
lt;!-- Author -->
lt;author>
lt ame> teven Smith</name>
lt;email> mith@a alliance.com</email>
lt;we ite>http://a alliance.com/stevesmith/</we ite>
lt;/author>
lt;!-- Examples from Chapter -->
lt;example gt;
lt;example>
lt;!-- The number from the book, e.g. 2.3 -->
lt;reference>2.1</reference>
lt;!-- Link to working example -->
lt;demo>
lt;url>hellovb.a lt;/url>
lt;link_text>HelloVB.a lt;/link_text>
lt;/demo>
lt;!-- Link to source code -->
lt ource>
lt;url>hellovb.txt</url>
lt;link_text>HelloVB.a source</link_text>
lt;/source>
lt;!-- Description of the example -->
lt;descriptio gt;Hello World using Cla ic A .</descriptio gt;
lt;/example>
lt;/example gt;
</chapter>
chapter.xsl
<?xml version="1.0" ?>
<xsl:tra form xml :xsl="http://www.w3.org/1999/XSL/Tra form" version="1.0" gt;
<xsl:output method="xml" />
<xsl:template match="/" gt;
lt gt;
lt;xsl:a ly-templates select="chapter" />
lt;/ gt;
</xsl:template>
<xsl:template match="/chapter" >
lt gt lt;xsl:value-of select=" ame"/> lt;/ gt;
lt r/>
y <xsl:a ly-templates select="author" />
lt r/> lt r/>
lt;xsl:a ly-templates select="example quot; />
</xsl:template>
<xsl:template match="author" >
lt;i> lt;xsl:value-of select=" ame"/> lt;/i>
</xsl:template>
<xsl:template match="example quot; >
lt;table bgcolor="#000033" gt;
lt;tr bgcolor="#CCCCFF" gt;
lt;th>Reference</th>
lt;th>Demo</th>
lt;th> ource</th>
lt;th>Descriptio lt;/th>
lt;/tr>
lt;xsl:a ly-templates select="example" />
lt;/table>
</xsl:template>
<xsl:template match="example" >
lt;tr bgcolor="#DDDDDD" gt;
lt;td> lt;xsl:value-of select="reference"/> lt;/td>
lt;td>
lt;a>
lt;xsl:attribute name="href" gt;
lt;xsl:value-of select="demo/url"/>
lt;/xsl:attribute>


