XML Getting Started fine solution file format definition

Source: Internet
Author: User
Tags format define definition empty end format definition string version
An XML DTD can actually be viewed as a template for one or more XML files, the elements in these XML files, the attributes of the elements, the arrangement/order of elements, the content that elements can contain, and so on, must conform to the definitions in the DTD. The elements in the XML file, the tags we create, are created based on the actual circumstances of our application. It is difficult to create a highly integrated, adaptable DTD because all walks of life have their own industry characteristics, so DTDs are usually defined in a range of areas of application, such as medicine, architecture, industry and commerce, and administration. The more extensive the scope of the elements covered by the DTD definition, the more complex it becomes.

A DTD can be a completely separate file, or it can be set directly in an XML file. Therefore, the DTD is divided into external DTDs (invoking the other edited DTD in the XML file) and internal DTDs (DTD is set directly in the XML file). For example, there are dozens of interconnected, partner-related companies, vendors, and they exchange electronic documents with each other in XML documents. So we can put the DTD of these XML documents somewhere so that all the XML documents that are exchanged use this DTD, which is the easiest way to do it, and it also applies to XML files within the company.

Internal DTD
The internal DTD is defined in the file preamble of the XML file. Grammar:

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

]〉

〈! DOCTYPE: Indicates that the DTD is set to start, noting that Docutype is uppercase.

Element-name: Specifies the name of the root element of this DTD, and an XML file can have only one root element. Note that if the XML file uses a DTD, then the root element in the file is specified here.

[.........] : in the [] tag, define the XML file to use the element, and then end the definition of the DTD.

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 thought of as a general framework for a DTD definition, a DTD for other XML files, a structure that is similar to the DTD in Example 1, and it may be necessary to add, delete, or change something. In the middle of the DTD definition is the element setting, which is the main part of a DTD, and its main syntax is as follows:

〈! ELEMENT Element-name element-definition〉

〈! Element: Indicates the start element setting, note that the element keyword here is uppercase.

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

Element-definition: Specify what you want to define for this element, that is, what can be included between elements 〉...〈/elements, other elements or general text.

In Example 1, you can see that! Element Reference (book) is a setting that declares the element "reference", and it is the parent element of the "book" element. 〈! The element book (name, author, price) declares the element of "book", and it is the parent element of the three elements, "name", "Author", "Price". and! The element name (#PCDATA) is set to declare the element "name", but this element contains only generic text and is the basic element, as defined by the #pcdata keyword.

In an element setting, if the element contains more than one child element, such as:! Element Books (name, author, price) This is a declaration with multiple child elements, so the tags "name", "Author", "Price" must appear in the order in which they are arranged in the XML file, and each tag must and can only occur once. If you set the element, follow the "! The element book is any〉 so that the elements can contain arbitrarily set elements, the number and order of occurrences are unrestricted, and, under that element, can contain general text in addition to the child elements. Sometimes, in an XML file, a tag may appear multiple times (or not), so we can control the number of times the tag appears by adding a specific symbol next to its parent element, in addition to the ANY keyword. These symbols are shown in table 1.
Table 1
Symbol represents the number of times the mark appears
? Do not appear or appear only once
* Does not appear or can occur multiple times
+ must appear more than once
No sign can only appear once

For example:! Element reference (book, newspaper +, magazine, website) This is set, the "book" tag in the XML file can not appear or multiple times; the "newspaper" tag must appear more than once; the "magazine" Mark may not appear or appear only once; The tag must appear and can only occur once.

In the declaration of some parent element, it is possible that the child elements it contains are selected in multiple child elements to use, then when we declare the parent element, we can declare it as a selective element, for example: ELEMENT Spouse (Wife | husband). Selectable child elements with "|" Delimited so that we can write this in an XML file:

Spouse

Husband John 〈/Husband

〈/Spouse

Select only one child element from.

In our XML file, there may also be a lot of "empty elements", that is, the element is separate, there is no 〈/element such as the end tag. Then the DTD is declared with the empty keyword. such as: "! Element name empty〉. In an XML file, an empty element does not require a closing tag, but it must be written in 〈/empty element name.

In a DTD, you can also declare something called entity and use it for DTDs and XML files. We can think of entity as a constant, and it has a certain value. In the DTD, the entity declaration syntax is:! ENTITY entity-name entity-definition〉. For example: We declare in the DTD! ENTITY PC "(#PCDATA)", then in the following element settings, you can use this ENTITY to replace the "(#PCDATA)" This string, such as: "! The ELEMENT author (#PCDATA) can be written as! ELEMENT author &&pc;〉. When referencing entity, you must precede the entity name with the "&&" symbol followed by the ";" Symbol.

In Example 1, the! Attlist Price currency Unit CDATA #REQUIRED This sentence is the attribute of the set element, and the syntax for the element property setting is: Attlist element-name attribute-name Type default-value〉. of which,! Attlist is the setting of the start attribute (note case). Element-name indicates what element this attribute setting is for, Attribute-name is the name of the Set property, type is the property value of the property, the property value has many, can be general text, or from several attribute values, and so on, the type of property value is shown in table 2. Default-value refers to the type of the property's default value, with four different property defaults (see table 3).

Let's take a few examples to take a look at some of the commonly used element attribute settings. Cases:

〈! attlist name Sex (male | female) "Male"

This element property setting is to set a property named "Gender" for the Element "name", the attribute value category of this property is enumerated, the value range is "male" or "female" (with "|" Delimited). If you do not assign a value to this attribute in the XML file, the value is "male" because the property default is a string "male".

〈! Attlist Name Number ID #REQUIRED

The attribute setting is to set a property named "Number" for the "name" element, and the attribute value category is an ID, meaning that when assigning a value to this attribute in an XML file, the value is unique in this XML file, such as the following XML statement appearing in the same XML file:

Name number = "1234567" John 〈/name

Name number = "1234567" Dick 〈/name

Note: The value of the number attribute is repeated so that an error message will appear during parsing. The default value of the property in this property setting is #required, which means that the attribute must appear in the name tag of the XML file, otherwise parsing will be an error.

〈! Attlist Phone number country code CDATA #FIX "86"

This property is set to set a property named "Country Code" for the element "phone number", which is a generic literal. This property cannot be set in the phone number tag because the property is set to a property with a fixed value (#FIX关键字), and the parser automatically adds the property and the value "86" to the "phone number" tag.
Table 2
Attribute value category description
CDATA attribute values are just general text.
enumerated lists the range of values for this property, and only one attribute value at a time can be assigned to the property.
NmToken indicates that the property value can only be composed of letters, numbers, underscores 、.、:,-these symbols.
Nmtokens indicates that a property value can consist of multiple nmtoken, separated by a space between each nmtoken.
ID This attribute is unique in an XML file and is commonly used to represent a person's identity card number.
IDREF indicates that the property value is referenced by another id attribute.
IDREFS indicates that the property value is referenced by multiple ID attributes, and the values of those ID attributes are separated by spaces.
Entity indicates that the setting value of this property is an external entity, such as a picture file.
Entities This property value contains more than one external entity, separated by a space between the different entity.
The notation property value is the notation declared in the DTD (declares what application software interprets some binary files, such as pictures).

In the specification of XML, there are also two default attributes, namely: Xml:space and Xml:lang, the default attribute name is xml: the name of the property you define cannot begin with XML: Otherwise, an error will occur when parsing.

As we've already said, whitespace is meaningful in an XML file, and the Xml:space property is the way to set the parser to pass the blank space in the XML file to the application. Xml:space is a enumerated type of property that can only be evaluated between default and preserver. Xml:space= "Defaule" means that when the parser passes a blank space to the application, the empty spaces are processed by the application's default method. If the Xml:space property is not set, the parser defaults to set the property by default. Xml:space= "preserver" means that when the parser passes a blank space to an application, the application is required to retain these spaces.

The Xml:lang property is used to set the language in which the text information in the markup is used, and ISO-639 stipulates the abbreviation for the representative of the different languages, such as: xml:lang= "en" for English; xml:lang= "la" for Latin; xml:lang= "en" to express Chinese information xml:lang= "ZH-CN" means Chinese (Simplified); xml:lang= "Zh-tw" represents Chinese (traditional). System is xml:lang= "en", that is, the middle of the marking of the text information are in English.

External DTD
An external DTD is a file that is independent of an XML file and is actually a text file, using only the. DTD as the file name extension. Because an external DTD is independent of an XML file, it can be used by more than one XML file, like a file that uses the same template to write multiple different content, and the multiple XML files have roughly the same structure because they refer to the same external DTD.

The external DTD is created in the same way as the syntax and internal DTD, and the example of the internal DTD in Example 1 is written with an external DTD, as shown in the following file. The file is saved as a file with a. DTD named suffix.

〈?xml version= "1.0" encoding= "GB2312"?

〈! ELEMENT Reference (Book *)

〈! ELEMENT Books (name, author, Price)

〈! ELEMENT name (#PCDATA)

〈! ELEMENT author (#PCDATA)

〈! ELEMENT Price (#PCDATA)

〈! Attlist Price currency Unit CDATA #REQUIRED

In addition to no internal DTD in the! DOCTYPE reference [...] Statement, the other is the same. Also, the number of elements, the sort order, the empty element setting, the optional element, the entity declaration, and the attribute setting are all the same as the internal DTD.

XML file Use! 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 default description
The #required indicates that this property must appear in markup.
This property may not appear in #implied tag.
The value of the #fix property is a fixed value.
If the value of the property is not specified in the string token, then this string is the value of this property.

This statement must be located in the file prolog of the XML file, where,! DOCTYPE indicates that a start declaration applies an external dtd;element-name to the name of the root element of the DTD; system means that the external DTD file is private, that we created it ourselves, that it was not published publicly, but that it was used by individuals or within the company or between several cooperating units While the pubic keyword means that the external DTD is public, and after a public discussion, there is a logical name for the DTD that is publicly--dtd-name, and we must specify this logical name at the time of the call. The Dtd-url is a URL that indicates the location of the external DTD file. For example, our DTD file is stored in the URL: http://www.xml.com/This place, the file name is CKZL.DTD. The declaration in the XML file is as follows:

〈?xml version= "1.0" encoding= "GB2312"?

〈! DOCTYPE reference SYSTEM " Http://www.xml.com/ckzl.dtd "

...

Introduction to Schemas
The syntax of a DTD is quite complex, and it does not conform to the standard of XML files and is a self-contained system. The above description is just an introduction to help you read the DTD file and create a simple DTD file if necessary, because many XML applications are now built on DTDs.

In addition, one of the obvious benefits of the Schema,schema, which replaces DTDs, is that the XML Schema document itself is also an XML document, rather than using self-contained syntax like a DTD. This facilitates both users and developers, because you can use the same tools to process XML schemas and other XML information without having to use special tools specifically for schemas. Schemas are straightforward, and people who know the syntax and rules of XML can immediately understand it. The concept of schema has been proposed for a long time, but the standards of the consortium have only recently come out, the corresponding application support has not been perfected, but the adoption of schema has become a trend of XML development.

Example 1
DTD definition Area:

〈?xml version= "1.0" encoding= "GB2312"?

〈! DOCTYPE Reference [

〈! ELEMENT Reference (book)

〈! ELEMENT Books (name, author, Price)

〈! ELEMENT name (#PCDATA)

〈! ELEMENT author (#PCDATA)

〈! ELEMENT Price (#PCDATA)

〈! Attlist Price currency Unit CDATA #REQUIRED

]〉

Resources

Books

Name 〉xml Introductory Refinement 〈/name

Author John 〈/author

Price currency unit = "RMB" 〉20.00〈/price

〈/Books

Books

Name 〉xml Grammar 〈/name

!--will be published--〉

Author Dick 〈/author

Price currency unit = "RMB" 〉18.00〈/price

〈/Books

〈/Resources

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.