xsd:具有可选属性的唯一
我有这个 Xml 文件:
<objects>
<object name="ID1" />
<object name="ID2" />
<object name="ID2" color="green" />
<object name="ID3" color="green" />
<objects>
我想根据 XSD 架构验证该文件,以便 name
和 color
之间的组合在文档中是唯一的。
问题是,如果我使用:
<xs:unique name="UniqueObjectNameColor">
<xs:selector xpath="./object" />
<xs:field xpath="@name" />
<xs:field xpath="@color" />
</xs:unique>
... 规则将忽略没有可选 color
属性的 object
元素。以下内容验证正确,但不应该验证。
<object name="ID2" />
<object name="ID2" />
您能否告诉我,当元素中不存在 color
属性时,如何指定强制执行唯一 name
和 color
组合的规则object
,它检查name
?
I have this Xml file:
<objects>
<object name="ID1" />
<object name="ID2" />
<object name="ID2" color="green" />
<object name="ID3" color="green" />
<objects>
I would like to validate this against an XSD Schema, so that the combination between name
and color
are unique in the document.
The problem is that, if I use:
<xs:unique name="UniqueObjectNameColor">
<xs:selector xpath="./object" />
<xs:field xpath="@name" />
<xs:field xpath="@color" />
</xs:unique>
... the rule will ignore object
elements without the optional color
attribute. The following validates correctly while it shouldn't.
<object name="ID2" />
<object name="ID2" />
Can you tell me how can I specify a rule that enforces unique name
and color
combinations and, when the color
attribute is not present in the element object
, it checks the name
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
use
和default
带或不带如下值:Use
use
anddefault
with or without a value like:老问题,但值得回答。每个元素可以使用多个
唯一
约束。这会做你想做的事:Old question, but worth to be answered. You can use more than one
unique
constraint per element. This would do what you want: