I am trying to generate classes from following common.xsd
which imports x.xsd
and y.xsd
.
common.xsd
is as follows:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="mynamespace:x" schemaLocation="x.xsd"/>
<xs:import namespace="mynamespace:y" schemaLocation="y.xsd"/>
</xs:schema>
I try to use a binding file that specifies a common interface that is to be implemented by the generated classes. My binding file is as follows:
jaxb:extensionBindingPrefixes="inheritance" version="2.1">
<jaxb:globalBindings>
<jaxb:javaType name="java.lang.Long" xmlType="xsd:integer"/>
</jaxb:globalBindings>
<jaxb:bindings schemaLocation="common.xsd" node="/xsd:schema">
<jaxb:bindings node="xsd:complexType[@name='Customer']">
<inheritance:implements>jaxb.BaseMessage</inheritance:implements>
<jaxb:class />
</jaxb:bindings>
<jaxb:bindings node="xsd:complexType[@name='Payments']">
<inheritance:implements>jaxb.BaseMessage</inheritance:implements>
<jaxb:class />
</jaxb:bindings>
I tried to generate the code but it complains that:
[ERROR] XPath evaluation of "xsd:complexType[@name='Customer']" results in empty target node
[ERROR] XPath evaluation of "xsd:complexType[@name='Payments']" results in empty target node
How can I define the nodes in the bindings files are actually in the individual external XSD files but not in common.xsd
?
The jaxb:bindings allows to target a specific element in the schema and apply customization to this specific element. For instance to customize the name of the generated class.
xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.
xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.
JAXB: Generate Classes from XSD 1 Requirements 2 XJC command. The JAXB XJC schema binding compiler transforms/binds, a source XML schema (XSD) to a set of JAXB content classes in the Java programming language. 3 XJC in Action: Generating classes form XSD. To see the command XJC in action, we will need an XSD file. ... 4 Conclusion. ...
The JAXB XJC schema binding compiler transforms/binds, a source XML schema (XSD) to a set of JAXB content classes in the Java programming language. To see the usage of XJC command, just type in the same in command prompt/shell: 3. XJC in Action: Generating classes form XSD To see the command XJC in action, we will need an XSD file.
Java™ Architecture for XML Binding (JAXB) is a Java technology that provides an easy and convenient way to map Java classes and XML schema for simplified web services development.
In intellij click .xsd file -> WebServices ->Generate Java code from Xml Schema JAXB then give package path and package name ->ok Show activity on this post.
I'm posting a second answer because I believe what you are trying to achieve is possible without vendor extensions, but I also think that my original answer may be useful to others who need vendor extensions.
This example also removes the namespaces, but they could be added back easily. The same build script is used for this answer as in my previous one.
src/main/resources/bindings.xjb
:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
jaxb:version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc"
xmlns:common="schema/common.xsd">
<jaxb:globalBindings>
<jaxb:javaType name="java.lang.Long" xmlType="xsd:integer" />
<xjc:superInterface name="jaxb.BaseMessage" />
</jaxb:globalBindings>
</jaxb:bindings>
src/main/resources/schema/common.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="x.xsd" />
<xsd:include schemaLocation="y.xsd" />
<xsd:complexType name="CustomerPayments">
<xsd:sequence>
<xsd:element name="customer" type="Customer" />
<xsd:element name="payments" type="Payments" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
src/main/resources/schema/x.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="Customer">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
src/main/resources/y.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="Payments">
<xsd:sequence>
<xsd:element name="amount" type="xsd:float" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With