Вложенный элемент XSD - PullRequest
       37

Вложенный элемент XSD

2 голосов
/ 22 марта 2012
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="game">
    <xsd:complexType>
        <xsd:all>
            <xsd:element name="info" type="infoType" minOccurs="0"/>
        </xsd:all>
    </xsd:complexType>
</xsd:element>
    <xsd:complexType name="infoType">
       <xsd:sequence>
        <xsd:element name="name" type="xsd:string" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="players" minOccurs="0" maxOccurs="1">
            <xsd:complexType mixed="true">
                <xsd:attribute name= "number" type="playernum"/>

                <!-- xsd:element name="screenname" type="xsd:string">
                    <xsd:complexType>
                    <xsd:attribute name= "player" type="playernum"/>
                    </xsd:complexType>
                </xsd:element -->

            </xsd:complexType>
        </xsd:element>
       </xsd:sequence>
    </xsd:complexType>
<xsd:simpleType name="playernum">
  <xsd:restriction base="xsd:int">
    <xsd:minInclusive value="1"/>
    <xsd:maxInclusive value="4"/>
  </xsd:restriction>
</xsd:simpleType>    
</xsd:schema>infoType

указанный код является закомментированным.

выдает ошибку:

game.xsd: 26: элемент element: ошибка синтаксического анализатора схем: элемент '{http://www.w3.org/2001/XMLSchema}complexType': Контент не действительный. Ожидается (аннотация ?, (simpleContent | complexContent | ((группа | все | выбор | последовательность) ?, ((attribute | attributeGroup) *, anyAttribute?)))).

но рассматриваемый элемент содержит завершенный тип, который по умолчанию имеет complexContent. Любая помощь будет очень кстати, заранее спасибо.

1 Ответ

3 голосов
/ 23 марта 2012

Я бы сказал, что вам не хватает xsd:sequence, более того screenname не может быть xsd:string И complexType одновременно - вы должны выбрать один или другой.

Вероятно, это то, что вам нужно:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="game">
    <xsd:complexType>
      <xsd:all>
        <xsd:element name="info" type="infoType" minOccurs="0"/>
      </xsd:all>
    </xsd:complexType>
  </xsd:element>
  <xsd:complexType name="infoType">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" minOccurs="0" maxOccurs="1"/>
      <xsd:element name="players" minOccurs="0" maxOccurs="1">
        <xsd:complexType mixed="true">
          <xsd:sequence>
            <xsd:element name="screenname">
              <xsd:complexType mixed="true">
                <xsd:attribute name= "player" type="playernum"/>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
          <xsd:attribute name= "number" type="playernum"/>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="playernum">
    <xsd:restriction base="xsd:int">
      <xsd:minInclusive value="1"/>
      <xsd:maxInclusive value="4"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>
...