Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

xml - How to add XSD datatype restrictions to a DTD?

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...