打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java程序员从笨鸟到菜鸟之(二十五)XML之Schema验证
userphoto

2016.06.25

关注

4.schema的数据类型

1.基本类型

 

2.扩展数据类型

3.数据类型的特性

二:schema的元素类型

1.schema元素:

作用:包含已经定义的schema 

用法: 

·属性

–xmlns –targetNamespace 

2.element元素作用:声明一个元素 

属性: –name –type –ref –minOccurs –maxOccurs 

–substitutionGroup –fixed –default 

示例:

源码打印

  1. xs:element name='cat' type='xs:string'/>  

  2.   

  3. xs:element name='dog' type='xs:string'/>  

  4.   

  5. xs:element name='pets'>  


3.group元素

作用:把一组元素声明组合在一起,以便它们能够一起被复合类型应用 

·属性:name/ref 

示例:

源码打印

  1. xs:group name='myGroupOfThings'>   

  2.   

  3. xs:sequence>   

  4.   

  5. xs:element ref='thing1'/>   

  6.   

  7. xs:element ref='thing2'/>   

  8.   

  9. xs:sequence>   

  10.   

  11. xs:group>   


4.attribute元素

作用:声明一个属性 

·属性:name/type/ref/use 

·示例: 

源码打印

  1. xs:complexType name='myComplexType'>   

  2.   

  3. xs:attribute name='mybaseattribute' type='xs:string' use='required'/>   

  4.   

  5. xs:complexType>   


5.attributeGroup元素

作用:把一组属性声明组合在一起,以便可以被复合类型应用

.属性:name/ref

.示例:

源码打印

  1. xs:attributeGroup name='myAttributeGroup'>  

  2.   

  3. xs:attribute name='someattribute1' type='xs:integer'/>  

  4.   

  5. xs:attribute name='someattribute2' type='xs:string'/>  

  6.   

  7. xs:attributeGroup>  


6.simpleType元素

作用:定义一个简单类型,它决定了元素和属性值的约束和相关信息

.属性:name

.内容:应用已经存在的简单类型,三种方式:

restrict→限定一个范围

list→从列表中选择

union→包含一个值的结合

1.子元素为: 定义一个约束条件 

2.子元素为:从一个特定数据类型的集合中选择定义一个简单类型元素

3.子元素为:从一个特定简单数据类型的集合中选择定义一个简单类型元素

示例:

源码打印

  1. xs:restriction base='xs:positiveInteger'>  

  2.   

  3. xs:enumeration value='46'/>  

  4.   

  5. xs:enumeration value='52'/>  

  6.   

  7. xs:enumeration value='55'/>  

  8.   

  9. xs:restriction>  

  10.   

  11. xs:simpleType>  

  12.   

  13. xs:simpleType name='mountainbikesize'>  

  14.   

  15. xs:restriction base='xs:string'>  

  16.   

  17. xs:enumeration value='small'/>  

  18.   

  19. xs:enumeration value='medium'/>  

  20.   

  21. xs:enumeration value='large'/>  

  22.   

  23. xs:restriction>  

  24.   

  25. xs:simpleType>  

  26.   

  27. xs:schema>  


7.complexTyep类型

作用:定义一个复合类型,它决定了一组元素和属性值的约束和相关信息 

·属性:name 

·示例: 

源码打印

  1. xs:complexType name='internationalShoeSize'> xs:simpleContent>   

  2.   

  3. xs:extension base='xs:decimal'>   

  4.   

  5. xs:attribute name='sizing' type='xs:string'/> xs:extension>   

  6.   

  7. xs:simpleContent>   

  8.   

  9. xs:complexType>   

  10.   

  11. xs:element name='myShoeSize' type='internationalShoeSize'/>  


simpleType元素和complexTyep类型的区别(重要)

simpleType类型的元素中不能包含元素或者属性。 

·当需要声明一个元素的子元素和/或属性时,用complexType; 

·当需要基于内置的基本数据类型定义一个新的数据类型时,用simpleType。 

8.simplecontent元素

作用:应用于complexType,对它的内容进行约束和扩展

9.choice元素

作用:允许唯一的一个元素从一个组中被选择

.属性:minOccurs/maxOccurs

10.sequence元素

作用:给一组元素一个特定的序列

一个完整的schema样例:

源码打印

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

  2.   

  3. targetNamespace='http://tempuri.org/po.xsd'   

  4.   

  5. xmlns='http://tempuri.org/po.xsd' >   

  6.   

  7. xs:element name='purchaseOrder' type='PurchaseOrderType'/>   

  8.   

  9. xs:element name='comment' type='xs:string'/>   

  10.   

  11. xs:complexType name='PurchaseOrderType'>   

  12.   

  13. xs:sequence>   

  14.   

  15. xs:element name='shipTo' type='USAddress'/>   

  16.   

  17. xs:element name='billTo' type='USAddress'/>   

  18.   

  19. xs:element ref='comment' minOccurs='0'/>   

  20.   

  21. xs:element name='items' type='Items'/>   

  22.   

  23. xs:sequence>   

  24.   

  25. xs:attribute name='orderDate' type='xs:date'/>   

  26.   

  27. xs:complexType>   

  28.   

  29. xs:complexType name='USAddress'>  

  30.   

  31. xs:sequence>  

  32.   

  33. xs:element name='name' type='xs:string'/>  

  34.   

  35. xs:element name='street' type='xs:string'/>  

  36.   

  37. xs:element name='city' type='xs:string'/>  

  38.   

  39. xs:element name='state' type='xs:string'/>  

  40.   

  41. xs:element name='zip' type='xs:decimal'/>  

  42.   

  43. xs:sequence>  

  44.   

  45. xs:attribute name='country' type='xs:NMTOKEN'  

  46.   

  47. fixed='US'/>  

  48.   

  49. xs:complexType>  

  50.   

  51. xs:complexType name='Items'>   

  52.   

  53. xs:sequence>   

  54.   

  55. xs:element name='item' minOccurs='0' maxOccurs='unbounded'>   

  56.   

  57. xs:complexType>   

  58.   

  59. xs:sequence>   

  60.   

  61. xs:element name='productName' type='xs:string'/>   

  62.   

  63. xs:element name='quantity'>   

  64.   

  65. xs:simpleType>   

  66.   

  67. xs:restriction base='xs:positiveInteger'>   

  68.   

  69. xs:maxExclusive value='100'/>   

  70.   

  71. xs:restriction>   

  72.   

  73. xs:simpleType>   

  74.   

  75. xs:element>   

  76.   

  77. xs:element name='USPrice' type='xs:decimal'/>   

  78.   

  79. xs:element ref='comment' minOccurs='0'/>   

  80.   

  81. xs:element name='shipDate' type='xs:date' minOccurs='0'/>   

  82.   

  83. xs:sequence>   

  84.   

  85. xs:attribute name='partNum' type='SKU' use='required'/>   

  86.   

  87. xs:complexType>   

  88.   

  89. xs:element>   

  90.   

  91. xs:sequence>   

  92.   

  93. xs:complexType>   

  94.   

  95.    

  96.   

  97. xs:simpleType name='SKU'>   

  98.   

  99. xs:restriction base='xs:integer'>   

  100.   

  101. xs: minInclusive=“2”/>   

  102.   

  103. xs:maxInclusivexs:maxInclusive=“10”>   

  104.   

  105. xs:restriction>   

  106.   

  107. xs:simpleType>   

  108.   

  109. xs:schema>   


Schema总结:

Schema是另一种文档类型定义,它遵循XML的语言规范。

.Schema是可扩展的,支持命名空间;

.Schema支持更多的数据类型与元素类型;

.Schemaelement声明元素,用attribute声明元素的属性;

.SchemasimpleType定义简单类型,用complexType定义复杂类型。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用W3C XML Schema
XML开发笔记 — 总结
XMLSchema全接触
理解XML Schema: XML Schema初步(II)
schema用法
WSDL浅析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服