xml-约束概述和约束schema

xml-约束概述

约束:规定xml文档的书写规则

  • 作为框架的使用者(程序员):
    • 能够在xml中引入约束文档
    • 能够简单的读懂约束文档
  • 分类
    • DTD:一种简单的约束技术
    • Schema:一种复杂约束技术
  •  DTD:
    • 引入dtd文档到xml文档中
      • 内部dtd:将约束规则定义在xml文档中
      • 外部dtd:将约束的规则定义在外部的dtd文件中
        • 本地:<!DOCTYPE 根标签名 SYSTEM “dtd文件的位置”>
        • 网络:<!DOCTYPE 根标签名 PUBLIC "dtd文件名字" “dtd文件的文字URL”>

图解

 

 dtd代码

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

xml代码

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

<students>
    <student number="s001">
        <name>张三</name>
        <age>23</age>
        <sex>male</sex>
    </student>

    <student number="s002">
        <name>李四</name>
        <age>24</age>
        <sex>female</sex>
    </student>
</students>
复制代码

约束schema

student.xsd

复制代码
<?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="aaa_\d{4}"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>
复制代码

student.xml

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!--
    1.填写xml文档的根元素
    2.引入xsi前缀.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.引入xsd文件命名空间.xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
    4.为每一个xsd约束声明一个前缀 作为标识 xmlns="http://www.itcast.cn/xml"
-->
<students>

        <student number="bokeyuan_0001">
            <name>张三</name>
            <age>11</age>
            <sex>male</sex>
        </student>

    <student number="bokeyuan_0002">
        <name>张三</name>
        <age>11</age>
        <sex>male</sex>
    </student>

</students>
复制代码
posted @   baimingze  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示