A class that parses XML into XHTML using XSLT styles (compatible with ff and ie7.0)

Source: Internet
Author: User
Tags xslt

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 Code In the system, the module JS is used for packaging. The module tool is specially used for packaging Js. Article I will only use it now, but I have not studied its underlying code. Here we will write Javascript in a file, including classes and class implementation methods,
The following is the JS Code: Transform. js Copy code 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 (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 + = "<br> Error Code :";
TXT + = serror. errorcode;
TXT + = "<br> error cause :";
TXT + = serror. reason;
TXT + = "<br> 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 codeThe Code is as follows: <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<SCRIPT type = 'text/JavaScript 'src = "transform. js"> </SCRIPT>
</Head>
<Body>
<SCRIPT type = "text/JavaScript">
VaR xml = "Enter the XML Path here ";
VaR XSL = "Enter the XSL path here ";
VaR tempobj = new transformbinder (XML, XSL );
Tempobj. registeraction (transformxslt );
Tempobj. BIND ();
</SCRIPT>
</Body>
</Html>

Analyze transform. JS:
Xmldom: This constructor is used to create DOM elements of XML. For IE and FF, the method for creating Dom is different. ie uses window. activexobject is created, and FF uses document. implementation. createdocument is created by using the createdocument method. We use these two attributes to determine whether it is IE or ff.
XML ["msxml2.domdocument. 5.0 "," msxml2.domdocument. 4.0 "," msxml2.domdocument. 3.0 "," msxml2.domdocument "," Microsoft. xmldom "], use the for loop to traverse and find the corresponding version, and then create a DOM using the new activexobject (arrsignatures [I;
Use document. Implementation. createdocument ("", "", null) in FF to directly create the DOM;
Throw error if the browser does not support xml dom objects.
The constructor of transformxslt converts XML into HTML using XSLT. There is no transformnode in FF, so we have constructed a method by ourselves, Copy code The Code is as follows: 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;
}

Then implement conversion using this method. In terms of handling errors, ie and FF have different processing methods. IE is relatively simple. There is a parseerror attribute that loads error information. errorcode is the error code, reason is the cause of the error, line is the row number of the error, and there are other information, as long as the main error information is displayed here, if the error is displayed, the error content is displayed, if no error occurs, the result sresult of the conversion is displayed. FF is a little more complex, using xmlserializer and xmlserializer. serializetostring () converts xmldom to a string, and then converts the string to a DOM object. If an error is reported during the conversion process, information containing parsererror can be obtained, determine whether the tagname of the string is parsererror. If yes, convert the DOM object to the content in the string. If not, the Conversion Result sresult is displayed.
Here are a few notes:
A. ie can check for xml dtd errors, while FF can only check for XML syntax errors;
B. It is helpless because it is necessary to judge the error in the browser and the final result is not good to be merged. It may seem unreasonable in the code structure.
This class is encapsulated with transformbinder to facilitate expansion and modification.Transformbinder. prototype. the registeraction prototype is used to register events and then use transformbinder. prototype. bind binds events. To use this class, you only need new transformbinder (XML, XSL), register the transformxslt event, and bind the event to achieve this effect. If you need to expand, create a new constructor and register and bind it to this class to achieve the effect.

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.