XSD 架构 - 如何在不同级别的元素之间创建依赖关系
XSD schema - how to create dependency between elements at different levels
对于下面的XSD,我想实现不同深度的元素之间的依赖。我如何修改此模式以实现对我的 XML 的以下验证:
- 如果 XML 中不存在“Info”元素,则每个元素都必须存在“Value”元素“对象数据”
如果在 XML 中未为一个或多个“ObjectData[=28=”指定名为“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)
Mathias I am using this online tool freeformatter.com/xml-validator-xsd.html. I guess its XSD 1.0. I will have to look at xs:assert feature. XSD 1.1 is new to me
是的,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 编写相同的规则。
对于下面的XSD,我想实现不同深度的元素之间的依赖。我如何修改此模式以实现对我的 XML 的以下验证:
- 如果 XML 中不存在“Info”元素,则每个元素都必须存在“Value”元素“对象数据”
如果在 XML 中未为一个或多个“ObjectData[=28=”指定名为“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)
Mathias I am using this online tool freeformatter.com/xml-validator-xsd.html. I guess its XSD 1.0. I will have to look at xs:assert feature. XSD 1.1 is new to me
是的,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 编写相同的规则。