|
ASP Kitchen: Classic ASP Articles: Practical uses of XML and XSL with Classic ASP Practical uses of XML and XSL with Classic ASPIntroductionNote: As of June 2007 this article has been archived and the example deactivated. This article shows some of the uses of XML and XSL, particularly in the display of data obtained from remote servers using HTTP. The article retrieves sample data obtained from my personal website and displays it on the ASPAlliance.com website in the table and chart below. The table and chart below shows the rough guide to the health of the web development job market. The figures are compiled from June 2001 and show the average daily number of permanent jobs listed on JobServe.co.uk that are based in London (UK) requiring the specific development skill listed in the table. Clicking on the skill name will display a chart of that skill's data. Page Error: Could not load XML from remote serverHow does this page work?A script within the global.asa on my personal website executes once a day and using the process of "screen scraping", does a search of JobServe.co.uk and extracts the number of advertised jobs for a specified skill (e.g. ASP). The screen scraping is carried out by making use of the AspHTTP server component from ServerObjects Inc., although other server components can be used (an article in the ASP Kitchen describes how to extract HTML using HTTP with PerlScript). The number of advertised jobs for a particular skill on a particular day are stored in an Access 95 database.
A monthly summary of the average number of jobs advertised requiring each skill is generated using the GetJobs query below: SELECT Format([DateTime],"mmmm")+"
"+CStr(DatePart("yyyy",[DateTime])) AS [Date], Avg(JobServe.ASP) AS ASP,
Avg(JobServe.PHP) AS PHP, Avg(JobServe.HTML) AS HTML, Avg(JobServe.JavaScript) AS
JavaScript, Avg(JobServe.JSP) AS JSP, Avg(JobServe.Java) AS Java, Avg(JobServe.Perl) AS
Perl, DatePart("m",[DateTime]) AS Month, DatePart("yyyy",[DateTime])
AS Year A Classic ASP page on the site is then used to convert the data to XML. This XML is accessible from the URL http://www.brettb.com/ASPAlliance/WebJobMarketXML.asp. Converting data to XML is straightforward. Later versions of ADO contain methods for automatically converting a RecordSet to XML, but in this example I've constructed the XML from a series of Response.Write statements. The important thing to remember is to change the Content-Type of the output to text/xml. The ASP is shown below: <% Retrieving XML from a remote URLThe FileSystemObject object can obviously be used to load XML from disk if it is located on the same machine/network as the web server. If the XML is on a remote server, then it is possible to load the XML via HTTP. There are various methods of retrieving XML from remote servers using HTTP. One solution is to use the AspHTTP server component from ServerObjects Inc.. Alternatively, PerlScript has built in support for HTTP. In this particular example, the Microsoft XMLHTTP component will be used. This component is supplied with Microsoft's XML Parser, and is installed with a variety of Microsoft products, including recent versions of Internet Explorer. The sample code below uses the component to save the XML to a variable named oResponseXML. The code checks to ensure that the XML has been retrieved by looking for a 200 HTTP status code. The bXMLLoadError variable is assigned a boolean value of True if the XML was not retrieved correctly. <% Formatting the XML into a tableAs can be seen at the top of the page, the job market data is displayed in two formats: as a table and as a chart. There are two methods of extracting the required information from the XML and formatting it as a table. The first method would be to write a script to parse the XML and extract the relevant data for each row in the table. Alternatively, use can be made of Extensible Stylesheet Language (XSL). This allows XML to be converted into HTML by making use of an appropriate stylesheet. The stylesheet is basically a template of HTML into which parts of the XML are substituted. XSL is described in more detail in some of the links in the further reading section. The stylesheet used in this example may be seen here. The Microsoft XML components make it straightforward to display XML as HTML using an XSL stylesheet. The sample code below will apply a stylesheet to the previously retrieved XML: <% XSL is a very detailed subject; additional resources are shown in the further reading section below. Formatting the XML as a chartThe XML is also formatted as a chart which shows the monthly average number of jobs advertised for a specific skill. The actual chart is produced using the AspImage server component from ServerObjects Inc. . The server component is called from an ASP file called JobChart.asp. The data to be displayed in the chart is sent in the querystring, using a format such as that shown below: http://www.brettb.com/JobChart.asp?Visit=June+2001%2C793&Visit=July+2001%2C763&Visit=August+2001%2C792&Visit=September+2001%2C785&Visit=October+2001%2C564&Visit=November+2001%2C491&Visit=December+2001%2C285 The link to the chart, therefore, requires data for the X (average number of visits) and Y (date) axes of the chart, which involves parsing the XML. For each skill, the date is extracted, together with the monthly average number of jobs advertised for the specific skill. The code for extracting these details is shown below. <% 'Initial part of the QueryString required to call
the ASP The chart is then included on the page using the following: <%If Not bXMLLoadError Then%> The use of the ASPImage server component to dynamically generate images is beyond the scope of this article, but may be covered in a future article. Incidentally, ASP.NET contains support for dynamic image generation using the System.Drawing functions. Further reading
Useful Development Tools
Author detailsBrett Burridge has worked as a web developer since 1997 and has developed web applications for a range of corporations, start up busiensses and educational establishments. Brett is presently employed as an Internet developer and technical writer through his own company, Winnersh Triangle Web Solutions Limited. The company produces a number of innovative products, including a range of software documentation tools, which include the ASP Documentation Tool, the .NET Documentation Tool for VB.NET and C#, and the SQL Server Documentation Tool. Other products include The Website Utility, which functions as a website error checker, search engine optimizer and ASP/ASP.NET search engine builder application. As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk, ASPToday, the software documentation portal www.softwaredocumentation.info, and has contributed recipes to the ASP.NET Developer's Cookbook. links Outside web development, Brett is interested in travelling (here are my travel logs from New York, Hong Kong and Tokyo), digital photography (here's my photo gallery), tropical fishkeeping and collecting contemporary works of art by artists such as Doug Hyde. Contact Brett by emailing Article history"Practical uses of XML and XSL with Classic ASP" published on ASPAlliance.com on 31 December 2001. ASP Kitchen: Classic ASP Articles: Practical uses of XML and XSL with Classic ASP |
|
|||||||||||||||||||||||||||
| © page content copyright Brett Burridge 1998 - 2009. | ||||||||||||||||||||||||||||