【问题标题】:XML File with local copy of XML Schema具有 XML Schema 本地副本的 XML 文件
【发布时间】:2010-06-16 13:15:17
【问题描述】:

我正在尝试一些 XML Schema 示例,并且必须使用示例 XML 文件对其进行验证。架构是一个本地文件 (someFile.xsd)。我正在使用 eclipse 并希望在 XML 文件中包含一个引用以指向此本地 xsd 文件,以便 eclipse 可以向我建议元素。

我发现很难想出包含本地文件的语法。有什么建议吗?

【问题讨论】:

    标签: java xml xsd


    【解决方案1】:

    您是否在使用xsi:schemaLocation 属性?

    例如:

    <?xml version="1.0" encoding="UTF-8"?>
    <root xmlns="http://foo/target/Namespace"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:someNamespace="someFile"     
           xsi:schemaLocation="
           someFile someFile.xsd" >
    ...
    </root>
    

    我相信someFile.xsd 必须在你的类路径中

    【讨论】:

      【解决方案2】:

      这样的东西有用吗?

      贾尼提醒这个周末别忘了我! 注>

      复制自http://www.w3schools.com/Schema/schema_howto.asp

      【讨论】:

        【解决方案3】:

        您可以将ResourceResolverLSInput的自己的实现设置为SchemaFactory,以便调用 LSInput.getCharacterStream() 将提供来自本地路径的架构。

        它试图提供comprehensive example here

        该方法基本上包括正确实现调用的内容

        getSchemaAsStream(input.getSystemId(), input.getBaseURI(), localPath)));
        

        在以下代码的末尾。此时,您必须使用自己的查找机制来查找本地路径上的架构文件。

        public void validate(InputStream xmlStream, InputStream schemaStream, String baseUri, String localPath)
                        throws SAXException, IOException {
            Source xmlFile = new StreamSource(xmlStream);
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            factory.setResourceResolver((type, namespaceURI, publicId, systemId, baseURI) -> {
                LSInput input = new DOMInputImpl();
                input.setPublicId(publicId);
                input.setSystemId(systemId);
                input.setBaseURI(baseUri);
                input.setCharacterStream(new InputStreamReader(
                                getSchemaAsStream(input.getSystemId(), input.getBaseURI(), localPath)));
                return input;
            });
        

        【讨论】:

          猜你喜欢
          • 2017-03-17
          • 2010-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多