SK19
SK19

Reputation: 169

How to combine elements and attributes?

I'm reading the W3school about XSD and what I'm missing is how I specify common elements AND attributes within a complex type. I suppose it goes like this

<xs:element name="character">
  <xs:complexType>
    <xs:attribute name="lang" type="xs:string" use="required"/>
    <xs:all>
      <xs:element name="firstName" type="xs:string"/>
      <xs:element name="lastName" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>

but I can't verify this and hope you can clarify. Alternatives I can think of: attribute into <xs:all> or use of mixed="true".

Additionally, if you know where I can check my schemes for validity, please drop a link in the comments.

Upvotes: 1

Views: 187

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

You're almost there, but (perversely) the attributes have to be specified last, that is, after the xs:all.

I'd strongly recommend getting a book on XML Schema to best understand the concepts. W3Schools is good at presenting things in a simple way, but if you only have a simple understanding, you'll never understand the error messages.

Also, I'd recommend investing in a development tool such as oXygen for editing and validating your schemas.

Upvotes: 1

Related Questions