I’m required to write two XML files and restrict them using DTD. Am I able to restrict element ID in one XML file to reference ID’s in another file?
<!ELEMENT SURVEY (QUESTION+)>
<!ATTLIST SURVEY
[...]>
<!ELEMENT QUESTION ([...])>
<!ATTLIST QUESTION
Id ID #REQUIRED
Text CDATA #REQUIRED
[...] >
And I want that in a different XML file with answers, the ID of answers must reference the ID of the questions in the first file. That is, only a QUESTION element in SURVEY may be answered in RESPONSES.
<!ELEMENT RESPONSES (STUDENT+)>
<!ELEMENT STUDENT (QUESTION*)>
<!ATTLIST STUDENT
id ID #REQUIRED>
<!ELEMENT QUESTION (ANSWER+)>
<!ATTLIST QUESTION
**id IDREF #REQUIRED>**
[...]
If the two files are separate, you won’t be able to restrict this with the DTD. The
IDREFtype attribute won’t be able to resolve. You will most likely get errors like ‘An element with the identifier “??” must appear in the document’ (??= the value of yourIDREFtype attributes).To enforce this with DTD only, you would need to combine the data sets (SURVEY file and RESPONSES file). Combining could either be physically merging the two data sets or by creating a third instance that will pull the two together through entity references. This would allow you to validate the single instance. If you did it this way, you wouldn’t have to combine the data again if either data sets change.
You would have to make some changes so your two data sets could coexist; especially with your “QUESTION” element and its attributes.
Here’s an example of what I’m thinking:
SURVEY.xml
RESPONSES.xml
COMBINED.xml
(This is the one that would be validated and shouldn’t need to change if SURVEY.xml/RESPONSES.xml change. I’ve also placed the DTD in the internal subset, but it could be used as an external DTD instead.)
Outside of using only DTD, you could also use something like XSLT or XQuery to check the data. (I can add an XSLT example if it will help.) You might be able to do this with Schema, but I’m not sure.