1. First find out why we should restrict xml writing?
<?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="text/css" href="a.css" ?> <users> <user id="zhangsan"> <name>Zhang San</name> <age>21</age> <gender>male</gender> <gender>female</gender> </user> </users>
From the above code segment, we can see that if there is no restriction on the writing of xml documents, there are two gender tags. Whether this is male or female, we can find that if there is no restriction, the writing of xml is still a little loose, which violates the strict principle of xml.
Therefore, in XML documents, a set of rules are defined to restrict the content of XML documents. This set of constraints is called XML constraints. At present, the two most commonly used constraints are DTD and Schema constraints.
2. To get back to business, how to import constraints?
2.1 DTD constraints
<?xml version="1.0" encoding="UTF-8" ?> <!--Mode 1: external dtd file--> <!DOCTYPE students SYSTEM "D:\Idea-workspace1\JavaWeb\06_xml\src\com\lin\dtd\student.dtd"> <!--Mode 2: internal dtd Constraint document--> <!--<!DOCTYPE students [ <!ELEMENT students (student+) > <!ELEMENT student (name,age,sex)> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT sex (#PCDATA)> <!ATTLIST student number ID #REQUIRED> ]>--> <students> <student number="s001"> <name>Zhang San</name> <age>23</age> <sex>male</sex> </student> <student number="s002"> <name>Zhang San</name> <age>23</age> <sex>male</sex> </student> </students>
*Internal dtd: define constraint rules in xml documents (less used, poor reusability)
*External dtd: define the constraint rules in the external dtd file
* if the dtd file is local: <! DOCTYPE root signature SYSTEM "dtd file local location" >
* if the dtd file is on the network: <! DOCTYPE root signature PUBLIC "dtd file name" "dtd file URL" >
2.2. Schema constraints
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE students SYSTEM "D:\Idea-workspace1\JavaWeb\06_xml\src\com\lin\dtd\student.dtd"> <students> <student number="s001"> <name>Zhang San</name> <age>10000</age> <sex>male</sex> </student> </students>
Because the dtd constraint cannot restrict the content of the label, it is obviously unreasonable to be 10000 years old in the above code, so a more binding Schema constraint is introduced.
<?xml version="1.0" encoding="UTF-8" ?> <!--Import Schema Constraints:--> <students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.itcast.cn/xml student.xsd" xmlns="http://www.itcast.cn/xml" > <student number="heima_0001"> <name>tom</name> <age>18</age> <sex>male</sex> </student> <student number="heima_0002"> <name>jane</name> <age>22</age> <sex>female</sex> </student> </students>
Import Schema constraint resolution steps:
<students xmlns="http://www.itcast.cn/xml" > </students>
Check the Schema constraint file, find the root tag students and write it out in xml, and then define xmlns [: namespace prefix] = "namespace"
xmlns:XML Name Space xml namespace. The namespace is viewed in the constraint file. The value of targetNamespace is. In schema, each constraint document is given a unique namespace identified by URI. When XML files reference schema constraints, it is through this namespace to declare which shema constraint document comes from. This namespace is to facilitate the XML parser to distinguish which constraint file the label comes from.
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.itcast.cn/xml student.xsd" > </students>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "Indicates the namespace prefixed with XSI. The prefix is to better represent this URI: http://www.w3.org/2001/XMLSchema-instance , just like your name. XSI has become an industry default namespace for representing all XSD files. Just remember. XSD files (also known as Schema constraint files) are used to define constraint XML documents.
xsi:schemaLocation=" http://www.itcast.cn/xml student.xsd "this represents the location of the specific Schema constraint file used in the current XML file, in front of http://www.itcast.cn/xml Is the namespace of the specific xsd constraint file, followed by student xsd is the location address of the specific xsd file.
Generally speaking, the front is the namespace for all xsds, and the back is to define the specific XSD files to be used. An abstract, a concrete.
<?xml version="1.0" encoding="UTF-8" ?> <!--Import Schema Constraints:--> <a:students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.itcast.cn/xml student.xsd http://www.itcast.cn/xml2 D:\...\student.xsd" xmlns:a="http://www.itcast.cn/xml" xmlns:a="http://www.itcast.cn/xml2" > <a:student number="heima_0001"> <a:name>tom</a:name> <a:age>18</a:age> <a:sex>male</a:sex> </a:student> <b:student number="heima_0002"> <b:name>jane</b:name> <b:age>22</b:age> <b:sex>female</b:sex> </b:student> </a:students>
The above code shows that two XSD constraint files are introduced into this XML file:
http://www.itcast.cn/xml student.xsd (use relative path)
http://www.itcast.cn/xml2 D:\...\student.xsd "(use absolute path)
to http://www.itcast.cn/xml The namespace declares a prefix a,
to http://www.itcast.cn/xml2 The namespace declares a prefix b,
In this way, adding a prefix to the label represents which constraint file the label element comes from, so we won't be confused about where the same element comes from.