开发者

JAXB substitution of complex type with primitive type

开发者 https://www.devze.com 2023-01-07 07:20 出处:网络
In my schema I have some types that simply extend a simple XSD type (int or string). JAXB creates a separate java class per such type. I want to drop this intermediate class and configure JAXB to use

In my schema I have some types that simply extend a simple XSD type (int or string). JAXB creates a separate java class per such type. I want to drop this intermediate class and configure JAXB to use primitives where possible (e.g. substitute CountryType with java.lang.String and DocumentType with int or lava.lang.Integer). For example, for a given XSD it would be nice to have DestinationType.setDocumentType(int) and List<String> StatesType.getCountry(). I am happy to write type-wide an adapter for that, but it looks like only conversions from primitive XML types are supported. Maybe it is possible to make per-property type conversion? Please, give any example of JAXB binding customization, that can help.

<?xml version="1.0" encoding="UTF-8"?>
<schema
    xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:exch="http://www.mycompany.org/exchange"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    targetNamespace="http://www.mycompany.org/exchange"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

    <complexType name="countryType">
        <simpleContent>
            <extension base="string"/>
        </simpleContent>
    </complexType>

    <complexType name="statesType">
        <sequence maxOccurs="unbounded">
            <element name="country" type="exch:countryType"/>
        </sequence>
    </complexType>

    <complexType name="documentType">
        <simpleContent>
            <extension base="integer"/>
        </simpleContent>
    </complexType>

    <complexType name="destinationType">
        <sequence>
            <element name="states" type="exch:statesType" maxOccurs="1"/>
            <element name="document-type" type="exch:d开发者_如何学CocumentType" minOccurs="1" maxOccurs="1"/>
        </sequence>
    </complexType>
</schema>


Another possibility, can you change your schema?:

The following schema change will generate your desired object model.

Use:

<simpleType name="documentType">
    <restriction base="integer"/>
</simpleType>

Instead of:

<complexType name="documentType"> 
    <simpleContent> 
        <extension base="integer"/> 
    </simpleContent> 
</complexType> 


Good question. In the past I've solved this by running the schema through an XSLT pre-processing step, "flattening" the type hierarchy whilst retaining the semantics of the documents.

For example, the XSLT would drop the definition of the documentType type, and replace every reference to documentType with integer. The resulting processed schema still represents the same instance documents, but is simpler, and makes for a nicer binding.

This (rather half-baked) solution can apply to a number of similar problems to do with over-complex schema (e.g. replacing substitution groups with choice structures).

0

精彩评论

暂无评论...
验证码 换一张
取 消