如何使用XSD

更多细节可以参考: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"?>
<note
xmlns="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>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值