提问人:Learner 提问时间:10/19/2023 最后编辑:Learner 更新时间:10/19/2023 访问量:30
对 ID 的唯一约束不起作用,并且它始终返回 true
unique constraint on id not working and it always return true
问:
嘿,我正在尝试使用唯一的约束,这是我的xml
<graph>
<id>g0</id>
<id>g0</id>
<name>test</name>
</graph>
现在我需要验证它,验证应该返回 false,因为 id 不是唯一的,以便验证我编写了以下约束,但我得到 true 没有错误
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="graph" type="AType"/>
<xsd:complexType name="AType">
<xsd:sequence>
<xsd:choice>
<xsd:sequence>
<xsd:element name="id" type="xsd:string">
<xsd:unique name="nodeId">
<xsd:selector xpath="id"/>
<xsd:field xpath="."/>
</xsd:unique>
</xsd:element>
<xsd:element name="id" type="xsd:string">
<xsd:unique name="nodeId1">
<xsd:selector xpath="id"/>
<xsd:field xpath="."/>
</xsd:unique>
</xsd:element>
<xsd:element name="name" type="xsd:string" />
</xsd:sequence>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
我错过了什么吗?为什么我的架构会验证 ID 的唯一性?
答:
1赞
Michael Kay
10/19/2023
#1
你把约束放在了错误的地方。每个 id 元素本身都有效;它是无效的图形元素,所以这就是约束需要去的地方。
如果规则是“Y 中的每个 X 都必须具有唯一的 Z 值”,则:
- 约束属于 Y
unique
- 选择器应从 Y 开始选择 X
- 该字段应选择从 X 开始的 Z
评论
0赞
Learner
10/20/2023
非常感谢您的准确和良好的回答。
评论