XML(DTD):我怎样才能确保元素的子元素之一是必需的,但不一定是特定的子元素?

XML(DTD): How can I make sure one of the children of an element is required, but not necessarily a specific one?

在我的 DTD 文件中我有:

<!ELEMENT name (firstname*, surname*, companyname*)>

如果xml中的type='person',则需要firstnamesurname元素,或者如果type='company',则需要companyname 元素。

如何在我的 DTD 中显示它?

我还有一个类似的问题:

<!ELEMENT telephone (home*, work*, mobile*, fax*, phone*)>

如果是个人,则至少需要 homeworkmobile 之一;如果它是一家公司,则需要 phone.

更新:

我想我通过以下方式解决了名称问题:

<!ELEMENT name ((firstname, surname) | companyname)>

我尝试了类似的电话元素:

<!ELEMENT telephone ((phone | (home | work | mobile)), fax*)>

但是在验证时出现错误:

The content of element type "telephone" must match "((phone|(home|work|mobile)),fax*)". [21] 

其中至少需要 homeworkmobile 之一,但可以有 multiple/all 三个。 对我在这里做错了什么有什么想法吗?

DTD 的设计原则是,如果元素有不同的验证要求,它们将被赋予不同的名称。如果您希望描述公司的元素具有一组要求,而描述人员的元素具有不同的要求,请将它们称为 companyperson,而不是 name。 DTD 验证将元素绑定到基于元素名称的声明,而不是基于属性的值。

所以:不,DTD 没有提供任何机制来说明 "If the value of the type attribute is person, then use this content model, and if the value is company, use that one. Of course, you can also express the constraint the other way around, and say "如果 name 元素包含一个 companyname 子元素,那么 type 属性必须具有值 company,如果 name 元素包含一对 firstname, surname,则 type 必须具有值 person。”在这个公式中,很明显 type 完全是多余的,很多设计者会舍弃它。一个需要知道它是在与公司打交道还是与人打交道的应用程序可以查看内容来了解​​。

不清楚您对电话phone号码的要求是什么。如果您希望至少需要一个 phone 号码,并且如果您还希望 phone 号码可以选择性地区分为家庭、工作或手机号码,并且您希望相同的父元素也包含任何传真号码,那么两个明显的设计是:

<!ELEMENT telephone-numbers ((phone | home | mobile 
                            | work)+, fax*) >

<!ELEMENT telephone-numers (phone+, fax*) >
<!ELEMENT phone (#PCDATA) >
<!ATTLIST phone type (home | mobile | work) #IMPLIED >