从XSD生成XML中的自定义属性

时间:2022-04-28 17:17:35

I am trying to marshal to xml using JAXB.

我正在尝试使用JAXB编组到xml。

My Requirement

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

如果任何元素值为null,那么xml应该将该元素的属性设置为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.

我尝试在XSD中输入minoccurs =“1”,nillable =“true”,但它在我的xml中给了我nil =“true”,但我想在我的xml中显示为nullable =“true”。

     <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 :

在我的XML中:

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

Please correct me if I am wrong.

如果我错了,请纠正我。

1 个解决方案

#1


0  

<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>

#1


0  

<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>