Fry Simpson
Fry Simpson

Reputation: 1275

XML Schema: Complex Type with one child only

What is the point in declaring an element as a Complex Type which has one child element only?

I was given an XSD file from which I quote the following excerpt:

<xsd:element name="From">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="Address" type="xsd:anyURI"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
 
<xsd:element name="To" type="xsd:anyURI"/>

I understand that the From element (which must occur once and only once in its parent) consists of a sequence (which must occur once and only once) composed of the only element Address which must occur once and only once and is a URI.

While the To element (which must occur once and only once in its parent) is an URI.

Am I right? Am I missing something?

When it comes to insert data, the user must insert one and only one URI in the Address element of the From element, and one and only one URI directly in the To element.

Which Is the point in defining From as a Complex Type while defining To as a Simple Type?

When it comes to design User Interface to insert "To" and "From" URIs both are represented by a text box which has to be validated as a well formed URI.

When converting such XSD structure to a SQL Server database, both "To" and "From" are just a varchar field in the Table representing the parent element (not shown in my excerpt) which have to be validated as a well formed URI.

But I still think I am missing some important difference. Do I?

Upvotes: 1

Views: 714

Answers (2)

kjhughes
kjhughes

Reputation: 111686

You're not missing anything: TMTOWTDI (there's more than one way to do it) applies to XSD (and to XML in general) and provides multiple design alternatives, including:

  • XML attribute vs XML element considerations alone might drive the decision based on future expansion expectations for Address substructure, governing organizational or industrial conventions, etc.
  • The parent From element may be expanded to include other semantically relevant elements such as Person.
  • An existing OO or relational design might be targeted that better fits the From-Address complex type pattern.

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163468

Possible reasons include:

  • You are constraining an existing document design which allows other children as well (e.g. From/Name, or multiple instances of From/Address)
  • You envisage a future extension that allows other children as well
  • You are following a design convention (or using a design toolset) that requires an element whose name is the type of value (Address) and another that indicates the relationship to the parent element (From)

Upvotes: 1

Related Questions