patrodyne / hisrc-basicjaxb

XJC plugins and tools for JAXB.
BSD 3-Clause "New" or "Revised" License
15 stars 6 forks source link

Inheritance not working for element #13

Closed thomasgermain closed 12 months ago

thomasgermain commented 12 months ago

Hello,

I'm trying have some generated class to implement a specific interface. It is working perfectly for complextType, but not for element.

The following example generates two classes: MyComplexType and MyElement, but only MyComplexType implements the interface.

Am I doing something wrong ?

my-xsd.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://myns" targetNamespace="http://myn" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="MyElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="elementMessage" type="xs:string" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="MyComplexType">
        <xs:sequence>
            <xs:element name="complexTypeMessage" type="xs:string" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

my-binding.xjb

<jxb:bindings
        version="3.0"
        xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:inheritance="http://jvnet.org/basicjaxb/xjc/inheritance"
        jxb:extensionBindingPrefixes="xjc">

    <jxb:bindings schemaLocation="my-xsd.xsd" node="//xs:element[@name='MyElement']">
        <inheritance:implements>org.example.MyInterface</inheritance:implements>
    </jxb:bindings>

    <jxb:bindings schemaLocation="my-xsd.xsd" node="//xs:complexType[@name='MyComplexType']">
        <inheritance:implements>org.example.MyInterface</inheritance:implements>
    </jxb:bindings>
</jxb:bindings>

Real life problem is more complex because I'm receiving XML from third party partner and they are using element and, of course, I don't have control on the XML,

By the way, thanks for your work. It's really painful to find working solution for jaxb 3+ and jakarta !

patrodyne commented 12 months ago

In your bindings file, the path for the MyElement binding needs to include the sub-path for the anonymous complex type:

my-binding.xjb

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
        version="3.0"
        xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:inheritance="http://jvnet.org/basicjaxb/xjc/inheritance"
        jxb:extensionBindingPrefixes="xjc">

    <jxb:bindings schemaLocation="my-xsd.xsd" node="//xs:element[@name='MyElement']//xs:complexType">
        <inheritance:implements>org.example.MyInterface</inheritance:implements>
    </jxb:bindings>

    <jxb:bindings schemaLocation="my-xsd.xsd" node="//xs:complexType[@name='MyComplexType']">
        <inheritance:implements>org.example.MyInterface</inheritance:implements>
    </jxb:bindings>
</jxb:bindings>
thomasgermain commented 12 months ago

Working perfectly thanks !