Reputation: 55
I've inserted an attribute called Classroom into an element called Lecture. Here's the XML schema :
<xsd:attribute name="Classroom" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="0"/>
<xsd:maxLength value="7"/>
</xsd:restriction>
</xsd:simpleType>
.
.
.
<xsd:complexType name="labType">
<xsd:complexContent>
<xsd:restriction base="eventType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Lecture" maxOccurs="10" minOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="Clasroom" use="prohibited"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
.
.
.
<xsd:element name="Lab" substitutionGroup="Event" type="labType"/>
The problem I'm having is that this schema does not apply the restriction on the attribute. I tried to validate this XML code:
<Lab>
<Title>Artificial Intelligence</Title>
<Lecture Classroom="BA">
<Day>Friday</Day>
<Time>17:00-18:00</Time>
</Lecture>
</Lab>
My problem is that this XML is reported as valid even though it uses the attribute "Classroom"(which it shouldn't be able to do). I'm new to XML so please don't be harsh. Thank you in advance!
Upvotes: 1
Views: 2052
Reputation: 111521
Your declaration of Lecture
,
<xsd:element name="Lecture" maxOccurs="10" minOccurs="1"/>
declares no type for Lecture
, so you're effectively allowing any content and any attributes on Lecture
, regardless of any restrictions you might have defined on Classroom
anywhere else in the XSD.
Here is a complete XSD that will successfully validate your XML:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Lab">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Lecture">
<xs:complexType>
<xs:sequence>
<xs:element name="Day" type="xs:string"/>
<xs:element name="Time" type="xs:string"/>
</xs:sequence>
<xs:attribute name="Classroom">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="7"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The above XSD will allow the Classroom
attribute on Lecture
but will restrict values to be strings of length between 0 and 7, inclusive.
You could also add xs:attribute/use="prohibited"
to prevent @Classroom
from appearing on Lecture
, but your original title, and this part of your question,
The problem I'm having is that this schema does not apply the restriction on the attribute.
imply that the focus of your question is on why the restriction wasn't having an effect. Perhaps you meant any restriction in the generic sense -- xs:restriction
or use="prohibit"
. Well, the answer is the same: By not assigning a type to Lecture
, you were allowing it to be of any type and have any attributes.
Upvotes: 2
Reputation: 4470
Typo in Clasroom
(only 1 's')
<xsd:attribute name="Clasroom" use="prohibited"/>
The other part of the problem is that the attribute restriction is being applied to the Lab
element rather than the Lecture
element.
Upvotes: 2