Xml之Schema XSD约束{详细}
问题:
学习Schema其他标签的定义 约束
引入的方式:
1:通过网络路径映入
<?xml version="1.0" encoding="UTF-8"?>
<Teams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.org/NewXMLSchema"
xsi:noNamespaceSchemaLocation="NewXMLSchema.xsd">
<Team>
<Teamname>Roma</Teamname>
<Country>Italy</Country>
<Member Name="AAA" Age="34" Sex="Male"/>
<Member Name="AAA" Age="34" Sex="Male"/>
</Team>
</Teams>
2:通过文件路径引入
xsi:noNamespaceSchemaLocation="{location}" # XSD文件名 本地文件
xsi:schemaLocation="{namespace} {location}" # 带全路径的XSD文件
<?xml version="1.0" encoding="UTF-8"?>
<Teams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.org/NewXMLSchema"
xsi:schemaLocation="http://www.dgwcom.com/NewXMLSchema.xsd">
<Team>
<Teamname>Roma</Teamname>
<Country>Italy</Country>
<Member Name="AAA" Age="34" Sex="Male"/>
<Member Name="AAA" Age="34" Sex="Male"/>
</Team>
</Teams>
基本格式:
targetNamespace:
在引入的时候需要写的命名空间
elementFormDefault
非全局的元素当设置为qualified时,必须添加命名空间的前缀。
非全局的元素当设置为unqualified时,不必也不能添加前缀。(使用具体元素的时候)
xmlns:xs="http://www.w3.org/2001/XMLSchema" 加上xs 那么目标元素也要写命名空间xm
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.dgwcom.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
1构建schema:
1.1 最基本的单位元素
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
# 对应元素
<element name="lastname" type="string" />
<element name="age" type="integer" />
<element name="dateborn" type="date" />
# 常用属性
<element
name="name" 元素名称
default="" 默认值
type="" 类型
fixed="" 固定值
id="" 定义id
ref 引用别的元素
maxOcuor 最大范围
min0cuor 最小范围
></element>
https://www.runoob.com/schema/schema-el-element.html
1.2 元素属性
<lastname lang="EN">Smith</lastname>
# 对应属性
<attribute name="lang" type="string" default="EN"></attribute>
<xs:attribute name="lang" type="xs:string" fixed="EN"/> //固定值
<xs:attribute name="lang" type="xs:string" use="required"/> //use 是固定是否要写的属性
1.3 simpleType 定义类型
两种构造方式
# 方式1
<element name="xx" type="agetype"></element>
<simpleType name="agetype">
<restriction base="integer">
<minInclusive value="0" />
<maxInclusive value="120" />
</restriction>
</simpleType>
# 方式2
<element name="xx222">
<simpleType>
<restriction base="integer">
<maxInclusive value="0"></maxInclusive>
<minInclusive value="120"></minInclusive>
</restriction>
</simpleType>
</element>
# 控制范围的相对性
控制范围内
<maxInclusive value="0"></maxInclusive>
<minInclusive value="120"></minInclusive>
控制范围外
<maxExclusive value=""></maxExclusive>
<minExclusive value=""></minExclusive>
例子
# 列表限定 车的类型
<simpleType name="cartype">
<restriction base="string">
<enumeration value="Audi"></enumeration>
<enumeration value="Golf"></enumeration>
<enumeration value="BMW"></enumeration>
</restriction>
</simpleType>
# 正则限定
<!-- 小写字母 -->
<simpleType name="letter">
<restriction base="string">
<pattern value="[a-z]"></pattern>
</restriction>
</simpleType>
<!-- 字母数字 -->
<simpleType name="letter">
<restriction base="string">
<pattern value="([a-z][A-Z]0-9])+"></pattern>
</restriction>
</simpleType>
# 空白字符的限定
<!-- 对空白字符的处理 -->
<simpleType name="address">
<restriction>
<whiteSpace value="preserve"></whiteSpace>
1:preserve : XML 处理器不会移除任何空白字符
2:collapse : XML 处理器将移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格
3:replace : XML 处理器将移除所有空白字符(换行、回车、空格以及制表符):
</restriction>
</simpleType>
# 对长度的限定
<!-- 对长度的限定 -->
<simpleType name="NameType">
<restriction base="integer">
<maxLength value="1"></maxLength>
<minLength value="5"></minLength>
</restriction>
</simpleType>
# 所有属性
限定 描述
enumeration 定义可接受值的一个列表
fractionDigits 定义所允许的最大的小数位数。必须大于等于0。
length 定义所允许的字符或者列表项目的精确数目。必须大于或等于0。
maxExclusive 定义数值的上限。所允许的值必须小于此值。
maxInclusive 定义数值的上限。所允许的值必须小于或等于此值。
maxLength 定义所允许的字符或者列表项目的最大数目。必须大于或等于0。
minExclusive 定义数值的下限。所允许的值必需大于此值。
minInclusive 定义数值的下限。所允许的值必需大于或等于此值。
minLength 定义所允许的字符或者列表项目的最小数目。必须大于或等于0。
pattern 定义可接受的字符的精确序列。
totalDigits 定义所允许的阿拉伯数字的精确位数。必须大于0。
whiteSpace 定义空白字符(换行、回车、空格以及制表符)的处理方式。
1.4 复合结构类型
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
# 对应实例
<element name="employee">
<complexType>
<sequence>
<element name="firstname" type="string"></element>
<element name="lastname" type="string"></element>
</sequence>
</complexType>
</element>
# 对有属性值的描述的
<!-- <shoesize country="france">35</shoesize> -->
<element name="shoesize">
<complexType>
<simpleContent>
<extension base="integer">
<attribute name="country" type="string"></attribute>
</extension>
</simpleContent>
</complexType>
</element>
# 空内容元素
<!-- <product prodid="1345" /> -->
<element name="product">
<complexType>
<attribute name="prodid" type="positiveInteger"></attribute>
</complexType>
</element>
# 有元素 又有属性
<!--
<user>
<name age='12' address=' '>Tom</name>
</user>
-->
<element name="user">
<complexType>
<sequence>
<element name="name" type="string">
<complexType>
<attribute name="age" type="int"></attribute>
<attribute name="address" type="string"></attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
## 对上面的还有种写法
<element name="name">
<complexType>
<complexContent>
<extension base="string">
<attribute name="age" type="int"/>
<attribute name="address" type="string"/>
</extension>
</complexContent>
</complexType>
</element>
# 复合类型的写法 基本上 complexType simpleType 都可以单独包装出来 作为一个共有类型
<element name="letter">
<complexType mixed="true">
<sequence>
<element name="name" type="string"></element>
<element name="orderid" type="positiveInteger"></element>
<element name="shipdate" type="date"></element>
</sequence>
</complexType>
</element>
# 多个元素嵌套
<element name="Persons">
<complexType>
<sequence>
<element name="user" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="name" type="string"/>
<element name="age" type="string" />
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
1.5指示器
order顺序指示器
1: all 不按照给出的顺序出现, 但是每个元素 可出现一次 或者不出现
2: sequence 必须按照给出的顺序出现
3: choise 多个ele选一
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Occurrence 指示器
对于所有的 "Order" 和 "Group" 指示器(any、all、choice、sequence、group name 以及 group reference),其中的 maxOccurs 以及 minOccurs 的默认值均为 1。
<maxOccurs> 指示器可规定某个元素可出现的最大次数:
<minOccurs> 指示器可规定某个元素能够出现的最小次数:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Group 指示器: 分为元素组 属性组 目的: 对某系元素 属性进行封装
<group name=""></group> // 元素组
<attributeGroup name=""></attributeGroup> //属性组 # 元素组
# 属性组
<attributeGroup name="telAtt">
<attribute name="tel" type="integer"></attribute>
</attributeGroup> <group name="personEle">
<sequence>
<element name="address" type="string" />
</sequence>
</group> <element name="Persons">
<complexType>
<sequence>
<element name="user" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="name" type="string" />
<element name="age" type="string">
<complexType>
<attributeGroup ref="telAtt"></attributeGroup>
</complexType>
</element>
<group ref="personEle"></group>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
1.6 扩展元素 属性
分为元素扩展<any> 属性扩展<anyAttribute>
# Person 中扩展一个child
<xs:element name="person"> //
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
<xs:element name="children">
<xs:complexType>
<xs:sequence>
<xs:element name="childname" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element> //以上两个xsd必须定义 # 具体使用
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com children.xsd">
<person>
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<children>
<childname>Cecilie</childname>
</children>
</person> <person>
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons> # 参数扩展 姓名中扩展性别
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
<xs:attribute name="gender"> //扩展的文件
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
## 具体例子
<person gender="female">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
<person gender="male">
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
1.7 元素替换与阻止
# 替换的例子
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/> //navn替换 name
#阻止替换的例子
<xs:element name="name" type="xs:string" block="substitution"/> // 被阻止替换
<xs:element name="navn" substitutionGroup="name"/>
# 替换后使用的 都是有效的
<customer>
<name>John Smith</name>
</customer>
或类似这样:
<kunde>
<navn>John Smith</navn>
</kunde>
2:了解一些类型
2.1 时间类型
# 1
<xs:element name="start" type="xs:date"/>
文档中的元素看上去应该类似这样:
<start>2002-09-24</start>
# 2
<xs:element name="start" type="xs:time"/>
文档中的元素看上去应该类似这样:
<start>09:00:00</start>
# 3
<xs:element name="startdate" type="xs:dateTime"/>
文档中的元素看上去应该类似这样:
<startdate>2002-05-30T09:00:00</startdate>
# 4 持续时间 不建议使用 xs:duration
问法规则:
P 表示周期(必需)
nY 表示年数
nM 表示月数
nD 表示天数
T 表示时间部分的起始 (如果您打算规定小时、分钟和秒,则此选项为必需)
nH 表示小时数
nM 表示分钟数
nS 表示秒数
<xs:element name="period" type="xs:duration"/>
文档中的元素看上去应该类似这样:
<period>P5Y</period>
上面的例子表示一个 5 年的周期。
或者类似这样:
<period>P5Y2M10D</period>
上面的例子表示一个 5 年、2 个月及 10 天的周期。
或者类似这样:
<period>P5Y2M10DT15H</period>
上面的例子表示一个 5 年、2 个月、10 天及 15 小时的周期。
或者类似这样:
<period>PT15H</period>
2.2 数值数据
@# 1 xs:decimal
<xs:element name="prize" type="xs:decimal"/>
文档中的元素看上去应该类似这样:
<prize>999.50</prize>
或者类似这样:
<prize>+999.5450</prize>
或者类似这样:
<prize>-999.5230</prize>
或者类似这样:
<prize>0</prize>
或者类似这样:
<prize>14</prize>
# 2
<xs:element name="prize" type="xs:integer"/>
文档中的元素看上去应该类似这样:
<prize>999</prize>
或者类似这样:
<prize>+999</prize>
或者类似这样:
<prize>-999</prize>
或者类似这样:
<prize>0</prize
2.3 布尔 二进制数据
# 1 xs:boolean
<xs:attribute name="disabled" type="xs:boolean"/>
文档中的元素看上去应该类似这样:
<prize disabled="true">999</prize>
注意: 合法的布尔值是 true、false、1(表示 true) 以及 0(表示 false)。
# 2 xs:hexBinary
- base64Binary (Base64 编码的二进制数据)
- hexBinary (十六进制编码的二进制数据)
<xs:element name="blobsrc" type="xs:hexBinary"/>
文档中的元素看上去应该类似这样:
<name>0x16</name>
# 3
下面是一个关于某个 scheme 中 anyURI 声明的例子:
<xs:attribute name="src" type="xs:anyURI"/>
文档中的元素看上去应该类似这样:
<pic src="http://www.w3schools.com/images/smiley.gif" />
最后:参考手册https://www.runoob.com/schema/schema-elements-ref.html 这些已久足够了

Xml之Schema XSD约束{详细}的更多相关文章
- 怎样用Google APIs和Google的应用系统进行集成(8)----怎样把Google Blogger(博客)的JSON Schema转换成XML的Schema(XSD)?
在Google RESTFul API中,Google Blogger API(Google博客API)应该和我们的生活离得近期:由于差点儿非常多人每天都在看博客,都在写博客,都听说过博客.在前面的G ...
- 怎样用Google APIs和Google的应用系统进行集成(5)----怎样把Google Tasks的JSON Schema转换成XML的Schema(XSD)?
前面说了一些Google API的介绍,可是在实际的开发其中,我们可能须要把Google RESTful API返回的JSON数据转换成XML数据输入到第三方系统,这在企业应用集成里面很的常见. 那么 ...
- xml语法、DTD约束xml、Schema约束xml、DOM解析xml
今日大纲 1.什么是xml.xml的作用 2.xml的语法 3.DTD约束xml 4.Schema约束xml 5.DOM解析xml 1.什么是xml.xml的作用 1.1.xml介绍 在前面学习的ht ...
- XML的Schema约束
XSD文档至少要包含:schema根元素和XML模式命名空间的定义.元素定义.需要注意的是XSD中必须定义一个且只能定义一个schema根元素,根元素中包括模式的约束,XML模式命名空间的定义,其他命 ...
- xml的schema约束(Java)
1.schema约束 *dtd语法:<!ELEMENT 元素名称 约束> schema符合xml的语法,是xml语句. 一个xml文件中可以有多个schema,多个schema使用名称空间 ...
- 做 Excel 的 XML schema.xsd
做 Excel 的 XML schema.xsd 因为需要将 BOM Cost 归档并使用 Git 管理,原来 XLSX 是二进制的,使用 Git 管理并不方便. 于是有了想用 XML 来管理想法,在 ...
- MyEclipse XML & XML架构教程:XML Schema (XSD)编辑器
[MyEclipse CI 2019.4.0安装包下载] 1. MyEclipse中的XSD编辑 本文档介绍MyEclipse XML Schema(XSD)编辑器中的一些可用函数.XML Schem ...
- XML的应用 ---- 从一个范例看xml数据、xsd验证、xslt样式
从一个范例看XML的应用 引言 如果你已经看了Asp.Net Ajax的两种基本开发模式 这篇文章,你可能很快会发现这样一个问题:在那篇文章的方式2中,客户端仅仅是发送了页面上一个文本框的内容到服务端 ...
- 在XML里的XSD和DTD以及standalone的使用2----具体使用详解
如何定义XSD并在XML中使用XSD 同时XSD可以对XML中的格式进行约束,当约束失败时给出提示. 下面以下使用VS2010为平台进行演示. 1.新建一个项目,然后在项目中添加xml架构文件(.xs ...
随机推荐
- 高维数据Lasso思路
海量数据的特征工程中, 如果数据特征维度达到几千乃至上万 常规的lasso很容易失效 这里介绍几种泛义lasso,是在实际数据处理中常用的 迭代与分块思路/分组的使用(有兴趣的同学可自行实践一下) 1 ...
- Centos7_sl命令跑火车
一.更新yum源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo 二.安装sl命令 yum ...
- 附007.Kubernetes ABAC授权
一 ABAC 1.1 ABAC授权 基于属性的访问控制(ABAC)定义了访问控制范例,通过使用将属性组合在一起的策略向用户授予访问权限. 使用--authorization-policy-file=S ...
- JS原型链与instanceof底层原理
一.问题: instanceof 可以判断一个引用是否属于某构造函数: 另外,还可以在继承关系中用来判断一个实例是否属于它的父类型. 老师说:instanceof的判断逻辑是: 从当前引用的proto ...
- vue element-ui父列表和子列表同时出现时的bug
在项目中遇到这样的问题 当第一个父列表下的子列表选择了1,切换到第二个父列表的时候,也会默认选择1 我最开始是计划通过修改子列表的default-active为-1,结果不行 后来发现出现这个问题的原 ...
- 《移动WEB前端高级开发实践@www.java1234.com》——3
React Flux: Flux将一个应用分成四个部分. View: 视图层 Action(动作):视图层发出的消息(比如mouseClick) Dispatcher(派发器):用来接收Actions ...
- Glide生命周期原理
本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/uTv44vJFFJI_l6b5YKSXYQ作者:连凌能 Android App中图片的展示是很 ...
- Flutter竟然发布了1.5版本!!!!
2018年2月,Flutter推出了第一个Beta版本,在2018年12月5日,Flutter1.0版本发布,当时用了用觉得这个东西非常好用,对于当时被RN搞的头皮发麻的我来说简直是看到了曙光.而在昨 ...
- 工作笔记 之 Python应用技术
python socket编程详细介绍 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket,建立网络通信连接至少要一对端口号(socket). Socket本质是 ...
- C#爬虫例子
公司需要抓取新闻,每次手动复制粘贴新闻,太麻烦了,业务人员就提出了要求,需要程序实现自动抓取新闻,因此就写了这个简单的爬虫程序. Html Agility Pack库 这是一个.NET下的HTML解析 ...