温习Schema

来源:互联网 发布:114啦网址导航源码 编辑:程序博客网 时间:2024/05/29 16:57
---------------------- android培训、java培训、期待与您交流! ----------------------


        Schema用来校验XML,它本身遵循XML的语言规范。Schema支持命名空间,也支持较多的数据类型与元素类型。Schemaelement声明对应于XML中的元素,

attribute声明XML元素的属性。其中,simpleType定义简单类型,complexType定义复杂类型。


        Schema中经常用到的关键字schemaelementattributegroupattributeGroupsimpleTypesimpleContentcomplexTypechoicelistunionuniquesequence

restriction

   示例:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/po.xsd" targetNamespace="http://tempuri.org/po.xsd"><xs:element name="purchaseOrder" type="PurchaseOrderType"/><xs:element name="comment" type="xs:string"/><xs:complexType name="PurchaseOrderType"><xs:sequence><xs:element name="shipTo" type="USAddress"/><xs:element name="billTo" type="USAddress"/><xs:element ref="comment" minOccurs="0"/><xs:element name="items" type="Items"/></xs:sequence><xs:attribute name="orderDate" type="xs:date"/></xs:complexType><xs:complexType name="USAddress"><xs:sequence><xs:element name="name" type="xs:string"/><xs:element name="street" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="state" type="xs:string"/><xs:element name="zip" type="xs:decimal"/></xs:sequence><xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/></xs:complexType><xs:complexType name="Items"><xs:sequence><xs:element name="item" minOccurs="0" maxOccurs="unbounded"><xs:complexType><xs:sequence><xs:element name="productName" type="xs:string"/><xs:element name="quantity"><xs:simpleType><xs:restriction base="xs:positiveInteger"><xs:maxExclusive value="100"/></xs:restriction></xs:simpleType></xs:element><xs:element name="USPrice" type="xs:decimal"/><xs:element ref="comment" minOccurs="0"/><xs:element name="shipDate" type="xs:date" minOccurs="0"/></xs:sequence><xs:attribute name="partNum" type="SKU" use="required"/></xs:complexType></xs:element></xs:sequence></xs:complexType><!-- Stock Keeping Unit, a code for identifying products --><xs:simpleType name="SKU"><xs:restriction base="xs:integer"><xs:minInclusive value="2"/><xs:maxInclusive value="10"/></xs:restriction></xs:simpleType></xs:schema>



schema就是一个schema文件中的根元素

element定义一个元素,可能是根元素,也可能是没有子结点的元素,取决于其属性type

complexType用来定义元素类型,指定其是复杂元素,可能包含多个属性和多个子元素,也可能包含复杂元素

sequence用来限制一组元素的顺序,XML中的元素必须按其制定的顺序出现

attribute表示XML中元素的属性

simpleType定义的元素类型,没有属性,没有子元素

restriction用来限制元素内容的值,限制一个范围


以下是例子中没有出现的关键字


simpleContent应用于complexType,对他的内容进行约束和扩展

choice允许唯一的一个元素从一组元素中获得

list从一个特点类型的数据集合中选择定义一个简单类型元素

union定义多个 simpleType 定义的集合,组成一个新的simpleType

group用来定义在复杂类型定义中使用的元素组,可以被多个复杂元素复用

attributeGroup属性组,可以一起被应用于复杂类型,也可以被多个复杂类型复用


上例可以校验的XML示例

<?xml version="1.0" encoding="UTF-8"?><n1:purchaseOrder xmlns:n1="http://tempuri.org/po.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tempuri.org/po.xsdC:\DOCUME~1\zhanglong\桌面\schema_source\test11.xsd"><shipTo><name/><street/><city/><state/><zip>1.2</zip></shipTo><billTo><name/><street/><city/><state/><zip>2.5</zip></billTo><items/></n1:purchaseOrder>

用到的XML开发软件:Altova   XMLSpy

---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima