WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (10/13)
[previous] [next] |
XML for ASP.NET Developers
.NET Classes Involved in Transforming XML
Now that you've seen the different XSLT elements and functions that are at your disposal, it's time to learn about what classes in the .NET framework can be used in your ASP.NET applications when XSL transformations are necessary. After all, XSLT is simply a text-based language that is of little utility without an XSLT processor.
Several classes built in to the System.Xml
assembly can be used when
transforming XML into other structures via XSLT. Back in Listing 7.3, a preview
of a few of these classes interacting with each other was given that demonstrated
how to transform an XML document into HTML. In this section you'll learn
more about these classes and a few others so that you are fully armed with everything
you need to know to use XSLT in your ASP.NET applications. Figure
7.3 presents an overview of the main classes used in XSL transformations.
Figure 7.3: .NET Classes involved in XSL transformations
Table 7.4 provides a description of each of these classes.
Table 7.4 .NET Classes Used in XSL Transformations
Class |
Description |
|
The |
|
The |
|
The |
|
Because the |
|
The |
|
The |
The XPathDocument Class
Before looking at the XslTransform
class, you need to familiarize
yourself with the XPathDocument
class. To use this class you must
reference the System.Xml.XPath
namespace in your ASP.NET applications.
As mentioned in Table 7.4, this class provides the most efficient way to
transform an XML document using XSLT because it provides a read-only
representation of a DOM structure. The XPathDocument
class is very
simple to use because it has only one XML-related method named
CreateNavigator()
that can be used to create an instance of the
XPathNavigator
class. However, it does have several constructors that
are worth mentioning. Table 7.5 shows the different constructors.
Table 7.5 XPathDocument Constructors
Constructor |
Description |
|
Accepts an |
|
Accepts an |
|
Accepts a |
|
Accepts a |
|
Accepts the string value of the path to an XML document and an |
|
Accepts the string value of the path to an XML document. |
Listing 7.3 used the last constructor shown in Table 7.5 that
accepts the path to the XML document to transform. You could also load the
XPathDocument
with XML data contained in a Stream
(a
FileStream
for instance), an XmlReader
, or a
TextReader
. Having these different constructors offers you complete
control over how transformations will be carried out in your ASP.NET
applications. Which one you use will depend on how you choose to access your
application's XML documents. Listing 7.8 instantiates an
XPathDocument
class by passing in an XmlTextReader
object.
Listing 7.8 Instantiating an XPathDocument Class
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs E) {
string xmlPath = Server.MapPath("listing7.1.xml");
string xslPath = Server.MapPath("listing7.2.xsl");
FileStream fs = new FileStream(xmlPath,FileMode.Open,
FileAccess.Read);
StreamReader reader = new StreamReader(fs,Encoding.UTF8);
XmlTextReader xmlReader = new XmlTextReader(reader);
//Instantiate the XPathDocument Class
XPathDocument doc = new XPathDocument(xmlReader);
Response.Write("XPathDocument successfully created!");
//Close Readers
reader.Close();
xmlReader.Close();
}
</script>
Running the code shown in Listing 7.5 will write out "XPathDocument
successfully created!" to the browser. You'll certainly agree that
because it has simply readied the XML document for transformation, this code
doesn't buy you much. To actually transform the XML document using XSLT,
you'll need to use another class named XslTranform
.
The XslTransform Class
The XslTransform
class is found in the System.Xml.Xsl
namespace. Using it is as easy as instantiating it, loading the XSLT document,
and then calling its Transform()
method. Tables 7.6 and 7.7 show the
different properties and methods found in the XslTransform
class.
Table 7.6 XslTransform Class Properties
Property |
Description |
|
The |
|
Chapter 5 showed an example of using the |
Table 7.7 XslTransform Class Methods
Method |
Description |
|
Loads an XSLT document. This method can accept an |
|
The |
|
|
Listing 7.9 builds on Listing 7.8 by adding in the
XslTransform
class.
Listing 7.9 Using the XslTransform Class
1: <%@ Import Namespace="System.Xml" %>
2: <%@ Import Namespace="System.Xml.Xsl" %>
3: <%@ Import Namespace="System.Xml.XPath" %>
4: <%@ Import Namespace="System.IO" %>
5: <%@ Import Namespace="System.Text" %>
6: <script language="C#" runat="server">
7: public void Page_Load(Object sender, EventArgs E) {
8: string xmlPath = Server.MapPath("listing7.1.xml");
9: string xslPath = Server.MapPath("listing7.2.xsl");
10:
11: FileStream fs = new FileStream(xmlPath,FileMode.Open,
12: FileAccess.Read);
13: StreamReader reader = new StreamReader(fs,Encoding.UTF8);
14: XmlTextReader xmlReader = new XmlTextReader(reader);
15:
16: //Instantiate the XPathDocument Class
17: XPathDocument doc = new XPathDocument(xmlReader);
18:
19: //Instantiate the XslTransform Class
20: XslTransform xslDoc = new XslTransform();
21: xslDoc.Load(xslPath);
22: xslDoc.Transform(doc,null,Response.Output);
23:
24: //Close Readers
25: reader.Close();
26: xmlReader.Close();
27: }
28: </script>
In the next section, you'll see how you can pass in parameter values to
XSLT style sheets using the XsltArgumentList
class.
[previous] [next] |
© Copyright Pearson Education and
Created: April 22, 2002
Revised: April 22, 2002
URL: https://webreference.com/authoring/languages/xml/aspnet/chap7/10.html