Switch to full style
XML Schema tutorial
Post a reply

lesson6: XSD Complex Empty Elements

Fri Mar 06, 2009 12:20 am

XSD Complex Empty Elements:

In your XSD schema, you can define empty XSD complex element with no content.
xml code
<xs:element name="student">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:integer">
<xs:attribute name="GPA" type="xs:positiveInteger"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>


This XSD snippet defines an XML element called "student" with a complex type consisting of an integer data type restriction. The restriction has an attribute called "GPA" of the data type "xs:positiveInteger". This means that the "student" element must contain an integer value, and it can also have an attribute called "GPA" that must contain a positive integer value. It is worth noting that this XSD snippet only defines the structure of the "student" element, it doesn't specify any rules or constraints on how many times the element can occur, where it can occur in the XML document, or what elements can come before or after it. If you want to specify these kinds of rules, you would need to include the "student" element in a larger XSD schema that defines the overall structure of the XML document. You can also specify the default value and fixed values for the attribute "GPA" by using the default and fixed attributes in the xs:attribute element.


The above Complex element type doesn't have any content elements.

xml code
<xs:element name="student">
<xs:complexType>
<xs:attribute name="GPA" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>

XML example:
xml code
<student GPA=3 />


Also remember, you can also can define you type by reference to complex type.
xml code
<xs:element name="student" type="studentType" />
<xs:complexType name="studentType" >
<xs:attribute name="GPA" type="xs:positiveInteger"/>
</xs:complexType>




Re: lesson6: XSD Complex Empty Elements

Mon Jan 09, 2012 2:46 pm

Using your first code snipped, I get this error on validation:

[Xerces-J 2.7.1] Validating "zzz-text.xml" against "file://zzz-text.xsd" ...
Ln 9 Col 45 - src-ct.1: Complex Type Definition Representation Error for type '#AnonType_studenttest'. When <complexContent> is used, the base type must be a complexType. 'integer' is a simpleType.
1 Errors

The second code is working. Should 'above' read 'below'?

Post a reply
  Related Posts  to : lesson6: XSD Complex Empty Elements
 lesson5: XSD Complex elements     -  
 lesson7: XSD Complex Text-Only Elements     -  
 Check empty string     -  
 check empty number     -  
 [Ajax/PHP] Registration - Check for empty textboxs     -  
 Complex Numbers     -  
 Complex numbers calculator (C++)     -  
 lesson9: XSD Complex Types Indicators     -  
 solve the complex numbers and do operations on it     -  
 lesson8: XSD Complex Type Mixed Content     -  

Topic Tags

XSD Elements