加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 08:02:58 所属栏目:百科 来源:网络整理
导读:我想在浏览器中看到xml文件,我在.xsd文件中定义。请检查以下两个文件给我,并指出我需要做什么。这两个文件在同一个文件夹下。 employee.xml ?xml version="1.0"?employee xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i
我想在浏览器中看到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>具有其他红色的值,但可能正是您想要测试的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读