Welcome to our comprehensive guide on XML DTD Attribute Types! This tutorial is designed for both beginners and intermediate learners, providing a detailed understanding of XML Document Type Definitions (DTD) attribute types. Let's dive in!
Attribute types in XML DTD help define the type of data that an attribute can hold. This can ensure consistency and validity in your XML documents.
CDATA is used to define a section of text that should not be parsed or interpreted as XML. It's useful when you have data containing XML-reserved characters.
<!ATTLIST element_name
attribute_name CDATA "default_value">These types are used for unique identifiers. ID is for defining unique identifiers within a single XML document. IDREF and IDREFS are used to reference those unique identifiers from other elements.
<!ATTLIST element_name
attribute_name ID "default_value">
<!ATTLIST element_name
attribute_name IDREF #IMPLIED>
<!ATTLIST element_name
attribute_name IDREFS #IMPLIED>Enumerated attribute types restrict the attribute value to a predefined set of options.
<!ATTLIST element_name
attribute_name (option1 | option2 | option3) "default_value">These types allow only alphanumeric characters and the underscore _. NMTOKEN is for a single token, NMTOKENS for multiple tokens separated by whitespace, and NMTOKEN# for a token followed by a number.
<!ATTLIST element_name
attribute_name NMTOKEN "default_value">
<!ATTLIST element_name
attribute_name NMTOKENS (token1 token2) "default_value">
<!ATTLIST element_name
attribute_name NMTOKEN# "default_value"><!ATTLIST element_name
attribute_name (value1 | value2 | value3) "default_value">
<!ATTLIST element_name
attribute_name (value1 value2) "default_value">
<!ATTLIST element_name
attribute_name NMTOKEN# "default_value">Let's create an XML document with attributes using some of the discussed types.
<!DOCTYPE Books SYSTEM "Books.dtd">
<Books>
<Book id="book1" category="Fiction">
<Title>The Great Gatsby</Title>
<Author>F. Scott Fitzgerald</Author>
</Book>
<Book id="book2" category="Non-Fiction">
<Title>The Power of Now</Title>
<Author>Eckhart Tolle</Author>
</Book>
</Books>Here's the corresponding DTD:
<!ELEMENT Books (Book+)>
<!ELEMENT Book (Title, Author)>
<!ATTLIST Book
id ID #REQUIRED>
<!ATTLIST Book
category (Fiction | Non-Fiction) "Fiction">What does the `CDATA` attribute type do in XML DTD?
Stay tuned for our next lesson on XML DTD Entity References! 🚀