2012-11-11 70 views
4

我在使用XML Schema時遇到問題。我的架構代碼:XML Schema無效 - [s4s-elt-invalid-content.1:]

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

 <xsd:element name="colection"> 
      <xsd:complexType> 
       <xsd:sequence> 
        <xsd:element name="author" type="TypAuthor" maxOccurs="1"/> 
        <xsd:element name="books" type="TypBooks"/> 
        <xsd:element name="films" type="TypFilms"/> 
        <xsd:element name="copyright" type="xsd:string"/> 
       </xsd:sequence> 
      </xsd:complexType> 
     </xsd:element> 

     <xsd:complexType name="TypAuthor"> 
      <xsd:sequence> 
       <xsd:element name="name" type="xsd:string" maxOccurs="1"/> 
       <xsd:element name="surname" type="xsd:string"/> 
       <xsd:element name="gender" type="TypSex"/> 
       <xsd:element name="age" type="xsd:integer"/> 
       <xsd:element name="city" type="xsd:string"/> 
       <xsd:element name="country" type="xsd:string"/> 
       <xsd:element name="study" type="xsd:string"/> 
       <xsd:element name="semester" type="xsd:integer"/> 
       <xsd:element name="group" type="xsd:string"/> 
      </xsd:sequence> 
     </xsd:complexType> 

     <xsd:simpleType name="TypSex"> 
      <xsd:restriction base="xsd:string"> 
       <xsd:enumeration value="male"/> 
       <xsd:enumeration value="female"/> 
      </xsd:restriction> 
     </xsd:simpleType> 

     <xsd:complexType name="TypBooks"> 
      <xsd:sequence> 
       <xsd:element name="book" type="TypBook" minOccurs="5" maxOccurs="unbounded"/> 
      </xsd:sequence> 
     </xsd:complexType> 

     <xsd:complexType name="TypBook"> 
      <xsd:attribute name="nr" type="TypNr" /> 
      <xsd:attribute name= "lang" type="TypLang"/> 
      <xsd:sequence> 
       <xsd:element name="title" type="xsd:string"/> 
       <xsd:element name="author" type="TypAuthor2"/> 
       <xsd:element name="genre" type="xsd:string"/> 
       <xsd:element name="pages" type="xsd:integer"/> 
       <xsd:element name="year" type="xsd:integer"/> 
      </xsd:sequence> 
     </xsd:complexType> 

     <xsd:complexType name="TypAuthor2"> 
      <xsd:simpleContent> 
       <xsd:extension base="xsd:string"> 
         <xsd:attribute name="country" type="TypCountry2" use="required"/> 
       </xsd:extension> 
      </xsd:simpleContent>  
     </xsd:complexType> 


     <xsd:simpleType name="TypCountry2"> 
      <xsd:restriction base="xsd:string"> 
       <xsd:enumeration value="USA"/> 
       <xsd:enumeration value="PL"/> 
       <xsd:enumeration value="UK"/> 
       <xsd:enumeration value="FR"/> 
      </xsd:restriction> 
     </xsd:simpleType> 

     <xsd:simpleType name="TypNr"> 
      <xsd:restriction base="xsd:integer"> 
       <xsd:maxInclusive value="5"/> 
      </xsd:restriction> 
     </xsd:simpleType> 

     <xsd:simpleType name="TypLang"> 
      <xsd:restriction base="xsd:string"> 
       <xsd:enumeration value="pl"/> 
       <xsd:enumeration value="en"/> 
       <xsd:enumeration value="fr"/> 
      </xsd:restriction> 
     </xsd:simpleType> 

     <xsd:complexType name="TypFilms"> 
      <xsd:sequence> 
       <xsd:element name="film" type="TypFilm" minOccurs="5" maxOccurs="unbounded"/> 
      </xsd:sequence> 
     </xsd:complexType> 

     <xsd:complexType name="TypFilm"> 
      <xsd:attribute name="nr" type="TypNr" /> 
      <xsd:attribute name= "lang" type="TypLang"/> 
      <xsd:sequence> 
       <xsd:element name="title" type="xsd:string"/> 
       <xsd:element name="director" type="TypDirector"/> 
       <xsd:element name="year" type="xsd:integer"/> 
       <xsd:element name="genre" type="xsd:string"/> 
       <xsd:element name="stars" type="xsd:string"/> 
      </xsd:sequence> 
     </xsd:complexType> 
     <xsd:complexType name="TypDirector"> 
      <xsd:simpleContent> 
       <xsd:extension base="xsd:string"> 
         <xsd:attribute name="country" type="TypCountry2" use="required"/> 
       </xsd:extension> 
      </xsd:simpleContent> 
     </xsd:complexType> 

我得到的錯誤信息是這樣的:

S4S-ELT-無效-content.1:本'TypBook'的內容無效。元素'complexContent'無效,錯位或發生頻率過高。

s4s-elt-invalid-content.1:'TypFilm'的內容無效。元素'complexContent'無效,錯位或發生頻率過高。

你有什麼建議嗎?我需要在晚上11點之前將這個有效的模式發送給我的Profesor。請幫忙!!

非常感謝

回答

1

我覺得你的問題是由於屬性之前傳來的序列,試試這個: 我確實花了一兩分鐘的努力也google了源出於此規則,但並沒有找到太多。然而,在w3Shools complex type docs語法是用屬性last指定的。雖然不是很明確。

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema id="XMLSchema1" 
    targetNamespace="http://tempuri.org/XMLSchema1.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://tempuri.org/XMLSchema1.xsd" 
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
> 
    <xsd:element name="colection"> 
    <xsd:complexType> 
     <xsd:sequence> 
     <xsd:element name="author" type="TypAuthor" maxOccurs="1"/> 
     <xsd:element name="books" type="TypBooks"/> 
     <xsd:element name="films" type="TypFilms"/> 
     <xsd:element name="copyright" type="xsd:string"/> 
     </xsd:sequence> 
    </xsd:complexType> 
    </xsd:element> 

    <xsd:complexType name="TypAuthor"> 
    <xsd:sequence> 
     <xsd:element name="name" type="xsd:string" maxOccurs="1"/> 
     <xsd:element name="surname" type="xsd:string"/> 
     <xsd:element name="gender" type="TypSex"/> 
     <xsd:element name="age" type="xsd:integer"/> 
     <xsd:element name="city" type="xsd:string"/> 
     <xsd:element name="country" type="xsd:string"/> 
     <xsd:element name="study" type="xsd:string"/> 
     <xsd:element name="semester" type="xsd:integer"/> 
     <xsd:element name="group" type="xsd:string"/> 
    </xsd:sequence> 
    </xsd:complexType> 

    <xsd:simpleType name="TypSex"> 
    <xsd:restriction base="xsd:string"> 
     <xsd:enumeration value="male"/> 
     <xsd:enumeration value="female"/> 
    </xsd:restriction> 
    </xsd:simpleType> 

    <xsd:complexType name="TypBooks"> 
    <xsd:sequence> 
     <xsd:element name="book" type="TypBook" minOccurs="5" maxOccurs="unbounded"/> 
    </xsd:sequence> 
    </xsd:complexType> 

    <xsd:complexType name="TypBook"> 
    <xsd:sequence> 
     <xsd:element name="title" type="xsd:string"/> 
     <xsd:element name="author" type="TypAuthor2"/> 
     <xsd:element name="genre" type="xsd:string"/> 
     <xsd:element name="pages" type="xsd:integer"/> 
     <xsd:element name="year" type="xsd:integer"/> 
    </xsd:sequence> 
    <xsd:attribute name="nr" type="TypNr" /> 
    <xsd:attribute name= "lang" type="TypLang"/> 
    </xsd:complexType> 

    <xsd:complexType name="TypAuthor2"> 
    <xsd:simpleContent> 
     <xsd:extension base="xsd:string"> 
     <xsd:attribute name="country" type="TypCountry2" use="required"/> 
     </xsd:extension> 
    </xsd:simpleContent> 
    </xsd:complexType> 


    <xsd:simpleType name="TypCountry2"> 
    <xsd:restriction base="xsd:string"> 
     <xsd:enumeration value="USA"/> 
     <xsd:enumeration value="PL"/> 
     <xsd:enumeration value="UK"/> 
     <xsd:enumeration value="FR"/> 
    </xsd:restriction> 
    </xsd:simpleType> 

    <xsd:simpleType name="TypNr"> 
    <xsd:restriction base="xsd:integer"> 
     <xsd:maxInclusive value="5"/> 
    </xsd:restriction> 
    </xsd:simpleType> 

    <xsd:simpleType name="TypLang"> 
    <xsd:restriction base="xsd:string"> 
     <xsd:enumeration value="pl"/> 
     <xsd:enumeration value="en"/> 
     <xsd:enumeration value="fr"/> 
    </xsd:restriction> 
    </xsd:simpleType> 

    <xsd:complexType name="TypFilms"> 
    <xsd:sequence> 
     <xsd:element name="film" type="TypFilm" minOccurs="5" maxOccurs="unbounded"/> 
    </xsd:sequence> 
    </xsd:complexType> 

    <xsd:complexType name="TypFilm"> 
    <xsd:sequence> 
     <xsd:element name="title" type="xsd:string"/> 
     <xsd:element name="director" type="TypDirector"/> 
     <xsd:element name="year" type="xsd:integer"/> 
     <xsd:element name="genre" type="xsd:string"/> 
     <xsd:element name="stars" type="xsd:string"/> 
    </xsd:sequence> 
    <xsd:attribute name="nr" type="TypNr" /> 
    <xsd:attribute name= "lang" type="TypLang"/> 
    </xsd:complexType> 
    <xsd:complexType name="TypDirector"> 
    <xsd:simpleContent> 
     <xsd:extension base="xsd:string"> 
     <xsd:attribute name="country" type="TypCountry2" use="required"/> 
     </xsd:extension> 
    </xsd:simpleContent> 
    </xsd:complexType> 
</xsd:schema> 
6

這是一個非常糟糕的錯誤消息,但正如Larsson所說,屬性必須在內容之後定義。

如果在規範的散文中找不到這樣的規則,那麼可能是架構文檔的架構,這是規範的規範部分。

+1

我有同樣的錯誤信息。當我定義'attribute'元素** AFTER **''sequence'元素的時候,它就起作用了。謝謝。 –

0

您可以更改定義xsd元素的順序。 例如,考慮以下情形

<xsd:complexType name="Element-A> 
    <xsd:attribute name="attribute1" type="xsd:string" use="required" /> 
    <xsd:attribute name="attribute2" type="xsd:int" use="required" /> 
</xsd:complexType> 

現在,在這種情況下,如果要添加屬性,你只能添加在開始。 此外,你必須用屬性包圍它。所以,上面的代碼將在下面的翻譯:

<xsd:complexType name="Element-A> 
    <xsd:sequence> 
     <xsd:element name="element1" type="f1:elementDefinition" minOccurs="0" maxOccurs="unbounded"/> 
    </xsd:sequence> 
    <xsd:attribute name="attribute1" type="xsd:string" use="required"/> 
    <xsd:attribute name="attribute2" type="xsd:int" use="required"/> 
</xsd:complexType> 

<xsd:complexType name="elementDefinition"> 
    <xsd:attribute name="id" type="xsd:string" /> 
</xsd:complexType> 

上面的代碼可以確保您有由部件1的XSD引用的類型「elementDefinition」你的「元素-A」的列表。