3

在使用非常基本的 XSD 验证 xml 时,我遇到了一个奇怪的问题。JDK 1.6 和 JDK 1.7 的行为不一样...

这是我的架构:

<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>

这是我的 XML:

<Salaries>
  <Salary institutionIDRef="someID">
    <AnnualBasis>someContent</AnnualBasis>
  </Salary>
</Salaries>

对于验证,我使用这个简单的程序:

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 !");

如果我只是使用 JDK 1.7 运行这段代码,则不会出现错误并且 XML 已验证(使用 JDK 1.5 也是如此)。但是在 JDK 1.6 中我有以下异常:

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)

如何让验证器在 JDK 1.6 中接受这个?

谢谢和最好的问候

4

1 回答 1

2

看起来像模式处理器中的错误。解决方案:使用更可靠且独立于 JDK 版本的模式处理器,例如 Apache Xerces 或 Saxon-EE。

JDK 中 XML 软件中的错误似乎永远无法修复,您可以等待数年的错误报告才能得到答复。而且我强烈怀疑 JDK 是在没有运行所有 W3C XML 一致性测试的情况下发布的。使用第三方库通常更可靠。

于 2013-07-08T18:52:35.773 回答