XSD nested element

2019-07-18 22:43发布

问题:

<?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

the code in question is the commented one.

it produces the error output:

game.xsd:26: element element: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}complexType': The content is not valid. Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))).

but the element in question contains a complextType which has complexContent by default. Any help would be very welcome, thanks in advance.

回答1:

I'd say you are missing a xsd:sequence, furthermore screenname cannot be xsd:string AND a complexType at the same time - you have to chose one or the other.

Probably this is what you need:

<?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>