XML Tutorials
Some XML elements have attributes. property contains information that is used by the application. Property provides additional information about the element (such as ID number, etc.) only when the program reads and writes the element, which is meaningless for the human to read and write elements. Learn about the various attribute types and how to declare them in the DTD in this chapter.
The contents of this chapter are as follows:
* What is a property?
* How to declare a property in a DTD
* How to declare multiple properties
* How to specify default values for properties
* Attribute Type
* Predefined Properties
* Based on the attributes of the baseball game statistical data DTD
10.1 What is a property?
In the 3rd chapter, it was discussed that the start tag and the null tag can contain pairs of attribute names and property values separated by the equal sign "=". For example:
<greeting language= "中文版" >
Hello xml!
<movie source= "Wavinghand.mov"/>
</GREETING>
In the example above, the greeting element has a language attribute whose property value is 中文版. The movie element has the Source property whose property value is Wavinghand.mov. The greeting element content is Hello xml!. The language of the writing is a useful message to the content itself, but the language is not part of the content.
Similarly, the movie element content is binary data saved in a Wavinghand.mov file. Although the filename tells us where to find the element content, it is not an element content itself. Again, the attribute contains information about the content of the element, not the element content itself.
An element can have multiple properties, such as:
<rectangle width= "height="/>
<script language= "javascript" encoding= "8859_1" >...</SCRIPT>
In the example above, the value of the script element property language is javascript,script element property encoding the value of 8859_1;rectangle element attribute width is the value of 30;rect element property height is 45. These attribute values are string data, not numeric data.
The end tag cannot be marked with an attribute, and the following example is illegal:
<script>...</script language= "javascript" encoding= "8859_1" >
10.2 Declaring properties in a DTD
Similar to elements and entities, you need to declare attributes in the document's DTD in order to maintain the legitimacy of the document. <! The attlist> tag is used to declare a property in the form of the following:
<! Attlist element_name attribute_name Type default_value>
Element_name is the name of the element that owns the property. Attribute_name is the property name, and type is one of the 10 valid attribute types listed in table 10-1. The most commonly used property types are CDATA. Finally, if no attribute value is specified, the property value is Default_value.
For example, study the following elements:
<greeting language= "Spanish" >
hola!
</GREETING>
In a DTD, you declare the element in the following format:
<! ELEMENT greeting (#PCDATA) >
<! Attlist greeting LANGUAGE CDATA "中文版" >
<! The element> tag simply shows that the greeting element contains analytical character data, and there is nothing new here. <! The attlist> tag indicates that the greeting element has the language attribute, whose value is a CDATA type, essentially the same as the #pcdata of the element content. If there is no language attribute in the greeting tag you see, the Language property value is the default specified 中文版.
Table 10-1 Property Types
Type
Meaning
Cdata
Character data is not a marked text
Enumerated
A list of possible values from which to choose the correct value
Id
A number that cannot be shared by any other ID type attribute in the document, unique
IDREF
The value of the ID type attribute of the element in the document
IDREFS
Several IDs separated by a space
ENTITY
Entity names declared in the DTD
Entities
The names of several entities declared in the DTD, separated by spaces
NmToken
XML name
notation
The name of the annotation declared in the DTD
Nmtokens
Multiple XML names separated by a space
Declare their own list of properties in their respective tags. The name of the element to which the attribute belongs is contained in the <! Attlist> tags, such as the property declarations in the previous example are used only for greeting elements. If the remaining elements also have language attributes, they require separate <! Attlist> statement.
For most declarations, the order in which a property declaration appears in a document is not strictly required and can be located either before or after the element declaration to which it is connected. In fact, you can even make multiple declarations of the same attribute. At this point, the first declaration is executed first.
Although unusual, you can even declare attributes for tokens that do not exist. When you initially edit the DTD, you can declare some properties that do not exist and plan to return here later to continue the work, declaring the elements for those attributes.
10.3 Declaring multiple properties
An element usually has more than one attribute. The IMG element of HTML can have height, WIDTH, ALT, BORDER, Align, and several other attributes. In fact, most HTML tags have multiple attributes, as are XML tags. For example, a natural rectangle element requires the length and width attributes:
<rectangle length= "70px" width= "85px"/>
You can also use several attribute declarations to declare these properties, and a property declaration to correspond to one property. For example:
<! ELEMENT RECTANGLE empty>
<! Attlist RECTANGLE LENGTH CDATA "0px" >
<! Attlist RECTANGLE WIDTH CDATA "0px" >
The example above shows that the rectangle element has the length and width properties, and their default values are 0px.
Two <! can be combined in the following manner Attlist> marked as a single declaration:
<! Attlist RECTANGLE LENGTH CDATA "0px"
WIDTH CDATA "0px" >
The declaration declares the length and width properties, two property types are CDATA, and the default value is 0px. This syntax structure can also be declared if the types or defaults of each property are different. As shown below:
<! Attlist RECTANGLE LENGTH CDATA "15px"
WIDTH CDATA "34pt" >
Personally, I don't like this style. It looks messy and, for easy identification, relies too much on the proper placement of extra spaces (although these spaces are not important to the actual meaning of the label). But you will certainly meet other people writing, this style of DTD, so you must master this writing method.
10.4 Specify the default value for the property
If you do not explicitly specify a default property value (such as 0px), a property declaration can require the author to provide a property value, or ignore the property value entirely, or even use the default value. These three types were #required, #IMPLIED, #FIXED加以指定 by three keywords.
10.4.1 #REQUIRED
Sometimes it's not easy to choose an appropriate default property value. For example, when you create a DTD for your intranet, you may require that all documents have at least one blank <AUTHOR1> tag, which is not usually displayed, but can be used to identify the author who created the document. tag with the name, email, and extension properties in order to contact the author. For example:
<author name= "Elliotte Rusty Harold"
email= elharo@metalab.unc.edu extension= "3459"/>
Suppose you want to enforce the identity of the person who posted the document on the internal network, and you do not take the method that provides the default value for these properties. However, XML cannot prevent anyone from identifying the author as "Luke Skywalker" (Rockwell Skywalker), but at least by using the default value of #required, the author is required to be a person. For example:
<! ELEMENT AUTHOR empty>
<! Attlist AUTHOR NAME CDATA #REQUIRED >
<! Attlist AUTHOR EMAIL CDATA #REQUIRED >
<! Attlist AUTHOR EXTENSION CDATA #REQUIRED >
If the parser encounters a <AUTHOR/> tag that does not contain one or more of these attributes, an error is returned.
You can also use #required to force the author to submit the width, height, and ALT attributes of an IMG element. For example:
<! ELEMENT IMG empty>
<! Attlist IMG ALT CDATA #REQUIRED >
<! Attlist IMG WIDTH CDATA #REQUIRED >
<! Attlist IMG HEIGHT CDATA #REQUIRED >
Any attempt to ignore these attributes (too many of these web pages) will produce an illegal document. The XML processor will notice this error and will notify the author of the missing attributes.
10.4.2 #IMPLIED
Sometimes you may find a good property default, but you do not want the document author to include this attribute value. For example, suppose some people who post documents on the internal Web have e-mail addresses, but they do not have a telephone extension, and for this reason they do not want to include the extension (extension) attribute section in the <AUTHOR/> tag. For example:
<author name= "Elliotte Rusty Harold"
Email= "elharo@metalab.unc.edu"/>
If you still do not want to provide a default property value for extension (extension number), you want to provide the ability of the author to introduce such a similar attribute. In this case, you can use the default value of #implied. As shown below:
<! ELEMENT AUTHOR empty>
<! Attlist AUTHOR NAME CDATA #REQUIRED >
<! Attlist AUTHOR EMAIL CDATA #REQUIRED >
<! Attlist AUTHOR EXTENSION CDATA #IMPLIED >
If the XML processor encounters a <AUTHOR/> tag that does not have a extension attribute, it does not provide useful property values to the XML application. The application makes the appropriate selections according to the notifications received. For example, an application feeds an element into a SQL database, the property is mapped to a field, and the application may insert blank data in the corresponding field in the database.
10.4.3 #FIXED
Finally, you may want to provide a property default value that does not allow authors to change. For example, you want to specify an equivalent company identity attribute for the author element of the person who posted the document on the internal network. The method is as follows:
<author name= "Elliotte Rusty Harold" company= "TIC"
Email= "elharo@metalab.unc.edu" extension= "3459"/>
You can require all people to use the default value for the company property by specifying a default value of #fixed and then following the actual default value. For example:
<! ELEMENT AUTHOR empty>
<! Attlist AUTHOR NAME CDATA #REQUIRED >
<! Attlist AUTHOR EMAIL CDATA #REQUIRED >
<! Attlist AUTHOR EXTENSION CDATA #IMPLIED >
<! Attlist AUTHOR company CDATA #FIXED "TIC" >
Document authors do not need to really refer to fixed attributes in their respective tags. If they do not include a fixed attribute, the default value is used, and if a fixed property is included, the parser will return an error signal if the property values they use must be the same anyway.
10.5 Property Types
All previous examples have properties of CDATA types. CDATA is the most common type, but it also allows for the use of nine other property types. All of the 10 types are as follows:
* CDATA
* Enumerated (enum)
* NmToken
* Nmtokens
* ID
* IDREF
* IDREFS
* ENTITY
* Entities
* notation
The nine of the above attribute types in the Type field are constants, and enumerated is a special type that indicates that the property value must be one of the list of possible values. The following is an in-depth study of each type.
10.5.1 CDATA Attribute Type
The most common type of property is CDATA, which indicates that the property value is any text string that does not include the less-than sign (<) and the quotation mark ("). The less-than and quotation-mark characters can be inserted by means of an ordinary entity reference (<; and ";") or by a character referencing a Unicode value. Original and number (&)