qazwsx
qazwsx

Reputation: 26868

How to write DTD to constrain multiple IDREF values in a string?

<Course Number="CS105A" Prerequisites="CS101A" Instructors="JC XX" Enrollment="610">
  <Title>Programming XXX</Title>
  <Description>Abstraction and its relation to programming.</Description>
</Course>

<Course Number="CS106B" Prerequisites="CS106A" Instructors="JC ER" Enrollment="620">
  <Title>Programming Abstractions</Title>
  <Description>Abstraction and its relation to programming.</Description>
</Course>

<Course Number="CS107" Prerequisites="CS106B CS105A" Instructors="JZ" Enrollment="500">
  <Title>Computer Organization and Systems</Title>
  <Description>Introduction to the fundamental concepts of computer systems.</Description>
</Course>

How to write the ATTLIST spec for the Prerequisites attribute which can be composed of multiple ID values such as "CS106B CS105A"? Would the following work?

<!ATTLIST Course Number ID #REQUIRED>
<!ATTLIST Course Prerequisites IDREF #IMPLIED>

Upvotes: 1

Views: 1842

Answers (3)

Bradley Andersen
Bradley Andersen

Reputation: 311

This is one of the DTD exercises for the current free online 'Intro to DBs' course @ Stanford (http://class2go.stanford.edu/db/Winter2013/interactive_exercises/DTDExercises).

As pointed out, it's easy to run this on your own. I run it against xmllint, for example, and xmllint tells me if my DTD doesn't pass. I follow the error trails until it does pass, then turn that in as my answer for this question (#3 in the referenced set).

Upvotes: 0

Daniel Haley
Daniel Haley

Reputation: 52858

Like Meng Lu said, you should use IDREFS (different link).

You also don't need to have separate attribute declarations (ATTLIST) for each attribute in an element.

You're also missing a very important ! in your declaration.

Example:

<!ATTLIST Course 
          Number        ID     #REQUIRED
          Prerequisites IDREFS #IMPLIED>

Upvotes: 2

Meng Lu
Meng Lu

Reputation: 14626

One can use IDREFS if the value of the attribute (Prerequisites) is a list of ID values as written as a joined string with a white space character as delimiter, i.e. id1 id2:

<ATTLIST Course Number ID #REQUIRED>
<ATTLIST Course Prerequisites IDREFS #IMPLIED>

One can validate a DTD against an XML online.

Upvotes: 2

Related Questions