tags:

views:

43

answers:

2

Hi I am trying to write a DTD, and I want to define an element BOOKTYPE, that can be either ONE, TWO or THREE. Is there a way I can ensure that only one of these values can be entered inside the element?

Note: 1. I know how to do this in an attribute, but not sure how to accomplish with elements. 2. I want a way of doing this INSIDE the DTD, not using a programming language.

A: 

Is there a way I can ensure that only one of these values can be entered inside the element?

Yes, with an XML Schema Definition (XSD) containing an XML Enumeration. Here is an example one for currencies:

<xsd:simpleType name = "iso3currency">
 <xsd:restriction base = "xsd:string">
  <xsd:enumeration value = "AUD"/><!-- Australian Dollar -->
  <xsd:enumeration value = "BRL"/><!-- Brazilian Real -->
  <xsd:enumeration value = "CAD"/><!-- Canadian Dollar -->
  <xsd:enumeration value = "CNY"/><!-- Chinese Yen -->
  <xsd:enumeration value = "EUR"/><!-- Euro -->
  <xsd:enumeration value = "GBP"/><!-- British Pound -->
  <xsd:enumeration value = "INR"/><!-- Indian Rupee -->
  <xsd:enumeration value = "JPY"/><!-- Japanese Yen -->
  <xsd:enumeration value = "RUR"/><!-- Russian Rouble -->
  <xsd:enumeration value = "USD"/><!-- US Dollar -->
  <xsd:length value = "3"/>
 </xsd:restriction>

This restricts the value of the element to one of the listed enumeration values and a length of 3.

To use it, you have to pass your XML and XSD through a validator. An example of how to do this in .NET is here:

HOW TO: Validate XML Fragments Against an XML Schema in Visual C#.NET http://support.microsoft.com/kb/318504

Robert Harvey
Indeed. But is it possible in DTD's as well? I believe they're more limited than XSD's in some ways.
Joren
How does this work. Can someone illustrate please?
myowns
+1  A: 

Not in DTD, sorry. You only get to specify what elements may appear as children, and whether text content (#PCDATA) can appear or not. You don't get a say in what the text content is.

You'd have to move the data to an attribute, or use a schema language more powerful than old-and-clunky DTD.

bobince