指定一个枚举作为内容,并且在xsd中也有一个单一类型的属性?

Specify an enum as the content and also have an attribute in a single type in xsd?

有没有办法在 xml 模式中以单一类型定义以下内容?

<Foo bar="1">ENUM</Foo>

它必须是 simpleType 因为它是关于字符串可以是什么的 restriction,但是它不能有 attribute.

<element name="Foo">
    <simpleType>
        <restriction base="string">
            <!-- some enums... -->
        </restriction>
    </simpleType>
</element>

如果我将其设为 simpleType 且属性为 extension,则无法指定枚举。

<element name="Foo">
    <simpleType>
        <extension base="string">
            <attribute name="bar" type="positiveInteger" />
        </extension>
    </simpleType>
</element>

我尝试执行 simpleContentcomplexContent,但您无法添加属性。另外我认为基本类型必须是复杂的。

有没有办法在不添加其他类型的情况下在单个 element 元素中执行此操作?

下面是一种使用附加类型的方法。

<element name="Foo">
    <complexType>
        <simpleContent>
            <extension base="this:FooEnum">
                <attribute name="bar" type="positiveInteger" />
            </extension>
        </simpleContent>
    </complexType>
</element>

<simpleType name="FooEnum">
    <restriction base="string">
        <!-- some enums... -->
    </restriction>
</simpleType>

XSD 1.0 解决方案

在 XSD 1.0 中,您可以通过限制 xs:anyType:

来做类似的事情
<xs:element name="foo">
    <xs:complexType>
        <xs:simpleContent>
            <xs:restriction base="xs:anyType">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="value1"/>
                        <xs:enumeration value="value2"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:attribute name="bar" type="xs:positiveInteger"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

请注意,如果您愿意,可以将 xs:enumeration 节点移动为 xs:attribute 节点的同级节点,但不能删除 xs:simpleType 节点:

<xs:element name="foo">
    <xs:complexType>
        <xs:simpleContent>
            <xs:restriction base="xs:anyType">
                <xs:simpleType>
                    <xs:restriction base="xs:string"/>                        
                </xs:simpleType>
                <xs:enumeration value="value1"/>
                <xs:enumeration value="value2"/>
                <xs:attribute name="bar" type="xs:positiveInteger"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

XSD 1.1 解决方案

使用 XSD 1.1,您可以使用 mixed=true 并将枚举表示为 complexType 的断言,尽管这不像 XSD 枚举那样具有表现力。基本示例:

<xs:element name="foo">
    <xs:complexType mixed="true">
        <xs:attribute name="bar" type="xs:positiveInteger"/>
        <xs:assert test=". = ('value1', 'value2')"/> <!-- Value in enum -->            
    </xs:complexType>
</xs:element>