XSD-学习总结

1.代码详细分析

<?xml version="1.0"?>
 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  //<schema>是所有元素的根元素;
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">

......
</xs:schema>

其中:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

表示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。

   同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs;

targetNamespace="http://www.w3school.com.cn" 

表示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。

elementFormDefault="qualified" 

表示任何 XML 实例文档所使用的元素,并且在此 schema 中声明过的元素必须被命名空间限定;

  

2.以下是schema.xml文件对schema.xsd的引用

<?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>
xmlns="http://www.w3school.com.cn" 

规定了默认命名空间的声明;在此 XML 文档中使用的所有元素都在 "http://www.w3school.com.cn" 这个命名空间中被声明。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

实例命名空间xsi;与schema.xsd文件中的xsi命名空间不一样,这里是实例一个命名空间;

xsi:schemaLocation="http://www.w3school.com.cn note.xsd"

此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置:

3. 元素的属性声明:

  语法:

<xs:attribute name="xxx" type="yyy"/>

  常用的类型:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time
实例
这是带有属性的 XML 元素:
<lastname lang="EN">Smith</lastname>
这是对应的属性定义:
<xs:attribute name="lang" type="xs:string"/>

  在缺省的情况下,属性是可选的。如需规定属性为必选,请使用 "use" 属性:

<xs:attribute name="lang" type="xs:string" use="required"/>
原文地址:https://www.cnblogs.com/weiyouqing/p/10666815.html