XML Schema使用技巧——unique
XML Schema使用技巧——unique
XML Scheam允许指定某个元素或属性的值在一定得范围内是唯一的。为了指定元素或属性值的唯一性,可以使用<xs:unqiue>元素,使用方法为选择一组xml示例元素作为范围,然后依据上下文关系定义一个field,这里的field就是要指定的唯一性的元素或属性。
1、元素唯一性
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.test.com" xmlns:tn="http://www.test.com"> <xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="bookName" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="bookUnique">
<xs:selector xpath="hn:bookName"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:unique>
</xs:element>
</xs:schema>
<xs:unique name="bookUnique"> 是一个unique声明,其中“bookUnique”是unique的名字。
<xs:selector xpath="hn:bookName"></xs:selector> 用来选择一组元素,作为元素或属性唯一性的范围。xpath属性是XML路径表达式。由于<xs:selector>声明在元素<books>的声明中,可以将当前路径深度看做<books>,代表books元素下的所有bookName。
<xs:field xpath="."></xs:field> 用来指定需要指定唯一性的元素或属性。
示例xml:
<?xml version="1.0" encoding="UTF-8"?>
<tn:books xmlns:tn="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <tn:bookName>Test Book</tn:bookName>
<tn:bookName>Test</tn:bookName>
<tn:bookName>Test Book</tn:bookName>
</tn:books>
在XMLSpy提示错误信息:
![]()
2、属性唯一性
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.test.com" xmlns:tn="http://www.test.com"> <xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="bookUnique">
<xs:selector xpath="tn:book"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
</xs:schema>
<xs:field xpath="@id"/> 表示对元素employee的id属性进行唯一性约束。
xml实例:
<?xml version="1.0" encoding="UTF-8"?>
<tn:books xsi:schemaLocation="http://www.test.com t.xsd" xmlns:tn="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <tn:book id="1">
<tn:title>a</tn:title>
<tn:price>10</tn:price>
</tn:book>
<tn:book id="2">
<tn:title>a</tn:title>
<tn:price>10</tn:price>
</tn:book>
<tn:book id="1">
<tn:title>a</tn:title>
<tn:price>10</tn:price>
</tn:book>
/tn:books>
XMLSpy错误信息:
![]()
XML Schema使用技巧——unique的更多相关文章
- XML详解:第二部分 XML Schema
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- rest-assured之Schema validation(包括JSON Schema validation及Xml Schema validation)
rest-assured从2.1.0版本开始支持 Schema 验证,包括JSON Schema validation及Xml Schema validation.我们之前断言响应体都是一个一个字段 ...
- 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 ...
随机推荐
- NYOJ-517 最小公倍数 TLE 分类: NYOJ 2013-12-29 14:49 253人阅读 评论(0) 收藏
#include <stdio.h> int main(){ int num[101]={0}; int result[21]={0}; int sum[101][21]={0}; int ...
- C语言预处理命令
1.#error Directive (C/C++) The #error directive emits a user-specified error message at compile time ...
- AVRStudio 的编译优化级别
-00 无优化. -01 减少代码尺寸和执行时间,不进行需要大量编译时间的优化. -02 几乎执行所有优化,而不考虑代码尺寸和执行时间. -03 执行 -02 所有的优化,以及内联函数,重命名寄存器的 ...
- bootstrap学习记录(慕课网教程)
1.当主标题下需要副标题时可在h中嵌套small<h1>nihao<small>a</samll></h1> 2.当某一段落需要突出显示时可添加lead ...
- SOA之(1)——SOA架构基础概念
在深入探讨什么是面向服务的架构(SOA)之前,先建立一些基本的概念和术语的基本描述而非严格定义,所以也许有些定义在业内还存留争议,此处暂且忽略. 架构基础 技术架构(Technology Archit ...
- 《暗黑世界GM管理后台系统》部署+功能说明文档
http://www.9miao.com/product-10-1073.html <暗黑世界GM管理后台系统>部署+功能说明文档 <暗黑世界GM管理后台系统>部署+功能说明文 ...
- HDU 2671 Can't be easier(数学题,点关于直线对称)
题目 //数学题//直线 y = k * x + b//直线 ax+by+c=0; 点 (x0,y0); 点到直线距离 d = (ax0+by0+c)/sqrt(a^2+b^2) /********* ...
- iOS应用间的跳转和传值
在第一个应用程序中info.plist设置 URL Identifier: 该字符串是你自定义的 URL scheme 的名字 注意: URL Schemes 是一个数组,允许应用定义多个 URL s ...
- Python下载Yahoo!Finance数据
Python下载Yahoo!Finance数据的三种工具: (1)yahoo-finance package. (2)ystockquote. (3)pandas.
- iOS多线程的初步研究(五)-- 如何让NSURLConnection在子线程中运行
可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...