我尝试使用UBL发票XSD文件验证UBL发票XML,该文件导入了一些其他XSD文件。我阅读了有相同问题的文档和各种SO问题。然而,我仍然没有找到我的修复。
我创建了一个基本的验证方法,如下所示:
private Schema validate(InputStream[] schemas, LSResourceResolver lsResourceResolver) {
Schema schema = null;
// Convert InputStream[] to StreamSource[]
StreamSource[] schemaStreamSources = new StreamSource[schemas.length];
for(int index1=0 ; index1<schemas.length ; index1++)
schemaStreamSources[index1] = new StreamSource(schemas[index1]);
// Create a compiled Schema object.
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setResourceResolver(lsResourceResolver);
try {
schema = schemaFactory.newSchema(schemaStreamSources);
} catch(SAXException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getCompiledSchema", ex);
}
return schema;
}
然后我为xsd文件中引用的所有XSD文件创建流,并运行如下所示的validate方法:
InputStream invoiceXSD = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/main/UBL-Invoice-2.0.xsd");
InputStream commonExt = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/UBL-CommonExtensionComponents-2.0.xsd");
InputStream unqualified = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/UnqualifiedDataTypeSchemaModule-2.0.xsd");
InputStream commonBasic = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/UBL-CommonBasicComponents-2.0.xsd");
InputStream qualified = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/UBL-QualifiedDatatypes-2.0.xsd");
InputStream extContent = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/UBL-ExtensionContentDatatype-2.0.xsd");
InputStream code1 = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/CodeList_CurrencyCode_ISO_7_04.xsd");
InputStream code2 = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/CodeList_LanguageCode_ISO_7_04.xsd");
InputStream code3 = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/CodeList_MIMEMediaTypeCode_IANA_7_04.xsd");
InputStream code4 = this.getClass().getClassLoader().getResourceAsStream("schemas/ubl20/common/CodeList_UnitCode_UNECE_7_04.xsd");
InputStream[] streams = {extContent, code1, code2, code3,code4, unqualified,qualified, commonBasic, commonExt, invoiceXSD};
validate(streams, null);
它们位于正确的文件夹中,当我删除主invoiceXSD时,所有编译都很好,这表明其他文件导入按预期工作。只有UBL-Invoice-2.0.xsd和UBL-CommonExtensionComponents-2.0.xsd之间的链接(其中错误的引用实际存在,但无法找到)在某种程度上不起作用。
我失去了这一点,如果有人对这个话题有任何经验并且可以指出我正确的方向,将非常感激。我已经阅读了有关实现自定义LSResourceResolver的内容,我可以继续尝试这一点。我对目前的实施失败原因一无所知。完整的堆栈跟踪:
org.xml.sax.SAXParseException; lineNumber: 44; columnNumber: 72; src-resolve: Cannot resolve the name 'ext:UBLExtensions' to a(n) 'element declaration' component.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:4156)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:4139)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1745)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(XSDElementTraverser.java:170)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseLocalElements(XSDHandler.java:3612)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:636)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:613)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:572)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:538)
at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:255)
at Main.validate(Main.java:49)
at Main.<init>(Main.java:35)
at Main.main(Main.java:19)