I'm facing a strange problem while validating an xml with a very basic XSD. The JDK 1.6 and JDK 1.7 behavior's are not the same...
Here is my schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Salaries" type="SalariesType">
<xs:unique name="Salary-Ctrl">
<xs:selector xpath="Salary" />
<xs:field xpath="@institutionIDRef" />
<xs:field xpath="Code" />
</xs:unique>
</xs:element>
<xs:complexType name="SalariesType">
<xs:sequence>
<xs:element name="Salary" type="SalaryType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="SalaryType">
<xs:sequence>
<xs:element name="ValidAsOf" type="xs:date" minOccurs="0" />
<xs:element name="Code" type="xs:string" minOccurs="0" />
<xs:element name="AnnualBasis" type="xs:string" />
</xs:sequence>
<xs:attribute name="institutionIDRef" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>
And here is my XML:
<Salaries>
<Salary institutionIDRef="someID">
<AnnualBasis>someContent</AnnualBasis>
</Salary>
</Salaries>
For the validation, I use this simple piece of program:
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new SAXSource(new InputSource(new ByteArrayInputStream(xsd.getBytes()))));
Validator validator = schema.newValidator();
validator.validate(new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))));
System.out.println("Validated !");
If I simply run this piece of code with the JDK 1.7, there is no error and the XML is validated (and so with the JDK 1.5). However in the JDK 1.6 I have the following exception:
Exception in thread "main" org.xml.sax.SAXParseException: Not enough values specified for <unique> identity constraint specified for element "Salaries".
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:423)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3188)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$ValueStoreBase.endValueScope(XMLSchemaValidator.java:3463)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endValueScopeFor(XMLSchemaValidator.java:1479)
at com.sun.org.apache.xerces.internal.impl.xs.identity.Selector$Matcher.endElement(Selector.java:235)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2169)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:824)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:565)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2939)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.validate(ValidatorHandlerImpl.java:700)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:97)
at javax.xml.validation.Validator.validate(Validator.java:127)
at ch.Test.main(Test.java:53)
How can I make the validator accept this in JDK 1.6 ?
Thanks & Best regards
Looks like a bug in the schema processor. Solution: use a schema processor that's more reliable and independent of the JDK version, such as Apache Xerces or Saxon-EE.
Bugs in the XML software in the JDK seem to never get fixed, and you can wait years for a bug report to be answered. And I strongly suspect that the JDK gets released without running all the W3C XML conformance tests. Using third-party libraries is generally much more reliable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.