1

Validation of XML with maven-created schema fails when namespace is defined, but validating against the schema works as long as I don't configure the namespace.

<transformSchemas>
<!--  doesn't work
    <transformSchema>
        <uri>tlm-classification</uri>
        <toPrefix>tlm-ec</toPrefix>
        <toFile>classification.xsd</toFile>
    </transformSchema>
-->
</transformSchemas>

If I define the namespaces, add a uri and also add the namespace to all the jaxb-annotated java classes there are two XSD files created instead of one and also the validation fails with:

src-resolve: Cannot resolve the name 'equipmentClassification' to a(n) 'type definition' component.

After I copied both xsd files into the classpath, I got

cvc-complex-type.2.4.a: Invalid content was found starting with element 'name'. One of '{"tlm-equipmentclassification":count}' is expected.

While creating the namespace-XSDs with maven I get this warning, that I couldn't fix, maybe the errors are related to that.

[INFO] --- jaxb2-maven-plugin:1.5:schemagen (default-cli) @ tlmsim ---
Note: Writing C:\dev\java\workspaces\tlm\tlmsim\schema1.xsd
Note: Writing C:\dev\java\workspaces\tlm\tlmsim\schema2.xsd
[WARNING] SimpleNamespaceResolver contained no localNamespaceURI; aborting rename.

I can also provide the xml, and the xsd files. But as the XMLs and the single non-namespace XSD validate, the errors must lie somewhere in the JAXB annotations.

Question

  • Why are there two XSD files created when I define a namespace?
  • What do I have to consider annotating the JAXB-annotated classes.
  • (Since it is suggested to use javac because schemagen will not be supported in future. Do you have any input on that?)

Additional info

I'm using org.codehaus.mojo:jaxb2-maven-plugin in version 1.5.

I have some kind of class hierarchy, and even use XmlAdapter to marshall a map thats contained in some of the XmlRootelements. Some parentclasses share the same child elements. My original plan was to automatically create a standalone scheme for each XmlRootelement in its corresponding directory.

If more information is needed, I can provide it.

mike
  • 4,929
  • 4
  • 40
  • 80
  • 1
    I'm just leaving the namespace out and doinng the renaming manually... but I still would like it to work, though. – mike Jul 24 '13 at 09:56
  • how do you define the namespace in Java annotated classes? – vincenzo iafelice Jul 29 '13 at 16:33
  • 2
    `@XmlRootElement` and `@XmlElement` have a `namespace` field, I did set it there in every related class. – mike Jul 29 '13 at 16:54
  • for me worked only defining all class with @XmlType annotation – vincenzo iafelice Jul 30 '13 at 08:51
  • What does it mean it worked for you? You don't get the `SimpleNamespaceResolver` warning, or that you get one correctly named file - as specified in the `` element - as output? – mike Jul 30 '13 at 11:34
  • warning is related to file rename, in fact error messages says "aborting rename." It seems that the only way to get the file renamed is to use only annotation `@XmlType` as described [here](http://mojo.codehaus.org/jaxb2-maven-plugin/usage_schemagen.html) – vincenzo iafelice Jul 30 '13 at 12:06
  • Still not working. I have some kind of class hierarchy, and even use XmlAdapter. Some parentclasses share the same child elements. My original plan was to automatically create a standalone scheme for each xml root element in its corresponding directory. – mike Jul 30 '13 at 12:35
  • 1
    it was my plan too. I think that this plugin is not suitable for us :-) – vincenzo iafelice Jul 30 '13 at 12:53
  • did you manager to resolve this? – obsessiveCookie Nov 01 '17 at 09:55

1 Answers1

1

I have managed to get it to work by making sure the Java class annotations share the same namespace.

   <transformSchemas>
    <transformSchema>
     <uri>http://some/namespace</uri>
     <toPrefix>some</toPrefix>
     <toFile>some_schema.xsd</toFile>
    </transformSchema>
   </transformSchemas>

and all the java classes have:

@XmlRootElement(namespace ="http://some/namespace")
@XmlType(namespace ="http://some/namespace")
obsessiveCookie
  • 1,130
  • 2
  • 18
  • 33