xml:如何在 .xml 文件中引用 .xsd 文件? [英] xml : how to reference a .xsd file at .xml file?
问题描述
我想在浏览器中查看 .xsd 文件中定义的 xml 文件.请帮我检查以下两个文件,并指出我需要做什么.这两个文件在同一个文件夹下.
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 folder.
员工.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>
员工.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>
推荐答案
您犯了两个错误:一个在架构文件中,另一个在架构文件的 xsi:schemaLocation
属性值的语法中XML 文件.
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.
主要错误是您的employee.xsd 文件只是XML Schema 的一个片段.您应该完成对employee.xsd 的包含.例如,
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>
和employee.xml:
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>
因为您在 XML 文件中定义了默认命名空间,架构位置属性 xsi:schemaLocation
必须由命名空间和以空格分隔的架构路径组成.我更改了命名空间名称,使其更加独特:"http://www.w3schools.com/RedsDevils"
而不是 "http://www.w3schools.com"
.
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"
.
最后我可以补充一点,XML 文件employee.xml 与架构employee.xsd 不对应,因为元素
的值other 为red
,但可能正是您想要测试的.
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.
这篇关于xml:如何在 .xml 文件中引用 .xsd 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!