XSD使用attributeGroup选项验证xml
问题描述: 我必须选择性地向现有的Value元素添加一组属性(见下文) 将出现sourceMsgType(必需)、sourceMsgVersion(可选)、destinationMsgType(必需)、destinationMsgVersion(可选)或suppressedSource(必需)、suppressedMsgVersion(可选) 我已尝试使用以下XSD,但它显示以下错误:XSD使用attributeGroup选项验证xml,xml,xsd,Xml,Xsd,问题描述: 我必须选择性地向现有的Value元素添加一组属性(见下文) 将出现sourceMsgType(必需)、sourceMsgVersion(可选)、destinationMsgType(必需)、destinationMsgVersion(可选)或suppressedSource(必需)、suppressedMsgVersion(可选) 我已尝试使用以下XSD,但它显示以下错误: "**S4s-elt-invalid-content.1: The Content Of '#AnonType
"**S4s-elt-invalid-content.1: The Content Of '#AnonType_messageTypearchiveConfig' Is Invalid. Element 'choice' Is Invalid, Misplaced, Or Occurs Too Often.**".
请帮帮我。提前谢谢
XML
净化
XSD
xsd中存在许多问题,但它们都来自一个错误的假设。您假设xs:choice可以包含一个属性组 事实上,选项组是一种“模型组”,模型组只能包含元素、组和通配符。不是属性。详细信息都在XML模式规范中。这本书不容易阅读,但它是此类问题的最终权威: 因此,您无法实现所寻求的确切验证规则,但您应该能够接近。只需将一些属性设置为可选。如果需要严格验证规则,则需要执行以下操作之一:
- 在应用程序中添加验证逻辑以强制执行规则
- 使用更强的XML验证语言(例如schematron/RelaxNG)
- 使用标记而不是属性为这些属性建模。然后,您将能够在xs:choice中使用包含元素的模型组
- 为messageType的两个变体(messageType1和messageType2)定义一个名称不同的标记
- 如果重要的是对两个变体使用相同的标记名,则定义两个全局复杂类型,它们都是基本复杂类型的扩展。然后,在XML中,通过添加指定复杂类型名称的xsi:type属性,确定要针对哪个复杂类型进行验证
<?xml version="1.0" encoding="UTF-8"?>
<archiveConfig>
<archivingStrategy>PURGE</archivingStrategy>
<messageType sourceMsgType="STL01" sourceMsgVersion="" destinationMsgType="195" destinationMsgVersion="01" cutOff="90" filter ="N"/>
<messageType sourceMsgType="STL01" destinationMsgType="CHESS197_01" cutOff="180"/>
<messageType sourceMsgType="STL03" destinationMsgType="101" destinationMsgVersion="02" cutOff="60" filter ="Y"/>
<messageType suppressedSource="STL03" cutOff="60" filter ="Y"/>
<messageType suppressedSource="STL01" suppressedMsgVersion="01" cutOff="60" filter ="Y"/>
</archiveConfig>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:element name="archiveConfig">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="archivingStrategy"/>
<xs:element name="messageType" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:short" name="cutOff" use="required"/>
<xs:attribute type="xs:string" name="filter" use="optional"/>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:attributeGroup ref="setOfAttrs1" />
<xs:attributeGroup ref="setOfAttrs2" />
<xs:assert test="(@sourceMsgType and @destinationMsgType
and (@sourceMsgVersion or not(@sourceMsgVersion)) and (@destinationMsgVersion
or not(@destinationMsgVersion)) not(@suppressedSource) and not(@suppressedMsgVersion))
or (@suppressedSource and (@suppressedMsgVersion or not(@suppressedMsgVersion))
and not(@sourceMsgType) and not(@sourceMsgVersion) and not(@destinationMsgType) and not(@destinationMsgVersion))"/>
</xs:choice>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:attributeGroup name="setOfAttrs1">
<xs:attribute type="xs:string" name="sourceMsgType" use="required"/>
<xs:attribute type="xs:string" name="sourceMsgVersion" use="optional"/>
<xs:attribute type="xs:short" name="destinationMsgType" use="required"/>
<xs:attribute type="xs:byte" name="destinationMsgVersion" use="optional"/>
</xs:attributeGroup>
<xs:attributeGroup name="setOfAttrs2">
<xs:attribute type="xs:string" name="suppressedSource" use="required"/>
<xs:attribute type="xs:string" name="suppressedMsgVersion" use="optional"/>
</xs:attributeGroup>
</xs:schema>