Can't Tell
Can't Tell

Reputation: 13476

Check if a String is HTML or XML

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.

Upvotes: 1

Views: 6703

Answers (3)

netrox
netrox

Reputation: 5326

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 common being the required <?xml version="1.0">"

http://en.wikipedia.org/wiki/XML

Upvotes: 3

RobG
RobG

Reputation: 147503

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.

Upvotes: 4

James Black
James Black

Reputation: 41848

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.org/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.net/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.com/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.

Upvotes: 0

Related Questions