我如何在 XSD 中进行任一/或验证?例如在以下 xml 片段中,如果 action
为 A
(add),则后续属性和元素是 required
:
<Post postID="22793" action="A" ...>
<Transaction ...>
<Disposition ...>
<AccountInformation ... />
<ThirdPartyInformation ... />
</Disposition>
<IndividualInformation ... />
</Transaction>
...
</Post>
但是如果操作是D,那么后续属性和属性将被禁止::
<Post postID="22793" action="D" />
也就是说:如果操作是删除 (D
),则其余属性将从
- 必填
至
- 禁止
随后的元素切换自
minOccurences="1" maxOccurrences="unbounded"
至
minOccurrences="0" maxOccurrences="0"
是否可以使用XSD来定义xml结构?
最佳答案
the big criticisms of XSD 之一是它的失败
...provide no facilities to state that the value or presence of one attribute is dependent on the values or presence of other attributes (so-called co-occurrence constraints).
因此它无法指定像您所描述的那样的约束。在大多数情况下,我见过一些项目使用 XSLT 来验证 XSD 失败的文档。然而other Schema languages确实存在,并且您可能会通过它们获得更多成功。我听说有些人改用 Relax NG由于各种原因。
关于xml - XSD:非此即彼语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4208477/