Declaring Attributes

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark     next disabled - end of section

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Appendix B.  XML: A Quick Tour


Attributes are a mechanism with which you can provide further information about an element. You declare the attributes for an element in a DTD after declaring the element. The attribute declaration is of the following form:

<!ATTLIST element_name attribute_definition> 

where attribute_definition takes the following form:

attribute_name attribute_type [default_declaration] 

The attribute definition provides the following information:

  • The name of the attribute

  • The type of the attribute

  • Default value for the attribute, or whether the attribute is required

Attribute Name

The name of the attribute is self-explanatory. Attribute names can be anything as long as they follow the naming rules that were described in the element declaration section of the chapter.

Attribute Type

The type of an attribute can be one of the following three types:

  • A string

  • A tokenized attribute

  • An enumerated attribute

String Attribute Type

An attribute of type string means that the attribute can take a string of characters as its value. To declare an attribute of type string, you need to do the following:

<!ATTLIST attribute_name CDATA> 

The CDATA keyword specifies that the attribute can take only string values. It is important to remember that the attribute can also take an internal entity reference. This is possible because the XML parser automatically replaces an entity reference with its replacement text while parsing the XML file.

In our DTD for the CarParts.xml file, the supplier element has name and URL attributes that can take string values. It is declared as follows:

<!ATTLIST supplier             name CDATA #REQUIRED             URL CDATA #REQUIRED > 

The entry in the CarParts.xml file for the supplier element is as follows:

<supplier name="&companyname;" URL="&companyweb;"> </supplier> 
Tokenized Attribute Types

A tokenized attribute type can have one of the following values: ID, IDREF, IDREFS, ENTITY, ENTITIES, NMTOKEN, or NMTOKENS.

ID

This attribute acts as the identifier for an element. The ID attribute value has to be unique for each element in a document. For example, consider the engine element that as an attribute called ID, which is declared as follows:

<!ATTLIST engine id ID #REQUIRED> 

The corresponding entry in the XML file is as follows:

<engine  type="Alpha37" capacity="2500" price="3500"> 
IDREF

The value of an attribute of type IDREF is a pointer to the value of an ID type attribute value existing in the XML document. For example, suppose the CarParts.xml file had another element called engineByOtherName, with an attribute called idref of type IDREF; the declaration would have been of the following form:

<!ATTLIST engineByOtherName idref IDREF> 

and implemented as

<engineByOtherName idref="E129"> 
IDREFS

The value of an attribute of type IDREFS is a collection of IDREF type values.

ENTITY

The attributes of type ENTITY are used to refer to a binary external entity defined in the DTD. This attribute type is often used to refer to graphic images in an XML document.

The following is an example of how you can use an ENTITY type attribute. Suppose you have an XML document that describes furniture. Now you want to add an image called tableImage.gif to your XML file. The place where you want to add it is to the tableImage element. To do so, you need to declare the tableImage.gif as an external entity, say TableImage, by doing the following:

<!ENTITY TableImage SYSTEM "/tableImage.gif"> 

Next, specify the tableImage element as an empty element with an attribute called picture, of the ENTITY type, by adding the following:

<!ELEMENT tableImage EMPTY> <!ATTLIST tableImage picture ENTITY> 

Then in the actual XML file, you will enter the value of the picture attribute as &tableImage;. The code snippet is as follows:

<tableImage picture='&TableImage;'/> 
ENTITIES

The value of an attribute of the ENTITIES type is a collection of ENTITY type values separated from each other by a space.

NMTOKEN

The value of an attribute of the NMTOKEN type is a string.

NMTOKENS

The value of an attribute of the NMTOKENS type is a collection of NMTOKEN-type values separated from each other by spaces.

Enumerated Attribute Types

An attribute that is of the enumerated type can take only one of the values that was declared in the DTD. For example, in the DTD for the CarParts.xml file, the capacity attribute of the engine element is an enumerated type attribute because the only values it can take are 1000, 2000, or 2500. The way to declare it is as follows:

<!ATTLIST capacity(1000|2000|2500) #REQUIRED> 

The corresponding entry in the CarParts.xml file is as follows:

<engine  type="Alpha37" capacity="2500" price="3500"> 

Default Attribute Values

Finally, let's look at the default values that an attribute can take. Default values are specified through the attribute declaration to specify the action that an XML parser will take when it does not find an attribute in an element. Default values are specified through one of the three keywords:

  • #REQUIRED Implies that the attribute value must be specified. If the XML processor does not find the attribute, it will raise an error.

  • #IMPLIED Means that the XML parser will inform the application that no value was specified for the attribute, and it will be an application code that has to determine what to do about it.

  • #FIXED Means that the value specified for the attribute must completely match the value specified in the attribute declaration. A mismatch will cause the XML parser to raise an error.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark     next disabled - end of section
Top

[0672324342/app02lev1sec15]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net