XJC binding file: override package based on namespace instead of schemaLocation
Asked Answered
A

2

2

I'd like to override package for my schema via binding .xjb file.

It is done with:

<schemaBindings>
    <package name="com.schema.common" />
</schemaBindings>

JXC compiler expects context for above code, defined via schemaLocation.

I store .xsd and .xjb files in separate directories and this looks ugly (like all Java EE):

<bindings schemaLocation="../../../../wsdl/common_v47_0/CommonReqRsp.xsd">
    <schemaBindings>
        <package name="com.schema.common" />
    </schemaBindings>
</bindings>

Is it possible to define bindings context without relative path in schemaLocation?

I've seen SCD ("schema component designator"). It looks promising but I can't find reference for this syntax...

Admit answered 7/2, 2019 at 1:47 Comment(0)
A
2

Here is the magic of SCD:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version='2.1'
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:tns="http://example/schema/common_v47_0">
    <bindings scd="x-schema::tns">
        <schemaBindings>
            <package name="com.schema.common" />
        </schemaBindings>
    </bindings>
</bindings>

All parts are important. SCD was introduced in version='2.1'.

I found reference at https://github.com/highsource/maven-jaxb2-plugin/wiki/Configure-Target-Packages-in-Binding-Files

SCD is extension of JAXB standard and implementation can be investigated in JAXB RI source tree:

Admit answered 7/2, 2019 at 7:59 Comment(4)
What is the x-schema part? I cannot find any documentation about it. Can you provide a link to documentation if you know where it is?Marcello
It is a magical constant of Metro JAXB implementation: github.com/eclipse-ee4j/jaxb-ri/search?q=x-schemaAdmit
Thanks. I wish I could figure out more about why they use the x-schema constant since the schema component designator (SCD) standard (w3.org/TR/xmlschema-ref) makes no mention of x-schema.Marcello
In my attempt to read SCD in Metro I found that the implementation lacks some features and no one updates it for years. It is non-standard and might be considered as a "dead" feature.Admit
R
2

If you are using a newer maven-jaxb2-plugin, for example version 3.1.0, then you should use JAXB 3 format for XJB files:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="3.0" xmlns="https://jakarta.ee/xml/ns/jaxb">

  <!-- Bindings for a particular XML namespace. -->
  <bindings scd="x-schema::tns" xmlns:tns="https://example.com/target/namespace">

    <!-- Binding Java package to the XML namespace. -->
    <schemaBindings>
      <package name="com.example.package"/>
    </schemaBindings>

    <!-- Binding a particular Java class to a particular XML type. -->
    <bindings scd="~tns:SomeXmlTypeName">
      <class ref="com.example.another.package.OverriddenJavaClassName"/>
    </bindings>

  </bindings>
</bindings>
Ringster answered 7/3 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.