JAXB class generation with external java class reference - xsd

I have added a maven dependency in my project. Example: Added dependency project com.test.classPackage which has SampleClassA
Now in my XSD project, how can I reference SampleClassA
<xs:element name="MyProjectClass">
<xs:complexType>
<xs:sequence>
<xs:element "want to reference SampleClassA here" minOccurs="1"
maxOccurs="1">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

Related

Using x:anyType instead of xsi:type gives Jaxb marshalling errors

I want to avoid xsi:type in an element and add the child elements at runtime based on some condition
I have option element defined as follows of type xs:anyType
<xs:complexType name="prod">
<xs:sequence>
<xs:element type="xs:anyType" name="option" minOccurs="0" maxOccurs="1"</xs:element>
</xs:sequence>
</xs:complexType>
used in element mapping as follows
<xs:element name="mappings">
<xs:complexType>
<xs:sequence>
<xs:element type="prod" name="productionSystem" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
I have need to add following option type as one of the following
<xs:complexType name="Text">
<xs:sequence>
<xs:element type="xs:short" name="id" />
<xs:element type="xs:string" name="name" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Value">
<xs:sequence>
<xs:element type="xs:short" name="id" />
<xs:element type="xs:string" name="name" />
<xs:element name="psValue" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="value" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
so basically one element option can be either of type Text or Value.
I want to avoid xsi:type in the xml. hence defined as xs:anyType. However at runt time Jaxb Marshalling fails with error
"any of its super class is known to this context". How to ensure Text and Value are in Jaxb Context.
Can someone pls guide on same.
Thanks,
Anjana

XSD inheritance causes problems in Jersey/JAXB

I have the following XSD file (contents shown):
<xs:element name="person" type="Person"/>
<xs:element name="child" type="Child"/>
<xs:complexType name="Person">
<xs:sequence>
<xs:element name="age" type="xs:int"/>
<xs:element name="sex" type="xs:string"/>
<xs:element name="fullname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Child">
<xs:complexContent>
<xs:extension base="Person">
<xs:sequence>
<xs:element name="grade" type="xs:string"/>
<xs:element name="school" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
I'm able to marshal instances of the Child class and send them to a client browser using the Jersey REST API.
But when I try to do this for the Person class it no longer works. I get the error message:
A message body writer for Java class app.Person, and Java type class app.Person, and MIME media type application/xml was not found
By extending the Person schema type, am I somehow rendering it no longer a root-level element? When Child doesn't extend Person, it works fine.

Difference between group and sequence in XML Schema?

What is the difference between an xs:group and an xs:sequence in XML Schema? When would you use one or the other?
xs:sequence - together with xs:choice and xs:all - is used to define the valid sequences of XML element in the target XML. E.g. the schema for this XML:
<mainElement>
<firstSubElement/>
<subElementA/>
<subElementB/>
</mainElement>
is something like:
<xs:element name='mainElement'>
<xs:complexType>
<xs:sequence>
<xs:element name="firstSubElement"/>
<xs:element name="subElementA"/>
<xs:element name="subElementB"/>
</xs:sequence>
</xs:complexType>
</xs:element>
xs:group is used to define a named group of XML element following certain rules that can then be referenced in different parts of the schema. For example if the XML is:
<root>
<mainElementA>
<firstSubElement/>
<subElementA/>
<subElementB/>
</mainElementA>
<mainElementB>
<otherSubElement/>
<subElementA/>
<subElementB/>
</mainElementB>
</root>
you can define a group for the common sub-elements:
<xs:group name="subElements">
<xs:sequence>
<xs:element name="subElementA"/>
<xs:element name="subElementB"/>
</xs:sequence>
</xs:group>
and then use it:
<xs:element name="mainElementA">
<xs:complexType>
<xs:sequence>
<xs:element name="firstSubElement"/>
<xs:group ref="subElements"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="mainElementB">
<xs:complexType>
<xs:sequence>
<xs:element name="otherSubElement"/>
<xs:group ref="subElements"/>
</xs:sequence>
</xs:complexType>
</xs:element>

JAXB customize bindings - skip generated classes from schema

I have a following schema:
<xs:element name="Invoice">
<xs:complexType>
<xs:sequence>
.....
<xs:element name="InvoiceLines" type="InvoiceLinesType">
</xs:element>
.....
</xs:complexType>
</xs:element>
<xs:complexType name="InvoiceLinesType">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="InvoiceLine" type="InvoiceLineType">
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="InvoiceLineType">
<xs:sequence>
.....
</xs:sequence>
</xs:complexType>
The problem is, that it generate classes:
Invoice - which contain member of InvoiceLinesType
InvoiceLinesType - which contain a collection of InvoiceLineType
InvoiceLineType
So there is one unnecessary class (InvoiceLinesType) and i prefer the following
Invoice - which contain a collection of InvoiceLineType
InvoiceLineType
Does anyone know how to tell the compiler not to generate this package (InvoiceLinesType).
My current external binding file is there
<jxb:bindings schemaLocation="invoice.xsd" node="/xs:schema">
<jxb:globalBindings>
.....
<xjc:simple/>
.....
</jxb:globalBindings>
</jxb:bindings>
Thank You for response.
You would have to modify your schema - drop InvoiceLinesType and have InvoiceLineType as unbounded element in Invoice.
<xs:element name="Invoice">
<xs:complexType>
<xs:sequence>
.....
<xs:element maxOccurs="unbounded" name="InvoiceLine" type="InvoiceLineType">
</xs:element>
.....
</xs:complexType>
</xs:element>
<xs:complexType name="InvoiceLineType">
<xs:sequence>
.....
</xs:sequence>
</xs:complexType>

XSD - Override element

I've a base class, marked as abstract:
<!-- TYPE: BASE CLASS -->
<xs:complexType name="BaseClass" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="implementation" type="BaseClass" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
Now the class that inherits BaseClass and overrides 'implementation' with null:
<xs:complexType name="MyClass">
<xs:complexContent>
<xs:extension base="BaseClass"/>
</xs:complexContent>
</xs:complexType>
I've tried this but is invalid:
<xs:complexType name="MyClass">
<xs:complexContent>
<xs:extension base="BaseClass">
<xs:sequence>
<xs:element name="implementation" fixed="xs:nil"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Is there a way to do this?
Marking something as abstract in XSD schema basically means that it cannot appear in an instance document. It should be used with the substitutionGroup attribute.
For example
<xs:element name="replaceme" abstract="true" />
<xs:element name="replacement1" substitutionGroup="replaceme" />
<xs:element name="replacement2" substitutionGroup="replaceme" />
This means that in an instance document you would be forced to use either a "replacement1" or a "replacement2" element, ie the xsd is forcing you to substitute the "replaceme" element with one of the replacements from the substitution group.
So to achieve what you want you need to create a substitution group consisting of the types you want to use instead of your base type. I am not sure this is possible with complex types but give it a go.

Resources