简体   繁体   中英

How to restrict an attribute or element using derived types

I worked at this over 2 weeks now and i cant find a solution.... :(

I have this XSD file and I want to make three derived types from eventType that have some of the same characteristics, but: the lessonType that is ok to add a Professor element , the seminarType tO have only one Lecture element and the labType not to have the attribute classroom. i have this xsd code but it seems that is not doing what i want....

<xsd:element name="Event" type="eventType"/>
<!-- global declaration of elements Lesson, 
     Seminar, Lab that are equivalent 
     lessonType, seminarType, labType-->
<!-- Substitution group for Event-->
<xsd:element name="Lesson" 
             substitutionGroup="Event" 
             type="lessonType"/>
<xsd:element name="Seminar" 
             substitutionGroup="Event" 
             type="seminarType"/>
<xsd:element name="Lab" 
             substitutionGroup="Event" 
             type="labType"/>
<!--end of global declaration of elements Lesson, 
    Seminar, Lab-->
<!-- End of Substitution group for  Event-->

<xsd:attribute name="Classroom">
  <xsd:simpleType>
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="4"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:attribute>

<xsd:complexType name="eventType">
  <xsd:sequence>
    <xsd:element name="Title" 
                 type="xsd:string"/>
    <xsd:element maxOccurs="5" 
                 minOccurs="0" 
                 name="Lecture">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="Day">
            <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                <xsd:enumeration value="Monday"/>
                <xsd:enumeration value="Tuesday"/>
                <xsd:enumeration value="Wednesday"/>
                <xsd:enumeration value="Thursday"/>
                <xsd:enumeration value="Friday"/>
                <xsd:enumeration value="Saturday"/>
                <xsd:enumeration value="Sunday"/>
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
          <xsd:element name="Time">
            <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                <xsd:pattern 
                    value="[0-9]{2}:[0-9]{2}\-[0-9]{2}:[0-9]{2}"/>
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="Classroom">
          <xsd:simpleType>
            <xsd:restriction base="xsd:string">
              <xsd:maxLength value="4"/>
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:attribute>
      </xsd:complexType>
    </xsd:element>
  </xsd:sequence>
</xsd:complexType>

<!--extension lessonType of eventType -->
<xsd:complexType name="lessonType">
  <xsd:complexContent>
    <xsd:extension base="eventType">
      <xsd:sequence>
        <xsd:element name="Professor" type="xsd:string"/>
      </xsd:sequence>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
<!--end of lessonType-->

<!--extension seminarType of eventType -->
<xsd:complexType name="seminarType">
  <xsd:complexContent>
    <xsd:extension base="eventType">
      <xsd:sequence>
        <xsd:element name="Lecture" maxOccurs="1" minOccurs="0" />
      </xsd:sequence>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
<!--end of seminarType-->

<!--extension labType of eventType -->
<xsd:complexType name="labType">
  <xsd:complexContent>
    <xsd:extension base="eventType">
      <xsd:attribute name="Classroom" use="prohibited"/>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
<!--end of labType-->

<xsd:element name="Schedule">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element maxOccurs="unbounded" 
                   ref="Event"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

The XML file that should not be valid is the following -- but it's being accepted by the schema ... :(

<Schedule xsi:schemaLocation="schedule schedule9.xsd" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns="schedule"> 
  <Lesson>
    <Title>Artificial Intelligence</Title>
    <Lecture Classroom="BA">
      <Day>Wednesday</Day>
      <Time>09:00-11:00</Time>
    </Lecture>
    <Lecture Classroom="BA">
      <Day>Thursday</Day>
      <Time>09:00-11:00</Time>
    </Lecture>
    <Professor>Hatzilygeroudis</Professor>
  </Lesson>
  <Lab>
    <Title>Artificial Intelligence</Title>
    <Lecture Classroom="BA">    
      <Day>Friday</Day>
      <Time>17:00-18:00</Time>
    </Lecture>                                  
  </Lab>
  <Seminar>
    <Title>Constraint Satisfaction Problems</Title>
    <Lecture Classroom="B3">
      <Day>Monday</Day>
      <Time>19:00-21:00</Time>
    </Lecture>
  </Seminar>            
  <Lesson>
    <Title>Knowledge Representation in Web</Title>
    <Lecture Classroom="P200">
      <Day>Friday</Day>
      <Time>15:00-17:00</Time>
    </Lecture>
    <Professor>Hatzilygeroudis</Professor>
  </Lesson>
</Schedule>

what do i do wrong????

If the purpose of seminarType is to restrict the Lecture element to a single appearance, instead of the five allowed by EventType , then you want to declare seminarType as a restriction of EventType , not as an extension.

If the purpose of labType is to forbid the Classroom attribute on child Lecture elements, then it, too, needs to be a restriction, not an extension, and you will find it helpful to make the complex type defined for the Lecture element a top-level named complex type.

Read up on XSD restriction and try again. You're not far from success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM