ComplexType 内部的 ComplexType

ComplexType inside ComplexType

我试图将可选元素放置在 simpleType 元素中,因此我将其更改为 complexType。现在我在 complexType 中有 complexType 并且验证器说它是错误的,我该如何解决它?

 <glowne_role>
    <aktor>William H. Macy</aktor>
    <aktor>
      Frances McDormand
      <komentarz>Prywatnie aktorka jest żoną reżysera.</komentarz>
    </aktor>
  </glowne_role>


<xs:element name="komentarz" type="xs:string" />

<xs:complexType name="glowne_role">
  <xs:complexType name="aktor" minOccurs="1" maxOccurs="3" type="xs:string">
    <xs:element ref="komentarz" minOccurs="0"/>
  </xs:complexType>
</xs:complexType>

您可以在 xs:complexType 中包含 xs:complexType,但不能直接使用。基于你的XML,你想定义一个元素,glowne_role,它是由一系列aktor元素组成的复杂类型,每个然后依次 也可以是复杂类型。请注意,要允许文本位于 komentarz 之前或之后,您可以在父 xs:complexType.

上设置 mixed="true"

总而言之,这个 XSD 将成功验证您的 XML:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="komentarz" type="xs:string"/>
  <xs:element name="glowne_role">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="aktor" minOccurs="1" maxOccurs="3">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element ref="komentarz" minOccurs="0"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>