我正在尝试使用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验证它,我收到了这些错误:
XML文档中的错误:
36:10 cvc-complex-type.2.3:元素'Grade'不能有字符[children],因为type的内容类型只是元素。
36:10 cvc-complex-type.2.4.b:元素'Grade'的内容不完整。预计会有一个'{“”:GradePoint}。
有点,我理解这些错误信息。我知道这是因为最后<Grade>
标签中缺少某些元素。为了解决它,我创建了2个全局类型并使用了选择。它现在有什么问题?
答案 0 :(得分:1)
您写的XSD本身无效,因为Grade
元素(Grades
的子元素)不能像您尝试的那样有两种不同的类型。您应该已收到有关此内容的错误消息,如下所示:
cos-element-consistent :类型'#AnonType_Grades'出错。多 名称为“Grade”且具有不同类型的元素将显示在模型中 基。
和
cos-nonambig :成绩和成绩(或替代成分) 小组)违反“独特粒子归因”。验证期间 针对这种模式,将为这两种模式创建歧义 颗粒
下面显示的简化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>