Instance method for parsing xml into xhtml using TransformBinder

Source: Internet
Author: User
Tags xsl xslt
Over the past few days, I have been studying how to convert xml into xhtml. the previous article introduced the use of xslt to parse xml into xhtml. over the past few days, I have been studying how to convert xml into xhtml, the previous article describes how to use xslt to parse xml into xhtml.

Because the preceding method xslt needs to be imported directly inside the xml file, and the xml file used in the project is generated by the system, it can only provide paths, but cannot rewrite the content in the xml file, therefore, you need to find a method that can associate xml with xslt in external parts, so that it can be applied to multiple xml files for convenient management.
First, run the code. in the system, use the module js for packaging. the module tool is specially used to package js. This tool will be introduced in later articles. I will only use it now, we haven't studied its underlying code. here we write js in a file, including classes and class implementation methods,
The following is the js code: transform. js

The code is as follows:

Var XmlDom = function () {if (window. activeXObject) {// IE var arrSignatures = ["MSXML2.DOMDocument. 5.0 "," MSXML2.DOMDocument. 4.0 "," MSXML2.DOMDocument. 3.0 "," MSXML2.DOMDocument "," Microsoft. xmlDom "]; for (var I = 0; I <arrSignatures. length; I ++) {try {var oXmlDom = new ActiveXObject (arrSignatures [I]); return oXmlDom;} catch (oError) {// ignore} throw new Error ("MSXML is not installed in your system. ");} else if (docu Ment. implementation. createDocument) {// Firefox var oXmlDom = document. implementation. createDocument ("", "", null); return oXmlDom;} else {throw new Error ("The browser does not support xml dom objects. ") ;}} var transformXSLT = function (_ XML, _ XSL) {if (window. node) {Node. prototype. transformNode = function (writable Dom) {var oProcessor = new writable tprocessor (); oProcessor. importStylesheet (writable Dom); var oResultDom = oProcessor. t RansformToDocument (myXmlDom); var oSerializer = new XMLSerializer (); var sXml = oSerializer. serializeToString (oResultDom, "text/xml"); return sXml ;}}var myXmlDom = new XmlDom (); myXmlDom. async = false; var mydomaindom = new XmlDom (); mydomaindom. async = false; myXmlDom. load (_ XML); mydomaindom. load (_ XSL); var sResult = myXmlDom. transformNode (my‑dom); if (window. activeXObject) {if (myXmlDom. parseError. errorCode! = 0) {var sError = myXmlDom. parseError; var txt = ""; txt + ="
Error code: "; txt + = sError. errorCode; txt + ="
Error cause: "; txt + = sError. reason; txt + ="
Error row number: "; txt + = sError. line; document. write (txt);} else {document. write (sResult) ;}} else if (document. implementation. createDocument) {var oSerializer = new XMLSerializer (); var sXmlDom = oSerializer. serializeToString (myXmlDom, "text/xml"); var oParser = new DOMParser (); var oXmlDom = oParser. parseFromString (sXmlDom, "text/xml"); if (oXmlDom.doc umentElement. tagName = "parsererror") {var oXmlSerializer = new XMLSerializer (); var sXmlError = oXmlSerializer. serializeToString (oXmlDom); alert (sXmlError);} else {document. write (sResult) ;}}var TransformBinder = function (XML, XSL) {this. XML = XML; this. XSL = XSL;} TransformBinder. prototype. registerAction = function (handlers) {this. handlers = handlers;} TransformBinder. prototype. bind = function () {var _ this = this; this. handlers (_ this. XML, _ this. XSL );}

The following is the html code: XSLTtransform.htm

The code is as follows:

   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.