Presence and value are separate decisions. minOccurs controls whether an element must occur. nillable controls whether it may carry xsi:nil="true". Its datatype determines whether ordinary empty content is valid. See the W3C explanation of nil values.
Try missing, empty, nil and a value
Start with an optional string. The empty element passes. Switch the datatype to integer and the same empty element fails. Now allow nil and select the explicit nil instance: the element passes again, for a different reason.
What does your schema accept?
An empty string is a valid xs:string value. It is not an absent or nilled element.
01 / Schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="record">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="xs:string"
minOccurs="0" nillable="false"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>02 / XML instance
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<value/>
</record>Focused teaching model with complete downloadable examples. No arbitrary schema validation. The default example and comparison table remain readable without JavaScript.
The complete presence-and-nil matrix
This table fixes the datatype at unrestricted xs:string. Each row tests the same four instances. It is a reference you can read or print without operating the experiment.
| minOccurs | nillable | Missing | Empty | xsi:nil | 42 |
|---|---|---|---|---|---|
| 1 | false | Fail | Pass | Fail | Pass |
| 1 | true | Fail | Pass | Pass | Pass |
| 0 | false | Pass | Pass | Fail | Pass |
| 0 | true | Pass | Pass | Pass | Pass |
For xs:integer, every ordinary empty case fails. For a string restricted with minLength="1", ordinary empty content also fails. Those are value constraints; they do not turn a missing element into a present one. W3C datatype reference.
Choose what each state means to your application
Imagine an import carrying a customer’s middle name. Your application might interpret absence as “leave the stored value unchanged,” explicit nil as “clear the stored value,” and an empty string as a value to reject. Those meanings belong in the interface contract. The schema does not invent them for you.
Write one example for each accepted operation before choosing the declaration. Then check the deserialized result as well as the validator result. A binding library that maps both missing and nilled input to the same language value can erase a distinction that your update operation needs.
For a required numeric measurement, consider whether a missing reading is represented by an explicit nil value or whether an incomplete record should be rejected. Pair the chosen representation with a documented reason field if consumers need to distinguish “not measured” from “sensor error.”
Download a reproducible example
The experiment emits a complete schema and XML document. Save both downloads in one folder and validate the current case with an XSD 1.0 processor. For example, with libxml2 installed:
xmllint --noout --schema nillable-example.xsd nillable-example.xml Change one control, save a second fixture, and record why its result should differ. In a production test, also assert the value your consumer receives. These examples have no default or fixed values, custom facets, attributes on the value element or surrounding choice groups; introduce those features as separate tests.
Next, compare sequence, choice and all to see how the containing group changes what may appear. Use the element-versus-type guide when deciding whether to share a declaration or just its value rules.
Frequently asked questions
01Is minOccurs="0" the same as nillable="true"?
No. minOccurs="0" permits the element to be absent. nillable="true" permits a present element to carry xsi:nil="true" with no character or element content. The settings are independent.
02Can an empty string be valid when nillable is false?
Yes. An empty element can contain the empty string, which is valid for xs:string unless additional restrictions exclude it. It does not carry a nil marker.
03Does nillable="true" make the element optional?
No. An element with minOccurs="1" remains required even when it is nillable. Include the element and use xsi:nil="true" if the contract permits an explicit nil value.
04Can I use xsi:nil="true" with an integer element?
Yes, if the declaration is nillable and the element has no content. An ordinary empty integer element without the nil marker is invalid.
05Does XML null work exactly like JSON null?
XML has no universal null literal. xsi:nil is a schema-instance marker. Mapping absent, nilled and empty XML elements to missing properties, null or empty strings in JSON is an application decision.