DTD - NMTOKEN and NMTOKENS Attribute Types in DTD

In XML Document Type Definitions (DTD), NMTOKEN and NMTOKENS are attribute types used to restrict the kind of text that can be assigned to an XML attribute. Both are based on XML name-token rules, but they differ in the number of values they allow. NMTOKEN allows a single name token, while NMTOKENS allows a space-separated list of name tokens. These attribute types are useful when an XML document needs controlled attribute values without requiring those values to match declared element or attribute names.

1. What is NMTOKEN?

NMTOKEN stands for Name Token. It specifies that an attribute value must contain a valid XML name token.

Unlike the ID type, an NMTOKEN value does not have to be unique within the document. It also does not need to correspond to an element or attribute name.

For example:

<!ELEMENT student EMPTY>
<!ATTLIST student code NMTOKEN #REQUIRED>

A valid XML document could contain:

<student code="STU123"/>

Here, code is declared as an NMTOKEN attribute, so STU123 is accepted as a name token.

Another example is:

<student code="student_101"/>

The value student_101 is also acceptable.

2. What values can NMTOKEN contain?

An NMTOKEN value can contain characters permitted in an XML name token. It may contain letters, digits, underscores, hyphens, periods, and other characters allowed by XML's Nmtoken production.

For example:

<!ATTLIST product reference NMTOKEN #REQUIRED>

Possible values include:

<product reference="P100"/>
<product reference="P-100"/>
<product reference="P_100"/>
<product reference="P.100"/>

The important point is that NMTOKEN is a lexical restriction. It checks whether the value follows the permitted name-token syntax. It does not check whether the value represents a particular predefined category.

For example:

<product reference="ABC123"/>

may be valid, but the DTD does not automatically know what ABC123 means.

3. What is NMTOKENS?

NMTOKENS is the plural form of NMTOKEN. It allows an attribute to contain multiple name tokens separated by whitespace.

For example:

<!ELEMENT book EMPTY>
<!ATTLIST book keywords NMTOKENS #REQUIRED>

A valid document could contain:

<book keywords="xml dtd validation"/>

The value contains three separate name tokens:

xml
dtd
validation

Another example is:

<book keywords="computer_science programming xml"/>

Here, computer_science, programming, and xml are individual name tokens.

4. Difference Between NMTOKEN and NMTOKENS

The primary difference is the number of values permitted.

Attribute Type Number of Values Example
NMTOKEN One name token code="STU101"
NMTOKENS Multiple name tokens subjects="xml dtd schema"

Consider the following DTD:

<!ELEMENT student EMPTY>
<!ATTLIST student code NMTOKEN #REQUIRED>

This is appropriate:

<student code="STU101"/>

But if multiple tokens are supplied:

<student code="STU101 STUDENT"/>

the value does not satisfy the single-token requirement.

For multiple values, the DTD should use NMTOKENS:

<!ELEMENT student EMPTY>
<!ATTLIST student codes NMTOKENS #REQUIRED>

Then this becomes appropriate:

<student codes="STU101 STUDENT"/>

5. NMTOKEN vs ID

NMTOKEN and ID may appear similar, but they serve different purposes.

An ID attribute identifies an element uniquely within an XML document.

For example:

<!ELEMENT student EMPTY>
<!ATTLIST student id ID #REQUIRED>

The following would be valid:

<student id="S101"/>
<student id="S102"/>

However, two elements cannot normally have the same ID value.

With NMTOKEN:

<!ATTLIST student code NMTOKEN #REQUIRED>

the same value may be used more than once:

<student code="STUDENT"/>
<student code="STUDENT"/>

Therefore:

ID = unique identifier

NMTOKEN = syntactically valid name token

This distinction is important when designing a DTD.

6. NMTOKEN vs Enumeration

Another important distinction is between NMTOKEN and an enumeration.

Suppose a DTD contains:

<!ATTLIST employee department (HR|Finance|Sales) #REQUIRED>

Only these values are permitted:

<employee department="HR"/>
<employee department="Finance"/>
<employee department="Sales"/>

A value such as:

<employee department="Marketing"/>

would not satisfy the enumeration.

With NMTOKEN:

<!ATTLIST employee department NMTOKEN #REQUIRED>

values such as:

<employee department="HR"/>
<employee department="Finance"/>
<employee department="Marketing"/>

can be accepted as long as they satisfy the name-token syntax.

Therefore, enumeration provides a fixed list of permitted values, whereas NMTOKEN provides a syntactic restriction.

7. Using NMTOKENS for Multiple Categories

NMTOKENS can be useful when an XML element belongs to several categories.

For example:

<!ELEMENT article EMPTY>
<!ATTLIST article categories NMTOKENS #IMPLIED>

The XML could be:

<article categories="technology programming education"/>

The categories are represented as separate whitespace-separated tokens.

This can be useful for metadata, classification, keywords, tags, or other situations where several labels need to be stored in one attribute.

However, the DTD does not verify whether the categories actually exist in some predefined list. It only applies the syntactic requirements of NMTOKENS.

8. Default Values with NMTOKEN and NMTOKENS

NMTOKEN and NMTOKENS can also be combined with attribute default declarations.

For example:

<!ELEMENT document EMPTY>
<!ATTLIST document type NMTOKEN "general">

If the XML document contains:

<document/>

the attribute has the declared default value:

general

For multiple tokens:

<!ELEMENT document EMPTY>
<!ATTLIST document tags NMTOKENS "general document">

the default consists of multiple whitespace-separated tokens.

The default value must satisfy the rules of the corresponding attribute type.

9. Important Characteristics

Several characteristics should be remembered when studying these attribute types.

First, NMTOKEN represents a single name token.

Second, NMTOKENS represents one or more name tokens separated by whitespace.

Third, neither type provides uniqueness. If uniqueness is required, ID is generally the appropriate DTD attribute type.

Fourth, neither type provides a predefined list of allowed values. If only specific values should be accepted, an enumeration is more appropriate.

Fifth, NMTOKENS is useful when several independent token values need to be stored within a single attribute.

10. Complete Example

Consider the following DTD:

<!ELEMENT course EMPTY>

<!ATTLIST course
    code NMTOKEN #REQUIRED
    topics NMTOKENS #REQUIRED
>

A corresponding XML document could be:

<?xml version="1.0"?>

<!DOCTYPE course SYSTEM "course.dtd">

<course
    code="CS101"
    topics="programming algorithms databases"/>

Here:

code="CS101"

uses NMTOKEN because CS101 is a single name token.

The following:

topics="programming algorithms databases"

uses NMTOKENS because it contains three separate name tokens.

The DTD ensures that the attribute values follow the required token syntax, but it does not determine whether CS101 is an actual course code or whether programming, algorithms, and databases are valid topics.

11. NMTOKEN and NMTOKENS in DTD Design

When designing a DTD, the choice between these types depends on the structure of the information.

Use NMTOKEN when an attribute should contain one token and you only need syntactic control.

Example:

<product code="P100"/>

Use NMTOKENS when an attribute should contain multiple whitespace-separated tokens.

Example:

<product tags="electronics computer hardware"/>

Use ID instead when the attribute represents a unique identifier.

Use an enumeration when the attribute must contain one value from a specific predefined set.

Understanding these differences helps prevent incorrect DTD design and makes XML documents easier to validate and maintain.

Conclusion

NMTOKEN and NMTOKENS are DTD attribute types designed to restrict attribute values according to XML name-token syntax. NMTOKEN permits a single token, whereas NMTOKENS permits a whitespace-separated collection of tokens. They are particularly useful for codes, labels, keywords, tags, and classification values where strict token formatting is required but uniqueness or a fixed list of values is not necessary.

The key distinction to remember is:

NMTOKEN  → One name token
NMTOKENS → One or more name tokens
ID       → Unique identifier
Enumeration → Value from a predefined list