Xml 属性组的选择 问题

Xml 属性组的选择 问题,xml,xsd,Xml,Xsd,我必须选择性地向现有的Value元素添加一组属性(见下文)。此元素不能包含多个属性集(请参见下文) 注意:属性集中不包括单位属性 <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> 价值要素声明: 试液 但这不是有效的XSD: 错误-第93行,第

我必须选择性地向现有的Value元素添加一组属性(见下文)。此元素不能包含多个属性集(请参见下文)

注意:属性集中不包括单位属性

<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>
价值要素声明:

试液

但这不是有效的XSD:

错误-第93行,第45行:org.xml.sax.SAXParseException;行号:93; 栏目号:45;s4s elt的内容无效。1:的内容 “#AnonType_值”无效。元素“choice”无效,位置错误, 或者发生得太频繁


有人知道如何解决这个问题吗?

您不能在XSD 1.0中表达这样的约束。

在XSD1.1中,您可以将属性设置为可选属性,并通过
xs:assert
表达复杂的需求约束。您希望允许以下三种情况:

  • 所有a、b和c,但不包括x、y和z:

    @a和@b和@c和not(@x)和not(@y)和not(@z)

  • a、b和c都不是,而是x、y、z的全部:

    not(@a)和not(@b)和not(@c)以及@x和@y和@z

  • a、b、c、x、y、z中的任何一个:

    not(@a或@b或@c或@x或@y或@z)

  • XSD 1.1
    在XSD 1.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>
    
    
    
    XSD 1.0不可能-在
    xs:choice
    中不允许使用属性。使用XSD 1.1断言或将元素放入
    xs:choice
    ,并使属性集成为元素之间的唯一区别-例如,@MathiasMüller是正确的,这不能在XSD 1.0中表达。请参阅my,了解如何在XSD1.1中使用断言执行此操作。
    <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>
    
    <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>
    
    <?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>
    
    <xs:element name="Value" type="ValueType">
      <xs:alternative test="@a" type="ValueTypeWithABC"/> 
      <xs:alternative           type="ValueTypeWithXYZ"/>
    </xs:element>