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 "Michael Glavassevich (JIRA)" <xe...@xml.apache.org> on 2006/01/16 07:21:21 UTC

[jira] Resolved: (XERCESJ-1129) Validator throws SAXParseException when validating a DOMSource against a Schema with attributeFormDefault="unqualified"

     [ http://issues.apache.org/jira/browse/XERCESJ-1129?page=all ]
     
Michael Glavassevich resolved XERCESJ-1129:
-------------------------------------------

    Resolution: Invalid

This is a problem with the DOM you've created. You're calling a (non-namespace-aware) DOM Level 1 method to create an attribute. You should never mix calls [1] to namespace and non-namespace-aware methods.  Specifically the problem here is that the attribute created with person.setAttribute( "id", "12345") has no local name.  Replacing this call with person.setAttributeNS(null, "id", "12345") will work.

[1] http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Namespaces-Considerations

> Validator throws SAXParseException when validating a DOMSource against a Schema with attributeFormDefault="unqualified"
> -----------------------------------------------------------------------------------------------------------------------
>
>          Key: XERCESJ-1129
>          URL: http://issues.apache.org/jira/browse/XERCESJ-1129
>      Project: Xerces2-J
>         Type: Bug
>   Components: JAXP (javax.xml.validation)
>     Versions: 2.7.1
>  Environment: JDK142_05/Windows XP
>     Reporter: Wilbin Chan
>     Priority: Critical

>
> 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 {
> 		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.setAttribute( "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 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();
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org