Reputation: 25447
I can't find out how to add a required attribute for my element shop-offer
. I have tried to place
<xs:attribute name="id" type="xs:integer" use="required"/>
in the schema root as well as in <xs:complexType>
of the element but it does not work. I always receive the error that this is not allowed here.
So how can I do that?
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shop-offer">
<xs:complexType mixed="true">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="tool">
<xs:complexType>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="widget">
<xs:complexType>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Upvotes: 1
Views: 641
Reputation: 111686
Your xs:attribute
declarations are fine, but their placements are requiring the presence of id
attributes on the tool
and widget
elements.
If you also want id
to be required on the shop-offer
root element, you'll have to place another just inside the xs:complexType
(after xs:sequence
) for shop-offer
:
This XSD,
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shop-offer">
<xs:complexType mixed="true">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="tool">
<xs:complexType>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="widget">
<xs:complexType>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
<!-- This @id is for the shop-offer root element -->
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
will successfully validate this XML,
<shop-offer id="1">
<tool id="2"/>
</shop-offer>
as requested.
Note: Are you sure that you want mixed="true"
, which will allow this XML to be valid,
<shop-offer id="1">
Text here.
<tool id="2"/>
And more text here.
</shop-offer>
perhaps not as desired.
Upvotes: 1