Using xjc to add additional attribute to jaxb class

67 views Asked by At

I have a huge xsd file which I would like to convert to jaxb classes using xjc. I have done that successfully, both using maven and manually using the xjc.sh script.

But I do have one very specific problem that I am struggling to find a good solution to. Here is the problem:

I have a lot of simpletype definitions like the following in my xsd:

<xsd:simpleType name="MyType" abc:reference="https://example.com/valid_values.xml">
 <xsd:restriction base="xsd:string"/>
</xsd:simpleType>

In this case, the simpleType [ defining MyType of type xsd:string] has an additional attribute abc:reference with a constant value of a url. This url contains all the valid values this MyType string can have.

Now, when I generate the jaxb classes, I lose this information as it's not included in any way in java classes. Without this information, I am not able to, for instance, generate test data (xml files).

How can I tell xjc to include this url in jaxb class, for instance as public static String? Or any other way, as long as I am able to access this url from the java sources.

The solution is not to define enums or embed to those values, as they are provided externally and used by many others.

My schema looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="urn:my.company:v2" xmlns:abc="http://www.mycompany.no/xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:my.company:v2">

<xsd:simpleType name="MyType" abc:reference="https://example.com/valid_values.xml">
 <xsd:restriction base="xsd:string"/>
</xsd:simpleType>


 <xsd:complexType name="MyTypeWrapper">
        <xsd:sequence>
            <xsd:element name="mt" type="MyType"/>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

I have gone through https://docs.oracle.com/javase/tutorial/jaxb/intro/custom.html and some other resouces without any hint as to how in can fix this problem.

0

There are 0 answers