DTD and XML Schema

XML is a data formatting language for describing complex data structures. The DTD / Schema specification is an XML document, for writing XML documents constrain

 XML DTD

one example

DTD constraint document

<!ELEMENT books (book)>
<!ELEMENT book (name,price)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT price (#PCDATA)>

Book Tag definitions related attributes in the DTD document

XML documents

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE books SYSTEM "books.dtd">
<books>
<book>
<name>java</name>
<price>$16</price>
</book>
</books>

Introduced in the XML document DTD constraint because the DTD provisions books Tag has a book composed, so in XML books Tag need for book Child Tag,
DTD specified book Tag by the name and price composition, and therefore XML document book Tag must have a name and price

Of course, DTD and XML can be written in the same file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE users [
<!ELEMENT users (user*)>
<!ELEMENT user (id,username,password,email,birthday)>
<!ELEMENT id      (#PCDATA)>
<!ELEMENT username    (#PCDATA)>
<!ELEMENT password (#PCDATA)>
<!ELEMENT email    (#PCDATA)>
<!ELEMENT birthday    (#PCDATA)>
]>
<users>
<user>
<id>1</id>
<username>1</username>
<password>1</password>
<email>1</email>
</user>
</users>

The introduction of XML DTD

XML DTD has introduced two ways, one is directly introduced, ie DTD and XML written in the same document. There is an external introduction. Introduction into local and external public. Local represents the custom DTD, while the public said that people commonly recognized DTD.

本地引入DTD 
    <!DOCTYPE root-element SYSTEM "filename">
引入公共DTD
    <!DOCTYPE root-element PUBLIC "DTD-NAME" "URL">

Few custom DTD, scenarios use a common DTD most developers, about the DTD can refer to the DTD

XML Schema

xml Schema using predefined elements and attributes developed, which itself is an XML document (which is different DTD), Schema again by the provisions of the XML instance document. Since an XML instance document can introduce multiple Schema, the elements in order to avoid duplicate names, each should have a Schema namespace (URI used here to distinguish). Since the use of pre-defined elements and attributes defining Schema document, and therefore need to be introduced under the W3C organization scheme, i.e.,xmlns="http://www.w3.org/2001/XMLSchema"

Bound documents

<?xml version="1.0" encoding="utf-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNameSpace="http://www.dy-pc.com"
elementFormDefault = "qualified"
>
<element name="books">
    <complexType>
        <sequence>
            <element name="book">
                <squence>
                    <element name="name" type="string" />
                    <element name="price" type="double" />
                </squence>  
            </element>
        </sequence>
    </complexType>
</element>

XML documents

<?xml version="1.0" encoding="utf-8" ?>
<books  xmlns="http://www.dy-pc.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.dy-pc.com  test.xsd">
    <book>
        <name>java</name>
        <price>$16</price>
    </book>
</books>

Guess you like

Origin www.cnblogs.com/xidongyu/p/12237183.html
DTD