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

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

的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>
您造成了两个错误:一个在模式文件中,另一个在XML文件的xsi:schemaLocation属性的值的语法中。

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇