Xml Schema 整理笔记(2)

来源:互联网 发布:剑灵秦夕颜捏脸数据 编辑:程序博客网 时间:2024/06/11 16:30

【了解更多请参考:http://www.w3schools.com】

简单类型

简单元素

什么是简单元素:只能包含文本不能包含其他元素和属性的元素节点
形式如<xs:element name="xxx" type="yyy" />
(xxx是元素名称 yyy是文本类型)

文本可以是很多类型(以下是XML Schema内建的一些文本类型)

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

    示例
    <lastname>Refsnes</lastname>
    <age>36</age>
    <dateborn>1970-03-27</dateborn>

    <xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date"/> 

    才外我们可以为元素设置默认值(default)和限定值(fixed)
    <xs:element name="lastname" type="xs:string" default="pan"/> <xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date" fixed="1983-9-26"/>
    简单属性
    简单属性定义使用方式和简单元素差不多
    说明一个,简单属性多了一个能设置可选还是必须属性(use)
    <xs:attribute name="dateborn" type="xs:date" default="1983-9-26" use="required"/>
    上面这个属性就是包含它的元素所必需具有的
    限制元素以及属性可用的值
    <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> 
    说明:定义一个简单元素age,取值0~120的整数类型(包括两端0,120)
    <xs:element name="car">
    <xs:simpleType>  <xs:restriction base="xs:string">    <xs:enumeration value="Audi"/>    <xs:enumeration value="Golf"/>    <xs:enumeration value="BMW"/>  </xs:restriction></xs:simpleType>
    </xs:element> 
    说明:定义一个简单元素car,取值只能是枚举值(Audi,Golf,BM)
    <xs:element name="car" type="carType"/>
    <xs:simpleType name="carType">  <xs:restriction base="xs:string">    <xs:enumeration value="Audi"/>    <xs:enumeration value="Golf"/>    <xs:enumeration value="BMW"/>  </xs:restriction></xs:simpleType>
    说明:定义一个简单元素car 值类型carType(我之类为了方便理解,把它当作css里面的class属性来理解了)
    carType这个自定义值类型还可以被其他元素所使用
     
    上面示例的都是限定一个值,如果要限定一个取值范围参加下面的示例
    <xs:element name="letter">
    <xs:simpleType>  <xs:restriction base="xs:string">    <xs:pattern value="[a-z]"/>  </xs:restriction></xs:simpleType>
    </xs:element>  
    说明:定义简单元素letter取值是(a-z)中的1个字符
     
    <xs:element name="initials">
    <xs:simpleType>  <xs:restriction base="xs:string">    <xs:pattern value="[A-Z][A-Z][A-Z]"/>  </xs:restriction></xs:simpleType>
    </xs:element> 
    说明:定义简单元素initials,取值是(A-Z)中的3个字符

    类似的pattern元素里面定义了限制可取值的定义 value里面你可以使用类似正则表达式的限制方式

    参见下表定义了限制的各种限制元素

    ConstraintDescriptionenumeration定义一组可接受的枚举值fractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zerolengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zeromaxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value)maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value)maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zerominExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value)minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value)minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zeropatternDefines the exact sequence of characters that are acceptable totalDigitsSpecifies the exact number of digits allowed. Must be greater than zerowhiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) is handled


    今天无意发现了一个翻译www.w3schools.com 的中文网站 www.w3school.com.cn大家可以直接浏览
  • 原创粉丝点击