xml : how to reference a .xsd file at .xml file?

后端 未结 1 535
攒了一身酷
攒了一身酷 2020-12-23 09:51

I want to see xml file in browser as I define in .xsd file. Please check the following two files for me and point out what do I need to do. These two files are under same fo

相关标签:
1条回答
  • 2020-12-23 10:23

    You made two errors: one in the schema file and another in the syntax of the value of the xsi:schemaLocation attribute of the XML file.

    The main error is that your employee.xsd file is only a fragment of the XML Schema. You should complete the contain of the employee.xsd. For example,

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema targetNamespace="http://www.w3schools.com/RedsDevils"
        elementFormDefault="qualified"
        xmlns="http://www.w3schools.com/RedsDevils employee.xsd"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
        <xs:element name="employee">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="firstname" type="xs:string" fixed="red" />
                    <xs:element name="lastname" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    and employee.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <employee xmlns="http://www.w3schools.com/RedsDevils"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.w3schools.com/RedsDevils employee.xsd">
    
        <firstname>John</firstname>
        <lastname>Smith</lastname>
    </employee>
    

    Because you define default namespace in the XML file, the schema location attribule xsi:schemaLocation must consist from the the namespace and the path to the schema devided with the blank. I changed the namespace name so that it will be a little more unique: "http://www.w3schools.com/RedsDevils" instead of "http://www.w3schools.com".

    At the end I can add that the XML file employee.xml not corresponds to the schema employee.xsd because the element <firstname>John</firstname> has the value other as red, but probably exactly this you wanted to test.

    0 讨论(0)
提交回复
热议问题