XML
  Home arrow XML arrow Page 4 - MSXML, concluded
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

MSXML, concluded
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2006-03-30

    Table of Contents:
  • MSXML, concluded
  • The DisplayTitles() Function
  • The ValidateDocument() Function
  • MSXML and XSLT
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    MSXML, concluded - MSXML and XSLT


    (Page 4 of 5 )

    MSXML can be used to transform an XML document using XSLT (see Chapter 6). Many times you’ll want to transform an XML document to an HTML page so a browser can display it. We’ll show you how to do this with MSXML. Here’s the table row in the HTML page that contains the XSLT stylesheet:

    <tr valign="top">
    <td nowrap><a href="#" onclick="TransformDocument(document.all(
        'stylesheet').value); return false;">Transform Document:</a></td>
    <td>
    <textarea id="stylesheet" rows="20" cols="80">
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl=
        "http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">
    <html>
       <body>
       <h2>CD Listing</h2>
       <table border="1">
          <tr>
             <th align="center">UPC</th>
             <th align="center">Artist</th>
             <th align="center">Title</th>
          </tr>
          <xsl:for-each select="catalog/cd">
          tr> 
            <td>
               <xsl:value-of select="@upc"/>
            </td>
            <td>
               <xsl:value-of select="artist"/>  
            </td>
            <td>
               <xsl:value-of select="title"/> 
            </td>
          </tr>
         
    </xsl:for-each>  
       </table>
       </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    </textarea>
    </td>
    </tr>

    The second table cell has a text area that contains the stylesheet. We’ve provided a default stylesheet, but you can change the default in the browser when you’re running this example. The first cell takes the stylesheet from the text area and passes it as an argument to the TransformDocument() function. Here’s the TransformDocument() function:

    function TransformDocument(stylesheet)
    {
       var xslProcessor;
       var xslTemplate = new ActiveXObject("Msxml2.XSLTemplate.4.0");
       var xslDocument = new ActiveXObject(
         "Msxml2.FreeThreadedDOMDocument.4.0"); 
       xslDocument.async = false; 
       xslDocument.loadXML(stylesheet);
       if (xslDocument.parseError.errorCode != 0)
       {
          var myErr = xmlDoc.parseError;
          alert("You have error " + myErr.reason);
          return;
       }
       xslTemplate.stylesheet = xslDocument; 
       xslProcessor = 
       xslTemplate.createProcessor();
       xslProcessor.input = objXML;
       xslProcessor.transform();
       window.frames.htmlresult.document.open();
       window.frames.htmlresult.document.clear();
       window.frames.htmlresult.document.write(xslProcessor.output); 
       window.frames.htmlresult.close();
    }

    The first line declares a variable.

    The second line creates an XSLTemplate object and assigns it to a variable.

    The third line creates a DOMDocument object and assigns it to a variable.

    The fourth line sets the async property to false so the next statement doesn’t execute until the document is loaded.

    The fifth line calls the loadXML() method and passes it the stylesheet.

    The sixth line determines if there is an error. If so, then an error message is displayed and the function returns to the statement that called it without transforming the XML document.

    The seventh line executes if there isn’t an error. This line assigns the xslDocument to the stylesheet property of the xslTemplate.

    The eighth line calls the createProcessor() method to create an xslProcessor.

    The ninth line assigns the XML document to the input property of the xslProcessor.

    The tenth line calls the transform() method to transform the XML document.

    Lines 11 through 14 write the transformed XML document to the browser. The results are shown next.

    CD Listing

    Here is the list of CDs organized by UPC, artist, and title that is produced by using MXSML to transform an XML document using XSLT. This is illustrated in the previous section of this chapter.

    UPC

    Artist

    Title

    602498678299

    U2

    How to Dismantle an Atomic Bomb

    75679244222

    Led Zeppelin

    Physical Graffiti

    75678367229

    Rush

    Rush in Rio

    74646938720

    Billy Joel

    Songs in the Attic

    75678263927

    Led Zeppelin

    Houses of the Holy

    8811160227

    Jimi Hendrix

    Are You Experienced?

    74640890529

    Bob Dylan

    The Times They Are A-Changin’

    More XML Articles
    More By McGraw-Hill/Osborne


       · This article is an excerpt from the book "XML DeMYSTified," published by...
     

    Buy this book now. This article is excerpted from chapter 10 of XML DeMYSTified, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109). Check it out today at your favorite bookstore. Buy this book now.

    XML ARTICLES

    - More on Triggers and Styles and Control Temp...
    - Looking at Triggers with Styles and Control ...
    - A Closer Look at Styles and Control Templates
    - Styles and Control Templates
    - Properties and More in XAML
    - Elements and Attributes in XAML
    - XAML in a Nutshell
    - Importing XML Files into Access 2007
    - Using MSXML3.0 with VB 6.0
    - MSXML, concluded
    - MSXML, continued
    - MSXML Tutorial
    - Generating XML Schema Dynamically Using VB.N...
    - XSL Transformations using ASP.NET
    - Applying XSLT to XML Using ASP.NET





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway