XSD嵌套元素

时间:2012-03-22 19:54:33

标签: xml parsing xsd xmllint

<?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:元素元素:模式解析器错误:元素   '{http://www.w3.org/2001/XMLSchema}complexType':内容不是   有效。预期是(注释?,(simpleContent | complexContent |   ((group | all | choice | sequence)?,((attribute | attributeGroup)*,   anyAttribute?))))。

但是有问题的元素包含一个ComplextType,默认情况下具有complexContent。 提前感谢,非常欢迎任何帮助。

1 个答案:

答案 0 :(得分:3)

我说你错过了xsd:sequence,而且screenname不能同时xsd:stringcomplexType - 你必须选择其中一个或另一个

可能这就是你所需要的:

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