【问题标题】:xml simple type with enumeration and union带有枚举和联合的xml简单类型
【发布时间】:2011-04-22 21:49:16
【问题描述】:

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

元素属性:模式解析器错误:属性声明。 'current-state',属性 'type':QName 值 'covered-state' 不解析为 (n) 简单类型定义。 WXS schema 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”。

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

【问题讨论】:

  • 嗯,好的,我已经成功了。我想发布答案,但作为新用户 ID,必须再等 8 小时。所以也许如果我记得我会发布它。也许它可以帮助有类似问题的人。

标签: xml types xsd union enumeration


【解决方案1】:

你的建议也可以,但我是这样解决的:

    <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>

还是谢谢!

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 或者,可以删除xsd:schema 元素中的目标命名空间。取决于命名空间是否真的很重要,我想。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2018-02-14
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多