如果模式没有目标命名空间,Citrus 的 XML 模式验证是否可用?

Is XML schema validation of Citrus usable if the schemas have no target namespace?

我试图在 Citrus 中设置一个 XsdSchemaRepository,其中包含一些 XML 模式。因为这些模式没有目标命名空间(它们不是我的,我无法解决这个问题),我还配置了一个 RootQNameSchemaMappingStrategy 因为映射命名空间的默认策略显然不起作用。

但是,由于 Spring class 中的以下异常,我失败了:

Caused by: java.lang.IllegalArgumentException: class path resource [path to my schema file] has no targetNamespace
    at org.springframework.util.Assert.hasText(Assert.java:181)
    at org.springframework.xml.xsd.SimpleXsdSchema.loadSchema(SimpleXsdSchema.java:128)
    at org.springframework.xml.xsd.SimpleXsdSchema.afterPropertiesSet(SimpleXsdSchema.java:117)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)

如果模式没有目标名称空间,是否可以使用 Citrus 进行 XML 模式验证?

目标名称空间限制来自 Spring 的 SimpleXsdSchema 实现。不过,您可以实现自己的子类来提供默认名称空间:

public class NoTargetNamespaceXsdSchema extends SimpleXsdSchema {

    @Override
    public String getTargetNamespace() {
        return Optional.ofNullable(super.getTargetNamespace())
                       .orElse("urn:uuid:" + UUID.randomUUID());
    }
}

您可以将自定义实现作为 Spring bean 引用添加到 Citrus 架构存储库:

<bean id="noTargetNamespaceXsdSchema" class="com.consol.citrus.xml.schema.NoTargetNamespaceXsdSchema">
  <property name="xsd" value="path/to/sample.xsd"/>
</bean>

<citrus:schema-repository id="schemaRepository">
  <citrus:schemas>
    <citrus:reference schema="noTargetNamespaceXsdSchema"/>
  </citrus:schemas>
</citrus:schema-repository>