Determine whether an XML document is used

Source: Internet
Author: User

The flash of XHTML brings a lot of problems. It aims to make HTML more like XML, so it is called XHTML. However, the engineer did not take the lead. Let's discuss how to determine that the document is XML.

In my impression, jquery has repeatedly implemented this method many times and should be more authoritative. It also shows that this determination is difficult. See the implementation of jquery1.42:

 
VaR isxml = function (ELEM) {// documentelement is verified for cases where it doesn't yet exist // (such as loading iframes in IE-#4833) vaR documentelement = (ELEM? ELEM. ownerdocument | ELEM: 02.16.doc umentelement; return documentelement? Documentelement. nodename! = "Html": false ;};

Well, do an experiment:

 
Window. onload = function () {try {var Doc = document. implementation. createdocument (null, 'html ', null); // create an XML document alert(doc.doc umentelement) Alert (isxml (DOC) in a standard browser only )) // return true} catch (e) {alert ("creatdocument method not supported ")}}

<Br/> <! Doctype HTML> <br/> <HTML lang = "en"> <br/> <pead> <br/> <meta charset = "UTF-8"/> <br/> <meta content = "Ie = 8" http-equiv = "X-UA-compatible"/> <br/> <title> judge whether it is XML by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> var isxml = function (ELEM) {<br/> var documentelement = (ELEM? ELEM. ownerdocument | ELEM: 02.16.doc umentelement; <br/> return documentelement? Documentelement. nodename! = "Html": false; <br/>}; <br/> window. onload = function () {<br/> try {<br/> var Doc = document. implementation. createdocument (null, 'html ', null); <br/> alert(doc.doc umentelement) <br/> alert (isxml (DOC) <br/>} catch (E) {<br/> alert ("The creatdocument method is not supported ") <br/>}< br/> </SCRIPT> <br/> </pead> <br/> <body> </P> <p> </body> <br/> </ptml> <br/>

RunCode

Another experiment:

<Br/> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" <br/> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br /> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> <br/> <title> judge whether it is XML by situ zhengmei </title> <br/> <SCRIPT type =" text/JavaScript "> <br />/* <! [CDATA [*/<br/> var isxml = function (ELEM) {<br/> var documentelement = (ELEM? ELEM. ownerdocument | ELEM: 02.16.doc umentelement; <br/> alert (documentelement. nodename) <br/> return documentelement? Documentelement. nodename! = "Html": false; <br/>}; <br/> window. onload = function () {</P> <p> alert (isxml (document )) <br/>}< br/>/*]> */<br/> </SCRIPT> <br/> </pead> <br/> <body> </P> <p> </body> <br/> </ptml> <br/>

Run code

The evaluation result. It can determine the XHTML document, but cannot determine the first XML document whose element is HTML.

It seems that we need more reliable feature detectives. We know that HTML of IE does not support XPath, but XML, which can be used. For standard browsers, see a series of methods mentioned in my other blog post JavaScript cross-document calling technology. Since createhtmldocument is available and HTML document objects should be available, the standard browser has always been generous and exposes many underlying methods for us to expand, such as _ PROTO __, node, window, element, htmlelement or something. Test this.

The Firefox official website also saw such a decision, which is based on XUL and only valid for Firefox.

 
// Https://developer.mozilla.org/En/XML/Identifying_XML_elements_and_documentsfunction isxmldoc (DOC) {var CI = components. interfaces; // remove the second condition if only wish to test for XML, not XUL return (Doc instanceof CI. nsidomxmldocument) | (Doc instanceof CI. nsidomxuldocument );}

In any case, it is easy to understand the class htmldocument. The following is my implementation:

VaR isxml = (function () {If (-[1,]) {return function (DOC) {return! (Doc instanceof htmldocument) }}else {return function (DOC) {return "selectnodes" in Doc }}})();

The following is the test code:

 
VaR createxml = function (STR) {If (typeof domparser! = "Undefined") {return (New domparser ()). parsefromstring (STR, "application/XML");} else if (activexobject) {var xml = new activexobject ("Microsoft. xmldom "); XML. async = "false"; XML. loadxml (STR); Return XML} window. onload = function () {var xml = createxml ('<HTML> <body> <book> <title> situ zhengmei </title> </book> </body>  

<Br/> <! Doctype HTML> <br/> <HTML lang = "en"> <br/> <pead> <br/> <meta charset = "UTF-8"/> <br/> <meta content = "Ie = 8" http-equiv = "X-UA-compatible"/> <br/> <title> judge whether it is XML by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> var createxml = function (STR) {<br/> If (typeof domparser! = "Undefined") {<br/> return (New domparser ()). parsefromstring (STR, "application/XML"); <br/>} else if (activexobject) {<br/> var xml = new activexobject ("Microsoft. xmldom "); <br/> XML. async = "false"; <br/> XML. loadxml (STR); <br/> return XML <br/>}</P> <p> var isxml = (function () {<br/> If (-[1,]) {<br/> return function (DOC) {<br/> return! (Doc instanceof htmldocument) <br/>}< br/>} else {<br/> return function (DOC) {<br/> return "selectnodes" in doc <br/>}< br/>}) (); </P> <p> window. onload = function () {<br/> var xml = createxml ('<HTML> <body> <book> <title> situ zhengmei </title> </book> </body> </ptml> '); <br/> alert (isxml (XML )) <br/>}< br/> </SCRIPT> <br/> </pead> <br/> <body> </P> <p> </body> <br/> </ptml> <br/>

Run code

Someone may ask, why not use xmlshortet? Okay, because xmldesket is supported late in opera. It is supported by opera9.6. This is the data I saw on my Japanese blog. However, opera is a niche browser. You can use xmldocument directly regardless of the old version.

In addition, I used to use the following judgment. It is equally feasible, but I want to create two element objects. In IE7, element nodes not added to the DOM tree will not be recycled. Special processing is required. This is troublesome and you can give up.

 
VaR isxml = function (DOC) {return Doc. createelement ("p"). nodename! = Doc. createelement ("p"). nodename ;};
 
// Safari 2 missing document. compatmode property // makes harder to detect quirks vs. Strict mode var isquirks = function (document) {return (document. compatmode? (Document. compatmode. indexof ('css ')
 function isxml (context) {context = context. ownerdocument | document; return context. createelement ("p "). nodename! = Context. createelement ("p"). nodename} 

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.