重用 DTD 中的元素
我开始了一份工作,我得到了一些 XML 文件和 DTD。它们工作得很好,但我注意到在 DTD 中它们重用了这样的元素。
DTD:
<!ELEMENT image EMPTY>
<!ATTLIST image
source CDATA #REQUIRED
signature (true|false|1|0) "false"
>
在 xml 中,图像元素出现在两个位置,但只有一个位置需要“signature”属性,在其他情况下则不相关。
XML:
<root>
<element-with-optional-signature-image>
<image source="1.jpg" singature="true" />
<image source="2.jpg" />
</element-with-optional-signature-image>
<other>
<image source="3.jpg" />
</other>
</root>
我以前从未见过这样编写的 DTD,只是想知道它是否常见或者是一种非常糟糕的方法?我会创建两个不同的元素 element-image
和 other-image
。
编辑——
上面的内容是否像这样的 DTD 一样被接受:
<!ELEMENT element-image EMPTY>
<!ATTLIST element-image
source CDATA #REQUIRED
signature (true|false|1|0) "false"
>
<!ELEMENT other-image EMPTY>
<!ATTLIST image
source CDATA #REQUIRED
>
使用这样的 XML:
<root>
<element-with-optional-signature-image>
<element-image source="1.jpg" singature="true" />
<element-image source="2.jpg" />
</element-with-optional-signature-image>
<other>
<other-image source="3.jpg" />
</other>
</root>
I have started a job where I was given some XML files and a DTD. They work fine but I noticed in the DTD they were reusing an element like this.
DTD:
<!ELEMENT image EMPTY>
<!ATTLIST image
source CDATA #REQUIRED
signature (true|false|1|0) "false"
>
and in the xml the image element appears in two place but only one of the places requires the 'signature' attribute, in the other case it is irrelevant.
XML:
<root>
<element-with-optional-signature-image>
<image source="1.jpg" singature="true" />
<image source="2.jpg" />
</element-with-optional-signature-image>
<other>
<image source="3.jpg" />
</other>
</root>
I've never seen a DTD written like this before and just wondering if it common or a really bad way of doing this? I would've created two different elements element-image
and other-image
.
EDIT --
Is the above just as accepted as say a DTD like this:
<!ELEMENT element-image EMPTY>
<!ATTLIST element-image
source CDATA #REQUIRED
signature (true|false|1|0) "false"
>
<!ELEMENT other-image EMPTY>
<!ATTLIST image
source CDATA #REQUIRED
>
with XML like this:
<root>
<element-with-optional-signature-image>
<element-image source="1.jpg" singature="true" />
<element-image source="2.jpg" />
</element-with-optional-signature-image>
<other>
<other-image source="3.jpg" />
</other>
</root>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为重用这样的元素是个好主意。它使 XML 保持简单且不那么冗长。
在这种情况下,我认为仅仅因为不需要使用可选属性而创建一个新元素就太过分了。
I think it's a great idea to reuse elements like that. It keeps the XML simple and less verbose.
In this instance I think it would be overkill to create a new element just because it doesn't need to make use of an optional attribute.
在此 DTD 中,不需要签名属性。源属性是。
In this DTD, the signature attribute is not required. The source attribute is.