问题
I wanted to ask if I could use the jQuery.parseXML(data) to validate XML string?
I mean I know it's used for parsing XML, and if the XML is not valid I will get an error message, but will the function fail every time that there will be even a minor error in the XML?
I've tried googling about it, but all I found is regarding parsing, and not about validation.
Thanks!
回答1:
No, not via jQuery (at least not in Mozilla and most likely the other non-IE browsers).
In the case of IE, it is stated at http://msdn.microsoft.com/en-us/library/windows/desktop/ms754585%28v=vs.85%29.aspx , with loadXML, "You cannot use it to validate the XML document against a schema.", so apparently the code used in jQuery at http://code.jquery.com/jquery-latest.js will not validate either.
However, some code at http://www.stylusstudio.com/xsllist/200301/post50460.html seems to suggest it is possible in IE (only) with the right IE--at least with the 4.0 version of Msxml2.DOMDocument -- since according to http://msdn.microsoft.com/en-us/library/aa468547.aspx#beginner_failure , "the MSXML parser will validate your document against a DTD or schema if either has been specified in the document" (unless validateOnParse property is set to false).
However, with Mozilla and the other browsers, these do not perform validation (I thought they had loaded external DTDs in the case of DOMParser() of Ajax, but this does not seem to work for me now, and in any case, even entities are not loaded in regular websites).
I don't recall whether http://code.google.com/p/jssaxparser/ or http://debeissat.nicolas.free.fr/relaxng.php will support external validation, but I think you'd need to go in this kind of direction.
Do you need to use XML? Perhaps JSONSchema could meet your needs? http://json-schema.org/implementations.html .
来源:https://stackoverflow.com/questions/12073972/xml-validation-using-jquery-parsexml-data