Jumat, 15 November 2013

Applying a Style Sheet to an XML Document

A style sheet object that passes into the transformNode method needs to be recompiled every time the method is called. Compiling a style sheet means setting all its template rules in an executable state. By using the XSLTemplate object and the XSLProcessor object to perform transformation facilitates  the reduction of overheads and increases the performance of an XSLT application.

The XSLTemplate Object


The XSLTemplate object is a dom object that is used to access an XSLT style sheet. This object is used to hold a cached style sheet that can then be dynamically associated with an XML document.

Before a style document can be applied to an XML document, it is converted into a tree structure by the parser. In the early versions of MSXML, the style sheet had to be compiled each time it was required for processing an XML document. In other words, the tree structure for a style sheet had to be recreated for the use of XSL document, as the compiled version was not stored in the system. This resulted in a longer processing time. However, in MSXML 2.6 and later, a compiled version of the XSLT document is stored in the cache memeory of the computer. This means that the XSLT tree is created the first time the document is compiled. For each successive use of the style sheet, the compiled version is reused.

The XSLT tree structure is loaded into the memory of the computer and used to process the XML document. This is because the XSLTemplate object stores the compiled XSLT document. Therefore, you must first create an XSLTemplate object. In the following example, an XSLTemplate object called xsltobj is created using JavaScript:

           xsltobj= new ActiveXObject ("MSXML2.XSLTemplate . 6 . 0") ;

This object must be associated with an XSLT style sheet document. Consider the following code snippet:

          var xsldocobj= new  ActiveXObject ("Msxm12.freeThreadedDOMDocument . 6 . 0") ;
           xsldocobj . load ("products . xsl") ;
           xsltobj . stylesheet=xsldocobj ;

In the preceding example, an instance of the FreeThreaded DOMDocument object is created. This object can be used to access an XML document as well as an XSLT style sheet. This is because XSLT is an application of XML. The load () method of the FreeThreaded DOMDocument object is used to associate the XSL document products.xsl with the xsldocobj object. The stylesheet property of the xsltobj object is then used to associate the XSLTemplate object with the object that holds the XSL document.

The XSLProcessor Object


To process an XML document by using a style sheet, you must create an instance of the XSLProcessor object. This object is used to apply a style sheet to an XML document and then process that document. The XSLProcessor object is supported only in IE 5.0 and higher versions.

The XSLProcessor object applies the given XSLT document to a specifc XML document. In other words, this object transforms an XML document by using the XSLT style sheet. For example, an XML document can be transformed into an HTML document by applying the appropriate XSLT style sheet by using XSLProcessor object.

The JavaScript code to create an XSLProcessor object is as follows:

        var  xsalprocobj= xsltobj.createProcessor ( ) ;

In this example, the createProcessor ( ) method is used to create a new XSLProcessor object called xslprocobj. The createProcessor ( ) method is called by xsltobj, which represents a cached version of a compiled XSLT template. The xsltobj object is associated with a specific style sheet contained in the variable xsldocobj.

You can then create the XML document for which the style sheet contained in the xsltobj object must be applied. Consider the following example:

        var xmldocobj = new ActiveXObject ("Msxm12 . DOMDocument . 6 . 0") ;
        xmldocobj . load ("products . xml") ;

In the example, a new DOMDocument object called xmldocobj is created. The load ( ) method of this object is used to associate an XML document called products . xml with this object. This XML document is then passed to the XSLProcessor object as an input by using the input property. This property is used to specify the XML tree that must be transformed, as follows:

        xslprocobj . input=xmldocobj ;

The transform ( ) method of the XSLProcessor object is then invoked. This method performs the transformation of an XML document. During transformation, the tree structures of the XML and XSLT documents are used as input. The XSLT tree is applied to the XML tree and the document is then processed. The output of this process is an XML document that is rendered in the manner specified in the XSLT document. Consider the following example:

       xslprocobj . transform ( ) ;

In the preceding example, the transform ( ) method of the XSLProcessor object is invoked. The transform() method can be called multiple times for an XML Document to transform the different sections of the XML document.. The result of the transform ( ) method is displayed using the output property of the XSLProcessor object. The output can be displayed in a browser window or a message box. Consider the following example:

      alert (xslprocobj . output ) ;

In this example, the alert ( ) method is used to display the data stored in the transformed XML document in a message box.

Tidak ada komentar:

Posting Komentar