[Technical learning] Getting started with XML DTD

Source: Internet
Author: User
Tags cdata

 

Getting started with XML DTD(09:08:49)
Tags: Miscellaneous  
 

DTD can actually be seen as a template for one or more XML files. The elements in these XML files, the attributes of elements, the arrangement/sequence of elements, and the content that can be contained by elements, must comply with the definition in the DTD. The elements in the XML file, that is, the tag we created, are created based on the actual situation of our application. 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 application field, such as medicine, construction, industry and commerce, and administration. The wider the covering range of elements defined by DTD, the more complicated it is.

DTD can be a completely independent file, or can be directly set in an XML file. Therefore, DTD can be divided into two types: External DTD (calling another edited 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 interact with each other, and their electronic documents are all in XML format. We can place the DTD of these XML documents somewhere so that all the exchanged XML documents can use this DTD. This is the most convenient method and also applies to internal XML files within the company.

Internal DTD

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

 

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

]>

 

<〈! Doctype: Specify the DTD. Note that docutype is in uppercase.

Element-Name: specify the name of the root element of this DTD. an XML file can have only one root element. Note: If the XML file uses a DTD, the root element in the file is specified here.

[...]>: Defines the elements used by the XML file 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 a general framework defined by DTD. It defines DTD for other XML files. The structure is similar to the DTD in Example 1, you may only need to add, delete, or modify something. Element settings are defined in the middle of a DTD definition. This is the most important part of a DTD. Its main syntax is as follows:

 

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

 

<〈! Element: Indicates starting element settings. Note that the element keyword is in uppercase.

Element-name: the 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 references (books)> The element is set to declare the "references" element, and it is the parent element of the "books" 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 # pcdata keyword.

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 appear in the order listed above in the XML file. Each tag must and can only appear once. If you set the element, follow the <〈! Element book any>, so that the element can contain any set element, and the number and sequence of occurrences are not limited, in addition to sub-elements, it can also contain general text. Sometimes, in an XML file, a tag may appear multiple times (or not). In addition to using the any keyword in their parent element, you can also add a specific symbol next to the element to control the number of occurrences of the tag. See table 1.

 

Table 1

Symbol

Indicates the number of times the tag appears.

?

Does not appear or appears only once

*

Does not appear or can appear multiple times

+

Must appear more than once

Unsigned

Only once

 

Example: <:〈! Element references (Books, Newspapers +, magazines ?, Website)> This element sets that the "books" Mark can appear in XML files no longer or multiple times; the "newspaper" mark must appear more than once; the "magazine" Mark can appear only once or not. The "website" mark must appear only once.
In some parent element declarations, it is possible that the child element contained in it is used by selecting one of multiple child elements. 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. In DTD, the empty keyword is used for declaration. 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 for using 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 use 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 element to which this attribute is set. Attribute-name indicates the name of the set attribute. type indicates the category 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 category 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 category 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 appears in the same XML file:

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. In DTD, the empty keyword is used for declaration. 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 for using 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 use 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 element to which this attribute is set. Attribute-name indicates the name of the set attribute. type indicates the category 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 category 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 category 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 appears 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 will appear during parsing. The attribute value in this attribute setting is # required, indicating that this attribute must appear in the "name" mark of the XML file; otherwise, an error occurs during parsing.

<〈! ATTLIST phone number Country Code CDATA # Fix "86"> "〉

This attribute is used to set a property named "country code" for the "phone number" element. The value of this property is a common text. This attribute cannot be set in the phone number tag because it is set to an attribute with a fixed value (# Fix keyword ), the parser automatically adds this property and the value "86" to the phone number mark.

 

Table 2

Attribute Value category

Description

CDATA

Attribute values are only common characters.

Enumerated

Lists the value ranges of this attribute. Only one attribute value can be assigned to the attribute at a time.

Nmtoken

Indicates that the attribute value can only contain letters, numbers, underscores,.,:, and.

Nmtokens

The attribute value can be composed of multiple nmtoken, and each nmtoken is separated by a space.

ID

This attribute is unique in an XML file and is often used to represent a person's ID card number.

Idref

This indicates that the property value is based on another ID attribute.

Idrefs

This attribute value refers to multiple ID attributes. The values of these id attributes are separated by spaces.

Entity

Indicates that the set value of this attribute is an external entity, such as an image file.

Entities

This attribute value contains multiple external entity, which are separated by spaces.

Notation

The property value is the notation declared in the DTD (which application software is used to interpret some binary files and slices ).

 

In the XML specification, two attributes are defined, namely, XML: space and XML: Lang. The attribute name specified in the Specification starts with XML, the property name you define cannot start with XML:. Otherwise, an error occurs during parsing.

As we have mentioned earlier, the blank space is meaningful in the XML file. The XML: space attribute is the processing method for setting the parser to pass the blank space in the XML file to the application. XML: space is an enumerated attribute. It can only be set between default and preserver. XML: Space = "defaule" indicates that after the parser passes the blank cells to the application, the method defined in the application will process the blank cells. If the XML: space attribute is not set, the parser uses default to set this attribute by default. XML: Space = "preserver" indicates that the parser requires the application to keep the blank cells after passing the blank cells to the application.

The XML: lang attribute is used to set the language in which the text information in the tag is used, and the ISO-639 specifies the representative abbreviations of different languages, such as: XML: lang = "en" for English; XML: lang = "La" indicates Latin; XML: lang = "ZH" indicates Chinese materials; XML: lang = "ZH-CN" indicates Chinese (simplified); XML: lang = "ZH-tw" indicates Chinese (traditional ). The system determines XML: lang = "en", that is, the text information in the mark is in English.

External DTD

An external DTD is a file independent of an XML file. In fact, it is also a text file. It only uses. DTD as the file extension. Because the external DTD is independent from the XML file, it can be used by multiple XML files, just like using the same template to write multiple files with different content, because these XML files reference the same external DTD, their structures are roughly the same.

The external DTD is created in the same way as the internal DTD. The internal DTD example in Example 1 is written using the external DTD. The file is as follows. The file is stored as a file with the suffix. DTD.

 

<〈? XML version = "1.0" encoding = "gb2312 "? > 〉

<〈! Element references (books *)> *)〉

<〈! Element books (name, author, price)> )〉

<〈! Element name (# pcdata)> )〉

<〈! Element author (# pcdata)> )〉

<〈! Element price (# pcdata)> )〉

<〈! ATTLIST price currency unit CDATA # required> 〉

 

Except for the <〈! Doctype references [...]>. In addition, the number of elements, the order of arrangement, the setting of null elements, the selective elements, the entity statement, and the attribute setting are the same as those of the internal DTD.

XML File Usage <〈! Doctype element-name system DTD-URL> or <〈! Doctype element-name public DTD-name DTD-URL> to reference the created external DTD file.

Table 3

Attribute Value

Description

# Required

This attribute must appear in the tag.

# Implied

This attribute does not appear in the tag.

# Fix

The attribute value is a fixed value.

String

If no attribute value is specified in the tag, this string is the value of this attribute.

This statement must be located in the preface of the XML file, where, <,〈! Doctype indicates that the external DTD is declared. element-name indicates the name of the root element of the DTD. system indicates that the external DTD file is private, that is, it is created by ourselves, no public release is available, but it is used by an individual or within the company or between several partners. The pubic keyword indicates that the external DTD is public. After public discussion, the public DTD has a logical name-DTD-name, which must be specified during the call. The DTD-URL is a URL that specifies the location of an external DTD file. For example, the document named ckzl. DTD is stored in the URL http://www.xml.com. The declaration in the XML file is as follows:

<〈? XML version = "1.0" encoding = "gb2312 "? > 〉

<〈! Doctype references System "http://www.xml.com/ckzl.dtd"> "〉

 

 

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.