【问题标题】:How to validate an .xsd (schema) having <xs-import> tag using schema-validator如何使用模式验证器验证具有 <xs-import> 标记的 .xsd(模式)
【发布时间】:2017-03-10 12:20:06
【问题描述】:

我正在尝试使用过滤器验证架构。问题是它无法找到父 .xsd 内的标记中提到的依赖 .xsd。 我也使用了 resourceResolver-ref,但仍然面临同样的问题。这是示例配置和类文件。

<mulexml:schema-validation-filter schemaLocations="${app.home}/sample.xsd" returnResult="false" ref="classpathResourceResolver" name="sample_schema_Validation" doc:name="Schema Validation" />

package org.test.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.xerces.dom.DOMInputImpl;
import org.mule.util.IOUtils;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;

public class ClasspathResourceResolver implements LSResourceResolver {

@Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {

    try {
        InputStream resource = IOUtils.getResourceAsStream(systemId, getClass());
        return new DOMInputImpl(publicId, systemId, baseURI, resource, null);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
}

我在这里很困惑如何传递引用来加载我的外部资源(依赖 xsd)? 请帮忙!!

【问题讨论】:

    标签: xml mule


    【解决方案1】:

    你的sample.xsdsrc/main/resources下面吗?

    试试这个:schemaLocations="${app.home}/classes/sample.xsd"

    【讨论】:

      【解决方案2】:

      Ricston 团队的 Alan Cassar 提供了一种解决方案,用于在 Schema Validation Filter 中解析导入的 xsd。请查看此链接https://www.ricston.com/blog/schema-validation-filter-gotchas/

      【讨论】:

        【解决方案3】:

        我举一个例子来说明解决方案。假设您尝试针对Mathml2.xsd 进行验证,该Mathml2.xsd 导入了一系列其他内容,包括common/math.xsd

        @Override
        public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
            try {
                String path = systemId;
                if (baseURI != null)
                    path = URI.create(baseURI).resolve(systemId);
                InputStream resource = IOUtils.getResourceAsStream(path, getClass());
                return new DOMInputImpl(publicId, systemId, baseURI, resource, null);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        

        这里的关键是,当从资源解析导入语句时,baseURI 是该资源的位置,systemId 是 schemaLocation(通常是相对的)。

        免责声明:为简洁起见,我跳过了 null 检查(在我遇到的一些极少数情况下,systemId 可能为 null)并假设 IOUtils.getResourceAsStream(从未从 mule 包中使用它)将检索相应的资源。代码可能无法直接开箱即用,但它应该会给您一些想法。

        【讨论】:

          猜你喜欢
          • 2014-02-26
          • 1970-01-01
          • 2010-11-26
          • 2016-12-19
          • 2018-02-09
          • 1970-01-01
          • 2023-03-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多