Tip Avoid Mixing DOM Objects from Different Versions of the Msxml Parser

If you take a look at Figure 17-24 (presented earlier in this chapter), you will notice three occurrences of the DOMDocument object: DOMDoc-ument, DOMDocument26, and DOMDocument30. To avoid ugly errors, do not mix the DOMDocument objects from different versions of MSXML parser. Notice how the TransformXML procedure shown above declares two instances of DOMDocument object—both of which use the DOM objects from the same version of MSXML parser (3.0). If you attempt to load one document into an instance of MSXML DOMDocument 2.6 and another one into an instance of MSXML DOMDocument 3.0, you are asking for trouble.

Using VBScript and XML DOM to Transform XML Documents

In the previous section you wrote a VBA procedure to transform the XML document into HTML. In this section you will learn how to process the formatting of an XML document on the client machine by creating an HTML file with some code written in VBScript.

The following exercise demonstrates how to use VBScript to transform an XML document into HTML.

1. Create a folder named TestXML under C:\Inetpub\wwwroot.

2. Right-click the folder name and choose Web Sharing from the shortcut menu.

3. When the TestXML Properties dialog box appears, activate the Web Sharing tab and click the Share This Folder option button.

4. In the Edit Alias dialog box, enter TestX in the Alias text box and click OK.

5. The Alias name (TestX) should appear in the Aliases list box. Click OK to exit the TestXML Properties dialog.

6. Copy the Courses.xml and MyCourses.xsl files to the TestXML folder.

7. Open Notepad and add the following code. When you are done, save the file as ClientTransform.htm in the TestXML folder.

<SCRIPT LANGUAGE="VBScnpt"> Dim xslDoc Dim xmlDoc

Sub LoadAndCombine() Set xslDoc = CreateObject("Microsoft.XMLDOM") Set xmlDoc = CreateObject("Microsoft.XMLDOM") xslDoc.Async = False xmlDoc.Async = False xslDoc.Load "MyCourses.xsl" xmlDoc.Load "Courses.xml"

Placeholder.innerHTML = xmlDoc.transformNode(xslDoc) End Sub </SCRIPT>

<BODY ONLOAD="LoadAndCombine"> <DIV ID="Placeholder"></DIV> </BODY> </HTML>

Notice that the example code above contains a VBScript code fragment placed in the Head section of the HTML document. First, two global object variables are declared—one will hold the reference to the XML document and the other will point to the XSL stylesheet. Next, within the subprocedure named LoadAndCombine, two references to the XML DOM are set via the CreateObject(Microsoft.XMLDOM) method. The next two lines tell the parser to load both the XML and the XSL document synchronously. Therefore, the browser will wait until both documents are loaded before proceeding with the transformation. The next two lines use the Load method to load the XML document (Courses.xml) and the XSL stylesheet document (MyCourses.xsl) into the client's memory. The last line of the procedure uses the transform-Node method to apply the stylesheet (xslDoc) to the XML document (xmlDoc) and uses the innerHTML property to fill a <DIV> tag (identified by the ID="Placeholder") with the result of the transform (the result of the transformation is XML text).

Following the VBScript code fragment is HTML code. Inside the <BODY> tag, you should specify the name of the procedure to be executed when the OnLoad event occurs. Finally, the line <DIV ID="Placeholder"></DIV> tells the browser to replace the information between the DIV tags with the obtained XML text.

8. Open the HTML file that you created in step 7. When you open the file in the browser, the OnLoad event fires and the browser displays a nicely formatted document.

0 0

Post a comment