Definition of the document structure in the XML solution

Source: Internet
Author: User
DTD can actually be seen as a template for one or more XML files. elements, Element attributes, arrangement methods/order of elements, and content that can be contained in these XML files, must comply with the definition in the DTD. Elements in the XML file, that is, the elements we create

DTD can actually be seen as a template for one or more XML files. elements, Element attributes, arrangement methods/order of elements, and content that can be contained in these XML files, must comply with the definition in the DTD. The elements in the XML file, that is, the markup we created, are created based on the actual usage. It is very difficult to create a DTD with high integrity and wide adaptability. because all walks of life have their own industry characteristics, the DTD is usually defined by a certain scope of use, such as medicine, construction, industry and commerce, and administration. The more extensive the elements defined by the DTD contain the covered categories, the more complex the elements are.

DTD can be a complete and independent file, or can be directly set in an XML file. Therefore, DTD can be divided into two types: external DTD (calling another compiled DTD in the XML file) and internal DTD (directly setting DTD in the XML file. For example, there are dozens of companies and vendors that are engaged with each other in partnership. the electronic documents they exchange are all in XML format. So we can place the DTD of these XML documents in a certain place, so that all the exchanged XML documents can apply this DTD, which is the most convenient way, it is also useful for internal XML file applications.

Internal DTD

The internal DTD is defined in the preface area of the XML file. Syntax:

Reference content is as follows:

<〈! DOCTYPE element-name [........

]>

<〈! DOCTYPE: Set the DTD for the beginning of the performance. pay attention that DOCUTYPE is capitalized.

Element-name: specify the name of the root Element of this DTD. an XML file can have only one root Element. Note that if the DTD is applied to the XML file, the root element in the file is specified here.

[...]>: Defines the XML file application elements in the [] tag, and ends the DTD definition with>.

Next, let's take a look at how to define a DTD for an XML file. see example 1.

The DTD definition area in example 1 can be considered as an approximate framework defined by a DTD. it defines a DTD for other XML files. The difference between the structure and the DTD in Example 1 is few, you may only need to add, delete, or modify something. In the middle of a DTD definition, it is element setting, which is the most important part of a DTD. its important syntax is as follows:

<〈! ELEMENT element-name element-definition> 〉

<〈! ELEMENT: indicates the starting ELEMENT settings. Note that the key words of the ELEMENT are in uppercase.

Element-name: name of the element to be set.

Element-definition: specifies the definition of the element, that is, the content that can be included between elements... or elements, whether it is other elements or general text.

In example 1, you can see, <,〈! ELEMENT reference material (book)> This ELEMENT is set to declare the "reference material" ELEMENT, and it is the parent ELEMENT of the "book" ELEMENT. <〈! ELEMENT Book (name, author, Price)> this ELEMENT sets the "book" ELEMENT, it is the parent element of the "name", "author", and "price" elements. And <〈! ELEMENT name (# PCDATA)> this ELEMENT declares the "name" ELEMENT, but this ELEMENT only contains general text and is a basic ELEMENT, which is defined by the key word # PCDATA.

In element settings, if an element contains multiple child elements, for example, <:〈! ELEMENT Book (name, author, Price)> This statement containing multiple child elements, the tags "name", "author", and "price" must be displayed in the order listed above in the XML file. each tag must and can only be presented once. Assume that when you set the element, follow <〈! ELEMENT book ANY>, so that the ELEMENT can contain ANY set ELEMENT, and the number and order of representation are not limited, in addition to sub-elements, it can also contain general text. Sometimes, in an XML file, a tag may be rendered multiple times (or not rendered). In addition to using ANY keyword in their parent element, you can also add a specific symbol next to the element to hold the number of times the tag is rendered. See table 1.

Table 1

Symbol indicates the number of times the mark is displayed.

? Not present or only present once

* Not rendered or rendered multiple times

Must be presented more than once

Unsigned display only once

Example: <:〈! ELEMENT reference material (books ?, Newspapers, Magazines ?, Website)> this element sets that the "books" Mark can be hidden or rendered multiple times in an XML file; the "newspaper" mark must be presented more than once; the magazine mark can be present only once or not, and the website mark must be present only once.

In some parent element declarations, it is possible that it contains a child element that is selected from multiple child elements for application. when we declare this parent element, it can be declared as a selective element, for example: <:〈! ELEMENT spouse (wife | husband)> )〉. The child elements available for selection are separated by "|". In this way, we can write in the XML file as follows:

<Spouse> 〉

<Husband> Zhang San

</Spouse> 〉

Select only one child element.

In our XML file, there may also be many "empty elements", that is, the elements exist independently and there is no ending mark like "/element. The key words EMPTY are used in the DTD. Example: <:〈! The ELEMENT name is EMPTY> 〉. In an XML file, empty elements do not need to end the tag, but must be written in the format of "/empty element name.

In DTD, you can also declare something called Entity to apply the DTD and XML files. We can regard Entity as a constant, which has a certain value. In DTD, the declaration syntax of Entity is: <:〈! ENTITY entity-name entity-definition> 〉. For example, we declare <〈! Entity pc "(# PCDATA)">, in the subsequent element settings, you can apply this Entity to replace the "(# PCDATA)" string, such as: <:〈! ELEMENT author (# PCDATA)> can be written as <〈! ELEMENT author & PC;> ;〉. When referencing Entity, you must add the "&" symbol before the Entity name, followed by the ";" symbol.

In example 1, <,〈! ATTLIST price currency unit CDATA # REQUIRED> specifies the attributes of an element. The syntax for setting an element attribute is: <:〈! ATTLIST element-name attribute-name Type Default-value> 〉. Where, <,〈! ATTLIST is the setting of the starting attribute (case sensitive ). Element-name indicates the elements for which this attribute is set. attribute-name indicates the name of the set attribute. Type indicates the Type of the attribute value. there are multiple attribute values, it can be a common text, or take one of several attribute values. the types of attribute values are shown in Table 2. Default-value refers to the type of the attribute's internal value, which has four different attribute internal values (see table 3 ).

Let's take a few examples to see how to set some common element attributes. Example:

<〈! ATTLIST name gender (male | female) "male"> "〉

This element property is set to "name". This element sets an attribute named "gender". the attribute value is Enumerated, the value range is "male" or "female" (separated by "| ). If this attribute is not assigned in the XML file, the value is "male" because the attribute value is a string "male ".

<〈! ATTLIST name Number ID # REQUIRED> 〉

This attribute is used to set an attribute named "number" for the "name" element. the attribute value is ID, which means that when you assign a value to this attribute in an XML file, the value is unique in this XML file. for example, the following XML statement is displayed in the same XML file:

<Name number = "1234567"> James </name> 〉

<Name number = "1234567"> Li Si </name> 〉

Note: the value of the "number" attribute is repeated, so that an error message is displayed during parsing. The attribute value in this attribute setting is # REQUIRED, indicating that this attribute must be displayed in the "name" Mark of the XML file. Otherwise, an error occurs during parsing.

<〈! ATTLIST phone number country code CDATA # FIX "86"> "〉

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.