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 ...
随机推荐
- 12C-使用跨平台增量备份减少可移动表空间的停机时间 (Doc ID 2005729.1)
12C - Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 20057 ...
- Samba共享文件
1 安装samba yum install -y samba* 2 添加用户 useradd smbuser 3 设置共享文件用户的密码 smbpasswd -a smbuser 4 创建公共共享文件 ...
- 附007.Kubernetes ABAC授权
一 ABAC 1.1 ABAC授权 基于属性的访问控制(ABAC)定义了访问控制范例,通过使用将属性组合在一起的策略向用户授予访问权限. 使用--authorization-policy-file=S ...
- getattribute方法,Python属性访问拦截器的用法
__getattribute__()方法是属性访问时的拦截器,每当访问属性的时候,会先执行这个方法,然后再执行访问属性的操作步骤,可以用来记录属性访问的log.代码示例如下: class Itca ...
- C#的语法----程序结构(1)
接下来的内容是整个C#学习的脉络,它将各个知识点串联了起来,是整个C#的重点,所以篇幅较长. 首先,我们类比一下PLC和C#执行代码的方式,其实不难发现都是顺序扫描,以Main为程序入口,从上到下一行 ...
- 面试知识点 html css(经常更新)
1.HTML5语义化 什么是语义化 用合理.正确的标签来展示内容,比如h1~h6定义标题 好处 易于用户阅读,样式丢失的时候能让页面呈现清晰的结构. 有利于SEO,搜索引擎根据标签来确定上下文和各个关 ...
- router-view 与 动态组件 区别
提问:router-view 可以页面跳转,使用 is特性 也可以进行页面跳转,有什么区别? 参考链接 https://segmentfault.com/q/1010000010750059
- ASP.NET MVC教程二:ASP.NET MVC应用程序结构详解
在上一篇文章中,讲解了一些MVC的概念,并且创建了第一个ASP.NET MVC项目,这篇文章将讲解ASP.NET MVC程序中的代码解构,新创建的MVC应用程序解构如下图所示: 一.App_Data ...
- C# 同步转异步 TaskCompletionSource
当我们遇到一些异步执行又无法等待时的逻辑,比如动画的执行. 而业务上又需要等待逻辑的完成,再去处理后续的操作.这时需要转成异步方法 如下,同步执行一个动画后,再输出日志: private async ...
- (转)GitHub Desktop 拉取 GitHub上 Tag 版本代码
转自:GitHub Desktop 拉取 GitHub上 Tag 版本代码 一直在使用 GitHub Desktop 图形化 git 管理工具,统一项目框架版本时需要切换到ThinkPHP Tag 分 ...