为什么我的 xsd:choice 在不同类型的元素之间失败?

Why is my xsd:choice between elements of different types failing?

我正在尝试用 XSD 验证 XML,但我不明白我的 XSD.

有什么问题

XML

<?xml version="1.0" encoding="UTF-8"?>
<Grades xs:noNamespaceSchemaLocation="grade.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
    <Grade description="Pass">
        <LetterGrade>A+</LetterGrade>
        <GradePoint>4.00</GradePoint>
        <Percentage>80% and above</Percentage>
    </Grade>
    <Grade description="Pass">
        <LetterGrade>A</LetterGrade>
        <GradePoint>4.00</GradePoint>
        <Percentage>70% to 80%</Percentage>
    </Grade>
    <Grade description="Pass">
        <LetterGrade>B</LetterGrade>
        <GradePoint>3.00</GradePoint>
        <Percentage>60% to 70%</Percentage>
    </Grade>
    <Grade description="Pass">
        <LetterGrade>C</LetterGrade>
        <GradePoint>2.00</GradePoint>
        <Percentage>50% to 60%</Percentage>
    </Grade>
    <Grade description="Pass">
        <LetterGrade>D</LetterGrade>
        <GradePoint>1.00</GradePoint>
        <Percentage>40% to 50%</Percentage>
    </Grade>
    <Grade description="Fail">
        <LetterGrade>F</LetterGrade>
        <GradePoint>0</GradePoint>
        <Percentage>Below 40%</Percentage>
    </Grade>
    <Grade description="Incomplete"> Incomplete module/project/independent study due
    to medical/financial/other reasons (may be in 1 or both components of the module).
        <LetterGrade>N</LetterGrade>
    </Grade>
</Grades>

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType name="gradeDetails">
    <xs:sequence>
        <xs:element name="LetterGrade" type="xs:string"/>
        <xs:element name="GradePoint" type="xs:decimal"/>
        <xs:element name="Percentage" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="description" type="xs:string"/>
</xs:complexType>

<xs:complexType name="gradeNode">
    <xs:sequence>
        <xs:element name="LetterGrade" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="description" type="xs:string"/>
</xs:complexType>

<xs:element name="Grades">
    <xs:complexType mixed="true">
        <xs:choice>
            <xs:element name="Grade" type="gradeDetails" minOccurs="1" maxOccurs="unbounded"/>
            <xs:element name="Grade" type="gradeNode" minOccurs="1" maxOccurs="unbounded"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

</xs:schema>

我尝试使用在线验证器 xmlvalidation.com 对其进行验证,但出现以下错误:

Errors in the XML document:
36: 10 cvc-complex-type.2.3: Element 'Grade' cannot have character [children], because the type's content type is element-only.
36: 10 cvc-complex-type.2.4.b: The content of element 'Grade' is not complete. One of '{"":GradePoint}' is expected.

有些,我理解了这些错误信息。我知道这是因为最后一个 <Grade> 标记中缺少元素。为了解决这个问题,我创建了 2 个全局类型并使用了 choice。现在有什么问题吗?

您所写的 XSD 本身无效,因为 Grade 元素(作为 Grades 的子元素)不能像您尝试的那样具有两种不同的类型.您应该已经收到一条关于此的错误消息,内容如下:

cos-element-consistent: Error for type '#AnonType_Grades'. Multiple elements with name 'Grade', with different types, appear in the model group.

cos-nonambig: Grade and Grade (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.

下面显示的简化 XSD 消除了这些错误并成功验证了您未更改的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="GradeType" mixed="true">
    <xs:sequence>
      <xs:element name="LetterGrade" type="xs:string"/>
      <xs:element name="GradePoint" minOccurs="0" type="xs:decimal"/>
      <xs:element name="Percentage" minOccurs="0" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="description" type="xs:string"/>
  </xs:complexType>

  <xs:element name="Grades">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element name="Grade" type="GradeType" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>