XML Schema 简易入门教程

XML Schema 简易入门教程

在阅读本文档之前请先了解xml语法

1.命名空间与导入
①在xsd文档中,根标签为:(xsd文档由xml编写)
<前缀:schema ></前缀:schema>这里的前缀一般是xs或xsd,也可以随便写(前缀的意思就是起一个别名)
②在<前缀:schema>中写命名空间
示例:

<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.itcast.cn/xml"
targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">

解释:
*xmlns="http://www.itcast.cn/xml"表示默认的命名空间,将来你可以使用http://www.itcast.cn/xml空间里
的元素,属性,类型,并且不加前缀。并且默认命名空间只可以有一个。
*xmlns:xs="http://www.w3.org/2001/XMLSchema",将来你可以使用http://www.w3.org/2001/XMLSchema空间里
的元素,属性,类型,并且要加前缀xs。这一步视为固定模式
*targetNamespace="http://www.itcast.cn/xml",表示在你编辑本xsd文档时,你定义的元素,属性,类型放在
http://www.itcast.cn/xml中。
*elementFormDefault="qualified"指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。

可能你会觉得这些好多,如果你不是很理解,请按照下面的步骤来:
.不管三七二十一,先写上xmlns:xsd="http://www.w3.org/2001/XMLSchema"(其实这是一个schema的版本号,表示你可以
根据,这个命名空间里的一些规定来写xsd文档,这个是属于w3c组织的,写文当前必须引入,并给与前缀)和 elementFormDefault="qualified"
II.写 targetNamespace="http://www.itcast.cn/xml",这个是你的xsd文档里的元素的命名空间。http://www.itcast.cn/xml这个东西其实
你可以随便写,毕竟你是在给你自己的文档元素起命名空间名,比如你写个啊a,b,c啥的无所为随意就行
III.写xmlns="http://www.itcast.cn/xml",这里的http://www.itcast.cn/xml你注意到没?和targetNamespace中的一样,也就是所targetNamespace
里写啥这里就写啥,因为这样写,你在写xsd文档是当要使用你自己定义的元素时就可以不加前缀了比较方便。
以上是编写xsd文档时导入命名空间的三大步骤。
③xml文件中的命名空间的导入
示例:

<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.itcast.cn/xml"
xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"

这里我就不解释了,如果你前面的看懂了,我想是没有问题的
步骤:
①先写根标签(在你的xsd文档里已经定义过了)示例中是<students></students>
②直接写xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"//不要改,要改也只改xsi,不过一般都用xsi
③ 写xsi:schemaLocation="targetNamespace里的名字 xsd文档路径"
④写xmlns="targetNamespace里的名字"
2.简易元素
*不含有属性与元素的元素
*定义方式

<xs:element name="car" type="xs:string"/>

*xsd中的常用元素类型:
string integer boolean date time
*简易元素的默认值与固定值:

<xs:element name="car" type="xs:string" default="123"/>  //在没给定值时xml解析器处理为123
<xs:element name="car" type="xs:string"  fixed="123"/>   //只能取值为123

3.属性:
*定义方式

<xs:attribute name="carType" type="xs:string"/>

*默认值与固定值
略,与简易元素一样
4.复合元素
*定义
方法①:

<xs:element name="employee">
<xs:complexType>
<xs:sequence>//指示器,后面会讲。
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

方法②:

<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType> 

补充内容:如果你想在已有的复杂元素中在补充元素咋办:

<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

5.简单元素与属性限定(以下列举常用的限定方式,限定restriction用于为 XML 元素或者属性定义可接受的值。对 XML 元素的限定被称为 facet。)
--------若是对属性的限定,将下面的element改为attribute,然后用写法1,对于方法二,将xs:simpleType标签及其内部部
分放在xsd:complex标签对外,前面的留下,或者全部放到xsd:complex标签对外(即写为全局元素)。(复杂元素的限定都是对其包含的简单元素与属性限定)
*值的大小限定(注意maxInclusive minInclusive表示闭区间,开区间用maxExclusive minExclusive)
写法1:

扫描二维码关注公众号,回复: 10639768 查看本文章
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="123"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

写法2:

<xs:element name="car" type="carP"/>
<xs:simpleType name = "carP">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="123"/>
</xs:restriction>
</xs:simpleType>

*枚举限定

<xs:element name="car">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:enumeration value = "1"/>
<xs:enumeration value = "2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

*模式限定

<xs:element name="car">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:pattern value = "正则表达式"/>
<xs:pattern value = "正则表达式"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

*长度限定

<xs:element name="car">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:length value = "1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="car">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:minLength value = "1"/>
<xs:maxLength value = "8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

*空白符(换行、回车、空格以及制表符):限定whiteSpace取值:collapse:(换行、回车、空格以及制表符会被替换为空格,
开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格) preserve:保留空白符 replace:去掉空白符

<xs:element name="car">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:whiteSpace value = "preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

6.指示器:
*Order 指示器:
All 任意顺序
Choice 从两个中选一个即可
Sequence 按顺序出现
例子:

<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element> 

*Occurrence 指示器:
maxOccurs 出现的最大次数
minOccurs 出现的最小次数
例子:

 <xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element> 

*Group 指示器:(不常用,知道有这个就行)
Group name
attributeGroup name
7.any与anyAttrbute元素及元素替换(Element Substitution)
个人认为不是很重要,所以不写了。有兴趣百度吧,补充一个use属性,use="requuired"表示该元素或属性必须被包含,缺省为可以不包含。

发布了22 篇原创文章 · 获赞 0 · 访问量 601

猜你喜欢

转载自blog.csdn.net/qq_44932835/article/details/104140183