WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (10/13) | WebReference

WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (10/13)

To page 1To page 2To page 3To page 4To page 5To page 6To page 7To page 8To page 9current pageTo page 11To page 12To page 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
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




The XmlDocument class implements the IXPathNavigable interface and extends the XmlNode class, which provides the capability to create nodes within a DOM structure. This class was discussed in Chapter 6. Because the XmlDocument class provides node-creation capabilities, it will not provide the fastest throughput in XSL transformations. However, in cases where a DOM structure must be edited first before being transformed, this class can be used.


The XmlDataDocument class extends the XmlDocument class. The XmlDataDocument class can be used when working with DataSets in ADO.NET. Chapter 8, "Leveraging ADO.NET's XML Features Using ASP.NET," covers this class in more depth.


The XPathDocument class implements the IXPathNavigable interface like the XmlDocument class does. However, the XPathDocument class does not extend the XmlNode class (as the XmlDocument class does) and therefore provides the fastest option for transforming XML via XSLT. You'll see this class used in the examples that follow.


Because the XPathDocument class implements the IXPathNavigable interface, it is able to leverage features built in to the abstract XPathNavigator class (which, in turn, uses the XPathNodeIterator abstract class for iteration over node-sets) to provide cursor-style access to XML data, resulting in fast and efficient XSL transformations.


The XslTransform class is used to transform XML data into other structures. Using the XslTransform class involves instantiating it, loading the proper style sheet with the Load() method, and then passing specific parameters to its Transform() method. This process will be detailed in the next few sections.


The XsltArgumentList class is used to provide parameter values to xsl:param elements defined in an XSLT style sheet. It can be passed as an argument to the XslTransform class's Transform() method.

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



Public XPathDocument(XmlReader, XmlSpace)

Accepts an XmlReader as well as an XmlSpace enumeration.

Public XPathDocument(XmlReader)

Accepts an XmlReader.

Public XPathDocument(TextReader)

Accepts a TextReader.

Public XPathDocument(Stream)

Accepts a Stream.

Public XPathDocument(string,XmlSpace)

Accepts the string value of the path to an XML document and an XmlSpace enumeration.

Public XPathDocument(string)

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,
    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

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




The XmlResolver property can be used to specify a resolver class used to resolve external resources. For example, it can be used to resolve resources identified in xsl:include elements. If this property is not set, the XslTransform class will use the relative path of the supplied XSLT style sheet to resolve any included style sheets.


Chapter 5 showed an example of using the XmlUrlResolver class to access authenticated documents.

Table 7.7 XslTransform Class Methods




Loads an XSLT document. This method can accept an XmlReader, a document URL, or a variety of other objects.


The Transform() method is overloaded and can therefore accept a variety of parameters. The most common form of the method that you'll likely use in your ASP.NET applications is shown next (check the .NET SDK for the other overloaded versions of the method):



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"); 
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);
16:     //Instantiate the XPathDocument Class
17:     XPathDocument doc = new XPathDocument(xmlReader);
19:     //Instantiate the XslTransform Class
20:     XslTransform xslDoc = new XslTransform();
21:     xslDoc.Load(xslPath);
22:     xslDoc.Transform(doc,null,Response.Output);
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.

To page 1To page 2To page 3To page 4To page 5To page 6To page 7To page 8To page 9current pageTo page 11To page 12To page 13
[previous] [next]

© Copyright Pearson Education and Created: April 22, 2002
Revised: April 22, 2002

URL: http://webreference.com/authoring/languages/xml/aspnet/chap7/10.html