Skip to main content

Identity Transform fixing schema validation

2 replies [Last post]
jodeen
Offline
Joined: 2007-02-07
Points: 0

I'm having a strange problem where a simple document that should be valid is being marked as invalid and doing an identity transform on the document fixes the issue.

Here's the main method:

public static void main(String[] args) throws Exception {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().newDocument();

Element fooElement = doc.createElementNS("http://foo", "Foo");
doc.appendChild(fooElement);
fooElement.setAttribute("fooId", "Foo");

Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new File("H:\\temp.xsd"));
Validator val = schema.newValidator();
val.validate(new DOMSource(doc));
}

And here's the schema in H:\temp.xsd

Fairly simple, all it does is build a document that should look like . As far as I can tell, the document should be valid, but the call to validate fails.

Exception in thread "main" org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'fooId' is not allowed to appear in element 'Foo'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:410)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3165)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processAttributes(XMLSchemaValidator.java:2630)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2037)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:273)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:240)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:100)
at javax.xml.validation.Validator.validate(Validator.java:127)
at scratch.SchemaURL.main(SchemaURL.java:49)

Commenting out the line where I set the fooId attribute results in another exception:
Exception in thread "main" org.xml.sax.SAXParseException: cvc-complex-type.4: Attribute 'fooId' must appear on element 'Foo'.

Now if I run the document through a transformer
Transformer tf = TransformerFactory.newInstance().newTransformer();
DOMResult dr = new DOMResult();
tf.transform(new DOMSource(doc), dr);

And then validate the result
val.validate(new DOMSource(dr.getNode()));

I don't get any errors.
Any ideas what's going on here?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
joehw
Offline
Joined: 2004-12-15
Points: 0

Are you using jdk1.6.0? It looks like a bug introduced in jdk1.6.0. I tried your test and the error went away using 1.6.0_02, as well as several different versions of jdk1.5. I suggest updating to JDK 6 Update 2 if that's ok with your environment.

jodeen
Offline
Joined: 2007-02-07
Points: 0

Yep, moving to 1.6.0_u2 worked.

Thanks for the help!