【发布时间】:2015-05-07 17:35:50
【问题描述】:
我有一个奇怪的 xml 场景,想用 xsd 验证它。 下面是xml
<tag1>
<tag2 attribute1="value1" />
<tag2 attribute2="value2" />
<tag1>
下面是我正在使用的xsd
<xs:element name="tag1">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" name="tag2">
<xs:complexType>
<xs:attribute name="attribute1" type="xs:string" use="optional" />
<xs:attribute name="attribute2" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
如果tag2 具有attribute1 或attribute2 两者或其中任何一个,则一切正常。 但我的情况是,如果没有如下属性 1 的 tag2
<tag1>
<tag2 attribute2="value2" />
<tag1>
然而它应该被认为是无效的
<tag1>
<tag2 attribute1="valueX" />
<tag1>
有效。
有什么方法可以构建 XSD 来验证这种情况吗?
【问题讨论】:
-
多年前,你问过this question - 并且已经提出了几个答案。请accept one of them or otherwise react to them.
-
同意@MathiasMüller 的观点,即您应该接受/对过去的答案做出反应,并且这个问题有点不清楚。如果每个
tag2都必须有一个attribute1,那么请参阅Mathias' answer。如果您说必须至少存在一个具有attribute1的tag2,那么您将需要XSD 1.1 来表达这样的约束。让我们知道 XSD 1.1 是否适合您。 -
不能使用 XSD 1.1 :( 所以我想我必须通过代码来验证它