如何使用XSD

来源:互联网 发布:淘宝男童装外套 编辑:程序博客网 时间:2024/04/29 13:37

更多细节可以参考:http://www.w3school.com.cn/schema/schema_howto.asp


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"?><notexmlns="http://www.w3school.com.cn"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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>


以上只是个很简单的示例,下面再来看个更复杂的,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/studentSchema.xsd">    <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>

0 0
原创粉丝点击