问题描述
我可以定义一个DTD以便其中的元素在外部DTD中定义其结构吗?
我的意思是类似以下内容(这是一个我知道在几个方面都无效的示例,但是希望它可以使您对我正在寻找的内容有所了解):
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)>
此处的尝试是表示一条消息需要MessageHdr和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.
推荐答案
是的,您与示例非常接近。您只需要引用。这是3个DTD和一个XML实例的示例。我只将 #PCDATA
用于 MessageHdr
和 MessageBody
,但是这可以是您定义的任何结构。
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>
这篇关于我可以参考外部DTD来了解DTD中ELEMENT的结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!