• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

complexType can be root element?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I want to use NAME elements inside PersonType elements in my Schema but I don't want to allow it to be the root element of my XML document. Also I want the freedom to use it differently in the future, e.g. NAME element inside a BookType element. Which of the following is the correct way of achieving it?
(I) <xsd:complexType name="NameType">
<xsd:sequence>
<xsd:element name="GIVEN" type="xsd:string"/>
<xsd:element name="FAMILY" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="NAME" type="NameType"/>
</xsd:sequence>
</xsd:complexType>
(II) <xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="NAME">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="GIVEN" type="xsd:string"/>
<xsd:element name="FAMILY" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

Only (I) is correct
Only (II) is correct
Both (I) and (II) are correct

XML@Whiz answer is: B is correct. By defining the name element inside the xsd:element element which itself is a child of xsd:complexType element we have prevented it from being used elsewhere other than the PersonType element. Thus it cannot be the root element. Also we can define another name element inside a BookType element and make it mean whatever we want. The two name elements thus defined have nothing to do with each other. A is incorrect as (I) allows the NameType element to be used as the root element.
Neither (I) nor (II) is correct

I wouldn't agree: a complexType can't be the root element - it just defines a type of data, not an element...
What's your opinion?
Regards
Berny
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic