XML文件的两种约束描述

1、DTD

  • student.dtd
    <!-- students元素下只能包含student元素 -->
    <!ELEMENT students (student*)>
    <!-- student元素下必须包含name、age、sex元素 -->
    <!ELEMENT student (name,age,sex)>
    <!--name元素类型为字符串(元素下必须包含字符串)-->
    <!ELEMENT name (#PCDATA)>
    <!--name元素类型为字符串(元素下必须包含字符串)-->
    <!ELEMENT age (#PCDATA)>
    <!--name元素类型为字符串(元素下必须包含字符串)-->
    <!ELEMENT sex (#PCDATA)>
    <!--属性列表,student下包含number属性,ID类型,必需-->
    <!ATTLIST student number ID #REQUIRED>
    
  • student.xml 的两个版本

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE students SYSTEM "student.dtd">
    <students>
      <student number="s001">
          <name>张三</name>
          <age>19</age>
          <sex>男</sex>
      </student>
      <student number="s002">
          <name>李四</name>
          <age>18</age>
          <sex>女</sex>
      </student>
    </students>
    
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE students [
          <!-- students元素下只能包含student元素 -->
          <!ELEMENT students (student*)>
          <!-- student元素下必须包含name、age、sex元素 -->
          <!ELEMENT student (name,age,sex)>
          <!--name元素类型为字符串(元素下必须包含字符串)-->
          <!ELEMENT name (#PCDATA)>
          <!--name元素类型为字符串(元素下必须包含字符串)-->
          <!ELEMENT age (#PCDATA)>
          <!--name元素类型为字符串(元素下必须包含字符串)-->
          <!ELEMENT sex (#PCDATA)>
          <!--属性列表,student下包含number属性,ID类型,必需-->
          <!ATTLIST student number ID #REQUIRED>
          ]>
    <students>
      <student number="s001">
          <name>张三</name>
          <age>18</age>
          <sex>男</sex>
      </student>
    </students>
    

2、Schema

  • student.xsd
    <?xml version="1.0" encoding="UTF-8" ?>
    <xsd:schema xmlns="https://www.yusian.com/xml"
              targetNamespace="https://www.yusian.com/xml"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              elementFormDefault="qualified">
      <!--类型定义-->
      <xsd:complexType name="studentsType">
          <xsd:sequence>
              <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
          </xsd:sequence>
      </xsd:complexType>
    
      <!--元素定义-->
      <xsd:element name="students" type="studentsType"/>
    
      <!--类型定义-->
      <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="ageType">
          <xsd:restriction base="xsd:integer">
              <xsd:minInclusive value="0"/>
              <xsd:maxInclusive value="255"/>
          </xsd:restriction>
      </xsd:simpleType>
    
      <!--类型定义-->
      <xsd:simpleType name="sexType">
          <xsd:restriction base="xsd:string">
              <xsd:enumeration value="male"/>
              <xsd:enumeration value="female"/>
          </xsd:restriction>
      </xsd:simpleType>
    
      <!--类型定义-->
      <xsd:simpleType name="numberType">
          <xsd:restriction base="xsd:string">
              <xsd:pattern value="s\d{3}"/>
          </xsd:restriction>
      </xsd:simpleType>
    
    </xsd:schema>
    
  • student.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <students xmlns="https://www.yusian.com/xml"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="https://www.yusian.com/xml student.xsd"
    >
      <student number="s001">
          <name>sian</name>
          <age>19</age>
          <sex>male</sex>
      </student>
    </students>
    

发表评论

邮箱地址不会被公开。 必填项已用*标注

Captcha Code