• No results found

Specifying a Tokenized Type

In document XML Step by Step, Second Edition (Page 101-105)

Like any attribute value, the value you assign to a tokenized type must be a quoted string that conforms to the general rules described in “Rules for Legal Attribute Values” on page 65.

In addition, the value must conform to the particular constraint that you specify in the attribute definition by using an appropriate keyword. For example, in the following XML document, the StockCode attribute is defined as a tokenized type using the ID keyword. (ID is only one of the keywords you can use to de-clare a tokenized type.) This keyword means that for each element the attribute must be assigned a unique value. (For example, assigning the stock code “S021”

to two ITEM elements would be invalid.)

<?xml version=”1.0"?>

<!DOCTYPE INVENTORY [

<!ELEMENT INVENTORY (ITEM*)>

Chapter 5 Creating Valid XML Documents Using Document Type Definitions 111

5Document Type Definitions

<!ELEMENT ITEM (#PCDATA)>

<!ATTLIST ITEM StockCode ID #REQUIRED>

]

>

<INVENTORY>

<!-- Each ITEM must have a different StockCode value. -->

<ITEM StockCode=”S021">Peach Tea Pot</ITEM>

<ITEM StockCode=”S034">Electric Coffee Grinder</ITEM>

<ITEM StockCode=”S086">Candy Thermometer</ITEM>

</INVENTORY>

Here’s a complete list of the keywords you can use to define tokenized type at-tributes and the constraints they impose on the attribute values:

ID. In each element the attribute must have a unique value. The value must begin with a letter or underscore (_) followed by zero or more letters, digits, periods (.), hyphens (-), or underscores. How-ever, the XML specification states that ID attribute values beginning with the letters xml (in any combination of uppercase or lowercase letters) are “reserved for standardization.” Although Internet Ex-plorer doesn’t enforce this restriction, it’s better not to begin names with xml to avoid future problems. In addition, a particular element type can have only one ID type attribute, and the attribute’s default declaration must be either #REQUIRED or #IMPLIED (described later in the chapter). You can see an example of this type of attribute in the INVENTORY document given above.

note

#REQUIRED and #IMPLIED are the two forms of default declaration in which you don’t specify a default value for the attribute. It doesn’t make sense to give

112 XML Step by Step

For example, you might add an IDREF attribute named GoesWith to the ITEM element:

<!ELEMENT ITEM (#PCDATA)>

<!ATTLIST ITEM

StockCode ID #REQUIRED GoesWith IDREF #IMPLIED>

You could then use this attribute to refer to another ITEM element, as shown here:

<ITEM StockCode=”S034">Electric Coffee Grinder</ITEM>

<ITEM StockCode=”S047" GoesWith=”S034">

Coffee Grinder Brush

</ITEM>

IDREFS. This type of attribute is just like an IDREF type, except that the value can include references to several identifiers—separated with white space characters—all within the quoted string. For ex-ample, if you assigned to the GoesWith attribute the IDREFS type like this:

<!ELEMENT ITEM (#PCDATA)>

<!ATTLIST ITEM

StockCode ID #REQUIRED GoesWith IDREFS #IMPLIED>

you could use it to refer to several other elements:

<ITEM StockCode=”S034">Electric Coffee Grinder</ITEM>

<ITEM StockCode=”S039">

1 pound Breakfast Blend Coffee Beans

</ITEM>

<ITEM StockCode=”S047" GoesWith=”S034 S039">

Coffee Grinder Brush

</ITEM>

ENTITY. The attribute value must match the name of an unparsed entity declared in the DTD. An unparsed entity refers to an external file, typically one storing non-XML data. I’ll discuss these entities in Chapter 6.

For example, in the DTD you might declare an element named IM-AGE to represent a graphic image and an ENTITY type attribute named Source to indicate the source of the graphic data, like this:

<!ELEMENT IMAGE EMPTY>

<!ATTLIST IMAGE Source ENTITY #REQUIRED>

Chapter 5 Creating Valid XML Documents Using Document Type Definitions 113

5Document Type Definitions

If you’ve declared an unparsed entity named Logo (using techniques you’ll learn in Chapter 6) that contains the graphic data for an im-age, you could assign that entity to the Source attribute of an IM-AGE element in the document, like this:

<IMAGE Source=”Logo” />

ENTITIES. This type of attribute is just like an ENTITY type, ex-cept that the value can include the names of several unparsed enti-ties—separated with white space characters—all within the quoted string. For example, if you defined the Source attribute to have the ENTITIES type, like this:

<!ELEMENT IMAGE EMPTY>

<!ATTLIST IMAGE Source ENTITIES #REQUIRED>

you could use it to refer to several unparsed entities (perhaps entities storing the graphics data in alternative formats), like this:

<IMAGE Source=”LogoGif LogoBmp” />

(This example assumes that LogoGif and LogoBmp are the names of unparsed entities that have been declared in the DTD using tech-niques you’ll learn in Chapter 6.)

NMTOKEN. The attribute value must be a name token, which con-sists of one or more letters, digits, periods (.), hyphens (-), or under-scores (_). A name token can also contain a single colon (:) except in the first character position. (As you can see, the rules for a name to-ken are less stringent than those for an ID attribute value or the name of an item such as an element, attribute, or entity.) For ex-ample, if you assigned to the ISBN attribute the NMTOKEN type, like this:

<!ELEMENT BOOK (#PCDATA)>

<!ATTLIST BOOK ISBN NMTOKEN #REQUIRED>

114 XML Step by Step

<!ELEMENT SHIRT (#PCDATA)>

<!ATTLIST SHIRT Codes NMTOKENS #REQUIRED>

you could assign it several name token values:

<SHIRT Codes=”38 21 97">long sleeve Henley</SHIRT>

In document XML Step by Step, Second Edition (Page 101-105)