• XML Schema 语法


    简单的类型:

    1:简单的类型:指只能包含文本的内容,不能够包含子元素,也没有属性的元素

    格式是:

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

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

    <xs:element name = "gender"  type = "xs:boolean"/>

    <xs:element name="性别"> 
     <xs:simpleType> 
      <xs:restriction base="xs:string"> 
       <xs:enumeration value="女"/> 
       <xs:enumeration value="男"/> 
      </xs:restriction> 
     </xs:simpleType> 
    </xs:element>

    2:属性:所有的元素属性均被声明为简单的类型,只有负责的类型的元素才可以拥有属性。

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

    例子:<xs:attribute name = "lang" type = "xs:string" />

    所有的属性默认都是可选的,我们可以通过使用use关键字明确的指出是可选的或者必需的。

    <xs:attribute name = "lang" type = "xs:string" use = "required" />

    <xs:attribute name = "lang" type = "xs:string" use = "optional" />

    我们可以通过使用default或fixed为简单的类型(简单元素,属性)指定默认值或者固定值。

    <xs:element name = "color" type = "xs:string" default = "red" />

    <xs:element name = "lang" type = "xs:string" fixed = "On" />

    对简单类型的约束

    约束                                             含义

    enumeratiion                                定义允许值的枚举

    fractionDigits                               指定最多允许的小数位数(必须大于或者等于零)

    length                                        精确指定允许的最大字符长度

    maxExclusive                              指定允许的最大的数值,必须小于该值。

    maxInclusive                              指定允许的最大的数值,必须小于或者等于该值。

    maxLength                                指定允许的最大字符长度

    minExclusive                              指定允许的最小数值,必须大于该值。

    minInclusive                               指定允许的最小数值,必须大于或者等于值。

    minLength                                 指定允许的最小字符长度

    pattern                                     指定允许值的模式,类似于正则表达式

    totalDigits                                 精确的指定数字的个数

    whiteSpace                              处理空白(保留:preserve 替换:replace :合并:collapse)

    复杂类型:

    复杂类型指包含其他元素/属性的元素类型

    <message>

      <to>rose</to>

      <from>alex</from>

      <body>Hi,My Girl!</body>

    </message>

    在上面的例子中,元素message就是一个复杂类型的元素,我们在Scheam中这样描述:

    <xs:element name = "message">

      <xs:complexType>

        <xs:sequence>

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

          <xs:elelment name = "from" type = "xs:string" />

        </xs:sequence><xs:sequence>

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

          <xs:elelment name = "from" type = "xs:string" />

        </xs:sequence>

      </xs:complexType>

    </xs:element>

    注意元素to.from.body包含在<xs:sequence></xs:sequence>中,表明这些元素必须按照定义的顺序出现在你的xml文件中。

    当然,message 元素可以包含一个type属性,指定我们定义的复杂类型

    像这样:

    <xs:element name = "message" type = "msg" />

    <xs:complexType name  = "msg">

      <xs:sequence>

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

          <xs:elelment name = "from" type = "xs:string" />

        </xs:sequence>

       </xs:complexType>

    复杂类型和简单类型之间最根本的区别是:复杂类型内容中可以包含其他的元素,可以带有属性(Attribute),但简单的类型不能包含子元素,也不能带有任何的属性。

    3:如何描述空元素,比如:<product id = "productId"/>

    因为是空元素,所以不包含子元素,同时由于包含属性,用attribute定义,

    这样:

    <xs:element name = "product">

      <xs:complexType>

        <xs:attribute name = "productId" type = "xs:positiveInteger"/>

      </xs:complexType>

    </xs:element>

    4:如何描述只有简单内容(文本、属性)的元素,比如:

    <message date = "206-2-9">Hi,My Love!</message>

    由于只包含简单的内容,所以我们在元素内容定义的外面用simpleContent指出,当描述简单的内容时候,我们需要在简单的内容里使用extension或者restriction来描述内容的数据类型,像这样:

    <xs:element name = "message">

      <xs:complexType>

        <xs:simpleContent>

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

            <xs:attribute name = "date" type = "xs:date"/>

          </xs:extension>

        </xs:simpleContent>

      </xs:complexType>

    </xs:element>

    5:如何定义混合类型,比如:

    <message>

      This message comes from <from>Alex</from>

    </message>

    对于这种情况:我们需要在complexType中使用属性mixed = "true"指出,以下是schema:

    <xs:element name = "message'>

      <xs:complexType mixed = "true">

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

      </xs:complexType>

    </xs:element>

     在xml的schema中,有3类共7中指示器(indicator)

    1:定义在元素如何出现,包括all,sequence,choice这三个

    all:默认值,不限制子元素的出现的顺序,每个元素必须出现且只能出现一次。

    <xs:element name = "person">

      <xs:complexType>

        <xs:all>

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

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

        </xs:all>

      </xs:complexType>

    </xs:element>

     sequence:子元素在xml文件中按照schema定义的顺序出现。

    choice:二个或者多个子元素中仅出现一次。

    <xs:element name = "person">

      <xs:complexType>

        <xs:choice>

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

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

        </xs:choice>

      </xs:complexType>

    </xs:element>

    2:二次限定类,包括minOccrus和maxOccrus,前者指定最少出现次数,后者指定最多出现的次数。

    <xs:element name = "person">

      <xs:complexType>

        <xs:squence>

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

          <xs:element name = "secondname" type = "xs:string" maxOccrus = "10" minOccrus = "0"/>

        </xs:squence>

      </xs:complexType>

    </xs:element>

    3:组限定:包括Group和attributeGroup,用来定义一组相关的元素

    <xs:group>

      <xs:sequence>

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

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

      </xs:sequence>

    </xs:attributeGroup>

    <xs:group>

      <xs:sequence>

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

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

      </xs:sequence>

    </xs:attributeGroup>

  • 相关阅读:
    网站添加手机短信功能
    FileWriter的正确使用,请及时关闭流
    myeclipse9.0没有提示
    Integer.getInteger,are you kinding me? 好吧, 我还是没怎么弄明白,求高人解答。。。
    构造方法充当临时对象&Calendar的使用
    关于Spring IOC的一点个人理解
    在JAVA中使用GUID
    动态生成table下的<tr>标签不显示
    MD5加密实例
    String,StringBuffer 和 StringBuilder 的区别
  • 原文地址:https://www.cnblogs.com/xlhblogs/p/2869401.html
Copyright © 2020-2023  润新知