构建 XML 文档的步骤:
1.陈述 XML 声明
<?xml version=”1.0” encoding=”” standalone=“”?>
位置:第一行
version:版本,目前是1.0
encoding:编码(utf-8/utf-16/gb2312/big5)
standalone:独立申明,指示文档的内容是否依赖来自外部源的信息 (yes/no),其缺省值为"no"。
2.创建根元素
描述文档功能
有且只能有一个
必须包括其他所有元素
3.创建XML代码
XML文档中的主要内容----元素和属性
元素:包含开始标签和结束标签;可以含有属性、文本和子元素;
属性:元素的特征
空元素:没有文本也没有子元素
字符数据:可以做为元素的子元素出现
XML管理元素的规则
1.至少需要一个元素
2.XML 标签区分大小写
3.应正确使用结束标签
4.正确嵌套标签
5.应使用合法标签
6.标记名称的长度
7.应定义有效的属性
8.应验证文档
xml约束规则之XML模式
schema包括:
xml包括:元素、属性和标记
而元素和属性又具有数据类型所以schema中不仅要有元素和属性的约束
而且还要对他们的数据类型进行定义,
数据类型又包括:简单数据类型和复杂数据类型,所schema中就有:
元素定义、属性定义、数据数型定义
在数据类型定义时,简单数据类型就相当于JAVA中的基本数据类型(没有子元素)
而复杂数据类型就相当于JAVA中的类(含有子元素)。
==========================================================================
为了达到代码重用的目地,可以进行模块化定义:也就是将属性或嵌套的元素单独
定义,然后通过type属性或者ref属性来引用,就可以实现一次定义多次引用了
-------------------------------------------------------------------------
代码重用一:模块化(将数据类型在元素体外定义)
------------------------------------------------
元素数据类型:约束的是元素体内的东西
一、复杂数据类型--- 对有子无素的元素进行约束
使用时用:
1.type="复杂数据类型名" (此时是对当前元素进行约束,当前元素不会被替换)
2.ref="复杂数据类型名" (用目标类型替换当前元素)
3.直接在当前元素体内定义<element> //在此处定义...</emement>
二、简单数据类型--- 对没有子无素的元素进行约束
使用时用:
1.type="简单数据类型名" (此时是对当前元素进行约束,当前元素不会被替换)
2.ref="简单数据类型名" (用目标类型替换当前元素)
<xs:element ref="数据类型名称">
3.直接在当前元素体内定义<element> //在此处定义...</emement>
xml元素的属性的数据类型:约束的是属性的值(此时的数据类型,只有简单数据类型和基本数据类型)
1.type="数据类型名"
2.<xs:attributeGroup ref="attrgp"/>
为了达到更好的代码重用,还可以再将经常在一起使用的元素或者属性进行分组
-------------------------------------------------------------------------
代码重用二:分组
----------------------------
元素分组:
<xs:group name="gp1">
<xs:all>
<xs:element name="Price">
<xs:simpleType>
<xs:restriction base="xs:float">
<xs:maxExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Publication" type="xs:string"/>
</xs:all>
</xs:group>
属性分组:
<xs:attributeGroup name="attrgp">
<xs:attribute name="name"/>
<xs:attribute name="type"/>
<xs:attribute name="ISBN" type="isbn"/>
</xs:attributeGroup>
<xs:simpleType name="isbn">
<xs:restriction base="xs:string">
<xs:pattern value="\d{3}-[A-Z]{2}"/>
</xs:restriction>
</xs:simpleType>
调用分组:
<xs:element name="Magazine">
<xs:complexType>
<xs:group ref="gp1"/>
<xs:attributeGroup ref="attrgp"/>
</xs:complexType>
</xs:element>
-------------------------------------------------------------------------
代码重用三:引用外部模式(创建来自多个部份的模式)
-------------------------
<xs:include schemaLocation="otherSchema.xsd">
==========================================================================
指定模式的使用:
1.指定一个XML模式(不使用名字空间)
<games xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="games.xsd">
2.指定一个XML模式(使用名字空间)
<games xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:SchemaLocation="arcade games.xsd" xmlns="arcade">
==========================================================================
注:凡是使用ref使用数据类型的 都是替换当前位置元素或者属性
type属性指明数据类型时,只是对当前的元素或者属性进行修饰,不替换当前位置
将自定义类型指定为list或union:
==============================================================
指定为list:
------------
<!-- 定义一个简单类型,它必须是一个整数列表,如:12 3 51-->
<xs:simpleType name="listtp">
<xs:list itemType="xs:int" />
</xs:simpleType>
<!-- 定义一个元素,它是上面声明的整数列表类型-->
<xs:element name="birth" type="listtp" />
**********************************************************
指定为union:
------------
<!--定义一个简单类型,它是2个数据类型的联合体,则它可以是整数数据,也可以是日期数据-->
<xs:simpleType name="untp">
<xs:union memberTypes="xs:int xs:date"/>
</xs:simpleType>
<!--定义一个元素,它是上面声明的整数列表类型-->
<xs:element name="birth" type="untp"/>
=================================================================
示例:test1.xml:
<?xml version="1.0" encoding="GB2312"?>
<BookStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test1.xsd">
<Book name="C#高级编程" type="计算机" ISBN="001-AB">
<Price>56.3</Price>
<Publication>清华大学出版社</Publication>
</Book>
<Book name="Java高级编程" type="计算机" ISBN="001-AC">
<Price>56.3</Price>
<Publication>清华大学出版社</Publication>
</Book>
<Disc name="Asp">05:33:50.0Z</Disc>
<Magazine name="读者" type="文摘" ISBN="001-BC" >
<Publication>北京大学出版社</Publication>
<Price>2.3</Price>
</Magazine>
</BookStore>
------------------------------------
test1.xsd代码 :
---------------
<?xml version="1.0" encoding="GB2312"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="qualified">
<xs:element name="BookStore">
<xs:complexType>
<xs:sequence>
<xs:element name="Book" type="BookInfo"
maxOccurs="unbounded"/>
<xs:element name="Disc" type="DiscInfo"/>
<xs:element name="Magazine">
<xs:complexType>
<xs:group ref="gp1"/>
<xs:attributeGroup ref="attrgp"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="id">
<xs:selector xpath="*"/>
<xs:field xpath="@ISBN"/>
</xs:unique>
</xs:element>
<xs:complexType name="BookInfo">
<xs:group ref="gp1"/>
<xs:attributeGroup ref="attrgp"/>
<!--<xs:sequence>
<xs:element type="xs:float" name="Price"/>
<xs:element type="xs:string" name="Publication"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>-->
</xs:complexType>
<xs:simpleType name="res">
<xs:restriction base="xs:string">
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="DiscInfo" mixed="true">
<xs:simpleContent> <!--simpleContent 简单文本 属性和文本的复合类型-->
<xs:extension base="xs:time">
<xs:attribute name="name" type="res"/>
</xs:extension>
</xs:simpleContent>
<!--<xs:attribute name="out" type="xs:dateTime"/>
<xs:sequence>
<xs:element name="out"/>
</xs:sequence>-->
</xs:complexType>
<xs:group name="gp1">
<xs:all>
<xs:element name="Price">
<xs:simpleType>
<xs:restriction base="xs:float">
<xs:maxExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Publication" type="xs:string"/>
</xs:all>
</xs:group>
<xs:simpleType name="isbn">
<xs:restriction base="xs:string">
<xs:pattern value="\d{3}-[A-Z]{2}"/>
</xs:restriction>
</xs:simpleType>
<xs:attributeGroup name="attrgp">
<xs:attribute name="name"/>
<xs:attribute name="type"/>
<xs:attribute name="ISBN" type="isbn"/>
</xs:attributeGroup>
</xs:schema>