zoukankan      html  css  js  c++  java
  • xml schema杂谈

    有一些场景,我们需要写xml,又需要对内容进行约束,比如智能输入某个值范围,只能写入固定值

    这个时候我们就需要xml schema

    这个,百度解释为 

    XML Schema 的作用是定义 XML 文档的合法构建模块,类似 DTD。

    我的个人理解是对xml进行约束,以及自动格式验证的一套结构体

    网上的例子很多,我解释一下我的一点经验

    内容包裹在一组xs:schema里面

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    </xs:schema>

    自定义元素格式如下

    <xs:element name="Table">

    Table是我们自定义的元素名称

    我们要自定义的类型,标签为xs:simpleType。

    可以是全局类型,也可以是局部类型

    全局的类型是一个通用的类型,局部类型是针对某一个元素的内容验证

    全局的类型在xs:schema标签内,在最上层的xs:element之上,如果写错了顺序,编译器会提示格式错误的

    示例如下

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:simpleType>
      </xs:simpleType>
      <xs:element>
      </xs:element>
    <xs:schema>

    标签xs:simpleType内name或者id两个至少得一个有值,name是他的名称,必须的id是选填的唯一ID,类似html

    比如我们有个自定义类型叫Status,类型是short

      <xs:simpleType name="Status">
        <xs:restriction base="xs:short">
          <xs:enumeration value="1"/>
          <xs:enumeration value="2"/>
        </xs:restriction>
      </xs:simpleType>

    这段代码的意思是,类型short,精确查找内容,只能1和2两个值允许输入

    比如我们有个类型Level,枚举类型,定为short,可以1到10,如果精确找值,就很麻烦了

    所以我们用区间元素 xs:pattern 替代掉 xs:enumeration,这个是正则表达式判断 

      <xs:simpleType name="Status">
        <xs:restriction base="xs:short">
            <xs:pattern value="[1-9]"/>
        </xs:restriction>
      </xs:simpleType>

    也可以用最大值,最小值判断

      <xs:simpleType name="Status">
        <xs:restriction base="xs:short">
            <xs:minExclusive value="1"/>
            <xs:maxInclusive value="9"/>
        </xs:restriction>
      </xs:simpleType>

    类似的验证元素还有

    具体对照如下

    enumeration   定义可接受值的一个列表
    fractionDigits  定义所允许的最大的小数位数。必须大于等于0。
    length             定义所允许的字符或者列表项目的精确数目。必须大于或等于0。
    maxExclusive 定义数值的上限。所允许的值必须小于此值。
    maxInclusive  定义数值的上限。所允许的值必须小于或等于此值。
    maxLength     定义所允许的字符或者列表项目的最大数目。必须大于或等于0。
    minExclusive  定义数值的下限。所允许的值必需大于此值。
    minInclusive   定义数值的下限。所允许的值必需大于或等于此值。
    minLength      定义所允许的字符或者列表项目的最小数目。必须大于或等于0。
    pattern            定义可接受的字符的精确序列。
    totalDigits        定义所允许的阿拉伯数字的精确位数。必须大于0。
    whiteSpace     定义空白字符(换行、回车、空格以及制表符)的处理方式

    元素和属性可以自由组合,元素和属性都在元素内的xs:complexType下一层,不同的是,元素内的元素只能在xs:sequence之内

    属性的标签为xs:attribute,属性和元素的值类型就是type元素,输入会有很多默认的类型,也可以输入我们自定义的全局类型

    元素和属性的差异是,属性写在元素的自属性里,输入会自动提示,可以不填

    元素必填,是上一级元素的下级标签

    元素需要配置属性maxOccurs="unbounded"

    这样就可以重复元素了

    最后附上实例

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:simpleType name="ModelType">
        <xs:restriction base="xs:string">
          <xs:enumeration value="Id"/>
          <xs:enumeration value="Id64"/>
          <xs:enumeration value="Status"/>
          <xs:enumeration value="nvarchar"/>
          <xs:enumeration value="int"/>
          <xs:enumeration value="decimal" />
          <xs:enumeration value="datetimeoffset" />
        </xs:restriction>
      </xs:simpleType>
      
      <xs:element name="Table">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Field" maxOccurs="unbounded">
              <xs:complexType>
                <xs:attribute name="Name" type="xs:string" />
                <xs:attribute name="Describe" type="xs:string" />
                <xs:attribute name="Type" type="ModelType" />
                <xs:attribute name="Length" type="xs:int" />
                <xs:attribute name="Precision" type="xs:int" />
                <xs:attribute name="IsNull" type="xs:boolean" />
              </xs:complexType>                  
            </xs:element>
            <xs:element name="x">
              <xs:simpleType>
                <xs:restriction base="xs:int">
                  
                </xs:restriction>
              </xs:simpleType>
            </xs:element>
          </xs:sequence>      
          <xs:attribute name="Name" type="xs:string" use="required" />
          <xs:attribute name="Describe" type="xs:string" use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>

    调用的例子

    <?xml version="1.0" encoding="utf-8" ?>
    <Table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="XMLSchema.xsd"
     Name="AgentUser" Describe="代理用户表"
    >
      <Field Name="Id" Type="int" Describe="ID" IsNull="false" Length="0" Precision="0" />
      <Field Name="Code" Type="string" Describe="编码" IsNull="false" Length="255" Precision="0" />
    </Table>

    Name为Code这行,编辑器会提示内容无效

  • 相关阅读:
    AcWing 1135. 新年好 图论 枚举
    uva 10196 将军 模拟
    LeetCode 120. 三角形最小路径和 dp
    LeetCode 350. 两个数组的交集 II 哈希
    LeetCode 174. 地下城游戏 dp
    LeetCode 面试题 16.11.. 跳水板 模拟
    LeetCode 112. 路径总和 递归 树的遍历
    AcWing 1129. 热浪 spfa
    Thymeleaf Javascript 取值
    Thymeleaf Javascript 取值
  • 原文地址:https://www.cnblogs.com/NCoreCoder/p/9892094.html
Copyright © 2011-2022 走看看