本文介绍了根据W3C XML架构定义验证XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一些XML模式,并想确保我们的生成器正在创建有效的XML模式文档(不是XML).我试图提出验证XML Schema文档的代码,但失败了.我不认为会这么复杂.

I am generating some XML Schemas and would like to ensure that our generator is creating valid XML Schema documents (Not XML). I was trying to come up with the code to validate the XML Schema document, but failing miserably. I didn't think it would be this complex.

  private void validateXsd( String xsdAsString ) {
    try {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setValidating(true);
      factory.setNamespaceAware(true);
      factory.setFeature( "http://apache.org/xml/features/validation/schema", true );

      SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );

      URL xmlSchemaXSD = this.getClass().getClassLoader().getResource( "com/metamodel/xsd/XMLSchema.xsd" );
      URL xmlSchemaDTD = this.getClass().getClassLoader().getResource( "com/metamodel/xsd/XMLSchema.dtd" );
      URL xmlSchemaDataTypes = this.getClass().getClassLoader().getResource( "com/metamodel/xsd/datatypes.dtd" );

      // requires that XMLSchema.dtd and datatypes.dtd are present in the same directory with the XMLSchema.xsd.
      factory.setSchema( schemaFactory.newSchema( xmlSchemaXSD ) );

      SAXParser parser = factory.newSAXParser();
      // parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema" );

      XMLReader reader = parser.getXMLReader();
      reader.setErrorHandler( new SimpleErrorHandler() );
      reader.parse( new InputSource( IOUtils.toInputStream( xsdAsString ) ) );
    } catch( SAXParseException e ) {
      e.printStackTrace( System.err );
    } catch ( ParserConfigurationException e ) {
      e.printStackTrace( System.err );
    } catch ( SAXException e ) {
      e.printStackTrace( System.err );
    } catch ( IOException e ) {
      e.printStackTrace( System.err );
    }
  }

此代码与我需要做的非常接近,但是出现以下错误.

This code is fairly close to what I need to do, but I'm getting the following errors.

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'jar:file:/r:/as4ot/lib/metamodel.jar!/com/metamodel/xsd/XMLSchema.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2245)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchema(XSDHandler.java:1590)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:438)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:556)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:523)
    at com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaFactoryImpl.newSchema(SchemaFactoryImpl.java:206)
    at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:489)
    at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:521)

我在调用setSchema(uri)时使用URL以确保XMLSchema.dtd和datatypes.dtd是相对的,希望它们可用,但是我不确定如何检查.

I'm using a URL when calling setSchema( uri ) to ensure that XMLSchema.dtd and datatypes.dtd are relative and I hope they are available, but I am unsure how I can check this.

我猜想它不喜欢XMLSchema.xsd,XMLSchema.dtd和datatypes.dtd捆绑在我的jar中,因此由类加载器加载的事实.无论如何有解决的办法,这样我就可以进入下一个障碍,即 org.xml.sax.SAXParseException:src-resolve:无法将名称"xml:lang"解析为一个(n)属性声明".假定它的行为与我从jar外部的目录路径加载XMLSchema时的行为相同.

I'm guessing that it doesn't like the fact the XMLSchema.xsd, XMLSchema.dtd and datatypes.dtd are bundled in my jar and hence loaded by the classloader. Is there anyway to solve this so that I can move onto my next hurdle which is org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'xml:lang' to a(n) 'attribute declaration' component. assuming that it behaves the same as when I loaded the XMLSchema from a directory path outside of a jar.

我的目标是:

  • 从jar内加载XMLSchema.xsd,以便将其与代码整齐地打包.
  • 克服有关xml:lang的下一个错误.

希望我没有问太多!

谢谢,斯图尔特

推荐答案

出于多种原因,我不会使用您描述的方法来验证XSD,最重要的是,XSD作为一种语言是薄弱的,因此本身并不能捕获完整的规格.因此,很可能您可以验证实际上无效的内容.

I wouldn't validate an XSD using the approach you described for a couple of reasons, the most important one being that the XSD as a language is weak so in itself it does not capture the full spec. So, most likely, you may validate something that in fact is not valid.

对于XSD验证,您具有专用的处理器;Java具有 XSOM ;遵循用户指南.

For XSD validation you have specialized processors; Java has XSOM; follow the user guide.

这篇关于根据W3C XML架构定义验证XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:42