I am taking my first steps in XML-schema land, using XSD, and I’m trying to figure out how to add a constraint such that an attribute (defined with a fixed value) may only be present on one element in an unbounded set of elements.
Here’s an example
<Object>
<Element>Value1</Element>
<Element>Value2</Element>
<Element Default="true">Value3</Element>
<Element>Value4</Element>
<Element>Value5</Element>
</Object>
I’d like to constrain the Default attribute such that it cannot appear on more than one Element.
Here’s the XSD schema for the above:
<xs:element name="Object">
<xs:complexType>
<xs:element name="Element" maxOccurs="unbounded">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Default" fixed="true"/>
</xs:extension>
</xs:simpleContent>
</xs:element>
</xs:complexType>
</xs:element>
I’ve tried messing with xs:unique, xs:selector and xs:field, but reached no meaningful results, not sure if that’s the correct approach.
Thanks!
I tried my best to design one xsd for you. The possible way to achieve the requirement is to define element
elementmore than ones, but this is not allowed under XSD causing a popular error “trying to make XSD Ambiguous”So your requirement is certainly impossible to achieve as is.
I would like to suggest to define different Element names.
example :: define
DefElementwith attribute and unboundedElements without attribute. It would work for you.