xsd – 在java 1.6中使用dom4j DOMDocument来提供validator.validate(DOMSource)失败(xsi:不允许noNamespaceSchemaLocation),适用于1.5


java 1.6中使用dom4j DOMDocument来提供validator.validate(DOMSource)失败(不允许xsi:noNamespaceSchemaLocation出现在根元素中),在1.5中工作

我发现以下问题非常棘手(好吧,这是轻描淡写) – 任何见解都将受到赞赏.目前看起来最好的想法是放弃dom4j以支持例如XOM(https://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j).

我一直在验证从dom4j’new DOMDocument()’创建的内存XML – 但这不适用于Java 6.

以下对dom4j(1.6.1)DOMDocument派生DOMSource的验证(源)的调用与Java 1.5.x一起使用但在Java 1.6.x中失败:

public void validate() throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setErrorHandler(null);
    Schema schemaXSD = schemaFactory.newSchema(new URL(getSchemaURLString()));
    Validator validator = schemaXSD.newValidator();
    DOMSource source = new DOMSource(getDocument());
    validator.validate(source);
}

getSchemaURLString()还用于将xsi:noNamespaceSchemaLocation属性添加到根节点,即:
的xsi:noNamespaceSchemaLocation = “HTTP://本地主机:8080 /积分/ XSD / fqlResponseSchema-2.0.XSD”

例外情况如下:

Exception:  org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'specialfields'.;                
complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'specialfields'.
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:417)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3182)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processAttributes(XMLSchemaValidator.java:2659)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2066)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)
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:104)
at javax.xml.validation.Validator.validate(Validator.java:127)

这是XML的开始 – 在禁用对validator.validate(source)的调用后生成:

<?xml version="1.0" encoding="utf-8"?> 
<meetings xsi:noNamespaceSchemaLocation="http://localhost:8080/integration/xsd/fqlResponseSchema-2.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
.............
</meetings>

和XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="meetings">
    <xs:complexType>
      <xs:choice>
    <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" ref="summary" />
          <xs:element minOccurs="0" maxOccurs="unbounded" ref="meeting" />
        </xs:sequence>
        <xs:element ref="error" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:element name="summary">
................

因此,我的root元素被拒绝,因为它包含xsi:noNamespaceSchemaLocation属性.架构本身并没有指定它作为我的根元素的有效属性?

在这一点上,在我看来,我需要放弃dom4j来执行此任务并切换到其他解决方案之一,例如:

但无论如何我想知道我做错了什么!

提前致谢.

最佳答案 我遇到了同样的问题,我发现了以下文档

http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi/index.html

Validate against a document-specified schema

Some documents specify the schema they expect to be validated against,
typically using xsi:noNamespaceSchemaLocation and/or
xsi:schemaLocation attributes like this:

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://www.example.com/document.xsd">  
 ...

If you create a schema without specifying a URL, file, or source, then
the Java language creates one that looks in the document being
validated to find the schema it should use. For example:

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 
Schema schema = factory.newSchema();

However, normally this isn’t what you want. Usually the document
consumer should choose the schema, not the document producer.
Furthermore, this approach works only for XSD. All other schema
languages require an explicitly specified schema location.

点赞