有没有办法指定XSD中需要2个属性中的一个?XSD - 需要2个属性之一?
例如,我有这样的定义。
<xs:attribute name="Name" type="xs:string" use="optional" />
<xs:attribute name="Id" type="xs:string" use="optional" />
我希望能够定义这些中的至少一个是必需的。那可能吗?
有没有办法指定XSD中需要2个属性中的一个?XSD - 需要2个属性之一?
例如,我有这样的定义。
<xs:attribute name="Name" type="xs:string" use="optional" />
<xs:attribute name="Id" type="xs:string" use="optional" />
我希望能够定义这些中的至少一个是必需的。那可能吗?
不,我不认为你可以用属性来做到这一点。您可以将两个< xs:元素>并入< xs:choice > - 但对于属性,恐怕没有等价的构造。
马克
马克是完全正确的...你不能有XS:在XSD选择父元素:一个XS内部属性的子元素。
逻辑似乎是,如果一个元素的两个实例具有互斥的一组属性,那么它们在逻辑上是两个不同的元素。
对此的解决方法已由Jeni Tennison here提出。
你应该看看在W3C维基这类网页:Simple attribute implication和Attribute muttex
XSD 1.1将为您做到这一点使用断言让。
<xsd:element name="remove">
<xsd:complexType>
<xsd:attribute name="ref" use="optional"/>
<xsd:attribute name="uri" use="optional"/>
<xsd:assert test="(@ref and not(@uri)) or (not(@ref) and @uri)"/>
</xsd:complexType>
</xsd:element>
的例子定义了一个名为“人”,它必须包含一个“雇员”元素或“构件”元素的元素。
<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>
小心引用您的源代码? http://www.w3schools.com/xml/el_choice.asp – Reisclef 2016-04-16 20:12:11
不错的解决方案,但自2012年发布以来(http://www.w3.org/TR/xmlschema11-1/),我使用.NET 4.0(2010年发布),它是不支持。 .NET 4.5支持它吗?示例类:https://msdn.microsoft.com/en-us/library/swxzdhc0(v=vs.110).aspx – 2015-10-08 16:37:33