9

I'm using

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>

to generate Java classes from XSD files.

I've added

<args>-npa</args>

so, the plugin doesn't generate anymore package-info.java, but with this option the generated java classes are different (namespace is added to every element).

So, I cannot customize the namespace using package-info.java.

How can I use a custom namespace without modifying manually generated files?

ovi2ut
  • 187
  • 1
  • 2
  • 10

2 Answers2

13

You may use the namespace-prefix plugin from jaxb2-common project (disclaimer : I wrote it) :

https://github.com/Siggen/jaxb2-namespace-prefix

This is a xjc pluging which allows to define namespace -> prefix mappings within the bindings.xml file :

<jxb:bindings schemaLocation="eCH-0007-3-0.xsd">
    <jxb:schemaBindings>
        <jxb:package name="ch.ech.ech0007.v3" />
    </jxb:schemaBindings>
    <jxb:bindings>
        <namespace:prefix name="eCH-0007" />
    </jxb:bindings>
</jxb:bindings>

Which will results in the following package-info.java file being generated (mind the added XmlNs annotation) :

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ech.ch/xmlns/eCH-0007/3", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = {
    @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://www.ech.ch/xmlns/eCH-0007/3", prefix = "eCH-0007-3")
})
package ch.ech.ech0007.v3;

Your pom.xml would look like :

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.0</version>
    <configuration>
        <schemaDirectory>src/main/resources</schemaDirectory>
        <catalog>src/main/resources/catalog.xml</catalog>
        <schemaIncludes>
            <include>*.xsd</include>
        </schemaIncludes>
        <bindingDirectory>src/main/resources</bindingDirectory>
        <bindingIncludes>
            <include>bindings.xml</include>
        </bindingIncludes>
        <args>
            <arg>-extension</arg>
            <arg>-Xnamespace-prefix</arg>
        </args>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-namespace-prefix</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</plugin>
Siggen
  • 2,147
  • 1
  • 19
  • 19
  • What if I want to override `@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ech.ch/xmlns/eCH-0007/3"` to `@javax.xml.bind.annotation.XmlSchema(namespace = ""` ? How can that be done? – icedek Apr 08 '13 at 18:09
  • @icedek, if you change the xml namespace, you are changing the very definition of your XSD : even though xml structures would remain identicals, they would not be compatibles. If you need to do that, why not modify the XSD itself ? – Siggen Mar 21 '14 at 14:21
  • 2
    Does anyone know if the same thing can be achieved with the jaxws-maven-plugin? – kovica Oct 23 '15 at 05:41
  • @kovica for jaxws-maven-plugin use `-B-Xnamespace-prefix`. – OrangeDog Sep 05 '16 at 17:28
  • "unrecognized parameter -Xnamespace-prefix" – Blessed Geek Nov 15 '16 at 19:55
  • @BlessedGeek, did you add the `jaxb2-namespace-prefix` dependency ? Note that it's a dependency of the `maven-jaxb2-plugin` plugin itself. – Siggen Nov 16 '16 at 07:27
  • gives me org.xml.sax.SAXParseException: The prefix "namespace" for element "namespace:prefix" is not bound. – teknopaul Jan 19 '17 at 17:31
  • Make sure to define `xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix` in header. Full example here : https://github.com/Siggen/jaxb2-namespace-prefix. – Siggen Jan 22 '17 at 16:52
  • What if I have multiple XSDs in a directory and all of them should have the same namespace prefix? – Dojo Jun 05 '20 at 01:33
  • Can I put directory name in schemaLocation? `` – Dojo Jun 05 '20 at 01:38
  • 1
    @Dojo To have multiple XSDs in a directory all sharing the same namespace prefix, I guess you'll have to repeat the binding definition for each XSD and each time specify the same prefix. I didn't try it though. And for using a directory as argument of _schemaLocation_ (kind of a wildcard criteria), it's more related to the way jxb binding works, and I don't really know. – Siggen Jun 06 '20 at 09:15
2

You can customise namespace by guiding JAX-B to generate the package-info.java and set the target namespace needed in your XSD.

<xs:schema version="1.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
    targetNamespace="yourTargetNameSpace"
    xmlns="yourTargetNameSpace"
    elementFormDefault="qualified">
dur
  • 15,689
  • 25
  • 79
  • 125
Dror B
  • 21
  • 1