1
votes

I am trying to marshal to xml using JAXB.

My Requirement

if any elements value is null then the xml should have that element's attribute as nullable="true".

I tried putting minoccurs="1", nillable="true" in XSD, but it gave me nil="true" in my xml, but I wanted to show as nullable="true" exactly in my xml.

     <xs:sequence>
     <xs:element name="itemName" type="xs:string" />
     <xs:element name="purchasedOn" type="xs:date" minOccurs="1" nillable="true"/>
     <xs:element name="amount" type="xs:decimal" />
    </xs:sequence>

In my XML :

 <purchasedOn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>

Please correct me if I am wrong.

1
Are you wanting the attribute to say that the element can be null or that it is null ? - kirsty
nullable means that it's okay for that element to be null. It sounds like you will need to define your own bespoke attribute for this purpose. Can I ask, what's wrong with an empty element tag? - kirsty
If you have a null attribute, what's going to be inside the element it belongs to if it can't be empty? - kirsty

1 Answers

0
votes
<xs:complexType name="CustomDate">
<xs:simpleContent>
<xs:extension base="xs:date">
<xs:attribute name="nullable" type="xs:string">
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>