我想在浏览器中看到xml文件,就像我在.xsd文件中定义的一样。请为我检查以下两个文件,并指出我需要做什么。这两个文件位于同一文件夹下。

employee.xml

 <?xml version="1.0"?>

<employee xmlns="http://www.w3schools.com"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="employee.xsd">

  <firstname>John</firstname>
  <lastname>Smith</lastname>
</employee>

员工文件
<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>

最佳答案

您犯了两个错误:一个在模式文件中,另一个在XML文件的xsi:schemaLocation属性的值的语法中。

主要错误是您的employee.xsd文件只是XML模式的一部分。您应该完成对employee.xsd的包含。例如,

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

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

因为您在XML文件中定义了默认 namespace ,所以模式位置属性xsi:schemaLocation必须由 namespace 和以空格分隔的模式路径组成。我更改了 namespace 名称,使其更具唯一性:"http://www.w3schools.com/RedsDevils"而不是"http://www.w3schools.com"

最后,我可以添加XML文件employee.xml与模式employee.xsd不对应,因为元素<firstname>John</firstname>具有其他值red,但可能正是您要测试的值。

09-10 02:53
查看更多