aspose file tools
The moose likes Product and Other Certifications and the fly likes Complex Type or Simple Type? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Product and Other Certifications
Reply Bookmark "Complex Type or Simple Type?" Watch "Complex Type or Simple Type?" New topic
Author

Complex Type or Simple Type?

Tong Chen
Ranch Hand

Joined: Apr 26, 2002
Posts: 1011
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,


Tong Chen (Seattle USA)<br />SCJP,SCWCD,SCDJWS,IBM XML,MCP.NET,MCAD.NET,MCSD.NET
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1129
Tong,
XML Spy accepts an empty element with no attribute to be -

The xml instance uses the empty element like this -

Cheers,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Tong Chen
Ranch Hand

Joined: Apr 26, 2002
Posts: 1011
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

Joined: Apr 26, 2002
Posts: 1011
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

Joined: Apr 26, 2002
Posts: 1011
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

Joined: Nov 14, 2000
Posts: 1953
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

Joined: Apr 26, 2002
Posts: 1011
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

Joined: Mar 25, 2002
Posts: 645
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

Joined: Mar 25, 2002
Posts: 645
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

Joined: Apr 26, 2002
Posts: 1011
I got it! Thank you very much Jayadev for your explanation!
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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 ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Complex Type or Simple Type?
 
Similar Threads
XML One liners for the exam....
Design pattern needed
Generating less JAXB classes
XSD query - same simple/complex type element?
difference between complex and simple type in XML schema