Introduction to XML-2

One, XML semantic constraints

1. The structure of the XML document is correct, but it may not be valid

For example, the "plant variety" tag is never allowed in the employee file xml. xml semantic constraints are used to specify which elements are allowed to appear in xml documents

2. There are two ways to define XML semantic constraints : DTD and XML Schema.

Two, DTD

1. Definition : DTD (Document Type Definition) is a simple and easy-to-use semantic constraint method.

2, DTD file name extension is .dtd

3. Using the <!ELEMENT> tag in the DTD, we can define the number and number of nodes allowed in the XML document. Take hr.xml as an example:

  • Define that only one employee child node is allowed under the hr node:
<!ELEMENT hr(employee)>
  • The employee node must contain the following four nodes, and they appear in order:
<!ELEMENT employee(name,age,salary,department)>
  • Define the name tag body can only be text, #PCDATA represents the text element
<!ELEMENT name (#PCDATA)>

If a child node needs to be repeated multiple times, the response descriptor needs to be added after the child node

  • At least one employee child node appears under the hr node
<!ELEMENT hr(employee+)>
  • 0...n employee child nodes can appear under the hr node
<!ELEMENT hr(employee*)>
  • At most 1 employee child node appears under the hr node
<!ELEMENT hr(employee?)>

4. Use the <!DOCTYPE> tag in XML to reference DTD files

Writing format:

<!DOCTYPE 根节点 SYSTEM "dtd文件路径">

Example:

<!DOCTYPE hr SYSTEM "hr.dtd">

Last demo

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hr SYSTEM "hr.dtd">

<hr>
    <employee no="3309">
        <name>kola</name>
        <age>13</age>
        <salary>2333</salary>
        <department>
            <dname>it</dname>
            <address>xx大厦</address>
        </department>
    </employee>
    <employee no="3319">
        <name>lily</name>
        <age>13</age>
        <salary>3211</salary>
        <department>
            <dname>it</dname>
            <address>xx大厦</address>
        </department>
    </employee>
</hr>
<?xml version="1.0" encoding="UTF-8"?>
        <!ELEMENT hr (employee+)>
        <!ELEMENT employee (name,age,salary,department)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT age (#PCDATA)>
        <!ELEMENT salary (#PCDATA)>
        <!ELEMENT department (dname,address)>
        <!ELEMENT dname (#PCDATA)>
        <!ELEMENT address (#PCDATA)>
        <!ATTLIST employee no "CDATA" "">

Note that when specifying the number of child tags in a parent tag, there must be "spaces" between the parent tag and the following parentheses.

//The constraint format when defining attributes

<!ATTLIST node name attribute name CDATA ""> //The default value is in quotes

Three, schema constraints

  1. XML Schema is more complex than DTD and provides more functions.
  2. XML Schema provides features such as data type, format limitation, and data range.
  3. XML Schema is a W3C standard.
    Because it is more complicated, just go to the code and read the comments~
<?xml version="1.0" encoding="UTF-8"?>
<!--<!DOCTYPE hr SYSTEM "hr.dtd">-->
<!--告诉xml文档约束使用的是schema-->
<hr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hr.xsd">
    <employee no="3309">
        <name>kola</name>
        <age>13</age>
        <salary>2333</salary>
        <department>
            <dname>it</dname>
            <address>xx大厦</address>
        </department>
    </employee>
    <employee no="3319">
        <name>lily</name>
        <age>13</age>
        <salary>3211</salary>
        <department>
            <dname>it</dname>
            <address>xx大厦</address>
        </department>
    </employee>
</hr>

<?xml version="1.0" encoding="utf-8" ?>
<!--定义schema:idea工具这句话要删掉,否则会报错-->
<!--<schema xmlns="http://www.w3.org/2001/XMLSchema">-->
    <!--声明根节点-->
<schema>
    <element name="hr">
        <!--coplexType标签含义是复杂节点,包含子节点时必须使用这个标签-->
        <complexType>
            <!--sequence表示序列,含义是子节点必须按照前后顺序书写-->
            <sequence>
                <!--约束子节点最多最少出现的次数-->
                <element name="employee" minOccurs="1" maxOccurs="999">
                    <complexType>
                        <sequence>
                            <element name="name" type="string"></element>
                            <element name="age">
                                <!--限制年龄-->
                                <simpleType>
                                    <restriction base="integer">
                                        <maxExclusive value="60"></maxExclusive>
                                        <minExclusive value="18"></minExclusive>
                                    </restriction>
                                </simpleType>
                            </element>
                            <element name="salary" type="integer"></element>
                            <element name="department">
                                <complexType>
                                    <sequence>
                                        <element name="dname" type="string"></element>
                                        <element name="address" type="string"></element>
                                    </sequence>
                                </complexType>
                            </element>
                        </sequence>
                    </complexType>
                </element>
            </sequence>
            <!--属性设置:定义use表示属性在任何节点必须存在-->
            <attribute name="no" type="string" use="required"></attribute>
        </complexType>
    </element>
</schema>

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/111477302