A class TransformBinder (

Source: Internet
Author: User
I have been studying how to convert xml into xhtml over the past few days. the previous article introduced the use of xslt to parse xml into xhtml. because the previous method xslt needs to be directly imported inside the xml file, the xml file used in the project is generated by the system and can only provide the path

I have been studying how to convert xml into xhtml over the past few days. the previous article introduced 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
Copy the code 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 (document. implementation. createDocument) {// Firefox
Var oXmlDom = document. implementation. createDocument ("", "", null );
Return oXmlDom;
} Else {
Throw new Error ("The browser does not support xml dom object .");
}
}
Var transformXSLT = function (_ XML, _ XSL ){
If (window. Node ){
Node. prototype. transformNode = function (writable Dom ){
Var oProcessor = new effectprocessor ();
OProcessor. importStylesheet (writable Dom );
Var oResultDom = oProcessor. transformToDocument (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
Copy the code 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.