Reputation: 879
I have a xsd schema where I have added a new field owner_email in the Cart object. I want this within our application when I am sending Cart Object to other system (as json request) or returning Cart object as response (as json) i don't want to pass owner_email field. Is there any attribute that I can set for owner_email field?
<xs:complexType name="Cart">
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="owner_id" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="owner_email" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
Here i am not editing the generated java code. I need to modify the xsd file itself. what is the attribute i should use in xsd to specify the same.
Upvotes: 0
Views: 952
Reputation: 2975
I have a way to make it work, but there are 2 "gotchas" that I am not very pleased with.
First, changing your xsd to this:
<xs:element name="Cart">
<xs:complexType>
<xs:all>
<xs:element name="id" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="owner_id" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="ownerEmail" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:all>
</xs:complexType>
</xs:element>
And then you can create bindings file to generate the transient annotation as mentioned to you by other users:
<jxb:bindings
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:annox="http://annox.dev.java.net"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
jxb:extensionBindingPrefixes="xjc annox" version="2.1">
<jxb:bindings schemaLocation="../xsd/cart.xsd">
<jxb:bindings node="//xs:element[@name='Cart']//xs:complexType//xs:all//xs:element[@name='ownerEmail']">
<annox:annotate target="field">
<annox:annotate annox:class="javax.xml.bind.annotation.XmlTransient" />
</annox:annotate>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
This would generate when marshaling the following xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Cart>
<id>id</id>
<name>name</name>
<owner_id>owner id</owner_id>
</Cart>
The gotchas:
Renamed field "owner_email" to "ownerEmail" because the first version would generate (along with transient) @XmlElement with the owner_email name and have the field according to naming conventions.
I will try to see if I can find workaround for these but for now maybe this helps. Cheers!
Upvotes: 1