Joshua Enfield
Joshua Enfield

Reputation: 18288

DTD - Is there a way to require an attribute with a specific value be present?

I want to do require something like this:

<div type="foobar"></div>

In layman speak I want to require the document to have a foobar type element. More specifically I want to require the attribute value pair type and "foobar" to be uniquely present.

I realize I can do something like:

<!ATTLIST div type CDATA #REQUIRED>

But this doesn't constrain the value like I want.

Alternatively

<!ATTLIST div type CDATA #FIXED "foobar">

Requires the attribute to be a certain value. This is close to what I want, but I want other values to be possible. I just want to make sure there is one definition of the attribute/value pair present. Is there a way to do this with a XML DTD?

Upvotes: 2

Views: 504

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52858

You can use an enumeration:

<!ATTLIST div
          type (foobar) #REQUIRED>

Other values are possible by adding them to the enumeration.

For example, the following declaration requires a type attribute on the div element with either the value foobar or barfoo.

<!ATTLIST div
          type (foobar|barfoo) #REQUIRED>

See "Enumerated Attribute Types" in http://www.w3.org/TR/REC-xml/#sec-attribute-types for more info.

Upvotes: 2

Related Questions