Using XML

One technology that is useful for abstracting display logic is XML. XML is not a type of code, although many programmers say that they know how to program in XML. Instead, XML is a hierarchical format used for describing data using text. Data described using XML has many benefits over other formats for data storage. XML is portable among operating systems. Text is a format that all operating systems can understand and parse. XML may be validated by software using a set of rules. XML should also be readable by a human being. XML may also be parsed and constructed using a common set of rules that may be documented or described to many potential consumers of the XML so that they can reliably parse the XML to perform data manipulation activities on the data.

XML has some disadvantages, too, in that it requires much more storage space than any of the comparative technologies it seeks to replace such as comma delimited or fixed width text. XML also requires a great deal of resources to parse and serialize. To illustrate the benefits and disadvantages of XML, it can be compared to a comma- delimited text file. Both the comma-delimited text file and XML file are text based, and as such, both are usable on any operating system. Both can be the result of the data stored in a database table serialized into each respective data format. The XML file will be three to five times larger than the comma-delimited version of the same data set, because no descriptors are used in the comma-delimited file for each row and field value of data, as would exist in an XML file. Parsing the XML will take much longer as well, based on its sheer size difference alone over the comma-delimited version.

Given a scenario in which an XML file and a comma-delimited file are sent for use to other third-party consumers, however, what would happen if another column was added to the database table that was the source of the two respective files? The comma- delimited file would likely end up with another column added to either side or somewhere in the middle, depending on the export logic used to create the comma-delimited file. The XML file would also have additional data, but since each data value in an XML document is explicitly defined and generally XML is parsed in an explicit way, there would likely be no failures arising from a consumer as long as the rules for creating the XML were followed. In contrast, the software parsing the comma-delimited file will likely fail because the format is not what the software was designed to expect.

Making a Transformation Using XSL

XML can provide abstraction of display logic by allowing the display logic to exist in another file. Using software that combines the XML and the display logic using a known instruction set, the XML may be transformed into any other type of document, such as an HTML document. Some of the known instruction sets in use are Extensible Stylesheet Language (XSL) or Extensible Stylesheet Language Transformation (XSLT). The display logic resides in the XSL sheet or XSLT sheet. The abstraction of display logic enables programmers to develop web applications from the perspective of producing XML documents that are meant to describe data or some object. The user interface may change as needed without necessitating a code change anywhere but in the XSL or XSLT file.

For an example of a simple transformation performed in an ASP, refer to Listing 12-11.

Listing 12-11: XMLToHTML.asp - Simple XML to HTML Transformation Performed in an ASP

start example
 <%@ Language=VBScript %> <HTML> <HEAD> </HEAD> <BODY> <% dim oXMLDoc dim oXMLXSL     Set oXMLDoc = Server.CreateObject("Microsoft.XMLDOM")     Set oXMLXSL = Server.CreateObject("Microsoft.XMLDOM")      oXMLDoc.load(Server.MapPath("employees.xml"))           oXMLXSL.load(Server.MapPath("employeesDisplay.xsl"))                 Response.Write( oXMLDoc.transformNode(oXMLXSL) ) %> </BODY> </HTML>
end example

The ASP page named XMLtoHTML.asp, shown in Listing 12-11, creates an instance of the XMLDOM class. The XMLDOM class is part of Microsoft’s MSXML library that is used to convert XML and XSL or XSLT into HTML. MSXML is a collection of classes and other software that also provides XML parsing, validation, construction, and other useful XML functions. The load function of the XMLDOM class is called using the complete filename as the argument to the function. The file being loaded resides in the web root, so the Server.MapPath function was called to get the whole physical path and filename to the XML file. The load function of the XMLDOM class can accept a string that contains an XML document as the argument or a string that contains the complete filename as the argument.

Refer to Listing 12-12 for the contents of the XML file that was loaded in this example.

Listing 12-12: employees.xml - tblEmployee Data Serialized into an XML Document

start example
 <?xml version="1.0"?> <Recordset Table = "MyMembership"             Command = "Select * From tblEmployee">      <Row number = "3" phone = "444-4444"             first_name = "Susy" last_name = "Lipschitz" />      <Row number = "4" phone = "444-555"             first_name = "Bertan" last_name = "Scudder" />      <Row number = "6" phone = "444-4444 "             first_name = "Joe" last_name = "Schmidlap" />      <Row number = "8" phone = "444-4444 "             first_name = "Tom" last_name = "Cleasak" /> </Recordset>
end example

This document is a simple XML document that describes the employees that were extracted from the tblEmployee table in Listing 12-10.

The same effort that was devoted to creating the class instance and loading the XML document was also performed on the XSL style sheet. XSL is also an XML document, so the XMLDOM class treats the file the same as the XML document. Refer to Listing 12-13 for the contents of the XSL file that was loaded in this example.

Listing 12-13: employeesDisplay.xsl - XSL Stylesheet to Make HTML from tblEmployee XML Document

start example
 <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <xsl:for-each select="Recordset"> <P><xsl:value-of select="@Table"/> <br/><xsl:value-of select="@Command"/><br/> </P> <TABLE BORDER="1" CELLSPACING="0" CELLPADDING="0">      <TR>           <TH>Number</TH>           <TH>Phone</TH>           <TH>First Name</TH>           <TH>Last Name</TH>      </TR>      <xsl:for-each select="Row">           <TR>                <TD><xsl:value-of select="@number"/></TD>                <TD><xsl:value-of select="@phone"/></TD>                <TD><xsl:value-of select="@first_name"/></TD>                <TD><xsl:value-of select="@last_name"/></TD>           </TR>                </xsl:for-each>                 </TABLE> </xsl:for-each> </xsl:template> </xsl:stylesheet>
end example

Once both the XSL and the XML documents are loaded into their own separate class instances of the XMLDOM class, the transformnode function is called from the XMLDOM instance that contains the XML document. The XMLDOM class instance that contains the XSL document is used as the argument to the transformnode function call, and the resulting string that is returned from transformnode is an HTML document. The product of the XSL and the XML document as it is displayed in a web browser can be seen in Figure 12-13.

click to expand
Figure 12-13: Resulting display of transformed XML into HTML in web browser

The HTML that was created from the XML shown in Listing 12-12 and the XSL shown in Listing 12-13 being transformed is shown in Listing 12-14.

Listing 12-14: HTML Resulting from XSL employeesDisplay.xsl and XML employees.xml Document Transformation

start example
 <HTML> <HEAD> </HEAD> <BODY> <P>MyMembership <br />Select * From tblEmployee<br /> </P> <TABLE BORDER="1" CELLSPACING="0" CELLPADDING="0"> <TR> <TH>Number</TH> <TH>Phone</TH> <TH>First Name</TH> <TH>Last Name</TH> </TR> <TR> <TD>3</TD> <TD>444-4444</TD> <TD>Susy</TD> <TD>Lipschitz</TD> </TR> <TR> <TD>4</TD> <TD>444-555</TD> <TD>Bertan</TD> <TD>Scudder</TD> </TR> <TR> <TD>6</TD> <TD>444-4444</TD> <TD>Joe</TD> <TD>Schmidlap</TD> </TR> <TR> <TD>8</TD> <TD>444-4444</TD> <TD>Tom</TD> <TD>Cleasak</TD> </TR> </TABLE> </BODY> </HTML>
end example




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