对于下面的XSD,我想在不同深度的元素之间实现依赖关系。如何修改此模式以实现对XML的以下验证:
- 如果XML中不存在"Info"元素,则每个"ObjectData"必须存在"Value"元素"Value"
-
如果XML中没有为一个或多个"ObjectData"元素指定名为"Value"的元素,则必须存在名为"Info"的元素
<xs:element name="Objects" minOccurs="0" maxOccurs="1" > <xs:complexType> <xs:all> <xs:element name="Info" type="Info" minOccurs="0" maxOccurs="1"/> <xs:element name="ObjectData" minOccurs="1" maxOccurs="unbounded"> <xs:complexType> <xs:all> <xs:element name="ObjType" type="ObjTypeEnum" /> <xs:element name="Value" type="xs:decimal" minOccurs="0" maxOccurs="1"/> <xs:element name="Prop" type="properties" /> </xs:all> </xs:complexType> </xs:element> </xs:all> </xs:complexType> </xs:element>
另一种理解上述两个要求的方法是:
if( Objects.Info.occurs == 0 )
For-each Objectdata in Objects
assert(ObjectData.Value.occurs == 1)
For-atleast-one ObjectData in Objects
if ( ObjectData.Value.occurs == 0 )
assert(Objects.Info.occurs == 1)
马蒂亚斯我正在使用这个在线工具freeformatter.com/xml-validator-xsd.html。我猜是XSD 1.0。我将不得不看看xs:assert特性。XSD 1.1对我来说是新的
是的,freeformatter在线验证器只支持XML Schema 1.0。我通过提交一个在类型定义中包含<xsd:assert test="true()" />
的XSD文档来测试这一点。结果是:
S4s-elt-invalid-content.1: The Content Of '#AnonType_list' Is Invalid.
Element 'assert' Is Invalid, Misplaced, Or Occurs Too Often.
这与您的情况有关,因为表示依赖约束(如x if y and z, otherwise not(x)
)的唯一方法是断言。不幸的是,断言是XML Schema 1.1独有的特性。
所以,如果不能使用其他工具/XSD 1.1验证引擎来执行验证,那么这是不可能的。
如果您可以使用像Xerces或Saxon-EE这样的引擎,我很乐意详细说明XSD 1.1中的断言解决方案。或者,可以用Schematron(另一种模式语言)编写相同的规则。