Validate XML against XSD with Javascript

Hi,

I'm trying to validate an XML that looks like this:
<?xml version="1.0"?><ROOT-Doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://10.255.255.54/schema/doc/myXSD.xsd">...</ROOT-Doc>

The code snippet I paste is not working for me, as it doesn't validate the XML against the XSD specified as an attibute in the root tag of the XML (http://10.255.255.54/schema/doc/myXSD.xsd) It validates the correct load balancing of the tags, but if does NOT check the validity against the XSD.

I've checked that the XSD is accessible from the computer I'm trying to make the validation by browsing it with the browser, so that's not the problem. Do I need anything else in the code for the validation against the XSD to work? Maybe the XSD attribute should go somewhere else in the XML and not as an attribute of the root tag?

Thanks in advance,
Carlos.
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.validateOnParse = true;
xml.loadXML(xmlString);
if (xml.parseError != '0') {
 alert("XML error");
}

Open in new window

cprietomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
The old (prior to v2) msxml did not do XSD validation,
so you need a more modern ActiveX component
use the ProgID "Msxml2.DOMDocument.6.0" (or 4.0, or 3.0) instead

var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
will work, but I recommmend that you install msxml6 and use 6.0

cheers

Geert
0
Geert BormansInformation ArchitectCommented:
other than that, I overlooked that your test is wrong

if (oXML.parseError.errorCode != 0)

parseError is the object, it will never be equal to 0,
you need for the errorCode property

full working code
         var oXML = new ActiveXObject("Msxml2.DOMDocument.4.0");
             oXML.async = false;
             oXML.validateOnParse = true;
             oXML.load("test.xml");
                              if (oXML.parseError.errorCode != 0)
                                    {
                                     alert("XML error");
                                    }
                              else
                                    {
                         alert(oXML.xml);
                        }        
0
cprietomAuthor Commented:
OK, it seems to be validating with Msxml2.DOMDocument.4.0, but something strange is happening, when I try to show the reason of the error:

                  if (xml.parseError.errorCode != 0) {
                        alert("XML error: " + this.xml.parseError.reason);
                  }

The alert that appears is: "XML error: Un caractère incorrect a été trouvé dans un contenu de texte". It looks like French!! but I'm in Spain, with a Windows XP in Spanish, an Internet Explorer 7 in English and configured with "Spanish" as the only language in the "Language Preference" of the IE. Why does that message appear? Couldn't it be something more descriptive and, if possible, in Spanish or English?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Geert BormansInformation ArchitectCommented:
It is indeed French and it says that there is an illegal character in your text content
(this is a wellformedness check rather than validation)

apparently you have installed a French msxml4
Uninstall msxml4 and install the english or spanish one.
There is a spanish one available here
http://www.microsoft.com/downloads/details.aspx?displaylang=es&FamilyID=3144b72b-b4f2-46da-b4b6-c5d7485f2b42

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cprietomAuthor Commented:
Thank you Gertone, I will investigate more on the validation, but this seems to be the right way for going against the XSD.
0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.