• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Complex Type or Simple Type?

 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question about XML Schema Part 0: Primer.
It says: "Elements that contain subelements or carry attributes are said to have complex types, whereas elements that contain numbers (and strings, and dates, etc.) but do not contain any subelements are said to have simple types. "
But for the empty element type:
<isInUS/>
First, it has not a complex type, because it has no subelement, no attribute.
Second, it should not have a simple type, because it is empty.
But I guess it is still a simple type, because empty means NULL string.
Please correct me if I am wrong.
Thanks,
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tong,
XML Spy accepts an empty element with no attribute to be -

The xml instance uses the empty element like this -

Cheers,
Dan
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Dan!
Well, what I really meant is:
How do I write the XML schema for the following?
<address>
<street>
<name>12345 Wonder Street</name>
<isInUS/>
</street>
<street>
</street>
</address>
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Dan!
Well, what I really meant is:
How do I write the XML schema for the following?
<address>
<street>
<name>12345 Wonder Street</name>
<isInUS/>
</street>
<street>
<name>8829 124th Ave.</name>
</street>
</address>
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to write the XML schema for the above XML snippet:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="street" type="streetType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="streetType">
<xsd:sequence>
<xsd:element name="name" type="xsd:NMTOKENS"/>
<xsd:element name="isInUS" type="emptyType">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<!-- HOW DO I DEFINE TYPE: emptyType -->
</schema>
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take type="emptyType" away, replace it as minOccurs="0", you are done. This is my guess, if I understand your problem correctly.
<xsd:element name="isInUS" minOccurs="0" />
[ November 04, 2002: Message edited by: Roseanne Zhang ]
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Roseanne!
Yes, you are right. I finally figure it out!
so the XML schema will be:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="street" type="streetType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="streetType">
<xsd:sequence>
<xsd:element name="name" type="xsd:NMTOKENS"/>
<xsd:element name="isInUS" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

The instance document conforms to the above schema:
<?xml version="1.0" encoding="UTF-8"?>
<address>
<street>
<name>12345 Wonder Street</name>
<isInUS/>
</street>
<street>
<name>8829 124th Ave.</name>
</street>
</address>

The issue I had was,
for <xsd:element name="isInUS" minOccurs="0"/>
there is no "type" attribute. Is that because the "type" attribute is not mandatory?
Thanks again,
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the type is not specified, it defaults to "xsd:anyType" which is defined as follows -
<xsd:complexType name="anyType" mixed="true">
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:anyAttribute/>
</xsd:complexType>
This shows that you can have "any" number of elements and attributes. This is a totally liberal and extensible way of modelling our schema. This is similar to saying that an element content is ANY in DTD.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tong,
there is no "type" attribute. Is that because the "type" attribute is not mandatory?

When an attribute is declared for an element in the schema, we have an attribute called "use" whose default value is "optional". Hence even if an attribute is declared for an element, there is no rule saying that the element instance should have that attribute unless "use='required'" in the schema. Hope that this answers your concern.
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it! Thank you very much Jayadev for your explanation!
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jayadev and dan were correct. My answer was wrong. Two answers, I think, are correct for now
1) from dan

2) from xml bible, simplfied by Roseanne. complexType with no elements, no attributes, and mixed="false" by default. It can be nothing but empty.

[ November 11, 2002: Message edited by: Roseanne Zhang ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic