使用XSD验证XML文件会产生不同的错误

时间:2019-06-18 18:51:12

标签: xml xsd schema xsd-validation

我一直在尝试使用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>

1 个答案:

答案 0 :(得分:0)

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元素。