xsd:simpleType:xsd:pattern 和 xsd:maxLength 可以一起使用吗?
假设我正在处理一个 xsd:simpleType ,它是一个字符串,需要具有特定的字符集和特定的最大长度,类似于下面的代码:
<xsd:simpleType name="MyType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]" />
<xsd:maxLength value="36" />
</xsd:restriction>
</xsd:simpleType>
所以我的 xsd 类型将是一个仅包含数字的字符串,最大长度为36 个字符。我的问题是 xsd:pattern 和 xsd:maxLength (或任何其他类似的标签,如 minLength)是否可以一起工作。我的直觉是不会;仅在 xsd:restriction 中的模式或基于长度的 xsd 元素。因此,我必须在模式中添加最大长度限制。
请注意,我确实通过在 Java 上解组 xml 对此进行了测试,但验证失败了。无论如何,我正在寻找的是有关模式和 maxLength 如何以及是否可以一起工作的两种信息。
Let's say I am dealing with an xsd:simpleType that is a string, needs to be of a certain character set and of a specific maximum length, similar to the below code:
<xsd:simpleType name="MyType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]" />
<xsd:maxLength value="36" />
</xsd:restriction>
</xsd:simpleType>
So my xsd type will be a string of only digits and maximum of 36 characters. My question is whether the xsd:pattern and xsd:maxLength (or any other similar tag like minLength) can work together. My intuition is no; either pattern or length-based xsd elements only in the xsd:restriction. Therefore, I would have to add the max length restriction into the pattern.
Please note that I did test this out by unmarshalling an xml on Java and the validation failed. Regardless, what I am looking for is information as two how and whether pattern and maxLength can work together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
限制上的所有方面都应用于类型。这意味着上面的定义确实定义了一个具有给定模式且长度限制为 36 个字符的类型。
以下是规范中的相关条目:
所有方面都是独立应用的,并且只有当该值满足所有限制时才被认为是有效的。这还包括对给定类型派生的类型施加的任何限制。
因此,可以创建一个始终无法验证的 simpleType - 如果您的
pattern
要求该值必须为 37 个字符长,并且maxLength
为 36,那么至少其中一个方面总是会失败。也就是说,给定类型中的多个
pattern
元素被视为替代元素(请参阅:“约束方面”中的“模式”)All facets on a restriction are applied to a type. This means that your definition above does define a type that has the given pattern and is limited to 36 characters in length.
Here's the relevant entry from the spec:
All facets are independently applied, and only if the value meets all restrictions will it be considered valid. This also includes any restrictions placed on the type from which the given type derives.
It is therefore possible to create a simpleType that will always fail validation - If your
pattern
were to mandate that the value must be 37 characters long, and themaxLength
is 36, then at least one of those facets will always fail.That said, multiple
pattern
elements in a given type are treated as alternatives, (See: "pattern" in "Constraining Facets")