การตรวจสอบความถูกต้องของไฟล์ XML ด้วย XSD ทำให้เกิดข้อผิดพลาดที่แตกต่างกัน

ฉันพยายามตลอดเวลาเพื่อตรวจสอบไฟล์ XML ด้วย XSD และลองใช้ตัวเลือกต่างๆ โดยเพิ่มองค์ประกอบ ลำดับ ประเภทที่ซับซ้อน และไม่มีอะไรช่วยแก้ปัญหาได้

ใครก็ได้ช่วยบอกฉันทีว่าปัญหาคืออะไร? ขอบคุณ.

<?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>


person kannzzmm2    schedule 18.06.2019    source แหล่งที่มา


คำตอบ (1)


ยกเว้น 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 ข้างต้น
ฉันย้ายองค์ประกอบ titleและ editor ออกจากองค์ประกอบ author และเพิ่มองค์ประกอบ firstname และ name แทน

person zx485    schedule 18.06.2019
comment
ขอบคุณสำหรับความช่วยเหลือของคุณ แต่ไฟล์ XML จะรู้ได้อย่างไรว่าควรใช้ Schema ใดหากไม่ได้ระบุไว้ในตอนต้น - person kannzzmm2; 19.06.2019
comment
โดยปกติแล้ว XSD จะเป็นพารามิเตอร์ของเครื่องมือตรวจสอบความถูกต้อง หากไม่เป็นเช่นนั้น ควรทำงานกับแอตทริบิวต์ xsi:noNameSpaceSchemaLocation="exerciseLiteraturSchema.xsd" ดังนั้นให้เพิ่มมันลงในไฟล์แล้วตรวจสอบกับเครื่องมือตรวจสอบความถูกต้องของคุณ - person zx485; 19.06.2019