最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if a String is HTML or XML - Stack Overflow

programmeradmin6浏览0评论

Is there a way to check if a String is HTML or XML in JavaScript? Preferably using jQuery rather than some other library? Why I need to do this is because I need to know if it possible to have a function into which XML or HTML can be passed. If it is HTML we take one action and if it is XML we take another action.

Is there a way to check if a String is HTML or XML in JavaScript? Preferably using jQuery rather than some other library? Why I need to do this is because I need to know if it possible to have a function into which XML or HTML can be passed. If it is HTML we take one action and if it is XML we take another action.

Share Improve this question edited Jun 25, 2014 at 22:22 Sam 7,39816 gold badges47 silver badges68 bronze badges asked Jun 20, 2011 at 0:35 Can't TellCan't Tell 13.5k9 gold badges65 silver badges93 bronze badges 6
  • 5 What if it's HTML that's also well-formed XML (which is also known as XHTML)? Or are you just checking if it's either one and you don't need to know which it is? – BoltClock Commented Jun 20, 2011 at 0:36
  • @BoltClock - I took it to mean "if a string is HTML OR XML" as in, it doesn't matter which. But I may be wrong :) – James Allardice Commented Jun 20, 2011 at 0:38
  • Do you want to check if the string is a plete, valid representation of an HTML document, or a well-formed XML document? What about XHTML, which is HTML as well-formed XML? Or do you just want to know if a string could be a valid HTML or XML fragment? – kojiro Commented Jun 20, 2011 at 0:38
  • @BoltClock,@James,@kojiro Sorry for the ambiguity. I added some more dtail. – Can't Tell Commented Jun 20, 2011 at 1:44
  • not reliable but try if( str.indexOf('<') == -1 || str.indexOf('>') == -1 ) { //its html :| } – Aamir Afridi Commented Oct 17, 2012 at 11:09
 |  Show 1 more ment

3 Answers 3

Reset to default 4

Is there a way to check if a String is HTML or XML in JavaScript?

Not reliably. You can test for one (e.g. against a DTD or XSD) and if it fails, assume it's the other. However, those tests are meant to be run on entire documents with a valid DOCTYPE. There are many cases where a snippet of markup will pass validation for multiple markup languages. What then?

I think you need to explain why you need to know the difference.

The rule is that if it starts with <?xml version="1.0"> then it's XML-based. If it doesn't have it, it should NOT be considered XML as XML requires that particular tag.

"An XML file or stream is made up of the following structures: One or more Processing directives, the most mon being the required <?xml version="1.0">"

http://en.wikipedia/wiki/XML

I can think of two ways to do this.

The brute-force method is to have a list of all the valid html elements and if it isn't one of those then it must be xml. This may be the cleanest approach.

If you use a namespace for the html then the xml will probably be in a default namespace, so if you look for this line (Implementing a default namespace for XML documents) on this page (https://developer.mozilla/en/Introduction_to_using_XPath_in_JavaScript) you will see that anything in the default namespace will return null when using an XPath evaluation.

According to this page (http://www.nczonline/blog/2009/04/04/xpath-in-javascript-part-3/), xpath doesn't work on HTML elements, which would be another way to tell xml from html.

I haven't used xpath, but the implementation may differ among different browsers, according to this page: http://www.yaldex./ajax-tutorial-4/BBL0029.html, as Firefox may allow xpath to work on html elements.

So, xpath may be a good solution, depending on browsers supported and whether your xml may use a namespace, but since the number of elements supported in html is finite it would seem that looking within that group would be the most precise solution.

发布评论

评论列表(0)

  1. 暂无评论