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

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

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

XML for ASP.NET Developers

Using Extension Objects with XSLT

While looking through the methods exposed by the XsltArgumentList class back in Table 7.8, you may have wondered how the extension object methods could be used to enhance XSLT/ ASP.NET applications. Using these methods is surprisingly easy and can provide your XSLT style sheets with even more power and flexibility. Keep in mind that by using extension objects in XSLT, you may render your XSLT unusable on other platforms or by other languages simply because extensions are not a part of the XSLT 1.0 specification (the XSLT 1.1 working draft does include extension elements and functions, however). If your application will be the only one that uses a particular XSLT style sheet and you need additional functionality not in the XSLT 1.0 specification, extension objects may be the answer. Some other benefits of using extension objects include:

What exactly is an extension object? Think of it as an external class that can be referenced and used within an XSLT style sheet. By using extension objects, you can get the current date and time, query a database to do a lookup based on a value found in the XML source document, hit a Web service, or trigger another application to begin running. All of this and much more can be done from within an XSLT style sheet.

To see how this works in practice, the next code sample shown in Listing 7.12 builds on the previous one shown in Listing 7.11 to add the capability to write out the current date/time of the server from a specific location within the style sheet. Let's first look at the class that will be instantiated and used as an extension object.

Listing 7.12 The Date/Time Extension Class (xsltDateObject.cs)

 1: namespace XsltTransformation.ExternalObjects {
 2:   using System;
 3: 
 4:   public class XsltDateTime {
 5:     DateTime _date;
 6:     public XsltDateTime() {
 7:       _date = DateTime.Now;
 8:     }
 9:     public DateTime GetDateTime() {
10:       return _date;
11:     }
12:   }
13: }

This class (named XsltDateTime) does nothing more than return the current system date and time. It must be instantiated within an ASP.NET page and then added to the external object collection of the XsltArgumentList class.

You may be wondering if it would be easier to pass the date and time into the style sheet using a regular XSLT parameter. The answer is "yes"; it would be easier because no external objects would be needed from within the style sheet. However, by calling the extension object from within the XSLT style sheet, a more up-to-date date/time value will be returned (assuming that accuracy matters in the application). And let's face it, the demo code wouldn't be as cool if a regular XSLT parameter was used, especially because you know all about those at this point!

Listing 7.13 demonstrates how to pass an extension object into an XSLT style sheet using the XsltArgumentList class. The lines of code relating to the extension object use are shown in bold.

Listing 7.13 Adding an External Object to the XsltArgumentList Class (xsltExtension.aspx.cs)

 1: protected void btnSubmit_Click(object sender, System.EventArgs e) {
 2:   
 3:   string xslPath = Server.MapPath("xsltExtension.xsl"); 
 4:   XsltDateTime xsltExtObj = new XsltDateTime(); //The Extension Object
 5:   XmlTextReader xmlReader = null;
 6:   StringBuilder sb = new StringBuilder();
 7:   StringWriter sw = new StringWriter(sb);
 8:   
 9:   try {
10:     xmlReader = new XmlTextReader(xmlPath);
11:   
12:       //Instantiate the XPathDocument Class
13:       XPathDocument doc = new XPathDocument(xmlReader);
14:   
15:       //Instantiate the XslTransform Classes
16:       XslTransform transform = new XslTransform();
17:       transform.Load(xslPath);
18: 
19:       //Add Parameters and Extension Object to the Collection
20:       XsltArgumentList args = new XsltArgumentList();
21:       args.AddParam("golferName","",
22:              this.ddGolferName.SelectedItem.Value);
23:       //Add the namespaceURI and object to the object collection
24:       args.AddExtensionObject("urn:xsltExtension-DateTime",
25:                   xsltExtObj);
26: 
27:       //Call Transform() method
28:       transform.Transform(doc, args, sw);
29: 
30:       //Hide ASP.NET Panels
31:       this.pnlSelectGolfer.Visible = false;
32:       this.pnlTransformation.Visible = true;
33:       this.divTransformation.InnerHtml = sb.ToString();
34:     }
35:     catch (Exception excp) {
36:       Response.Write(excp.ToString());
37:     }
38:     finally {
39:       xmlReader.Close();
40:       sw.Close();
41:     }
42: }

The XSLT style sheet obviously needs to be able to reference the XsltDateTime object that is passed in and call its GetDateTime() method. This is accomplished by adding the proper namespace prefix and URI into the style sheet. For this example, a namespace URI of urn:xsltExtension-DateTime is used along with a namespace prefix of dateTimeObj. Any namespace URI can be used as long as it is consistent between the ASP.NET page and the XSLT style sheet. Listing 7.14 shows the complete style sheet and highlights where the external object is referenced and used.

Listing 7.14 Calling External Objects Within an XSLT Style Sheet (xsltExtension.xsl)

 1: <?xml version="1.0"?>
 2: <xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
 3:  xmlns:dateTimeObj="urn:xsltExtension-DateTime" version="1.0">
 4:   <xsl:output method="html" indent="yes"/>
 5:   <xsl:param name="golferName" select="'Dan'"/>
 6:   <xsl:template match="/">
 7:     <xsl:apply-templates 
 8:      select="//golfer[name/firstName=$golferName]"/>
 9:   </xsl:template>
 10:   <xsl:template match="golfers">
 11:     <xsl:apply-templates select="golfer"/>
 12:   </xsl:template>
 13:   <xsl:template match="golfer">
 14:     <table class="borders" border="0" width="640" cellpadding="4"
 15:      cellspacing="0" bgcolor="#efefef">
 16:       <xsl:apply-templates select="name"/>
 17:       <tr class="blackText">
 18:         <td width="12%" align="left">
 19:           <b>Skill: </b>
 20:         </td>
 21:         <td width="12%" align="left">
 22:           <xsl:attribute name="style">
 23:             <xsl:choose>
 24:               <xsl:when test="@skill='excellent'">
 25:                 color:#ff0000;font-weight:bold;
 26:               </xsl:when>
 27:               <xsl:when test="@skill='moderate'">
 28:                 color:#005300;
 29:               </xsl:when>
 30:               <xsl:when test="@skill='poor'">
 31:                 color:#000000;
 32:               </xsl:when>
 33:               <xsl:otherwise>
 34:                 color:#000000;
 35:               </xsl:otherwise>
 36:             </xsl:choose>
 37:           </xsl:attribute>
 38:           <xsl:value-of select="@skill"/>
 39:         </td>
 40:         <td width="12%" align="left">
 41:           <b>Handicap: </b>
 42:         </td>
 43:         <td width="12%" align="left">
 44:           <xsl:value-of select="@handicap"/>
 45:         </td>
 46:         <td width="12%" align="left">
 47:           <b>Clubs: </b>
 48:         </td>
 49:         <td width="40%" align="left">
 50:           <xsl:value-of select="@clubs"/>
 51:         </td>
 52:       </tr>
 53:       <tr>
 54:         <td colspan="6">&#xa0;</td>
 55:       </tr>
 56:       <tr class="blackText">
 57:         <td colspan="6" class="largeBlackText">
 58:           Favorite Courses 
 59:         </td>
 60:       </tr>
 61:       <tr>
 62:         <td colspan="2">
 63:           <b>City: </b>
 64:         </td>
 65:         <td colspan="2">
 66:           <b>State: </b>
 67:         </td>
 68:         <td colspan="2">
 69:           <b>Course: </b>
 70:         </td>
 71:       </tr>
 72:       <xsl:apply-templates select="favoriteCourses"/>
 73:     </table>
 74:     <p/>
 75:     <xsl:value-of select="dateTimeObj:GetDateTime()"/>
 76:   </xsl:template>
 77:   <xsl:template match="name">
 78:     <tr>
 79:       <td colspan="6" class="largeYellowText" bgcolor="#02027a">
 80:         <xsl:value-of select="firstName"/>
 81:         &#xa0;
 82:         <xsl:value-of select="lastName"/>
 83:       </td>
 84:     </tr>
 85:   </xsl:template>
 86:   <xsl:template match="favoriteCourses">
 87:     <xsl:apply-templates/>
 88:   </xsl:template>
 89:   <xsl:template match="course">
 90:     <xsl:call-template name="writeComment"/>
 91:     <tr class="blackText">
 92:       <td colspan="2" align="left">
 93:         <xsl:value-of select="@city"/>
 94:       </td>
 95:       <td colspan="2" align="left">
 96:         <xsl:value-of select="@state"/>
 97:       </td>
 98:       <td colspan="2" align="left">
 99:         <xsl:value-of select="@name"/>
100:       </td>
101:     </tr>
102:   </xsl:template>
103:   <xsl:template name="writeComment">
104:     <xsl:comment>List Course Information</xsl:comment>
105:   </xsl:template>
106: </xsl:stylesheet>

How is the GetDateTime() method called on the XsltDateTime object that is passed into the style sheet? This is accomplished by referencing the namespace prefix (dateTimeObj) associated with the namespace URI assigned to the object in the ASP.NET page (urn:xslt Extension-DateTime). The prefix is declared in line 3 and then used to call the GetDate Time() method in line 75. Although this is a fairly straightforward example of using external objects within XSLT style sheets, much more complicated functionality, such as querying databases or calling Web services, can be accomplished with XSLT external objects.

Before this chapter ends, the next section will show how to create a reusable XSLT class that can be used within ASP.NET applications and demonstrate the asp:Xml Web control.


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

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

URL: https://webreference.com/authoring/languages/xml/aspnet/chap7/12.html