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.

objXMLDOMDocument2.validate();  

Return Value

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

Example

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.  
xmldoc.load("http://server/myData.xml");  
// 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");  
}  
else  
{  
   WScript.Echo("Validation error:" + err.reason);  
}   

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 valueHexadecimal valueDescription
E_PENDING0x8000000ADocument readyState is not 4, indicating the document is not completely loaded.
S_OK0The document is valid according to DTD or schemas.
S_FALSE1The 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 IDError Code (Hex Value)Message
XML_E_NODTD0xC00CE224The validate method failed because a DTD or schema was not specified in the document.
XML_E_NOTWF0xC00CE223The validate method failed because the document does not contain exactly one root node.
XML_ENTITY_UNDEFINED0xC00CE002Reference to undefined entity '%1'.
XML_INFINITE_ENTITY_LOOP0xC00CE003Entity '%1' contains an infinite entity reference loop.
XML_NDATA_INVALID_PE0xC00CE004Cannot use the NDATA keyword in a parameter entity declaration.
XML_REQUIRED_NDATA0xC00CE005Cannot use a general parsed entity '%1' as the value for attribute '%2'.
XML_NDATA_INVALID_REF0xC00CE006Cannot use unparsed entity '%1' in an entity reference.
XML_EXTENT_IN_ATTR0xC00CE007Cannot reference an external general parsed entity '%1' in an attribute value.
XML_ELEMENT_UNDECLARED0xC00CE00DThe element '%1' is used but not declared in the DTD or schema.
XML_ELEMENT_ID_NOT_FOUND0xC00CE00EThe attribute '%1' references the ID '%2', which is not defined in the document.
XML_EMPTY_NOT_ALLOWED0xC00CE011Element cannot be empty according to the DTD or schema.
XML_ELEMENT_NOT_COMPLETE0xC00CE012Element content is incomplete according to the DTD or schema.
XML_ROOT_NAME_MISMATCH0xC00CE013The name of the top-most element must match the name of the DOCTYPE declaration.
XML_INVALID_CONTENT0xC00CE014Element content is invalid according to the DTD or schema.
XML_ATTRIBUTE_NOT_DEFINED0xC00CE015The attribute '%1' on this element is not defined in the DTD or schema.
XML_ATTRIBUTE_FIXED0xC00CE016Attribute '%1' has a value that does not match the fixed value defined in the DTD or schema.
XML_ATTRIBUTE_VALUE0xC00CE017Attribute '%1' has an invalid value according to the DTD or schema.
XML_ILLEGAL_TEXT0xC00CE018Text is not allowed in this element according to the DTD or schema.
XML_MULTI_FIXED_VALUES0xC00CE019An attribute declaration cannot contain multiple fixed values: '%1'.
XML_ELEMENT_UNDEFINED0xC00CE01CReference to undeclared element: '%1'.
ML_XMLNS_FIXED0xC00CE01EAttribute '%1' must be a #FIXED attribute.
XML_REQUIRED_ATTRIBUTE_MISSING0xC00CE020Required attribute '%1' is missing.
XML_DTD_EXPECTING0xC00CE026Expecting: %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

readyState Property (DOMDocument)
IXMLDOMParseError
IXMLDOMDocument2

Show: