简体   繁体   English

如何使用派生类型限制属性或元素

[英]How to restrict an attribute or element using derived types

I worked at this over 2 weeks now and i cant find a solution.... :( 我现在已经在这工作了2个多星期,我找不到解决方法。

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. 我有这个XSD文件,我想从具有某些相同特征的eventType派生出三个类型,但是:可以添加Professor元素的lessonType,workshopType仅具有一个Lecture元素,而labType则不需要属性教室。 i have this xsd code but it seems that is not doing what i want.... 我有这个XSD代码,但似乎没有按照我的意愿做...。

<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 ... :( 以下是无效的XML文件-架构... :(

<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. 如果seminarType的目的是将Lecture元素限制为一个外观,而不是EventType允许的五个外观,那么您想将seminarType声明为EventType的限制,而不是扩展。

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. 如果labType的目的是禁止Lecture子元素上的Classroom属性,那么它也必须是一个限制,而不是扩展,并且您会发现将为Lecture元素定义的复杂类型设置为顶部是有帮助的。级别称为复杂类型。

Read up on XSD restriction and try again. 阅读有关XSD限制的信息,然后重试。 You're not far from success. 您离成功并不遥远。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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