DTD – Attributes

Welcome to The Coding College! In this tutorial, we’ll explore DTD Attributes, a vital component of Document Type Definitions (DTD) that allows XML elements to carry additional information. Attributes enhance the flexibility of XML documents by providing metadata or qualifying details about elements.

What Are Attributes in DTD?

Attributes in DTD are used to define additional properties for elements. They allow elements to store data or references in a structured manner. In DTD, attributes are defined using the <!ATTLIST> declaration.

Attribute Characteristics

  1. Attributes are always associated with elements.
  2. Each attribute has a name, type, and default value or behavior.
  3. Attributes are optional or required based on their definition.

Syntax for Defining Attributes

The general syntax for defining attributes in DTD is:

<!ATTLIST element-name attribute-name attribute-type default-value>
  • element-name: The element to which the attribute belongs.
  • attribute-name: The name of the attribute.
  • attribute-type: Specifies the type of the attribute (e.g., CDATA, ID, IDREF, etc.).
  • default-value: Specifies whether the attribute is required, optional, or has a default value.

Attribute Types in DTD

1. CDATA (Character Data)

The CDATA type allows the attribute to contain any string of characters.

Example

<!ATTLIST book title CDATA #REQUIRED>

Usage in XML:

<book title="Learning XML"/>

2. ID

The ID type specifies a unique identifier for an element within the XML document.

Example

<!ATTLIST book id ID #REQUIRED>

Usage in XML:

<book id="b101"/>
  • An ID must be unique across the entire XML document.
  • Only one attribute of type ID is allowed per element.

3. IDREF

The IDREF type specifies a reference to an existing ID in the document.

Example

<!ATTLIST reference bookref IDREF #REQUIRED>

Usage in XML:

<book id="b101"/>
<reference bookref="b101"/>
  • The value of IDREF must match the ID of another element in the XML document.

4. IDREFS

The IDREFS type allows multiple references to IDs, separated by spaces.

Example

<!ATTLIST library bookrefs IDREFS #IMPLIED>

Usage in XML:

<library bookrefs="b101 b102"/>
<book id="b101"/>
<book id="b102"/>

5. Enumerated Values

Attributes can have a predefined list of valid values.

Example

<!ATTLIST book category (Fiction | Non-Fiction | Science) "Fiction">

Usage in XML:

<book category="Science"/>
  • If no value is provided, the default value (Fiction) is used.

6. ENTITY

The ENTITY type references an external unparsed entity.

Example

<!ATTLIST image src ENTITY #REQUIRED>

Usage in XML:

<!ENTITY img1 "image1.jpg">
<image src="img1"/>

7. ENTITIES

The ENTITIES type allows multiple external entities, separated by spaces.

Example

<!ATTLIST gallery images ENTITIES #IMPLIED>

Usage in XML:

<gallery images="img1 img2"/>

8. NMTOKEN

The NMTOKEN type specifies a single valid XML name.

Example

<!ATTLIST item code NMTOKEN #REQUIRED>

Usage in XML:

<item code="item_123"/>

9. NMTOKENS

The NMTOKENS type specifies multiple valid XML names, separated by spaces.

Example

<!ATTLIST group members NMTOKENS #IMPLIED>

Usage in XML:

<group members="John Mary Paul"/>

Default Attribute Values

DTD allows you to define default behaviors for attributes:

1. #REQUIRED

The attribute must be specified in the XML document.

Example

<!ATTLIST book title CDATA #REQUIRED>

Usage in XML:

<book title="Learning XML"/>

2. #IMPLIED

The attribute is optional.

Example

<!ATTLIST book category CDATA #IMPLIED>

Usage in XML:

<book title="Learning XML"/>

3. Default Value

The attribute has a predefined value if not specified.

Example

<!ATTLIST book category CDATA "Fiction">

Usage in XML:

<book title="Learning XML"/>

Result:

<book title="Learning XML" category="Fiction"/>

Example: Combining Elements and Attributes

Here’s a complete DTD example with attributes:

DTD Definition

<!ELEMENT library (book+)>
<!ELEMENT book (title, author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>

<!ATTLIST book id ID #REQUIRED>
<!ATTLIST book category (Fiction | Non-Fiction | Science) "Fiction">
<!ATTLIST author nationality CDATA #IMPLIED>

XML Document

<!DOCTYPE library SYSTEM "library.dtd">
<library>
  <book id="b101" category="Science">
    <title>Learning XML</title>
    <author nationality="American">John Doe</author>
  </book>
  <book id="b102">
    <title>Mastering XQuery</title>
    <author>Jane Smith</author>
  </book>
</library>

Benefits of Attributes in DTD

  1. Metadata: Attributes provide additional context or metadata for elements.
  2. Flexibility: Allow elements to carry unique or reusable data.
  3. Validation: Ensure attributes follow predefined rules (e.g., types, values).
  4. Efficiency: Minimize redundancy by using attributes instead of nested elements.

Tools for Working with DTD Attributes

  1. XML Validators: Validate XML files against DTD definitions (e.g., XMLValidation.com).
  2. Oxygen XML Editor: A robust tool for creating and testing XML with DTDs.
  3. Altova XMLSpy: Popular for designing XML and DTDs visually.

Conclusion

DTD attributes enhance XML documents by adding flexibility and control over data representation. By defining attributes effectively, you ensure that your XML structure is both robust and easy to manage.

For more XML tutorials and programming insights, visit The Coding College! Keep exploring, keep coding!

Leave a Comment