validate Method1


Performs run-time validation on the currently loaded document using the currently loaded document type definition (DTD), schema, or schema collection.

MSXML 6.0 doesn't support XDR schemas; the validate method validates only against DTDs and XSD schemas.

JScript Syntax


Return Value

A ParseError object indicating exactly what error occurred, if any.


The following Microsoft JScript example shows how to validate at runtime.

var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");  
xmldoc.async = false  
// This will validate on load because validateOnParse is set to true   
// by default.  
// You make a change to an attribute:  
xmldoc.documentElement.setAttribute("a", "123");  
// Now you want to verify your document is still valid:  
var err = xmldoc.validate();  
if (err.errorCode == 0)  
   WScript.Echo("Document is valid");  
   WScript.Echo("Validation error:" + err.reason);  

C/C++ Syntax

HRESULT validate(IXMLDOMParseError ** errorObj);  

Return Values

An IErrorInfo containing a formatted error message indicating what went wrong, and one of the following HRESULTs (also returned in the ErrorInfo.Number property).

Return value Hexadecimal value Description
E_PENDING 0x8000000A Document readyState is not 4, indicating the document is not completely loaded.
S_OK 0 The document is valid according to DTD or schemas.
S_FALSE 1 The document is invalid according to DTD or schemas. For error information, see the returned IXMLDOMParseError object.

The following possible validation errors returned with S_FALSE are listed, together with the errorCode value.

Message ID Error Code (Hex Value) Message
XML_E_NODTD 0xC00CE224 The validate method failed because a DTD or schema was not specified in the document.
XML_E_NOTWF 0xC00CE223 The validate method failed because the document does not contain exactly one root node.
XML_ENTITY_UNDEFINED 0xC00CE002 Reference to undefined entity '%1'.
XML_INFINITE_ENTITY_LOOP 0xC00CE003 Entity '%1' contains an infinite entity reference loop.
XML_NDATA_INVALID_PE 0xC00CE004 Cannot use the NDATA keyword in a parameter entity declaration.
XML_REQUIRED_NDATA 0xC00CE005 Cannot use a general parsed entity '%1' as the value for attribute '%2'.
XML_NDATA_INVALID_REF 0xC00CE006 Cannot use unparsed entity '%1' in an entity reference.
XML_EXTENT_IN_ATTR 0xC00CE007 Cannot reference an external general parsed entity '%1' in an attribute value.
XML_ELEMENT_UNDECLARED 0xC00CE00D The element '%1' is used but not declared in the DTD or schema.
XML_ELEMENT_ID_NOT_FOUND 0xC00CE00E The attribute '%1' references the ID '%2', which is not defined in the document.
XML_EMPTY_NOT_ALLOWED 0xC00CE011 Element cannot be empty according to the DTD or schema.
XML_ELEMENT_NOT_COMPLETE 0xC00CE012 Element content is incomplete according to the DTD or schema.
XML_ROOT_NAME_MISMATCH 0xC00CE013 The name of the top-most element must match the name of the DOCTYPE declaration.
XML_INVALID_CONTENT 0xC00CE014 Element content is invalid according to the DTD or schema.
XML_ATTRIBUTE_NOT_DEFINED 0xC00CE015 The attribute '%1' on this element is not defined in the DTD or schema.
XML_ATTRIBUTE_FIXED 0xC00CE016 Attribute '%1' has a value that does not match the fixed value defined in the DTD or schema.
XML_ATTRIBUTE_VALUE 0xC00CE017 Attribute '%1' has an invalid value according to the DTD or schema.
XML_ILLEGAL_TEXT 0xC00CE018 Text is not allowed in this element according to the DTD or schema.
XML_MULTI_FIXED_VALUES 0xC00CE019 An attribute declaration cannot contain multiple fixed values: '%1'.
XML_ELEMENT_UNDEFINED 0xC00CE01C Reference to undeclared element: '%1'.
ML_XMLNS_FIXED 0xC00CE01E Attribute '%1' must be a #FIXED attribute.
XML_REQUIRED_ATTRIBUTE_MISSING 0xC00CE020 Required attribute '%1' is missing.
XML_DTD_EXPECTING 0xC00CE026 Expecting: %1.


This method only validates fully loaded documents (readyState == 4).

The validate method returns an IXMLDOMParseError that is independent of the value returned by the parseError property on a document. Only the errorCode and reason properties of the returned value are set.

Unlike the load method, validate will fail if there is no DTD or schema applied to the document element. Therefore, validate will not be able tell you whether the document is just well-formed.

The validate method does not parse new schemas, but can import a schema from a SchemaCache associated with the document through the schemas property. If there is no schema for a given namespace, the elements in that namespace will not be validated.


Implemented in: MSXML 3.0 and MSXML 6.0

See Also

readyState Property (DOMDocument)