How to make two attributes in XML to be unique (DTD)

667 Views Asked by At

How can I make two attributes in XML to be unique, for example in SQL I can do this by this code:

create table tablename(
  StudentID varchar(20),
  SubjectID varchar(20),
  UNIQUE(StudentID,SubjectID)
)

On the other hand, I have the following code in XML:

<root>
  <student StudentID="s1" />
  <subject SubjectID="sb1" />
  <tablename StudentID="s1" SubjectID="sb1" />
</root>

How can I make that the attributes StudentID and SubjectID both become UNIQUE?

1

There are 1 best solutions below

0
On
<!ATTLIST student StudentID ID #REQUIRED>
<!ATTLIST subject SubjectID ID #REQUIRED>
<!ATTLIST tablename StudentID IDREF #REQUIRED
                    SubjectID IDREF #REQUIRED>

From your comment ("StudentID is ID in element Student, SubjectID is ID in element Subject, and StudentID, SubjectID are IDREF in tablename. How can i make sure that the same value doesn't appear twice in these attributes?") it sounds as if this is what you already have in the DTD.

In other words: you have already ensured that the same value does not appear twice in the student/StudentID and subject/SubjectID attributes. No value can appear more than once in a document as the value of an attribute declared as ID.

If what you mean is "How do I ensure that no two tablename elements have the same StudentID, SubjectID pair?", the answer is: with application code. There is no good way to do it with the DTD.