编写 DTD:如何实现此子设置
元素 tasklist
最多可以包含一个 title
和最多一个 description
,此外还可以包含任意数量(包括 0)task 元素以任意顺序。
天真的方法不适用,因为顺序不重要:
<!ELEMENT tasklist (title?, description?, task*) >
或者,我可以明确命名所有可能的选项:
(title, description?, task*) |
(title, task+, description?, task*) |
(task+, title, task*, description?, task*) |
(description, title?, task*) |
(description, task+, title?, task*) |
(task+, description, task*, title?, task*) |
(task*)
但是编写一个非确定性规则非常容易,而且它看起来像直接通往最黑暗的疯狂。有什么想法,如何可以更优雅地做到这一点?
不,XSD 或 RelaxNG 不是选择。我需要一个简单的、旧的 DTD。
The element tasklist
may contain at most one title
and at most one description
, additionally any number (incl. 0) task
elements in any order.
The naive approach is not applicable, since the order should not matter:
<!ELEMENT tasklist (title?, description?, task*) >
Alternatively, I could explicitly name all possible options:
(title, description?, task*) |
(title, task+, description?, task*) |
(task+, title, task*, description?, task*) |
(description, title?, task*) |
(description, task+, title?, task*) |
(task+, description, task*, title?, task*) |
(task*)
but then it's quite easy to write a non-deterministic rule, and furthermore it looks like the direct path to darkest madness. Any ideas, how this could be done more elegantly?
And no, an XSD or RelaxNG is no option. I need a plain, old DTD.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这总结了您需要的内容:
在
description
之前/之后出现的title
的替换。但是,这不是确定性内容模型,如 @13ren 在他的回答中解释了。 [这是 Microsoft 的另一个示例](http:// /msdn.microsoft.com/en-us/library/9bf3997x(VS.71).aspx)。
简而言之,
您的要求是拥有一个非确定性模型,因此,您的场景不可能有有效 DTD。
元素
如果您设置一个简单的限制,即如果
task
和description
都必须是最后一个元素,则task
或description
必须是最后一个 提供后,您可以使用此确定性 DTD 声明:示例:
或者,可能更自然地,强制
title
或description
元素必须是第一个元素,并且两个title
和description
元素必须存在或不存在。示例:
否则
否则,您需要使用 RELAX NG,它允许非确定性模型。
This summarises what you need:
Alternation for the
title
appearing before/afterdescription
.However, this is not a deterministic content model, as @13ren explains in his answer. [Here is another example from Microsoft](http://msdn.microsoft.com/en-us/library/9bf3997x(VS.71).aspx).
In short
Your requirements is to have a non-deterministic model, and as such, there is no possible valid DTD for your scenario.
Alternatives
If you place a simple restriction that either
task
ordescription
must be the last element if bothtask
anddescription
are provided, you can use this deterministic DTD declaration:Examples:
Or, possibly more naturally, enforce that a
title
ordescription
element must be the first element, and bothtitle
anddescription
elements must exist or be non-existent.Examples:
Otherwise
Otherwise, you need to use RELAX NG, which allows for non-deterministic models.
为什么顺序不重要?
在我看来,这里的顺序似乎相当重要;唯一合理的顺序是标题?、描述?、任务*。
灵活性固然很好,但有时并不是必需的。
Why is the order unimportant?
It seems to me as if the order is rather a bit important right here; and that the only sensible order is title?, description?, task*.
Flexibility is nice and all, but sometimes it's just not neccessary.