从 xsd 模式引用 spring bean 值 [英] Referencing spring bean values from xsd schema

查看:27
本文介绍了从 xsd 模式引用 spring bean 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 xml 架构 xsd,其中包括验证(字符串模式、int 的最小值/最大值等).我希望验证的限制取决于外部 xml 配置文件(spring beans 文件).

I am trying to write an xml schema xsd that includes validation (patterns for strings, min/max for int etc). I want the restrictions in place for the validation to depend on an external xml config file (spring beans file).

例如在 beans/config 文件中,我有如下内容:

For instance in the beans/config file I have something like the following:

....
<bean id=bean1 class="com.example.package.Class1">
     <property name=validation value="[a-zA-Z]">
</bean>
...

在我的 xsd 模式中,我想引用验证属性作为匹配字符串的模式.

in my xsd schema I would like to reference the validation property as the pattern to match a string to.

理想情况下,我喜欢以下内容:

I'd ideally like something like the following:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:simpleType name="elementToValidate">
        <xsd:restriction base="xsd:string">
            <xsd:pattern ref="bean1/validation" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

但我无法让导入工作.据我尝试, importinclude 标签只允许使用其他 xsd 文件扩展架构,而不是其他 xml.使用

but I can't get the imports to work. As far as I've tried, the import and include tags only allow to extend the schema with other xsd files, not other xml. Using

<import resource="path/to/file.xml"/>

似乎也不起作用.

有没有办法做到这一点,或者我需要寻找其他方法吗?谢谢

Is there a way to do this, or do I need to look into other ways around it? Thanks

感谢@helderdarocha 的回答.这突出了另一个类似的问题.有没有办法从 xml 访问值以用于验证文件的其余部分?例如,对于

Thanks to @helderdarocha for the answer. This has highlighted an additional similar problem. Is there any way of accessing values from the xml to use in validation of the rest of the file? for instance, for

<xmlNode>
    <property name="prop1">3</property>
    <property name="prop2">4</property>
</xmlNode>

确保 prop2 > prop1 或以其他方式使用 prop1 的值来验证 prop2?

ensuring that prop2 > prop1 or otherwise using the value of prop1 to validate prop2?

推荐答案

使用 XSLT,您可以生成 XSD,从 beans.xml 文件中提取选定的数据并将它们放置在您希望的任何位置.您还可以执行更多的额外处理、修改数据等.

Using XSLT you can generate your XSD, extract selected data from your beans.xml file and place them wherever you wish. You could also perform much more additional processing, modify data, etc.

我假设您有一个像这样的 beans.xml 文件:

I assume you have a beans.xml file like this one:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="bean1" class="com.example.package.Class1">
        <property name="validation" value="[a-zA-Z]"/>
    </bean>

    <bean id="bean2" class="com.example.package.Class2">
        <property name="validation" value="[0-9]"/>
    </bean>

</beans>

使用上面的 spring.xml 作为输入源,以及下面的 XSLT 样式表:

Using the spring.xml above as your input source, and the XSLT stylesheet below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:spring="http://www.springframework.org/schema/beans"
    exclude-result-prefixes="spring">

    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <xsd:schema elementFormDefault="qualified">
            <xsl:apply-templates select="//spring:property"/>
        </xsd:schema>
    </xsl:template>

    <xsl:template match="spring:property[@name='validation']">
        <xsd:simpleType name="{parent::spring:bean/@id}-validation-rule">
            <xsd:restriction base="xsd:string">
                <xsd:pattern ref="{@value}" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsl:template>

</xsl:stylesheet>

您将生成此 XSD 文档:

You will generate this XSD document:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xsd:simpleType name="bean1-validation-rule">
      <xsd:restriction base="xsd:string">
         <xsd:pattern ref="[a-zA-Z]"/>
      </xsd:restriction>
   </xsd:simpleType>
   <xsd:simpleType name="bean2-validation-rule">
      <xsd:restriction base="xsd:string">
         <xsd:pattern ref="[0-9]"/>
      </xsd:restriction>
   </xsd:simpleType>
</xsd:schema>

查看此 XSLT Fiddle 以获取实时工作示例.

See this XSLT Fiddle for a live working example.

这篇关于从 xsd 模式引用 spring bean 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆