XSD中各个元素的唯一价值 [英] Unique value across elements in XSD
问题描述
我试图在多个complexType元素中使用的元素上强制使用唯一值.
I'm trying to enforce a unique value across an element used in multiple complexType elements.
例如:
<xs:element name="lmt">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="lf" maxOccurs="unbounded"/>
<xs:element ref="cdl" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
<xs:unique name="uniquePos">
<xs:selector xpath="./*/pos"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
<xs:element name="pos">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="\d{1}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="lf">
<xs:complexType>
<xs:sequence>
<xs:element ref="pos"/>
... more elements ...
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="cdl">
<xs:complexType>
<xs:sequence>
<xs:element ref="pos"/>
... more elements ...
</xs:sequence>
</xs:complexType>
</xs:element>
如果我有类似的东西,我希望这样会失败:
I'd like this to fail if I have something like :
<lmt>
<lf>
<pos>1</pos>
... more elements ...
</lf>
<cdl>
<pos>1</pos>
... more elements ...
</cdl>
</lmt>
但是验证
<lmt>
<lf>
<pos>1</pos>
... more elements ...
</lf>
<cdl>
<pos>2</pos>
... more elements ...
</cdl>
</lmt>
目前两者似乎都可以验证.我已经为选择器尝试了./*/pos
和.//pos
,但是都导致第一个示例无法通过验证.
currently both seem to validate. I've tried both ./*/pos
and .//pos
for the selector, but either cause the first example to fail validation.
推荐答案
如果希望元素A中的每个B都具有C的唯一值,则应在元素A上定义唯一性约束,则selector
表达式应从A中选择B,然后field
表达式应从B中选择C.
If you want every B within an element A to have a unique value for C then your uniqueness constraint should be defined on element A, the selector
expression should select B from A, and the field
expression should select C from B.
因此,在您的情况下,应在未显示给我们的容器元素上定义约束,该容器元素具有lf
和cdl
作为其子元素.选择应为.//pos
或./*/pos
,然后该字段应为.
So in your case the constraint should be defined on the container element that you haven't shown us, which has lf
and cdl
as its children. The selection should be .//pos
or perhaps ./*/pos
, and the field should then be .
这篇关于XSD中各个元素的唯一价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!