Reputation: 364
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>**
[...]
Upvotes: 1
Views: 1723
Reputation: 52888
If the two files are separate, you won't be able to restrict this with the DTD. The IDREF
type 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 your IDREF
type 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
<SURVEY>
<QUESTION id="Q1" text="Question #1">
<ANSWER>Answer to question #1.</ANSWER>
</QUESTION>
</SURVEY>
RESPONSES.xml
<RESPONSES>
<STUDENT id="S1">
<QUESTION ref="Q1">
<ANSWER>Students answer to question #1.</ANSWER>
</QUESTION>
</STUDENT>
</RESPONSES>
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.)
<!DOCTYPE COMBINED [
<!ELEMENT COMBINED (SURVEY,RESPONSES)>
<!ELEMENT SURVEY (QUESTION+)>
<!ELEMENT RESPONSES (STUDENT+)>
<!ELEMENT STUDENT (QUESTION*)>
<!ATTLIST STUDENT
id ID #REQUIRED>
<!ELEMENT QUESTION (ANSWER+)>
<!ATTLIST QUESTION
id ID #IMPLIED
ref IDREF #IMPLIED
text CDATA #IMPLIED>
<!ELEMENT ANSWER (#PCDATA)>
<!--These are the entities that point to the other XML files.-->
<!ENTITY survey-file SYSTEM "SURVEY.xml">
<!ENTITY responses-file SYSTEM "RESPONSES.xml">
]>
<COMBINED>
&survey-file;
&responses-file;
</COMBINED>
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.
Upvotes: 3