This section describes a tutorial example on how to declare an empty element by defining a special complexType datatype.
What is an empty element? An empty element is an element with no text content, no sub (child) element,
and no attribute. There are ways to declare an empty element:
1. An empty element can be declared with a "simleType" datatype derived from "string" with length of "0".
Here is a sample schema, empty_simple.xsd, that declares an empty element using "simpleType":
2. An empty element can be declared with a "complexType" datatype with "complexContent" containing no "element"
and no "attribute".
Here is a sample schema, empty_complex.xsd, that declares an empty element using "complexType":
The second way seems to be better than the first way.
It extends the generic built-in datatype "anyType" with no attribute and no sub element.
To test these 2 schema documents, I wrote the following XML document, empty_error.xml.
<?xml version="1.0"?>
<br> </br>
Here are the errors generated from XsdSchemaValidator.java:
They confirm that the second schema is better:
>java XsdSchemaValidator empty_simple.xsd empty_error.xml
Error:
Line number: 2
Column number: 11
Message: cvc-length-valid: Value ' ' with length = '1' is not
facet-valid with respect to length '0' for type '#AnonType_br'.
Error:
Line number: 2
Column number: 11
Message: cvc-type.3.1.3: The value ' ' of element 'br' is not
valid.
Failed with errors: 2
>java XsdSchemaValidator empty_complex.xsd empty_error.xml
Error:
Line number: 2
Column number: 11
Message: cvc-complex-type.2.1: Element 'br' must have no character
or element information item [children], because the type's content
type is empty.
Failed with errors: 1