XSD 指定只能在 XML 中使用一次的属性 [英] XSD Specifying an attribute that can be used only once in the XML

查看:47
本文介绍了XSD 指定只能在 XML 中使用一次的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有具有多个属性的 complexType:

Having a complexType with several attributes:

<xs:complexType name="baseType" mixed="true">
    <xs:attribute name="name" type="xs:string" use="required"/>
    <xs:attribute name="leader" type="xs:boolean" use="optional"/>
</xs:complexType>
<xs:element name="person" type="baseType"/>

如何限制 leader 属性在 XML 文件中只使用一次?所以以下不会验证(不能是两个领导)

How can I restrict the leader attribute to be used only once in the XML file? so the following will not validate (cannot be two leaders)

<person name="Charlie"/>
<person name="Megan" leader="true"/>
<person name="Moe"/>
<person name="Jonathan" leader="true"/>
<person name="Claire"/>

推荐答案

如果你给 leader 属性一个只有一个可能值的类型(例如一个类型通过限制从 boolean 派生并且只允许为 true),然后在包含 person 元素的 parent 元素级别添加一个 unique 约束,说明 person 元素必须具有其 leader 属性的唯一值.由于唯一约束仅适用于字段实际存在的元素,这将确保不超过一个人拥有 leader 属性,并且当该属性存在时,它必须为真.

You can enforce "at most once" if you give the leader attribute a type that has only one possible value (e.g. a type that derives by restriction from boolean and allows only true), then add a unique constraint at the level of the parent element that contains the person elements, stating that person elements must have unique values of their leader attribute. Since unique constraints only apply to elements where the field is actually present, this will ensure that no more than one person has a leader attribute, and when that attribute is present it must be true.

我认为在 XML Schema 1.0 中没有强制执行至少一个"的方法,如果您可以使用 1.1,您可以通过断言来实现.

I don't think there's a way to enforce "at least one" in XML Schema 1.0, you could do it with an assertion if you can use 1.1.

这篇关于XSD 指定只能在 XML 中使用一次的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆