XSD - one of two attributes required?

Is there a way to indicate that one of 2 attributes is required in XSD?

For example, I have a definition like this:

<xs:attribute name="Name" type="xs:string" use="optional" /> <xs:attribute name="Id" type="xs:string" use="optional" /> 

I want to be able to determine that at least one of them is required. Is it possible?

+54
xsd
Apr 18 '09 at 7:13
source share
5 answers

No, I don’t think you can do this with attributes. You can wrap two <xs:element> in <xs:choice> but I'm afraid there is no equivalent construct for attributes.

+41
Apr 18 '09 at 7:56
source share

XSD 1.1 will allow you to do this with statements.

 <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> 
+21
Aug 01 '13 at 18:40
source share

Mark is absolutely right ... You cannot have an xs: attribute of child elements inside an xs: choice parent element in XSD.

The logic seems to be that if two instances of an element have a mutually exclusive set of attributes, then they logically represent two different elements.

A workaround for this was introduced by Jeni Tennison here .

+9
Apr 18 '09 at 8:10
source share

You should take a look at these pages in the W3C Wiki : Simple Attribute Usage and Muttex Attribute.

+5
Aug 12 2018-12-12T00:
source share

The example defines an element named "person" that should contain the element "employee" or the element "member".

 <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> 
-2
Mar 24 '15 at 14:43
source share



All Articles