Ask Your Question
0

What is the way to prevent null or empty fields from being serialized into XML and still conduct validation with a schema?

asked 2023-05-28 16:38:52 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-28 16:58:01 +0000

qstack gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-28 16:38:52 +0000

Seen: 10 times

Last updated: May 28 '23