Can I reference an external DTD for the structure of an ELEMENT in a DTD?
Asked Answered
G

1

4

Can I define a DTD such that elements in it have their structures defined in an external DTD? I mean something like the following (this is an example that I know is invalid in several ways, but which hopefully will give you an idea of what I am looking for):

<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY % MessageHdr-DTD SYSTEM "./messagehdr.dtd">
<!ENTITY % MessageBody-DTD SYSTEM "./messagebody.dtd">
<!ELEMENT Message (MessageHdr, MessageBody)>
<!ELEMENT MessageHdr (MessageHdr-DTD)>
<!ELEMENT MessageBody (MessageBody-DTD)>

The attempt here is to represent that a Message requires a MessageHdr and a MessageBody, but that those two elements are defined in externally referenced DTDs.

Guttery answered 6/6, 2011 at 23:59 Comment(0)
A
8

Yes and you are very close with your example. You just need to reference the parameter entities. Here's an example of 3 DTD's and an XML instance. I only used #PCDATA for MessageHdr and MessageBody, but this can be any structure you define.

message.dtd

<!ENTITY % MessageHdr-DTD SYSTEM "./messagehdr.dtd">
<!ENTITY % MessageBody-DTD SYSTEM "./messagebody.dtd">
%MessageHdr-DTD;
%MessageBody-DTD;
<!ELEMENT Message (MessageHdr, MessageBody)>

messagehdr.dtd

<!ELEMENT MessageHdr (#PCDATA)>

messagebody.dtd

<!ELEMENT MessageBody (#PCDATA)>

message.xml

<!DOCTYPE Message SYSTEM "message.dtd">
<Message>
  <MessageHdr>header info</MessageHdr>
  <MessageBody>body info</MessageBody>
</Message>
Andie answered 7/6, 2011 at 0:34 Comment(1)
Excellent... exactly the answer I was looking for. I tried it and it validates perfectly. Thanks.Guttery

© 2022 - 2024 — McMap. All rights reserved.