2012-11-16 39 views
8

Tôi đã xác định hai loại phần tử phức tạp - Nhà phát triển và Ứng dụng.Cách xác định người dùng xác định loại dữ liệu trong lược đồ XML?

Childs Developer - ID, Name, Email

App Childs - ID, Name, Developer

Ở đây Developer trong phần tử phức tạp App đề cập đến phát triển/ID.

Cách xác định mối quan hệ này trong lược đồ xml. Tôi đang sử dụng trình soạn thảo XML spy2013.

Tôi đã thử chỉ định tên trong khai báo loại Developer-> ID đơn giản. Và sử dụng tên này trong App-> Kiểu dữ liệu nhà phát triển. Nhưng nó mang lại cho lỗi ..

<?xml version="1.0" encoding="UTF-8"?> 
<!-- edited with XMLSpy v2013 (x64) (http://www.altova.com) by Piyush (USC) --> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"   attributeFormDefault="unqualified"> 
<xs:element name="appinfo"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element name="Developer"> 
       <xs:complexType> 
        <xs:all> 
         **<xs:element name="ID">** 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:length value="5"/> 
            <xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
         <xs:element name="Name"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:pattern value="([a-zA-Z])+"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
         <xs:element name="Email"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:pattern value="[^@][email protected][^\.]+\..+"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
         <xs:element name="Company" type="xs:string"/> 
         <xs:element name="Website" type="xs:string"/> 
         <xs:element name="Phone"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:length value="13"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
        </xs:all> 
       </xs:complexType> 
      </xs:element> 
      <xs:element name="App"> 
       <xs:complexType> 
        <xs:all> 
         <xs:element name="ID"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:length value="5"/> 
            <xs:pattern value="[0-9][0-9][0-9][0-9][A-Z]"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
         <xs:element name="Name"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:pattern value="([a-zA-Z])+"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
         <xs:element name="Developer" ***type="xs:anySimpleType"/>*** 
         <xs:element name="Price"> 
          <xs:simpleType> 
           <xs:restriction base="xs:float"> 
            <xs:minInclusive value="0.00"/> 
            <xs:maxInclusive value="6.99"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
         <xs:element name="TabletCompatible" type="xs:boolean" minOccurs="0"/> 
         <xs:element name="Category"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:enumeration value="Travel"/> 
            <xs:enumeration value="Productivity"/> 
            <xs:enumeration value="Game"/> 
            <xs:enumeration value="Music"/> 
            <xs:enumeration value="Education"/> 
            <xs:enumeration value="Lifestyle"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
         <xs:element name="Platform"> 
          <xs:simpleType> 
           <xs:restriction base="xs:string"> 
            <xs:enumeration value="Android"/> 
            <xs:enumeration value="iOS"/> 
            <xs:enumeration value="Blackberry"/> 
           </xs:restriction> 
          </xs:simpleType> 
         </xs:element> 
        </xs:all> 
       </xs:complexType> 
      </xs:element> 
      <xs:element name="AppStat"> 
       <xs:complexType> 
        <xs:all> 
         <xs:element name="AppID" type="xs:anySimpleType"/> 
         <xs:element name="Statistics"> 
          <xs:complexType> 
           <xs:sequence maxOccurs="unbounded"> 
            <xs:element name="Platform" type="xs:anySimpleType"/> 
            <xs:element name="Downloads" type="xs:positiveInteger"/> 
            <xs:element name="Rating"> 
             <xs:simpleType> 
              <xs:restriction base="xs:float"> 
               <xs:minInclusive value="0.0"/> 
               <xs:maxInclusive value="5.0"/> 
              </xs:restriction> 
             </xs:simpleType> 
            </xs:element> 
           </xs:sequence> 
          </xs:complexType> 
         </xs:element> 
         <xs:element name="LastChecked" type="xs:date"/> 
        </xs:all> 
       </xs:complexType> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

+0

Tôi không biết ai upvoted câu hỏi này hoặc lý do tại sao, nhưng tôi downvoting nó. Nếu bạn viết một số mã và nó cho chúng tôi một lỗi, bạn nên cho chúng tôi thấy mã mà bạn đã viết và lỗi mà nó đã đưa ra, sau đó chúng tôi có thể thấy bạn đã đi sai ở đâu. Bạn không được sợ hãi hoặc xấu hổ về việc hiển thị các nỗ lực mã hóa của bạn; cách tốt nhất để tìm hiểu là phải chịu những nỗ lực của bạn để đánh giá phê bình bởi những người có nhiều kinh nghiệm về công nghệ bạn đang học. –

Trả lời

10

Cách sử dụng một loại đơn giản phổ biến đối với các nhà phát triển id là phải khai báo nó như là một loại tên ngay từ đầu:

<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"   
    attributeFormDefault="unqualified"> 

    <xs:simpleType name="developerID"> 
    <xs:restriction base="xs:string"> 
     <xs:length value="5"/> 
     <xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/> 
    </xs:restriction> 
    </xs:simpleType> 

    . . . 

và sau đó sử dụng nó:

. . . 
    <xs:element name="Developer" maxOccurs="unbounded"> 
     <xs:complexType> 
     <xs:all> 
      <xs:element name="ID" type="developerID"> 
. . . 
    <xs:element name="App" maxOccurs="unbounded"> 
     <xs:complexType> 
     <xs:all> 
      . . . 
      <xs:element name="Developer" type="developerID"/> 

Nhưng điều này là không đủ để tạo ra một contraint để appinfo/App/Developer ca n chỉ chứa một trong các id của nhà phát triển được khai báo trong appinfo/Developer/ID. Để làm điều đó là cần thiết để tạo ra một định nghĩa khóa duy nhất sử dụng xs:key và tham chiếu nó bằng cách sử dụng xs:keyref (xem here).

Đây là XSD hoàn chỉnh:

<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"   
    attributeFormDefault="unqualified"> 

    <xs:simpleType name="developerID"> 
    <xs:restriction base="xs:string"> 
     <xs:length value="5"/> 
     <xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:element name="appinfo"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Developer" maxOccurs="unbounded"> 
      <xs:complexType> 
      <xs:all> 
       <xs:element name="ID" type="developerID"> 
       </xs:element> 
       <xs:element name="Name"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
        <xs:pattern value="([a-zA-Z])+"/> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
       <xs:element name="Email"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
        <xs:pattern value="[^@][email protected][^\.]+\..+"/> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
       <xs:element name="Company" type="xs:string"/> 
       <xs:element name="Website" type="xs:string"/> 
       <xs:element name="Phone"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
        <xs:length value="13"/> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
      </xs:all> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="App" maxOccurs="unbounded"> 
      <xs:complexType> 
      <xs:all> 
       <xs:element name="ID"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
        <xs:length value="5"/> 
        <xs:pattern value="[0-9][0-9][0-9][0-9][A-Z]"/> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
       <xs:element name="Name"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
        <xs:pattern value="([a-zA-Z])+"/> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
       <xs:element name="Developer" type="developerID"/> 
       <xs:element name="Price"> 
       <xs:simpleType> 
        <xs:restriction base="xs:float"> 
        <xs:minInclusive value="0.00"/> 
        <xs:maxInclusive value="6.99"/> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
       <xs:element name="TabletCompatible" type="xs:boolean" minOccurs="0"/> 
       <xs:element name="Category"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
        <xs:enumeration value="Travel"/> 
        <xs:enumeration value="Productivity"/> 
        <xs:enumeration value="Game"/> 
        <xs:enumeration value="Music"/> 
        <xs:enumeration value="Education"/> 
        <xs:enumeration value="Lifestyle"/> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
       <xs:element name="Platform"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
        <xs:enumeration value="Android"/> 
        <xs:enumeration value="iOS"/> 
        <xs:enumeration value="Blackberry"/> 
        </xs:restriction> 
       </xs:simpleType> 
       </xs:element> 
      </xs:all> 
      </xs:complexType> 
      <xs:keyref name="developerIDref" refer="developerID"> 
      <xs:selector xpath="."/> 
      <xs:field xpath="Developer"/> 
      </xs:keyref> 
     </xs:element> 
     <xs:element name="AppStat"> 
      <xs:complexType> 
      <xs:all> 
       <xs:element name="AppID" type="xs:anySimpleType"/> 
       <xs:element name="Statistics"> 
       <xs:complexType> 
        <xs:sequence maxOccurs="unbounded"> 
        <xs:element name="Platform" type="xs:anySimpleType"/> 
        <xs:element name="Downloads" type="xs:positiveInteger"/> 
        <xs:element name="Rating"> 
         <xs:simpleType> 
         <xs:restriction base="xs:float"> 
          <xs:minInclusive value="0.0"/> 
          <xs:maxInclusive value="5.0"/> 
         </xs:restriction> 
         </xs:simpleType> 
        </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
       </xs:element> 
       <xs:element name="LastChecked" type="xs:date"/> 
      </xs:all> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:key name="developerID"> 
     <xs:selector xpath="Developer"/> 
     <xs:field xpath="ID"/> 
    </xs:key> 
    </xs:element> 

</xs:schema> 
Các vấn đề liên quan