Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to prevent null or empty fields from being serialized into XML and still conduct validation with a schema is to use the "nillable" attribute in the schema definition. The "nillable" attribute allows a field to be explicitly marked as null or absent, while still being considered a valid field by the schema.

To use the "nillable" attribute, simply add it to the field definition in the schema, like this:

<xs:element name="myField" type="xs:string" nillable="true"/>

This will allow the field to be marked as null or absent, and still pass schema validation. When serializing the XML, any fields that are null or empty can be explicitly marked as "nil" using the "xsi:nil" attribute, like this:

<myfield xsi:nil="true"/>

This will prevent null or empty fields from being serialized as empty tags, while still allowing them to be validated against the schema.