This is how I understand the question. The <payment>
element has a type
attribute. If the attribute value is "check", then there must also be a checknum
attribute on that element. How do I enforce this in the DTD?
Constraints on attributes and elements based on the value or occurrence of other elements and attributes is often called "co-occurrence constraints". This cannot be expressed in a DTD. There is no ATTLIST
syntax for your requirement. So the answer is: it is not possible.
But you can do it with RELAX NG. Here is a RELAX NG schema with two possible content models for the payment
element that only differ in the treatment of the type
attribute:
start = payment1 | payment2
payment1 = element payment { attribute type { "check" }, attribute checknum { text },
to, amount, date, description }
payment2 = element payment { attribute type { "transfer" | "atm" },
to, amount, date, description }
to = element to { attribute category { text }, text}
amount = element amount { xsd:float }
date = element date { xsd:date }
description = element description { text }
Test document to validate:
<payment type="check">
<to category="food">Pizza Now</to>
<amount>33.24</amount>
<date>2005-08-28</date>
<description>Dinner</description>
</payment>
When checked by Jing, the result is this:
$ java -jar jing.jar -c payment.rnc payment.xml
payment.xml:1:23: error: element "payment" missing required attribute "checknum"
There is a section on co-occurrence constraints in the book "RELAX NG" by Eric van der Vlist. Here is a quote:
While co-occurrence constraints provide powerful capabilities, they unfortunately don't survive conversion to DTDs or W3C XML Schema.
Since that was written (about 10 years ago, I guess), W3C XML Schema has been augmented with new features in this area (see http://www.ibm.com/developerworks/xml/library/x-xml11pt2/index.html). But the capabilities of DTDs have not improved.