I am trying to make a DTD in which I want to add some restrictions like:
- Only allow the introduction of telephone numbers with 9 numbers
- Only allow the introduction of an ID with 7 numbers and 1 letter
But I don't know how I can do that. (I know it is easier to add these restrictions with a XML Schema, but I want to do it with a DTD).
DTDs cannot restrict data to numeric types, let alone limit the number of digits:
Elements: DTDs define the content spec of an element as
[46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
Through Mixed
we can declare #PCDATA (parsed character data) but
make no further datatype specifications. Through children
we can
declare child elements, recursively.
None of the element type possibilities afford the specification a numeric type or length.
Attributes: DTDs define an attribute type as a string, a set of tokenized types, or an enumerated type:
[54] AttType ::= StringType | TokenizedType | EnumeratedType
TokenizedType
can be one of ID
, IDREF
, IDREFS
, ENTITY
, ENTITIES
, NMTOKEN
, or NMTOKENS
; EnumeratedType
can be an notation or an enumeration.
None of the attribute type possibilities afford the specification a numeric type or length.
Overall, there is no support for numeric types for elements or attributes.1 Use XSD instead.
1 If you're in the extraordinary situation of needing to extend DTD to express broader datatypes such as those found in XSD, see Datatypes for DTDs (DT4DTD) 1.0. However, do not expect existing validating parsers to enforce such specifications, which are more for providing the foundation for a smoother future transition path to XML Schema.