XML.更好的文档类型定义.XML Schema

来源:互联网 发布:电子商务网络平台技术 编辑:程序博客网 时间:2024/05/17 04:23
  • 相对于DTD,XML Schema则是专门属于XML的文档类型定义
  • 还是专有的好

特点

  • 一致性:基本语法规则与XML一致
  • 完备性:完美支持XML各种特性
  • 规范性准确性:嗯
  • 面向对象特征:OO
  • 扩展性:DTD中没有则XML必然不能有,但XMLSchema则允许一些实现无法确定的元素出现

数据类型

  • 简单类型:仅文本或空白,不能包含子元素和属性
  • 复杂类型:包含了元素或属性或二者的类型

XMLSchema数据类型

  • 常用
    • 字符串
      • string:任意
      • normalizedString:不包含空白字符,解析的时候会自动删除
      • token:进一步删除开头结尾的空格和连续的空格
      • language:包含合法语言id的字符串
      • Name:合法XML名称
      • NCName:合法XML名称,不能包含命名空间前缀
      • ID,IDREFS……:来自DTD
    • 数值
      • float:符合IEEE标准的单精度
      • decimal:十进制实数
      • integer:十进制整数
      • long:限制最大为9223372036854775807,最小-9223372036854775808
      • int:限制最大为2147483647,最小-9223372036854775808
      • nonPositiveInteger:最大为0,即负数

简单数据类型的自定义扩展


  • 限制restriction
<xsd:simpleType>     <xsd:restriction base="BaseType">     ... facets descriptions ...    </xsd:restriction> </xsd:simpleType>

最大值或最小值

<xsd:element name="MyIntegerElement">    <xsd:simpleType>        <xsd:restriction base="xsd:integer">            <xsd:minInclusive value="0"/>            <xsd:maxInclusive value="100"/>        </xsd:restriction>    </xsd:simpleType> <xsd:element>

枚举

<xsd:simpleType>     <xsd:restriction base="xsd:string">         <xsd:enumeration value="Audi"/>         <xsd:enumeration value="Golf"/>         <xsd:enumeration value="BMW"/>     </xsd:restriction> </xsd:simpleType> 

正则表达式

<xsd:simpleType>     <xsd:restriction base="xsd:string">         <xsd:pattern value="[a-z]"/>     </xsd:restriction> </xsd:simpleType> 

限制文本内容中字符串的长度

<xsd:simpleType>     <xsd:restriction base="xsd:string">         <xsd:length value="8"/>     </xsd:restriction> </xsd:simpleType> 

限制文本内容中数值的位数和小数位数

<xsd:simpleType>    <xsd:restriction base="xsd:decimal">        <xsd:totalDigits value="4"/>        <xsd:fractionDigits value="2"/>    </xsd:restriction></xsd:simpleType> 

其他XXXXXX


  • List派生
<xsd:simpleType name="myIntegerList">    <xsd:list>        <xsd:simpleType>            <xsd:restriction base="xsd:integer">              <xsd:minInclusive value="100"/>           </xsd:restriction>        </xsd:simpleType>    </xsd:list></xsd:simpleType> 

则该类型的元素可以是这样的

<MyIntegers>100 101 102</MyIntegers> 

  • union派生
<xsd:simpleType name="myIntegerUnion">    <xsd:union>        <xsd:simpleType>            <xsd:restriction base="xsd:integer"/>        </xsd:simpleType>        <xsd:simpleType>            <xsd:restriction base="xsd:string">                <xsd:enumeration value="N/A"/>            </xsd:restriction>        </xsd:simpleType>    </xsd:union></xsd:simpleType> 

则可以是

<student2>70 80 N/A</student2> 

复杂类型


  • 声明。
    • name是可选的。有name则可以通过name来对元素使用,否则只能位于一个元素内部来作用于该元素
<xsd:complexType name="ComplexTypeName"> ......</xsd:complexType> 

  • 空元素(只包含属性、不包含子元素和文本内容)
<xsd:complexType name="ComplexType">    <xsd:attribute name="Att1Name" type="someSimpleType1"/>    <xsd:attribute name="Att2Name" type="someSimpleType2"/>    ......</xsd:complexType>

  • 只包含子元素,不包含文本内容(可能包含属性)
    • 容器:因为子元素是有序的,所以需要一个容器来包装子元素

xsd:sequence :有序序列

<xsd:complexType name="studentType">    <xsd:sequence>        <xsd:element name="firstname" type="xsd:string"/>        <xsd:element name="lastname" type="xsd:string"/>    </xsd:sequence></xsd:complexType><xsd:element name="student" type="studentType"/> 

xsd: choice :选择其一

<xsd:complexType name="PurchaseOrderType">    <xsd:sequence>        <xsd:choice>            <xsd:sequence>                <xsd:element name="shipTo" type="USAddress"/>                <xsd:element name="billTo" type="USAddress"/>            </xsd:sequence>            <xsd:element name="singleUSAddress" type="USAddress"/>        </xsd:choice>        <xsd:element name="items" type="Items"/>    </xsd:sequence>    <xsd:attribute name="orderDate" type="xsd:date"/></xsd:complexType><xsd:element name="PurchaseOrder" type="PurchaseOrderType"/> 

xsd:all :所有,不区分顺序

<xsd:complexType name="studentType">    <xsd:all>        <xsd:element name="firstname" type="xsd:string"/>        <xsd:element name="lastname" type="xsd:string"/>    </xsd: all></xsd:complexType><xsd:element name="student" type="studentType"/> 

  • 只包含文本内容和属性,不包含子元素
<xsd:element name="ele_name">  <xsd:complexType>    <xsd:simpleContent>      <xsd:extension base="basetype">      ......      </xsd:extension>    </xsd:simpleContent>  </xsd:complexType></xsd:element>

其实就是一个复杂类型,但复杂类型内部又声明了其内容是简单内容,这就让出了属性的控制,可以有简单类型(文本)和属性了


此时可以进一步的“扩充”简单内容

<xsd:complexType name="shoeType1">    <xsd:simpleContent>        <xsd:extension base="xsd:integer">            <xsd:attribute name="country" type="xsd:string"/>        </xsd:extension>    </xsd:simpleContent></xsd:complexType>

  • 同时包含子元素和文本(可能包含属性)
<xsd:element name="letter">    <xsd:complexType mixed="true">         <xsd:sequence>            <xsd:element name="name" type="xsd:string"/>            <xsd:element name="orderid" type="xsd:positiveInteger"/>            <xsd:element name="shipdate" type="xsd:date"/>        </xsd:sequence>        <xsd:attribute name="letter_id" type="xsd:positiveInteger"/>    </xsd:complexType></xsd:element> 

数据类型完整语法


简单类型

<simpleType    final = (#all | List of (list | union | restriction))     id = ID    name = NCName>       Content: (annotation?, (restriction | list | union))</simpleType> 
  • final表示不允许对这个类型进行的操作,但不影响自己

复杂类型

<complexType  abstract = boolean : false  block = (#all | List of (extension | restriction))   final = (#all | List of (extension | restriction))   id = ID  mixed = boolean : false  name = NCName>  Content: (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))</complexType> 

Schema的元素的属性

<element  abstract = boolean : false  block = (#all | List of (extension | restriction | substitution))   default = string  final = (#all | List of (extension | restriction))   fixed = string  form = (qualified | unqualified)  id = ID  maxOccurs = (nonNegativeInteger | unbounded)  : 1  minOccurs = nonNegativeInteger : 1  name = NCName  nillable = boolean : false  ref = QName  substitutionGroup = QName  type = QName>  Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))</element> 
  • maxOccurs和minOccurs可以限制这个元素出现的次数,无限次为“unbounded”
  • default和fixed:指定缺省值和固定值
  • ref:引用某个全局元素
<xsd:element name="person">    <xsd:complexType>        <xsd:sequence>            <xsd:element name="firstname" type="xsd:string"/>            <xsd:element name="lastname" type="xsd:string"/>        </xsd:sequence>    </xsd:complexType></xsd:element> <xsd:element name="employee">    <xsd:complexType>        <xsd:sequence>            <xsd:element name="ID" type="xsd:string"/>            <xsd:element ref="person"/>         </xsd:sequence>    </xsd:complexType></xsd:element> 
  • nillable和xsi:nil
    • nillable为false表示不允许为空,但 < elem>< /elem> 会被解释为字符串“”而不是空
    • xsi:nil则允许“”的存在
  • Substitutiongroup替换组:允许使用其他元素来替换,一般针对同个元素的多种表达,比如
<xsd:element name="name" type="xsd:string"/> <xsd:element name="姓名" substitutionGroup="name"/> 
  • abstract缺省false,如果为true表示纯虚元素,必须被替换
  • block
<xsd:complexType name="Class1" block="extension">    ......</xsd:complexType><xsd:element name=“obj” type=“Class1”/> 禁止obj元素使用Class1类型的扩展子类型的实例----------------------------------------------<xsd:complexType name="Class2" >    ......</xsd:complexType><xsd:element name="obj" type="Class2" block="extension"/><xsd:element name="anotherobj" type="Class2"/> 禁止obj元素使用Class2类型的扩展子类型的实例,但是允许anotherobj元素使用Class2类型的(扩展或限制)子类型的实例
0 0
原创粉丝点击