【发布时间】:2015-04-13 14:26:33
【问题描述】:
问题
我必须选择性地向现有的 Value 元素添加一组属性(见下文)。此元素不得包含一组以上的属性(见下文)。
注意:unit 属性不包含在属性集中。
Value元素的声明:
<xs:element name="Value">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="unit" use="required" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
声明我的两个attributeGroup:
<xs:attributeGroup name="setOfAttrs1">
<xs:attribute name= "a" type="xs:int" use="required" />
<xs:attribute name="b" type="xs:int" use="required" />
<xs:attribute name="c" type="xs:int" use="required" />
</xs:attributeGroup>
<xs:attributeGroup name="setOfAttrs2">
<xs:attribute name= "x" type="xs:int" use="required" />
<xs:attribute name="y" type="xs:int" use="required" />
<xs:attribute name="z" type="xs:int" use="required" />
</xs:attributeGroup>
正确示例(针对模式)
<Value unit="m3">i'm correct</value>
<Value unit="m3" x="1" y="1" z="0">i'm correct</value>
<Value unit="m3" a="1" b="1" c="0">i'm correct</value>
不正确的示例(针对架构)
第一个错过了z属性;第二个包含两组属性。
<Value unit="m3" x="1" y="1">i'm incorrect</value>
<Value unit="m3" x="1" y="1" z="0" a="0" b="1" c="0">i'm incorrect</value>
尝试过的解决方案
<xs:element name="Value">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="PhysicalUnit" use="required" type="xs:string" />
<xs:choice minOccurs="0" maxOccurs="1">
<xs:attributeGroup ref="setOfAttrs1" />
<xs:attributeGroup ref="setOfAttrs2" />
</xs:choice>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
但这不是一个有效的 XSD:
错误 - 第 93、45 行:org.xml.sax.SAXParseException;行号:93; 列号:45; s4s-elt-invalid-content.1:内容 “#AnonType_Value”无效。元素“选择”无效、错位、 或发生得太频繁。
有人知道如何解决这个问题吗?
【问题讨论】:
-
XSD 1.0 不可能 -
xs:choice中不允许使用属性。要么使用 XSD 1.1 断言,要么将 elements 放入xs:choice,并使属性集成为元素之间的唯一区别 - 例如 deriving two actual types from an abstract type。 -
@MathiasMüller 是正确的,这不能在 XSD 1.0 中表达。请参阅我的answer below,了解如何在 XSD 1.1 中使用断言。