带有枚举和联合的xml简单类型

时间:2011-04-21 23:52:17

标签: xml types xsd union enumeration

解析以下xml架构会产生此错误:

元素属性:模式解析器错误:属性decl。 'current-state',属性'type':QName值'covered-state'不解析为(n)简单类型定义。 WXS模式memory.xsd无法编译

继承责任的代码:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.com">

    <xsd:simpleType name="covered-state">
        <xsd:union>
            <xsd:simpleType>
                <xsd:restriction base="xsd:integer">
                    <xsd:enumeration value="0"/>
                    <xsd:enumeration value="1"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="COVERED"/>
                    <xsd:enumeration value="UNCOVERED"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:union>
    </xsd:simpleType>

    <xsd:complexType name="MemoryCard">
        <xsd:attribute name="current-state" type="covered-state" use="required"/> <!-- here i get the error -->
    </xsd:complexType>

</xsd:schema>

所以这应该是联合字符串和整数的枚举,以便xml文件接受属性当前状态的“0”或“1”或“COVERED”或“UNCOVERED”。

有人能指出我正确的方向吗?谢谢!

2 个答案:

答案 0 :(得分:5)

你的建议也会奏效,但我解决了这个问题:

    <xsd:attribute name="current-state" use="required">
        <xsd:simpleType>    
            <xsd:union>
                <xsd:simpleType>
                    <xsd:restriction base="xsd:integer">
                        <xsd:enumeration value="0"/>
                        <xsd:enumeration value="1"/>
                    </xsd:restriction>
                </xsd:simpleType>
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="COVERED"/>
                        <xsd:enumeration value="UNCOVERED"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:union>
        </xsd:simpleType>
    </xsd:attribute>

非常感谢!

答案 1 :(得分:2)

type="covered-state"是对无命名空间中的类型的引用,但您希望在名称空间"covered-state"中引用具有本地名称"http://www.example.com"的类型。要实现这一点,您需要将前缀(例如e)绑定到此命名空间,并将其称为type="e:covered-state"