XMLSchema学习总结

XML Schema的总结

一,什么是XML Schema?

Xml Schema是对XML数据结构的描述,可以通过XML Schema来定义 XML 文档的合法构建模块,可以用于检测元数据是否合法。

· 比如:<date type="date">2004-03-11</date>,可确保对内容一致的理解,这是因为 XML 的数据类型 "date" 要求的格式是 "YYYY-MM-DD"

二,XML Schema可以定义以下内容

· 定义可出现在文档中的元素

· 定义可出现在文档中的属性

· 定义哪个元素是子元素

· 定义子元素的次序

· 定义子元素的数目

· 定义元素是否为空,或者是否可包含文本

· 定义元素和属性的数据类型

· 定义元素和属性认值以及固定值

三,关于Xml Schema用法

1,定义简易元素的语法:

<xs:element name="age" type="xs:interger"/>

1.1 对元素值的限定:

例如:

<xs:element name=”age>

<xs:simpletype>

<xs:restriction base=xs:integer>

< xs:minInclusive value=”0”>

<xs:maxInclusive value =”120”>

 <xs:restriction>

</xs:simpletype>

</xs:element>

2,定义属性的语法:

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

3, 什么是复杂元素:

复合元素指包含其他元素及/属性 XML 元素。

复合元素分为四类:

· 空元素

· 包含其他元素的元素

· 仅包含文本的元素

· 包含元素和文本的元素

1,空元素:

Xml <product pid="1345"/>

2,仅包含其他元素:

<employ>

<firstname>john</firstname>

<lastname> smith</lastname>

</employ>

对应的xml schema描述:

<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>

3,含有文本和属性的要素:

XML: <shoesize country="france">35</shoesize>

对应的XmlShema:

<xs:element name="shoesize">

<xs:complexType>

<xs:simpleContent>

<xs:extension base="xs:integer">

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

</xs:extension>

</xs:simpleContent>

</xs:complexType>

</xs:element>

4,含有混合内容的复合类型

XML:

<letter>

Dear Mr.<name>John Smith</name>.

Your order <orderid>1032</orderid>

will be shipped on <shipdate>2001-07-13</shipdate>.

</letter>

XMLSchema:

<xs:element name="letter">

<xs:complexType mixed="true">

<xs:sequence>

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

<xs:element name="orderid" type="xs:positiveInteger"/>

<xs:element name="shipdate" type="xs:date"/>

</xs:sequence>

</xs:complexType>

</xs:element>

最后,XMLSchema有很多数据类型:1String 2 Date 3Numeric Data 4 Boolean 5 Binary 6 AnyURI。对数值的约束有以下内容 enumeration fractionDigits maxExclusive maxInclusive minexclusive minInclusive pattern totalDigits whiteSpace

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念