如何使用XSD
2016-06-03 21:07
337 查看
更多细节可以参考:http://www.w3school.com.cn/schema/schema_howto.asp
XSD是XML Schema Definition的简称,顾名思义,它是用xml语言来定义和验证xml数据格式的文件,后缀.xsd
XML Schema可以用来定义什么?:
定义可出现在文档中的元素
定义可出现在文档中的属性
定义哪个元素是子元素
定义子元素的次序
定义子元素的数目
定义元素是否为空,或者是否可包含文本
定义元素和属性的数据类型
定义元素和属性的默认值以及固定值
一个简单的XML文件,命名node.xml:
给上面的xml创建一个xsd文件约束其数据格式,文件名为node.xsd:
xs:sequence 元素用来定义子节点
在node.xml中引用schema:
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>[/code]
以上只是个很简单的示例,下面再来看个更复杂的,schema文件名为studentSchema.xsd:
这个schema中定义了student的一个符合类型,子节点,数据类型,出现次数的限制都有(还有枚举取值限制),甚至还有list do的限制
接下来看如何引用这个schema做数据验证,创建一个名为student.xml的文件:
XSD是XML Schema Definition的简称,顾名思义,它是用xml语言来定义和验证xml数据格式的文件,后缀.xsd
XML Schema可以用来定义什么?:
定义可出现在文档中的元素
定义可出现在文档中的属性
定义哪个元素是子元素
定义子元素的次序
定义子元素的数目
定义元素是否为空,或者是否可包含文本
定义元素和属性的数据类型
定义元素和属性的默认值以及固定值
一个简单的XML文件,命名node.xml:
<?xml version="1.0"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
给上面的xml创建一个xsd文件约束其数据格式,文件名为node.xsd:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" 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>
xs:sequence 元素用来定义子节点
在node.xml中引用schema:
<?xml version="1.0"?> <note xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" [code]xsi:schemaLocation="http://www.w3school.com.cn note.xsd">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>[/code]
以上只是个很简单的示例,下面再来看个更复杂的,schema文件名为studentSchema.xsd:
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://galaxy/school/student" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://galaxy/school/student" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:element name="student"> <xsd:complexType> <xsd:sequence> <xsd:element name="stdName" type="xsd:string" maxOccurs="1"/> <xsd:element name="age" type="xsd:int" maxOccurs="1"/> <xsd:element name="address" type="addressType" maxOccurs="1"/> <xsd:element name="courses" type="coursesListType"/> <xsd:element name="nation"> <!-- 枚举限制--> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="CHINA"/> <xsd:enumeration value="INDIA"/> <xsd:enumeration value="USA"/> <xsd:enumeration value="BRITISH"/> <xsd:enumeration value="FRANCE"/> <xsd:enumeration value="GERMAN"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <!-- list数据组--> <xsd:complexType name="coursesListType"> <xsd:group ref="courseGroup"/> </xsd:complexType> <!-- maxOccurs必须设置为unbounded,否则默认限制个数为1--> <xsd:group name="courseGroup"> <xsd:sequence> <xsd:element name="course" type="courseType" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:group> <!-- 枚举值限制--> <xsd:simpleType name="courseType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="English"/> <xsd:enumeration value="Chinese"/> <xsd:enumeration value="Math"/> <xsd:enumeration value="Phyisc"/> <xsd:enumeration value="Artistic"/> </xsd:restriction> </xsd:simpleType> <xsd:complexType name="addressType"> <xsd:sequence> <xsd:element name="country" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="building" type="xsd:string"/> <xsd:element name="room" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
这个schema中定义了student的一个符合类型,子节点,数据类型,出现次数的限制都有(还有枚举取值限制),甚至还有list do的限制
接下来看如何引用这个schema做数据验证,创建一个名为student.xml的文件:
<?xml version="1.0" encoding="UTF-8"?> <stdo:student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdo="http://galaxy/school/student" xsi:schemaLocation=" http://galaxy/school/student http://galaxy/school/student/[b]studentSchema.xsd"[/b]> <stdo:stdName>zhangsan</stdo:stdName> <stdo:age>18</stdo:age> <!--内嵌复合类型--> <stdo:address> <stdo:country>CHINA</stdo:country> <stdo:city>SHANGHAI</stdo:city> <stdo:street>BOHUA ROAD</stdo:street> <stdo:building>18</stdo:building> <stdo:room>602</stdo:room> </stdo:address> <!-- 内嵌list--> <stdo:courses> <stdo:course>Artistic</stdo:course> <stdo:course>Math</stdo:course> <stdo:course>English</stdo:course> </stdo:courses> <stdo:nation>USA</stdo:nation> </stdo:student>
相关文章推荐
- 如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。
- 【Poj3420】Quad Tiling 状压DP 矩阵快速幂
- C运行时库(C Run-Time Libraries)
- 如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。
- STM32F0xx_TIM输出PWM配置详细过程
- SVN
- Cydia Substrate Android SO Hook
- Linux内核中的GPIO驱动
- 安全密码
- 【HDU】 1452 Happy 2004
- Codeforces Round #248 (Div. 1) B. Nanami's Digital Board 暴力 前缀和
- 血条HpBar跟随敌人
- js ,jsp获取项目应用名
- js,javascript,jquery网页返回顶部代码
- Android 几何图形工具类GeometryUtil
- mysql zip 免安装版 安装教程
- centos6.5 ssh配置与使用
- perl使用小结
- iOS开发——搜索框(UISearchController)
- Java集合框架图解