Skip to main content

How to handle xsi:nil="true" together with a required attribute

Please note these forums are being decommissioned and use the new and improved forums at
3 replies [Last post]
Joined: 2012-09-05

Please consider the following snippet of a xsd that I have to deal with:

<element name="VALUE" nillable="true">
<extension base="double">
<attribute name="UOM" type="string" use="required" />

As you can see, the VALUE element is nillabel and the UOM attribute is required.

How should I use JAXB generated classes to marshall them to the following valid XML:
<VALUE UOM="cm" xsi:nil="true" xmlns:xsi=""/>

The problem is that in order to get xsi:nil=true I have to set "VALUE" to NULL.
This marshalls to the invalid XML (required UOM attribute is not there):
<VALUE xsi:nil="true" xmlns:xsi=""/>

But how can I set the required UOM attribute "cm" if it is earlier set to NULL?

Is there a workaround?
Please help?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-05-05


To be honest it's not very clear what are you trying to do.
First of all, your mentioned XML is not included (I think because of some formating in your post).
And regarting your question why do you need to set UOM if VALUE is null?

Joined: 2012-09-05

Thanks for your reaction.
I hope the XML is visible now.

I want to set UOM because the XSD requires me to do so (attribute UOM is required).
Otherwise the XML is not valid.

I agree with you that it is not logical to set the uom to any value when VALUE = null, but again the validation rules force me to it and JAXB generated code won't let me.

Joined: 2014-07-27

I hope it's not bad form to reply to an old post.

I'm trying to solve this exact problem. Visuel or anyone else, is there a way to do this?