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这行,编辑器会提示内容无效
xml schema杂谈的更多相关文章
- XML Schema and XMLspy notes
Introduction An xml documents consists of elements, attributes and text. There are two structures in ...
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...
- XML Schema命名空间解析
URI Web中汇集了各种资源.资源可以是具有标识的任何事物, 如文档. 文件. 菜单项. 计算机. 服务等, 甚至可以包括人. 组织和概念[BernersLee 1998].在Web体系结构中, ...
- 【转】XSD (xml Schema Definition)
来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1. 定义一个Xml文档中都有什么元 ...
- C#与XML Schema的问题
http://bbs.csdn.net/topics/50493564 weileily: 用XmlSchema.Read方法读取了一个xsd文件,请问如何遍历检索器中的ComplexType与Sim ...
- XML Schema的基本语法(转)
XML Schema的基本语法(转) XSDL(XML Schema定义语言)由元素.属性.命名空间和XML文档种的其他节点构成的. 一.XSD中的元素 XSD文档至少要包含:schema根元素和XM ...
- Xml Schema:C#访问在complextype中插入新元素
最近用c#写Xml Schema代码,找了很久也找不到如何在已有的complextype中插入新的element,最后我充分发挥自己的聪明才智,哈哈,终于从...中找到了灵感. XmlSchemaSe ...
- [BTS] System.Xml.Schema.XmlSchemaException: The complexType has already been declared when generate IDoc schema.
I use wcf-sap adapter for generate the schema of IDoc that named "YHREMPMASTER". but throw ...
- onfiguration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...
随机推荐
- 测试必备工具之抓包神器 Charles 如何抓取 https 数据包?
之前发过一篇文章讲解了Charles抓包工具的基本使用(有需要的小伙伴可以去看上一篇文章), 讲的数据包主要是http协议,大家可以看到数据包并直接显示具体详细的内容: 但是如果抓到的是https的 ...
- pythonl操作数据库
目录 今日内容详细 Navicat软件 提示 练习题 pymysql模块 sql注入 navicat可视化界面操作数据库 数据库查询题目讲解(多表操作) python如何操作MySQL(pymysql ...
- 静态方法中注入bean
@Componentpublic class ScriptExecuteContent { @Autowired private static SignRepository signRepositor ...
- Scala 面向对象(十三):隐式转换和隐式参数
隐式转换的实际需要=>指定某些数据类型的相互转化 1 隐式函数基本介绍 隐式转换函数是以implicit关键字声明的带有单个参数的函数.这种函数将会自动应用,将值从一种类型转换为另一种类型 隐式 ...
- 机器学习实战基础(三十):决策树(三) DecisionTreeRegressor
DecisionTreeRegressor class sklearn.tree.DecisionTreeRegressor (criterion=’mse’, splitter=’best’, ma ...
- python 装饰器(八):装饰器基础(四)types.MethodType的作用
1 types.MethodType的作用—添加实例方法 import types class cla(object): def __init__(self, name, age): self.nam ...
- Python之 爬虫(十二)关于深度优先和广度优先
网站的树结构 深度优先算法和实现 广度优先算法和实现 网站的树结构 通过伯乐在线网站为例子: 并且我们通过访问伯乐在线也是可以发现,我们从任何一个子页面其实都是可以返回到首页,所以当我们爬取页面的数据 ...
- easyui获取datagrid中的某一列的所有值
function getCol(){ var rows = $("#dg").datagrid("getRows"); var total = "&q ...
- 简易防止U盘中毒
1.将U盘插入电脑,打开u盘 2.在U盘里面新建一个文本文档,将文本文档重命名autorun.inf保存完成. 3.为了防止误删次文件可以将属性设为影藏,就完成了.
- Python Ethical Hacking - TROJANS Analysis(1)
TROJANS A trojan is a file that looks and functions as a normal file(image, pdf, song ..etc). When e ...