属性组的选择
Choice of attributeGroup
问题
我必须选择性地向现有的 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>
声明我的两个 attributeGroups:
<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,但 none x、y、z:
@a and @b and @c and not(@x) and not(@y) and not(@z)
None a、b 和 c 但所有 x、y、z:
not(@a) and not(@b) and not(@c) and @x and @y and @z
None 的 a、b、c、x、y、z:
not(@a or @b or @c or @x or @y or @z)
XSD1.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>
XSD1.1 中的另一种实现方式是使用条件类型赋值。这里定义了两种类型,一种具有属性a/b/c,另一种具有属性x/y/z,您通过条件测试在这两种类型之间进行选择:
<xs:element name="Value" type="ValueType">
<xs:alternative test="@a" type="ValueTypeWithABC"/>
<xs:alternative type="ValueTypeWithXYZ"/>
</xs:element>
问题
我必须选择性地向现有的 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>
声明我的两个 attributeGroups:
<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,但 none x、y、z:
@a and @b and @c and not(@x) and not(@y) and not(@z)
None a、b 和 c 但所有 x、y、z:
not(@a) and not(@b) and not(@c) and @x and @y and @z
None 的 a、b、c、x、y、z:
not(@a or @b or @c or @x or @y or @z)
XSD1.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>
XSD1.1 中的另一种实现方式是使用条件类型赋值。这里定义了两种类型,一种具有属性a/b/c,另一种具有属性x/y/z,您通过条件测试在这两种类型之间进行选择:
<xs:element name="Value" type="ValueType">
<xs:alternative test="@a" type="ValueTypeWithABC"/>
<xs:alternative type="ValueTypeWithXYZ"/>
</xs:element>