问题
我必须选择性地向现有的 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:
Error - Line 93, 45: org.xml.sax.SAXParseException; lineNumber: 93; columnNumber: 45; s4s-elt-invalid-content.1: The content of '#AnonType_Value' is invalid. Element 'choice' is invalid, misplaced, or occurs too often.
有人知道如何解决这个问题吗?
最佳答案
您不能在 XSD 1.0 中表达这样的约束。
在 XSD 1.1 中,您可以将属性设为可选,并通过 xs:assert
表达复杂的需求约束。您希望允许三种情况:
所有 a、b 和 c 但没有 x、y、z:
@a and @b and @c and not(@x) and not(@y) and not(@z)
不是 a、b 和 c,而是所有 x、y、z:
not(@a) and not(@b) and not(@c) and @x and @y and @z
a, b, c, x, y, z 都不是:
不是(@a 或@b 或@c 或@x 或@y 或@z)
XSD 1.1
<?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="Value">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="unit" use="required" type="xs:string" />
<xs:attributeGroup ref="setOfAttrs1" />
<xs:attributeGroup ref="setOfAttrs2" />
<xs:assert test="(@a and @b and @c and not(@x) and not(@y) and not(@z))
or (not(@a) and not(@b) and not(@c) and @x and @y and @z)
or not(@a or @b or @c or @x or @y or @z)"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:attributeGroup name="setOfAttrs1">
<xs:attribute name="a" type="xs:int"/>
<xs:attribute name="b" type="xs:int"/>
<xs:attribute name="c" type="xs:int" />
</xs:attributeGroup>
<xs:attributeGroup name="setOfAttrs2">
<xs:attribute name="x" type="xs:int"/>
<xs:attribute name="y" type="xs:int" />
<xs:attribute name="z" type="xs:int"/>
</xs:attributeGroup>
</xs:schema>
关于xml - 属性组的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29607823/