使用 XSD 验证 XML 文件会给出不同的错误

Validating XML File with XSD gives different errors

我一直在尝试用 XSD 验证 XML 文件,并尝试了不同的选项添加元素、序列、复杂类型,但没有解决问题。

谁能告诉我,到底是什么问题?谢谢。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<literature
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:NoNameSpaceSchemaLocation="exerciseLiteraturSchema.xsd">

    <!-- First reference -->
    <reference number="1">
        <author>
            <firstname>Ron</firstname>
            <name>Jefferson</name>
        </author>
        <title>XML for Beginners</title>
        <editor>McGraw Hill</editor>
        <!-- <isbn>12-3344-567-00</isbn> -->
    </reference>

</literature>

<?xml version="1.0" encoding="UTF-8"?>

<xsd:element name="literature">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="reference">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="author">
                            <xsd:complexType>
                                <xsd:sequence>
                                    <xsd:element name="title" type="xsd:string" />
                                    <xsd:element name="editor" type="xsd:string" />
                                </xsd:sequence>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="number" type="xsd:integer" />
    </xsd:complexType>
</xsd:element>

除了 xsi:noNameSpaceSchemaLocation="exerciseLiteraturSchema.xsd" 你可以使用以下 XSD 获得令人满意的结果:

XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<literature
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <!-- First reference -->
    <reference number="1">
        <author>
            <firstname>Ron</firstname>
            <name>Jefferson</name>
        </author>
        <title>XML for Beginners</title>
        <editor>McGraw Hill</editor>
        <!-- <isbn>12-3344-567-00</isbn> -->
    </reference>

</literature>

XSD:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xs:element name="literature">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="reference">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="firstname" type="xs:string" />
                                        <xs:element name="name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="editor" type="xs:string" />
                        </xs:sequence>
                        <xs:attribute name="number" type="xs:integer" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这 XSD 验证了上述 XML.
我将 titleeditor 元素移出了 author 元素,并添加了 firstnamename 元素。