Skip to main content

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

2 replies [Last post]
visuel
Offline
Joined: 2012-09-05
Points: 0

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

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

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="http://www.w3.org/2001/XMLSchema-instance"/>

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="http://www.w3.org/2001/XMLSchema-instance"/>

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.
yaroska
Offline
Joined: 2006-05-05
Points: 0

Hi,

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?

visuel
Offline
Joined: 2012-09-05
Points: 0

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.