如何使用schema-validator验证具有<xs-import>标记的.xsd(模式)

时间:2016-04-20 08:17:55

标签: xml mule

我正在尝试使用过滤器验证架构。问题是它无法找到父.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)? 请帮助!!

3 个答案:

答案 0 :(得分:0)

sample.xsd是否在src/main/resources下?

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我举一个例子来说明解决方案。假设您尝试对导入包括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;
    }
}

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

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