XML-Schema:如何将枚举设置为每个文档的一个外观 [英] XML-Schema: how to set enumerations to one appearance per document
问题描述
我想为我的所有元素添加一个名称属性.为了确保只允许某些属性值,我创建了一个限制模式.有没有办法确保每个枚举值每个文档只使用一次?
I want to add a name-attribute to all my elements. To make sure that only some attribute values are allowed I created a restriction-pattern. Is there a way to make sure, that each enumeration value is used exactly once per document?
简单样式:
<xsd:simpleType name="myname">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="1"/>
<xsd:enumeration value="2"/>
</xsd:restriction>
</xsd:simpleType>
<小时>
XML
<element name="1"/>
<element name="2"/>
<element name="1"/>
不应该允许最后一个.
这可能吗?
推荐答案
有一个类似的概念,称为身份约束,它可能适合您的目的.您想使用 xs:unique
,它是对父元素的约束,而不是枚举的属性.类似的东西:
There is a similar concept called identity constraints, which may suit your purposes. You want to use xs:unique
, which is a constraint on the parent element, not a property of an enumeration. Something like:
<xs:unique name="myconstraint">
<xs:selector xpath=".//*"/>
<xs:field xpath="@name"/>
</xs:unique>
selector
表示约束应用于哪些子元素(本例中的所有后代),field
表示哪一部分必须是唯一的.
The selector
says which child elements the constraint applies to (all descendants in this example), and the field
indicates which part must be unique.
这篇关于XML-Schema:如何将枚举设置为每个文档的一个外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!