Integrating XML and XSL

Class clsChair supports the ability to abstract the presentation logic. The GetChairState function delivers a serialized version of the chair object to a consumer that can render a page. Typically, COM web applications use the ASP page for parsing the HTML submission, instantiating the classes, and facilitating the page rendering by stuffing the XML objects with the XML documents necessary to construct the page. In the COM - ASP - XSL (Extensible Stylesheet Language) or XSLT (Extensible Stylesheet Language Transformation) scenario, a multitiered web application may be created that abstracts the types of logic and technology summarized in Table 13-1.

Table 13-1: Tiers and Technology in COM Web Application

Tier Name

Supporting Technology

Presentation Logic

XSL or XSLT sheet

Business Logic

VB COM and ASP

Data Access Logic

ADO

Data Logic

SQL Server

The ASP page is a unique part of the solution, because it glues together the business logic and presentation logic. In the solution presented earlier in this chapter, the ASP is not a pure abstraction from either the business tier or the presentation tier, since it contains aspects of each. Listing 13-14 shows the source code to the dispatching ASP that was used with clsChair. XMLCOMChair.asp contains no HTML. All the page display is in the XSL sheet. Regardless of the way the data from clsChair is displayed in the browser, the ASP will not require any editing. Because the ASP does have to know something about the interface of the class clsChair, some business logic is included. If the class clsChair is updated with more functions or properties, the ASP will need to be edited. The ASP could be easily parameterized to accommodate a configurable class launching system. In that particular case, the ASP would be fully abstracted from the business logic and the presentation logic.

Listing 13-14: Source Code for XMLCOMChair.asp object dispatcher

start example
 <%@ Language=VBScript %> <% '~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Drawpage '     handles XML transformation '~~~~~~~~~~~~~~~~~~~~~~~~~~~ sub Drawpage(sXML) CONST XSL_FILE = "formatChair.xsl" dim oXML dim oXMLXSL     Set oXMLXSL = Server.CreateObject("Microsoft.XMLDOM")     Set oXML = Server.CreateObject("Microsoft.XMLDOM")      'load XML      oXML.loadXML(sXML)            'load the XSL      oXMLXSL.load(Server.MapPath(XSL_FILE))            'write the page      Response.Write(oXML.transformNode(oXMLXSL))      'destroy object      set oXML = nothing      set oXMLXSL = nothing end sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' main '     dispatches calls '~~~~~~~~~~~~~~~~~~~~~~~~~~~ sub Main() dim o dim XMLDOM      'create the object chair      set o = Server.CreateObject("ConfigSeat.clsChair")            'check for color setting      if len(Request("color"))>0 then           o.color = Request("color")      end if      'decide what to do      select case Request("Action")           case "New"                o.CreateChair           case "Open"                o.OpenChair Request("ID")      end select      'get the object state      set XMLDOM = o.GetChairState()            'write the page      Drawpage XMLDOM.XML            'destroy object      set o = nothing      set XMLDOM = nothing       end sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Script entry point '~~~~~~~~~~~~~~~~~~~~~~~~~~~ Main() %>
end example

After the class instance for clsChair has performed the business logic it was dispatched to do, the state of clsClass is serialized into XML and used to build the HTML page. The name of the XSL sheet, formatChair.xsl, is stored in the ASP code, but it could be obtained through another means such as HTTP arguments in form submissions or links. The Drawpage subroutine takes a string argument that contains XML and draws the entire web page output. The XSL sheet defines how the web page is built and how the values in the XML will be displayed. Other data may be placed in the XSL sheet, too, such as JavaScript routines that are meant to run in the web browser. Listing 13-15 shows the XSL sheet formatChair.xsl.

 

Listing 13-15: Source code for formatChair.xsl

start example
 <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <xsl:comment> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ XSL Sheet primarily for the purposes of building an HTML page to allow a user to open or create a new chair and see the results with the presentation logic abstracted completely ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ </xsl:comment> <xsl:comment>     Start the HTML page here </xsl:comment> <HTML>      <HEAD>           <TITLE>XML COM page</TITLE>      </HEAD> <BODY> <xsl:comment>      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ build table for chair data ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ </xsl:comment> <xsl:for-each select="Chair">      <P>current</P>      <TABLE BORDER="1" CELLSPACING="0" CELLPADDING="0">           <TR>                <TH>ID</TH>                <TH>Color</TH>           </TR>           <TR>                <TD><xsl:value-of select="@ID"/></TD>                <TD><xsl:value-of select="@Color"/></TD>           </TR>                </TABLE> </xsl:for-each> <xsl:comment>      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ XSL parts are completed Please never forget, all HTML must be well formed also. XSL is also case sensitive. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ </xsl:comment> <P>command </P> <xsl:comment>      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ build table for commanding new chair data ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ </xsl:comment> <FORM METHOD = "POST" ACTION = "XMLCOMChair.asp" ID = "frmImage"  NAME = "getInfo" >      <TABLE BORDER="1" CELLSPACING="0" CELLPADDING="0">           <TR>                <TD>Action</TD>                <TD>             <input TYPE="text" NAME="Action" VALUE="Open or New">             </input>               </TD>           </TR>                     <TR>                <TD>ID</TD>                <TD><input TYPE="text" NAME="ID" VALUE=""></input></TD>           </TR>           <TR>                <TD>color</TD>                <TD>             <input TYPE="text" NAME="color" VALUE="puce">             </input>               </TD>           </TR>      </TABLE>      <input  NAME="subR" TYPE="submit" VALUE="Go"></input> </FORM> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
end example

A few issues should be considered during the coding of XSL. All XSL must be well formed, which means that it must comply with the standards for properly formed XML according to the World Wide Web Consortium (W3C) organization. Well-formed errors are generally caused from the following problems in the XML:

  • Element tags do not match Make certain that the tags used include matching beginning and ending tags-including the HTML tags.

  • Inappropriate Spacing between characters The proper amount of spaces between certain parts within an element must be maintained. An extra space between a / character and a > character could make the XML not well-formed.

  • Special Characters encountered An element's and an attribute's values must have the special characters escape coded. Please see the sidebar 'Escape Coding XML' for more information about escaping characters.

XML editors can greatly improve your ability to eliminate well-formed errors in XML. You should consider using an XML editor to help diagnose errors in a solution that uses XML or for the purposes of developing software that constructs XML. If any XML document is not well formed, virtually nothing that requires the XML document will work. The existing XML parses do not offer a great deal of information about the point of failure, so finding the problem can be difficult.

When the whole solution is deployed to a server and the XMLCOMChair.asp is requested in a browser, the page should be drawn in the browser, as shown in Figure 13-21.

click to expand
Figure 13-21: XMLCOMChair.asp using the clsChair and formatChair.xsl to draw a web page

start sidebar
Escape Coding XML

Since XML uses plain text to describe data, problems may arise deciphering the data from the XML structure. For example, what would happen if you had an employee named O'Brien and your software put a list of the employees into an XML document to be rendered in the browser? The XML element might read as follows: <employee name='O'Brien'>. If the apostrophe found in O'Brien were not escape coded, the XML parser would bomb when XML was loaded because an apostrophe is considered a special character. If the XML were escape coded, the XML element should read as follows: <employee name='O&apos;Brien'>.

Many characters require escape coding. Special characters may be escaped using the numeric character references from the ISO/IEC 10646 character set. Some characters were provided special escape sequences, called general entities, that use an escape sequence similar to the characters themselves, as shown here:

Character

General Entity Escape Sequence

&

&amp;

<

&lt;

>

&gt;

&apos;

'

&quot;

For example, the numeric character reference for the & symbol is &#38; but you could also use &amp;. The numeric character reference for the = symbol is &#61;. There is no general entity escape sequence available for this symbol. Please refer to the Appendix for more characters and equivalent escape sequences. For more information on XML, please visit http://www.w3.org/TR/REC-xml to see the W3C specification on XML.

end sidebar




IIS 6(c) The Complete Reference
IIS 6: The Complete Reference
ISBN: 0072224959
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net