usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

XML element with attribute and String content

In XML Schema, how do I define a ComplexType that renders like the following?

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

If I try the following my tools* will say it is not valid

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

Tools I use are Altova XMLSpy for visual editing and wsdl2java to generate classes

Update: I have tried 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 said Invalid XML schema: 'Value 'xs:string' is not allowed for attribute 'base'.'

Upvotes: 3

Views: 819

Answers (1)

usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

Okay, I learned the lesson

  1. Attributes can extend a simpleContent but must appear within the extension
  2. I mistyped the xsd prefix in my second example which was almost correct

The correct form is

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

Upvotes: 4

Related Questions