2

I wonder why instead of having to write

<xs:element name="elementName">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="subElementName" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

I cannot specify as an attribute of element that it is of xs:complexType? Or is there an xs:superSecretThingIdontKnowAbout which combines xs:element and xs:complexType? While we're at it is there even something combining also xs:sequence. What I would expect is that the code above could be written the following way.

<xs:superSecretThingIdontKnowAbout name="elementName" sequence="true">
    <xs:element name="subElementName" type="xs:string"/>
<xs:superSecretThingIdontKnowAbout>

Context

Since XSD is XML and can be extended at wish, I want to add that I'm writing the schema for a field in a MySQL database. I don't know if this restricts anyhow the possibility. Any answer which doesn't work for that is welcome too, since this would confirm that I'm not the only one bothered by this fact.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Yannick Widmer
  • 1,286
  • 3
  • 19
  • 30

1 Answers1

1

The only superSecretThingIdontKnowAbout that you're missing is that attributes are better for representing simple, structureless values; elements are better for representing structure. See XML Elements vs XML Attributes.

A complex type has structure; it is not a scalar. Moving from an element to an attribute frustrates the expression of structure. For example, in your alternative design, how would you represent occurrence indicators on a sequence? More attributes? Ugh. How about a sequence composed of choices mixed among subsequences? And so on.

Most people lament the lack of orthogonality in XSD. Your suggestion would make matters worse.

Do note, however, that you can define complex types and reference them via an attribute:

 <xs:element name="a" type="aType"/>

This is very useful in being able to share complex type definitions.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks for your insigthfull answer. If I may ask you further, what element would an `element` contain which allready contains `complextype` beside this one? – Yannick Widmer Apr 15 '16 at 13:18
  • An `xs:element` declaration might consist of a `xs:simpleType` rather than a `xs:complexType`. It might also include an `xs:annotation` or a number of `xs:unique`, `xs:key`, or `xs:keyref` children. – kjhughes Apr 15 '16 at 13:26
  • 1
    I see your point now, and actually it helped me understand things I've allready read. I think I grasped the concept now. Thanks again. – Yannick Widmer Apr 15 '16 at 15:30