Norms and constraints of XML documents

XML ##:
 1. concept : Extensible Markup Language Extensible Markup Language
  * Scalable: tags are customizable. <user> <student>
  * Function
   * store data
    1. profile
    2. the network transmission
  * html and xml difference of
   1. tags are custom xml, html tag is predefined.
   2. xml strict syntax, html syntax loose
   3. xml data is stored, html is showing data
  * W3c: World Wide Web Consortium
  2. Syntax:
   * Basic syntax:
   extension .xml 1. xml document
   2. The first line must be defined as XML document declaration
   3. xml document has only one root tag
   4. The attribute values in quotation marks (single and double can) to cause
   5. Close the label must be properly
   6. xml tag names are case sensitive
* Components:
    1. Document Statement
    1. Format: <xml list of attributes??>
    2. The list of attributes:
     * Version: The version number must be the property
     * encoding: encoding. Inform parsing engine used in the current document character set, the default value: ISO-8859-1
     * Standalone: independence
      * Value:
       * yes: does not depend on other files
       * no: dependent on other files
    2. Directive (understand) : combination of css
    * <? xml-stylesheet of the type = "text / CSS" href = "a.css"?>
    3. label : custom label name
    * rules:
     * the name can contain letters, numbers and other characters
     * names can not be digital or punctuation start
     * name can not letter xml (or XML, Xml etc.) begin
     * The name can not contain spaces
    4. attributes :
    ID attribute value unique  5. Text :     * CDATA regions: the data in this area will be show as      * format: <[CDATA [Data]]!>
   

 
XML a small example:
 1 <?xml version="1.0" encoding="utf-8" standalone='no' ?>
 2 <!--<?xml-stylesheet type="text/css" href="a.css" ?>-->
 3 
 4 <users>
 5 
 6     <user id='1'>
 7         <name>张三</name>
 8         <age>23</age>
 9         <gender>male</gender>
10         <br/>
11     </user>
12 
13     <user id='2'>
14         <name>lisi</name>
15         <age>24</age>
16         <gender>female</gender>
17 
18         <code>
19                <!-- if(a &lt; b &amp;&amp; a &gt; c){}-->
20             <![CDATA[
21                 if(a < b && a > c) {}
22             ]]]>
23 
24 
25         </code>
26 
27     </user>
28 
29 </users>

 

* Constraints :
The role and relationship with the constraints of the XML file:
 
 
   The concept : the provisions of the rules of writing xml document
   * As a user frame (programmer):
    1. constraints can be introduced in xml document
    2 can be easily read the bound document
   
    * Classification:
    1. the DTD: A simple technical constraints
    2. the Schema: a complex technical constraints

    * DTD:
    * introducing dtd document to xml document
     * internal dtd: the constraint rules defined in xml document
     * external dtd: rules define the constraints in external dtd file
      * Local: <DOCTYPE root tag name SYSTEM "dtd! location of the file ">
      * network:! <DOCTYPE root tag name PUBLIC" dtd file name to "" URL dtd file ">
 
 
   Examples introduction and use of DTD :( Learn)
  Create a dtd file: student.dtd
1 <!ELEMENT students (student+) >
2 <!ELEMENT student (name,age,sex)>
3 <!ELEMENT name (#PCDATA)>
4 <!ELEMENT age (#PCDATA)>
5 <!ELEMENT sex (#PCDATA)>
6 <!ATTLIST student number ID #REQUIRED>

  dtd file import and use in the XML file: student.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE students SYSTEM "student.dtd">
 3 
 4 <!--<!DOCTYPE students [
 5 
 6         <!ELEMENT students (student+) >
 7         <!ELEMENT student (name,age,sex)>
 8         <!ELEMENT name (#PCDATA)>
 9         <!ELEMENT age (#PCDATA)>
10         <!ELEMENT sex (#PCDATA)>
11         <!ATTLIST student number ID #REQUIRED>
12 
13 
14         ]>-->
15 <students>
16     
17     <student number="s001">
18         <name>zhangsan</name>
19         <age>abc</age>
20         <sex>hehe</sex>
21     </student>
22 
23     <student number="s002">
24         <name>lisi</name>
25         <age></24age>
26         <sex>female</sex>
27     </student>
28     
29 </students>

 


    Schema *:
    * introduction:
     1. Fill xml root element of the document
     2. Introduction xsi prefix xmlns:. Xsi = "http://www.w3.org/2001/XMLSchema-instance"
     3. introducing xsd file namespace xsi. : the schemaLocation = "http://www.itcast.cn/xml student.xsd"
     4. xsd for each constraint specifies a prefix, as the identification 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">
 

   Schema using the example of references and understand :()

  xsd files created:

student.xsd

 

 1 <?xml version="1.0"?>
 2 <xsd:schema xmlns="http://www.itcast.cn/xml"
 3         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 4         targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">
 5     <xsd:element name="students" type="studentsType"/>
 6     <xsd:complexType name="studentsType">
 7         <xsd:sequence>
 8             <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
 9         </xsd:sequence>
10     </xsd:complexType>
11     <xsd:complexType name="studentType">
12         <xsd:sequence>
13             <xsd:element name="name" type="xsd:string"/>
14             <xsd:element name="age" type="ageType" />
15             <xsd:element name="sex" type="sexType" />
16         </xsd:sequence>
17         <xsd:attribute name="number" type="numberType" use="required"/>
18     </xsd:complexType>
19     <xsd:simpleType name="sexType">
20         <xsd:restriction base="xsd:string">
21             <xsd:enumeration value="male"/>
22             <xsd:enumeration value="female"/>
23         </xsd:restriction>
24     </xsd:simpleType>
25     <xsd:simpleType name="ageType">
26         <xsd:restriction base="xsd:integer">
27             <xsd:minInclusive value="0"/>
28             <xsd:maxInclusive value="256"/>
29         </xsd:restriction>
30     </xsd:simpleType>
31     <xsd:simpleType name="numberType">
32         <xsd:restriction base="xsd:string">
33             <xsd:pattern value="heima_\d{4}"/>
34         </xsd:restriction>
35     </xsd:simpleType>
36 </xsd:schema> 

  xsd constraint file referenced and used in the XML file:

student.xml

1  <? Xml Version = "1.0" encoding = "UTF-8" ?> 
2  ! <-  
3      1. Fill xml document root element
 4      2. Introduction xsi prefix xmlns: xsi = "http: // www.. w3.org/2001/XMLSchema-instance "
 . 5      3. incorporated xsd namespace file the xsi:. the schemaLocation =" http://www.itcast.cn/xml student.xsd "
 . 6      4. for each constraint specifies a prefix xsd as the ID = xmlns "http://www.itcast.cn/xml" 
 . 7      
. 8      
. 9   -> 
10  < Students.    xmlns: the xsi = "http://www.w3.org/2001/XMLSchema-instance" 
. 11              xmlns = "http://www.itcast.cn/xml" 
12              xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
13 >
14     <student number="heima_0001">
15         <name>tom</name>
16         <age>18</age>
17         <sex>male</sex>
18     </student>
19 
20 </students>

 

 

 

Guess you like

Origin www.cnblogs.com/fangtingfei/p/11404031.html