首页 > 如何理解xml文件和xsd文件的关系

如何理解xml文件和xsd文件的关系

标题

xsd文件:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">
<xs:element name="note">
   <xs:complexType>
     <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="heading" type="xs:string"/>
        <xs:element name="body" type="xs:string"/>
     </xs:sequence>
   </xs:complexType>
</xs:element>
</xs:schema>

xml文件:

<?xml version="1.0"?>
<note
xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3school.com.cn note.xsd">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

另外,非IE浏览器下打开xml文档总是提示:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

  1. XSD文件在某个namespace中定义element和type。此处定义的type被element(此namespace或者其他namespace中的)使用,而XML文件不使用。

  2. XML文件有且唯一root element。

  3. 创建XML时,在root element中可以指定使用哪些namespace,从而使用其中的element。

    讨论:root element也是属于某个namespace,因为其他的element都是此root element的子element,那么是不是只需要

    指定使用root element的namespace?这个要看root element的type的定义情况。

    如果在root element的type的定义中(以及type定义中的element,反复下去)引用其他namespace的element或者type,那么就需要引入其他namespace

综上所述,XML文件和XSD文件没有直接的联系,他们之间通过namespace关联,即

              add element                                 use element

XSD文件-------------------------->namepsae--------------------------->XML文件


xml 声明里有一个属性 xs:schemaLoaction

【热门文章】
【热门文章】