具有属性和字符串内容的XML元素

时间:2017-02-10 15:05:25

标签: xml xsd

在XML Schema中,如何定义呈现如下所示的ComplexType

<ValidationError code="X501">I love cats!</ValidationError>

如果我尝试以下操作,我的工具*会说它无效

        <xsd:complexType name="ValidationErrorType">
            <xsd:attribute name="code" type="xsd:string"></xsd:attribute>
            <xsd:simpleContent>
                <xsd:extension base="xs:string"/>
            </xsd:simpleContent>
        </xsd:complexType>

我使用的工具是用于可视化编辑的Altova XMLSpy和用于生成类的wsdl2java

更新:我尝试了another flavour

        <xsd:complexType name="ValidationErrorType">
            <xsd:simpleContent>
                <xsd:extension base="xs:string">
                    <xsd:attribute name="code" type="xsd:string" />
                </xsd:extension>
            </xsd:simpleContent>
        </xsd:complexType>

Altova说Invalid XML schema: 'Value 'xs:string' is not allowed for attribute 'base'.'

1 个答案:

答案 0 :(得分:2)

好的,我吸取了教训

  1. 属性可以扩展simpleContent,但必须出现在extension
  2. 我在第二个例子中错误输入xsd前缀几乎是正确的
  3. 正确的表格是

            <xsd:complexType name="ValidationErrorType">
                <xsd:simpleContent>
                    <xsd:extension base="xsd:string">
                        <xsd:attribute name="code" type="xsd:string" />
                    </xsd:extension>
                </xsd:simpleContent>
            </xsd:complexType>
    
相关问题