You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Somsak AsSawakulpaibool <so...@comhill.com> on 2000/12/14 18:15:21 UTC

Bug in Schema validation feature

Xerces version: 1.2.3
   JDK version: 1.3.0

Problem:
I tested the Xerces's DOMParser with the below schema. It doesn't give me any error. 
It supposed to print out error about the second id element in the XML file (the maxOccurs is limitted to 1) but it didn't.

What did I do wrong?

Thanks
A.Somsak

Code sample:
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.apache.xerces.dom.*;
import org.apache.xerces.parsers.*;

public class ElementList
{
    public static void main(String[] args) throws Exception
    {
        DOMParser parser = new DOMParser();
        //parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setFeature( "http://apache.org/xml/features/validation/schema", true);
        parser.setErrorHandler(new ErrorHandler()
        {
            public void error(SAXParseException exception)
            {
                System.out.println(exception.getMessage());
            }

            public void fatalError(SAXParseException exception)
            {
                System.out.println(exception.getMessage());
            }

            public void warning(SAXParseException exception)
            {
                System.out.println(exception.getMessage());
            }
        });

        parser.parse("test.xml");
        Document document = parser.getDocument();
        System.out.println((Node)document);
    }
}

XML sample:
<?xml version="1.0" encoding="UTF-8"?>
<staff xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
	<person>
		<id no="12345"/>
		<id no="1234567890"/>
	</person>
</staff>

Schema sample:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
	<xsd:element name="staff">
		<xsd:complexType>
			<xsd:sequence>
				<xsd:element ref="person" minOccurs="0" maxOccurs="1"/>
			</xsd:sequence>
		</xsd:complexType>
	</xsd:element>

	<xsd:element name="person">
		<xsd:complexType>
			<xsd:sequence>
				<xsd:element ref="id" minOccurs="0" maxOccurs="1"/>
			</xsd:sequence>
		</xsd:complexType>
	</xsd:element>

	<xsd:element name="id">
		<xsd:complexType>
			<xsd:attribute name="no" type="xsd:string" use="required"/>
		</xsd:complexType>
	</xsd:element>
</xsd:schema>