XML: DTD: Schema

Source: Internet
Author: User

1. The structure of the XML document can be abstracted up to two aspects: node and attribute

<Topnode name = "TOP">
<Midtop name = "mid"/>
</Topnode>

2. DTD Constraints

I. Peer nodes:

Constraints on nodes, it is obvious to describe the structure and type of nodes, etc.

<! Element name (type)>

All the descriptions in the DTD are completed by the above type.

See the following example:

A. <book> my book </book> --- DTD ---> <! Element book (# pcdata)>

B. <book/> --- DTD ---> <! Element book empty>

C. <book>
<Title> mybook </title>
<Author> yife </author> --- DTD ---> <! Element book (title, author)>
</Book>

* # Pcdata indicates character data

I don't need to explain more, so I should be able to understand it first, but now I will discuss the third case above:

D. <book>
<Title> mybook </title>
<Chapter> No.1 </Chapter>
...
Unknow, the number will be change sometimes
<Chapter> NO. n </Chapter>
</Book>

--- DTD ---> <! Element book (title, Chapter +)>

The wildcard table is provided here.
[Default]: Must be 1 time (1)
? : Must be 1 time or 0 (1 | 0)
+: 1 times or more (1... n)
*: 0 times or more (0... n)

E. <chapter> No.1 </Chapter>
<Chapter>
<Havechild/>
</Chapter>

--- DTD ---> <! Element book (# pcdata | (havechild)>

* The symbol "|" indicates the meaning of "or ".

Ii. attributes:

The attribute is bound to the node, and the attribute has no structure description.
<! Attilist element attribute type modifer>

Example:

A. <book Focus = "XML"/> --- DTD ---> <! Attilist book Focus CDATA # required>

* CDATA indicates a character.
* # Required indicates required. # impled indicates saving. # Fix indicates unchangeable.

In addition to characters, the attribute can also be enumerated.

B. <book Focus = "XML"/>
<Book Focus = "Java"/> --- DTD ---> <! Attilist book Focus (XML | Java) # impled>

C. <book Focus = "XML"> mE </book>
.....
<Book Focus = "XML"> You </book>

--- DTD ---> <! Attilist book Focus (XML | Java) "XML">

* "XML" is the default value, which is the default representation.

2. schema Constraints

I. Peer nodes: (schema also complies with XML standards)

<Element name = "name" type = "type" [Options...]>

Here types is more than DTD, very like Java

See the following example:

A. <book> my book </book>-schema --> <element name = "book" type = "string"/>

B. <book>
<Title> mybook </title>
<Author> yife </author>
</Book>

-Schema -->

<Element name = "book" type = "booktype"/>
<Complextype name = "booktype">
<Element name = "title" type = "string">
<Element name = "author" type = "string">
</Complextype>

C. You can write the code in the previous example (I hope you can think about the differences between the two methods)
<Element name = "book">
<Complextype>
<Element name = "title" type = "string">
<Element name = "author" type = "string">
</Complextype>
</Element>

A special example:

D. <author/>

-Schema -->

<Element name = "author">
<Complextype content = "booktype"/>
</Element>

Remember <! Element book (title, Chapter +)>? "+" In Schema:

E. <element name = "chapter" type = "string" minoccurs = "1" maxoccurs = "*">

In fact, minoccurs = "1" is not mandatory because the default value is 1.

Ii. attributes:

<Attribute name = "name" type = "type" [Options]>

Example:

A. <book Focus = "XML"/>

-Schema -->

<Element name = "book">
<Complextype>
<Attribute name = "Focus" type = "string"/>
</Complextype>
</Element>

* Attribute is an entry in complextype.

The schema does not contain # reuqired, so to express this meaning:

B. <attribute name = "Focus" type = "string" minoccurs = "1"/>

The default value of minoccurs is 0.

C. <attribute name = "Focus" type = "string" default = "XML"/> default value representation

D. schema representation <! Attilist book Focus (XML | Java) "XML">

<Attribute name = "Focus">
<Simpletype base = "string">
<Enumeration value = "XML">
<Enumeration value = "Java">
</Simpletype>
</Attribute>

3. xml usage:

Schema:
<Book xmlns: xsi = "http://www.w3.org/1999/XMLSchema/instance"
Xmlns = "you name space"
Xsi: schemalocation = "You XSD file location"
>

DTD:
<! Doctype book system "path to DTD file">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.