PRB: MSXML SAX Validation with "exhaustive-errors" Feature Reports Only One Error for Each XML Tag (315497)



The information in this article applies to:

  • Microsoft XML 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 "exhaustive-errors" 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.

MORE INFORMATION

Steps to Reproduce Behavior

  1. 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:schema targetNamespace="xsdPerson" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="xsdPerson" 
               elementFormDefault="qualified">
    
      <xs:element name="People" type="PeopleData"/>
    
      <xs:complexType name="PeopleData">
            <xs:sequence>
               <xs:element name="Person" type="PersonData" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>    
      </xs:complexType>
    
      <xs:complexType name="PersonData">
            <xs:sequence>
               <xs:element name="Name" type="xs:string"/>        
            </xs:sequence>         
            <xs:attribute name="age" type="xs:integer" use="required"/>           
      </xs:complexType>
    
    </xs:schema>
    					
  2. 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="1.0"?>
    <People xmlns="xsdPerson" xsi:schemaLocation='xsdPerson person.xsd' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
    <Person age="aa" rank="1">      
     <Name>John</Name>
     <Gender>Male</Gender>
    </Person>
    </People>
    					
    NOTE: The <person> element in Person.xml includes an invalid value for the age attribute. In addition, the rank attribute is not defined in the schema. The <person> element also contains a <Gender> subelement that is not defined in the schema.

  3. In Microsoft Visual Basic 6.0, create a new Standard EXE project. Add a project reference to Microsoft XML, version 4.0.
  4. Add a class module named SAXValidator to the Visual Basic project.
  5. 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 & " Line : " & oLocator.lineNumber
    End SubPrivate Sub IVBSAXErrorHandler_fatalError(ByVal oLocator As MSXML2.IVBSAXLocator, strErrorMessage As String, ByVal nErrorCode As Long)
      Debug.Print strErrorMessage & " Line : " & 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.

  6. Drag a CommandButton control onto Form1.
  7. 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 "xsdPerson", "c:\person.xsd"
    
    rdr.putFeature "schema-validation", True
    rdr.putFeature "exhaustive-errors", True
    rdr.putProperty "schemas", sc
    
    Set rdr.errorHandler = Validator
    
    rdr.parseURL "c:\person.xml"
    					
    NOTE: You must modify the document paths if you saved them in different locations.

  8. 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:

    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 <person> 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.

Modification Type:MajorLast Reviewed:3/4/2002
Keywords:kbprb KB315497