DOMParser: Can we make it cry less about multiply defined attributes?

166 views Asked by At

"Be liberal in what you accept, and conservative in what you send." -Jon Postel

Right now I'm using the chrome browser, but I've experienced the same in the past with Firefox.

JS code like...

(new DOMParser).parseFromString("<doc attr=\"foo\" attr=\"foo\" />","text/xml")

returns a pukey parsererror page instead of anything useful.

Presently, I'm consuming what superficially appear to be XML documents that actually have this problem. That is, some elements have the same attribute twice.

In accordance with the above-mentioned principle, I'd like my program (built upon the browser's DOMParser object) to do better than just throw its hands up in the face of inputs like these.

I don't really care if the first, last, or otherwise value for the attribute is taken.

Question: Is there a way to have the parser be less strict?

1

There are 1 answers

1
Michael Kay On BEST ANSWER

XML's philosophy has always been the opposite of Postel's law: If you're liberal in what you accept, this will encourage content creators to be negligent in what they create. If you're strict in what you accept, content creators will be forced to be strict in what they produce, otherwise what they produce will be useless. Fix the process that generated this non-XML file. (And never make the mistake of referring to such an object as an "XML document" - if it were XML, you wouldn't have the problem.)