Details
-
Bug
-
Status: Resolved
-
Critical
-
Resolution: Invalid
-
2.7.1
-
None
-
None
-
JDK142_05/Windows XP
Description
When validating a DOMSource against a schema with attributeFormDefault="unqualified", the validator throws this exception
org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'a:person'.
The sample program is pasted below. It shows 3 scenarios. All three use a handbuilt DOM object.
1) testUnqualifiedAttr validates a DOM that is built with an unqualified attribute, the schema has attributeFormDefault="unqualified", this validation fails
2) testQualifiedAttr validates a DOM that is built with a qualified attribute, the schema has attributeFormDefault="qualified", this validation succeeds
3) testQualifiedAttr2 validates a DOM that is built with a qualified attribute, the schema has attributeFormDefault="unqualified", this validation fails
TEST PROGRAM FOLLOWS:
=========================================
import java.io.ByteArrayInputStream;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class XMLValidate {
public static void testUnqualifiedAttr() throws Exception
public static void testQualifiedAttr() throws Exception
{ String xsd = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<xs:schema " + "targetNamespace=\"mytest\" " + "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" " + "elementFormDefault=\"qualified\" " + "attributeFormDefault=\"qualified\">" + "<xs:element name=\"person\">" + "<xs:complexType>" + "<xs:sequence>" + "<xs:element name=\"fullName\" type=\"xs:string\"/>" + "</xs:sequence>" + "<xs:attribute name=\"id\" type=\"xs:integer\"/>" + "</xs:complexType>" + "</xs:element>" + "</xs:schema>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware( true ); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element person = doc.createElementNS( "mytest", "a:person" ); Element fullName = doc.createElementNS( "mytest", "a:fullName" ); fullName.setTextContent( "John Smith" ); person.appendChild( fullName ); person.setAttributeNS( "mytest", "a:id", "12345" ); doc.appendChild( person ); SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ); Schema schema = sf.newSchema( new StreamSource( new ByteArrayInputStream( xsd.getBytes() ) ) ); Validator validator = schema.newValidator(); validator.validate( new DOMSource( doc ) ); }public static void testQualifiedAttr2() throws Exception
{ String xsd = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<xs:schema " + "targetNamespace=\"mytest\" " + "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" " + "elementFormDefault=\"qualified\" " + "attributeFormDefault=\"unqualified\">" + "<xs:element name=\"person\">" + "<xs:complexType>" + "<xs:sequence>" + "<xs:element name=\"fullName\" type=\"xs:string\"/>" + "</xs:sequence>" + "<xs:attribute name=\"id\" type=\"xs:integer\"/>" + "</xs:complexType>" + "</xs:element>" + "</xs:schema>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware( true ); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element person = doc.createElementNS( "mytest", "a:person" ); Element fullName = doc.createElementNS( "mytest", "a:fullName" ); fullName.setTextContent( "John Smith" ); person.appendChild( fullName ); person.setAttributeNS( "mytest", "a:id", "12345" ); doc.appendChild( person ); SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ); Schema schema = sf.newSchema( new StreamSource( new ByteArrayInputStream( xsd.getBytes() ) ) ); Validator validator = schema.newValidator(); validator.validate( new DOMSource( doc ) ); }public static void main( String args[] ) throws Exception
{ //testQualifiedAttr(); testUnqualifiedAttr(); //testQualifiedAttr2(); }}