我有以下 XSD 元素:
<xsd:element name="password">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<!-- Length check -->
<xsd:pattern value=".{9,12}" />
<!-- Format check -->
<xsd:pattern value=".*\d{2,}.*" />
<xsd:pattern value=".*[A-Z]{1,}.*" />
<xsd:pattern value=".*[a-z]{1,}.*" />
<xsd:pattern value=".*\p{Punct}.*" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
我想单独应用这些模式中的每一个。它应该首先检查长度是否合适。如果是,则检查它是否至少有 2 位数字,依此类推。相反,它将所有表达式连接在一起并尝试将它们应用在一起。
这是非常糟糕的设计。如果只允许一种模式<xsd:restriction>
应该定义 <xsd:pattern>
的基数为 1。允许多个 <xsd:pattern>
给人的印象是支持多种模式。
有没有办法将多个模式应用于 XSD 元素?
最佳答案
Multiple patterns per restriction被支持,但它们并不意味着 AND;他们的意思是:
Note: An XML
<restriction>
containing more than one<pattern>
element gives rise to a single ·regular expression· in the set; this ·regular expression· is an "or" of the ·regular expressions· that are the content of the<pattern>
elements.
如果您希望同时应用多个正则表达式约束,请将它们写在一个 <pattern>
中.
对于最小/最大长度约束,而不是使用 <pattern>
, 考虑使用 <minLength>
和 <maxLength>
方面。
关于java - 在 XSD 的限制中指定多个模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26992786/