我想在.xsd文件中定义浏览器中的xml文件.请为我检查以下两个文件,并指出我需要做什么.这两个文件位于同一文件夹下.
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>
Run Code Online (Sandbox Code Playgroud)
的employee.xsd
<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>
Run Code Online (Sandbox Code Playgroud)
Ole*_*leg 40
您犯了两个错误:一个在架构文件中,另一个在xsi:schemaLocationXML文件属性值的语法中.
主要错误是您的employee.xsd文件只是XML Schema的一个片段.您应该完成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>
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
因为您在XML文件中定义默认命名空间,所以架构位置属性xsi:schemaLocation必须包含命名空间和用空白分隔的架构的路径.我更改了名称空间名称,以便它更加独特:"http://www.w3schools.com/RedsDevils"而不是"http://www.w3schools.com".
最后,我可以补充说,XML文件employee.xml不对应于架构employee.xsd,因为该元素<firstname>John</firstname>具有其他值red,但可能正是您想要测试的.