Microsoft KB Archive/315497

= PRB: MSXML SAX Validation with &quot;exhaustive-errors&quot; Feature Reports Only One Error for Each XML Tag =

Article ID: 315497

Article Last Modified on 3/4/2002

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q315497



SYMPTOMS
When you use the Microsoft XML (MSXML) 4.0 Simple API for XML (SAX) parser to validate an Extensible Markup Language (XML) document against an XML Schema Definition (XSD) schema, the SAX parser reports only one error for each element. This occurs even when you set the SAXXMLReader coclass &quot;exhaustive-errors&quot; feature to True. For example, when an element in an XML document contains more than one invalid attribute or attribute value, the MSXML 4.0 SAX parser only reports one error.



RESOLUTION
To resolve this problem, you can remove or correct the first error the MSXML 4.0 SAX Parser reports for an XML element, and then rerun the validation to identify any additional errors.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
  Use the following sample code to create an XSD document named Person.xsd, and then save Person.xsd in the root folder of your hard disk: 



    

  <xs:element name=&quot;Name&quot; type=&quot;xs:string&quot;/> </xs:sequence> <xs:attribute name=&quot;age&quot; type=&quot;xs:integer&quot; use=&quot;required&quot;/> </xs:complexType>

</xs:schema> </li> <li> Use the following sample code to create an XML document named Person.xml, and then save Person.xml in the root folder of your hard disk: <?xml version=&quot;1.0&quot;?> <People xmlns=&quot;xsdPerson&quot; xsi:schemaLocation='xsdPerson person.xsd' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <Person age=&quot;aa&quot; rank=&quot;1&quot;> <Name>John</Name> <Gender>Male</Gender> </Person> </People> NOTE: The   element in Person.xml includes an invalid value for the age attribute. In addition, the rank attribute is not defined in the schema. The   element also contains a <Gender> subelement that is not defined in the schema.

</li> <li>In Microsoft Visual Basic 6.0, create a new Standard EXE project. Add a project reference to Microsoft XML, version 4.0.</li> <li>Add a class module named SAXValidator to the Visual Basic project.</li> <li> Paste the following code in the SAXValidator class module: Implements MSXML2.IVBSAXErrorHandler

Private Sub IVBSAXErrorHandler_error(ByVal oLocator As MSXML2.IVBSAXLocator, strErrorMessage As String, ByVal nErrorCode As Long) Debug.Print strErrorMessage & &quot; Line : &quot; & oLocator.lineNumber End SubPrivate Sub IVBSAXErrorHandler_fatalError(ByVal oLocator As MSXML2.IVBSAXLocator, strErrorMessage As String, ByVal nErrorCode As Long) Debug.Print strErrorMessage & &quot; Line : &quot; & oLocator.lineNumber End Sub

Private Sub IVBSAXErrorHandler_ignorableWarning(ByVal oLocator As MSXML2.IVBSAXLocator, strErrorMessage As String, ByVal nErrorCode As Long)

End Sub In this code, the class module, SAXValidator, implements the IVBSAXErrorHandler interface. The IVBSAXErrorHandler interface traps and reports errors that the MSXML 4.0 SAX parser encounters when it parses and validates an XML document.

</li> <li>Drag a CommandButton control onto Form1.</li> <li> Paste the following code in the Click event procedure of the CommandButton control to parse and to validate Person.xml by using Person.xsd: Dim rdr As New MSXML2.SAXXMLReader40 Dim Validator As New SAXValidator Dim sc As New MSXML2.XMLSchemaCache40

sc.Add &quot;xsdPerson&quot;, &quot;c:\person.xsd&quot;

rdr.putFeature &quot;schema-validation&quot;, True rdr.putFeature &quot;exhaustive-errors&quot;, True rdr.putProperty &quot;schemas&quot;, sc

Set rdr.errorHandler = Validator

rdr.parseURL &quot;c:\person.xml&quot; NOTE: You must modify the document paths if you saved them in different locations.

</li> <li>Run the project. Click the CommandButton when the form displays. The code in the IVBSAXErrorHandler_error procedure in the SAXValidator module displays the following output in the Visual Basic Immediate window:

<pre class="fixed_text">The attribute 'rank' on this element is not defined in the DTD/Schema. Line : 3 Element content is invalid according to the DTD/Schema. Line : 5

Notice that the error message reports that the rank attribute is not defined in the schema. The error message also reports that the <Gender> element on line 5 is invalid according to the schema.

In addition, notice that the error message does not report the invalid, noninteger value that is assigned to the age attribute of the   element. You can generate this error if you remove the rank attribute from the XML document and rerun the validation.

NOTE: The parser may return the validation error for the rank attribute before the parser returns the validation error for the age attribute. The schema information set does not consider the significance of the order of the attributes, and the order in which the attributes appear in the instance document does not have to be the same as the order in which the attributes are defined in the schema.

</li></ol>

Keywords: kbprb KB315497

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.