首页新闻找找看学习计划

怎样用xsd定义xml中复杂的键

0
悬赏园豆:10 [已关闭问题]

我定义了一个XSD文件如下:ForMeXMLSchema.xsd

<?xml version="1.0" encoding="utf-8"?>

<xs:schema id="ForMeXMLSchema"

    targetNamespace="http://tempuri.org/ForMeXMLSchema.xsd"

    elementFormDefault="qualified"

    xmlns="http://tempuri.org/ForMeXMLSchema.xsd"

    xmlns:mstns="http://tempuri.org/ForMeXMLSchema.xsd"

    xmlns:xs="http://www.w3.org/2001/XMLSchema"

>

  <xs:element name="school">

    <xs:complexType>

      <xs:sequence>

        <xs:element name="students">

          <xs:complexType>

            <xs:sequence>

              <xs:element ref="student" minOccurs="1" maxOccurs="unbounded"></xs:element>

            </xs:sequence>

          </xs:complexType>

        </xs:element>

        <xs:element name="courses">

          <xs:complexType>

            <xs:sequence>

              <xs:element ref="course" minOccurs="1" maxOccurs="unbounded"></xs:element>

            </xs:sequence>

          </xs:complexType>

        </xs:element>

        <xs:element name="sces">

          <xs:complexType>

            <xs:sequence>

              <xs:element ref="sc" minOccurs ="1" maxOccurs="unbounded"></xs:element>

            </xs:sequence>

          </xs:complexType>

        </xs:element>

      </xs:sequence>

    </xs:complexType>

    <xs:key name="studentID">

      <xs:selector xpath="students/student"></xs:selector>

      <xs:field xpath="sno"></xs:field>

    </xs:key>

    <xs:key name="courseID">

      <xs:selector xpath="courses/course"></xs:selector>

      <xs:field xpath="cno"></xs:field>

    </xs:key>

    <xs:keyref name="courseRefID" refer="courseID">

      <xs:selector xpath="courses/course"></xs:selector>

      <xs:field xpath="cpno"></xs:field>

    </xs:keyref>

    <xs:key name="scID">

      <xs:selector xpath="sces/sc"></xs:selector>

      <xs:field xpath="sno"></xs:field>

      <xs:field xpath="cno"></xs:field>

    </xs:key>

    <xs:keyref name="scRefKey1" refer="studentID">

      <xs:selector xpath="sces/sc"></xs:selector>

      <xs:field xpath="sno"></xs:field>

    </xs:keyref>

    <xs:keyref name="scRefKey2" refer="courseID">

      <xs:selector xpath="sces/sc"></xs:selector>

      <xs:field xpath="cno"></xs:field>

    </xs:keyref>

  </xs:element>

  <xs:element name="sno">

    <xs:simpleType>

      <xs:restriction base="xs:string">

        <xs:pattern value="[0-9]{8}"></xs:pattern>

      </xs:restriction>

    </xs:simpleType>

  </xs:element>

  <xs:element name="sname" type="xs:string"></xs:element>

  <xs:element name="ssex">

    <xs:simpleType>

      <xs:restriction base="xs:string">

        <xs:pattern value="男|女"></xs:pattern>

      </xs:restriction>

    </xs:simpleType>

  </xs:element>

  <xs:element name="sage">

    <xs:simpleType>

      <xs:restriction base="xs:integer">

        <xs:maxInclusive value="28"></xs:maxInclusive>

        <xs:minInclusive value="12"></xs:minInclusive>

      </xs:restriction>

    </xs:simpleType>

  </xs:element>

  <xs:element name="ssdept" type="xs:string"></xs:element>

  <xs:element name="cno">

    <xs:simpleType>

      <xs:restriction base="xs:string">

        <xs:pattern value="[0-9]{5}"></xs:pattern>

      </xs:restriction>

    </xs:simpleType>

  </xs:element>

  <xs:element name="cname" type="xs:string"></xs:element>

  <xs:element name="cpno">

    <xs:simpleType>

      <xs:restriction base="xs:string">

        <xs:pattern value="[0-9]{5}"></xs:pattern>

      </xs:restriction>

    </xs:simpleType>

  </xs:element>

  <xs:element name="ccredit">

    <xs:simpleType>

      <xs:restriction base="xs:integer">

        <xs:maxInclusive value="8"></xs:maxInclusive>

        <xs:minInclusive value="1"></xs:minInclusive>

      </xs:restriction>

    </xs:simpleType>

  </xs:element>

  <xs:element name="grade">

    <xs:simpleType>

      <xs:restriction base="xs:integer">

        <xs:maxInclusive value="100"></xs:maxInclusive>

        <xs:minInclusive value="0"></xs:minInclusive>

      </xs:restriction>

    </xs:simpleType>

  </xs:element>

  <xs:element name="student">

    <xs:complexType>

      <xs:sequence>

        <xs:element ref="sno"></xs:element>

        <xs:element ref="sname"></xs:element>

        <xs:element ref="ssex"></xs:element>

        <xs:element ref="sage"></xs:element>

        <xs:element ref="ssdept"></xs:element>

      </xs:sequence>

    </xs:complexType>

  </xs:element>

  <xs:element name="course">

    <xs:complexType>

      <xs:sequence>

        <xs:element ref="cno"></xs:element>

        <xs:element ref="cname"></xs:element>

        <xs:element ref="cpno" minOccurs="0"></xs:element>

        <xs:element ref="ccredit"></xs:element>

      </xs:sequence>

    </xs:complexType>

  </xs:element>

  <xs:element name="sc">

    <xs:complexType>

      <xs:sequence>

        <xs:element ref="sno"></xs:element>

        <xs:element ref="cno"></xs:element>

        <xs:element ref="grade"></xs:element>

      </xs:sequence>

    </xs:complexType>

  </xs:element>

</xs:schema>

我的xml文件:school.xml

<?xml version="1.0" encoding="utf-8" ?>

<school xmlns="http://tempuri.org/ForMeXMLSchema.xsd"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema"

>

  <students>

    <student>

      <sno>05138001</sno>

      <sname>A</sname>

      <ssex>男</ssex>

      <sage>20</sage>

      <ssdept>IS</ssdept>

    </student>

    <student>

      <sno>05108001</sno>

      <sname>B</sname>

      <ssex>女</ssex>

      <sage>21</sage>

      <ssdept>IS</ssdept>

    </student>

    <student>

      <sno>05138003</sno>

      <sname>C</sname>

      <ssex>女</ssex>

      <sage>22</sage>

      <ssdept>CH</ssdept>

    </student>

  </students>

  <courses>

    <course>

      <cno>00001</cno>

      <cname> 数据库</cname>

      <ccredit>4</ccredit>

    </course>

  </courses>

  <sces>

    <sc>

      <sno>05138001</sno>

      <cno>00001</cno>

      <grade>80</grade>

    </sc>

  </sces>

</school>

为什么我定义的键值没验证?也就是说为什么可以出现2个sno为05138001的student同时存在呢?我想实现的功能是这样的功能如下:

             student主键是sno

             course主键是cno

             course外键cpno

             sc联合主键(sno,cno)

             sc外键sno和cno

请问我上面的xsd应该如何修改?

Joe·Zhou的主页 Joe·Zhou | 初学一级 | 园豆:190
提问于:2009-04-27 22:28
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册