posts - 397,comments - 0,views - 25332

分类

  DTD:一种简单的约束技术

  Schema:一种复杂的约束技术

 

DTD  

  引入dtd文档到xml文档中

    内部:dtd:将约束规则定义在xml文档中

    外部:dtd:将约束规则定义在外部的dtd文件中

      本地<!DOCTYPE 根标签 名  SYSTEM "dtd文件的位置">

      网络<!DOCTYPE 根标签 名  PUBLIC "dtd文件名字" "dtd文件的位置url">

 

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE students SYSTEM "student.dtd">

<!--<!DOCTYPE students [-->

<!--        <!ELEMENT students (student+) >-->
<!--        <!ELEMENT student (name,age,sex)>-->
<!--        <!ELEMENT name (#PCDATA)>-->
<!--        <!ELEMENT age (#PCDATA)>-->
<!--        <!ELEMENT sex (#PCDATA)>-->
<!--        <!ATTLIST student number ID #REQUIRED>-->
<!--        ]>-->
<students>
        <student number="s001">
            <name>zhangsan</name>
            <age>23</age>
            <sex>male</sex>
        </student>
        
        <student number="s002">
            <name>lisi</name>
            <age>24</age>
            <sex>female</sex>
        </student>
</students>
复制代码

 

 

<!ELEMENT students (student+) >
        <!ELEMENT student (name,age,sex)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT age (#PCDATA)>
        <!ELEMENT sex (#PCDATA)>
        <!ATTLIST student number ID #REQUIRED>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

xml_约束_schema

填写xml文档的根元素
引入xsi前缀.xmlns:xsi="http: / / www. w3.org/2001/xNLSchema-instance"
引入xsd文件命名空间. xsi:schemaLocation="http: / /www.itcast.cn/xml student.xsd"

为每一个xsd约束声明一个前缀,作为标识xmlns="http://www.itcast.cn/xml"

<students   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.itcast.cn/xml"
xsi:schemaLocation="http://www.itcast.cn/xml student.xsd">
复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<students   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://www.itcast.cn/xml"
            xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd">

    <student number="heima_0001">
        <name>zhangsan</name>
        <age>11</age>
        <sex>male</sex>
    </student>

    <student number="heima_0002">
        <name>wangwi</name>
        <age>14</age>
        <sex>female</sex>
    </student>

</students>
复制代码
复制代码
<?xml version="1.0"?>
<xsd:schema xmlns="http://www.itcast.cn/xml"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">
    <xsd:element name="students" type="studentsType"/>
    <xsd:complexType name="studentsType">
        <xsd:sequence>
            <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="studentType">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="age" type="ageType" />
            <xsd:element name="sex" type="sexType" />
        </xsd:sequence>
        <xsd:attribute name="number" type="numberType" use="required"/>
    </xsd:complexType>
    <xsd:simpleType name="sexType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="male"/>
            <xsd:enumeration value="female"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="ageType">
        <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="256"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="numberType">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="heima_\d{4}"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>
复制代码
posted on   淤泥不染  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示