xml:如何在 .xml 文件中引用 .xsd 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3965357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:21:24  来源:igfitidea点击:

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

xmlxsd

提问by RedsDevils

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.

我想在浏览器中查看 .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>

employee.xsd

员工.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>

回答by Oleg

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

您犯了两个错误:一个在架构文件中,另一个在xsi:schemaLocationXML 文件的属性值的语法中。

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,

主要错误是您的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>

and employee.xml:

和员工.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:schemaLocationmust 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 文件中定义了默认命名空间,所以架构位置属性xsi:schemaLocation必须由命名空间和以空格分隔的架构路径组成。我更改了命名空间名称,使其更加独特:"http://www.w3schools.com/RedsDevils"而不是"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.

最后,我可以补充一点,XML 文件employee.xml 与架构employee.xsd 不对应,因为该元素<firstname>John</firstname>的值为 other as red,但可能正是您想要测试的。