XML document structure ( https://www.runoob.com/xml/xml-syntax.html )
- The first line must be the xml declaration: <?xml version="1.0" encoding="UTF-8"?> , which defines the version of XML (1.0) and the encoding used (UTF-8: Universal Code, which can display various Language).
- There is exactly one root node.
- The writing rules of xml tags are the same as HTML.
- Use English lowercase for label names, separated by "-" between words: <shop-cart>shopping cart</shop-cart>
- Put the character "<" in the XML element, an error will occur, such as: message>if salary <1000 then</message>, solution 1, entity reference. 2. CDATA tag: The CDATA part starts with " <![CDATA[ " and ends with " ]]> ".
- In the sub-elements in the xml multi-level nesting, the order of the tags should be consistent.
example:
hr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hr SYSTEM "hr.dtd">
<!-- 注释,人力资源管理系统 -->
<hr>
<employee no="001">
<name>李白</name>
<age>21</age>
<salery>5000</salery>
<department>
<dname>开发部</dname>
<address>北京</address>
</department>
</employee>
<employee no="002">
<name>杜甫</name>
<age>23</age>
<salery>7000</salery>
<department>
<dname>市场部</dname>
<address>广州</address>
</department>
</employee>
</hr>
xml semantic constraints: DTD and xml Schema
The purpose of DTD is to define the structure of XML documents: https://www.runoob.com/dtd/dtd-intro.html
1. Bind the .xml and the corresponding .dtd file:
<!DOCTYPE hr SYSTEM "hr.dtd">
hr.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT hr (employee+)>
<!ELEMENT employee (name,age,salery,department)>
<!ATTLIST employee no CDATA "">
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT salery (#PCDATA)>
<!ELEMENT department (dname,address)>
<!ELEMENT dname (#PCDATA)>
<!ELEMENT address (#PCDATA)>
xml Schema
Binding:
<hr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hr.xsd">
Schema file: hr.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="hr">
<!--complexType标签的含义是复杂节点,包含子节点是必须使用这个标签 -->
<complexType>
<sequence>
<element name="employee" minOccurs="1" maxOccurs="9999">
<complexType>
<sequence>
<element name="name" type="string"></element>
<element name="age" >
<simpleType>
<restriction base="integer">
<minInclusive value="18"></minInclusive>
<maxInclusive value="60"></maxInclusive>
</restriction>
</simpleType>
</element>
<element name="salery" type="string"></element>
<element name="department">
<complexType>
<sequence>
<element name="dname" type="string"></element>
<element name="address" type="string"></element>
</sequence>
</complexType>
</element>
</sequence>
<attribute name="no" type="string" use="required"></attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>