I am trying to generate Java classes from a XSD schema using the xjc command that comes with JAXB.
My schema looks like:
<xs:schema xmlns="..."
xmlns:ext="http://schemas.myco.com/ext" ... >
<xs:import namespace="http://schemas.myco.com/ext"
schemaLocation="http://myco.com/schemas/ext.xsd"/>
...
The problem is that the schemaLocation URI does not exist and I can not modify the XSD file. That is why the generation process fails with errors such as "src-resolve: Cannot resolve the name 'ext:Resource_Type' to a(n) 'type definition' component".
Is there any way to force JAXB to use a local copy of ext.xsd file during the process without modifying the original XSD file ?
Two possible solutions:
JAXB Episode File:
http://weblogs.java.net/blog/2006/09/05/separate-compilation-jaxb-ri-21
or
XML Catalogs (search Google)
Related
I am a bit new to XSD, I tried updating a XSD and it is giving me the below error
Code i have added:
<xs:element ref="cards:paymentrelationship" />
For this i have added below thing to the xml:schema
xml:cads="uri"
and below import statement
<xs:import namespace="cads" schemalocation="uri"/>
i am getting the below error
componenets from this namespace are not referencable from this xsd , appropriate import tag needs to be added
Here the cards text is a namespace-prefix that you are using in the xsd.
The namespace-prefix must be associated to an namespace-value (here "uri") not with xml: attribute but rather xmlns::
xmlns:cards="uri"
Then you need to use correctly you import statement: target an external xsd resource that is defining the elements belonging to namespace uri. Let's say you have another xsd called 'simpleTypes.xsd', this xsd is declaring targetnamespace='uri', it is the one you have to import in your current XSD:
<xs:import namespace="uri" schemalocation="./simpleTypes.xsd"/>
To start with XML namespaces, I would advice starting with w3schools rather than looking at the W3C specifications.
Have a look at:
http://www.w3schools.com/xml/xml_namespaces.asp
http://www.w3schools.com/xml/schema_schema.asp
I am using fasterxml-jaxb(2.3) module to generate XML but it seems its not reading Xmlns annotation while creating XML. I have the prefix defined in my package-info as below
#javax.xml.bind.annotation.XmlNs(prefix = "bla"....
Jackson does not apply the prefix however if i user I use JDK's JAXB for marshalling the prefix is applied to the XML
Jackson does not use Package annotations for anything; so while JAXB annotations are supported (if using JAXB annotations module), no annotations are read from package-info.java.
This because Jackson annotation module is based on inheritance (you can add all annotations on super-classes, and they are visible for sub-types).
You may be able to add a mix-in annotation for super-class (or regular annotation), and that would work.
I have a main xsd built against another one containing shared types (they are in the same directory).
That's the header of the main one :
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="XMLSchema_Module.xsd"
xmlns:cs="XML_Common"
targetNamespace="XMLSchema_Module.xsd"
elementFormDefault="qualified">
<xsd:import schemaLocation="XML_Common.xsd" namespace="XML_Common"/>
That's the header of the shared types one :
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="XML_Common"
elementFormDefault="qualified">
That's the shared type enumeration I want to use in an attribute of the main schema :
<xsd:simpleType name="TypesType">
<xsd:restriction base ="xsd:token">
<xsd:enumeration value="int"/>
And here is the reference in the main one :
<xsd:attribute name="Type" type="cs:TypesType"/>
I am using xsd.exe to generate the serialization classes (integrated in the build process as a pre-build event). These xsd were built using VisualStudio.
<Exec Command=""C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\xsd.exe" XSchemas\XMLSchema_Module.xsd /c /o:SerializationClasses /n:ScenarioManager" />
I am translating the actual message I obtain :
The type XML_Common:TypesType is not declared or is not a simple type.
It seems to be an import problem but I do not know how to solve it.
My bad, it seems that xsd.exe does not resolve schema imports.
From this article :
Now that the type has been defined in another file, the xsd.exe will
generate this error if you attempt to create the create the Request
XML: C:\Solution\Project>xsd.exe Request.xsd /c Schema validation
warning: Type 'http://www.Example.com/Types:Meeting' is not declared.
Warning: Schema could not be validated. Class generation may fail or
may produce incorrect results. Error: Error generating classes for
schema 'C:\Solution\Projects\Request'.
The datatype 'http://www.Example.com/Types:Meeting' is missing. If you would like more help, please type "xsd /?". This is due to the
fact that the xsd.exe does not use the schemaLocation hint to find the
imported schema. You’ve got to include it as a parameter. in your
xsd.exe call: C:\Solution\Project>xsd.exe Types.xsd Request.xsd /c
It should be emphasized that despite the pessimistic opening of LB40's answer, its last sentence yields the solution: in your xsd.exe call you have to include the referenced xsd as a parameter before the referencing xsd:
C:\Solution\Project>xsd.exe Types.xsd Request.xsd /c
This will generate a single cs source file containing the classes for both the schemas.
I'm trying to generate Java classes from a set of XML schemas. The following binding file is used to handle mixed content in the schemas:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings generateMixedExtensions="true"/>
</jaxb:bindings>
The code generation works fine but one of the generated classes has an #OverrideAnnotationOf from the com.sun.xml.internal.bind.annotation package. This package is included in rt.jar but the regular java compiler can't find it (and probably shouldn't find it because it is sun internal).
Is there a way to deal with the mixed content and not have the OverrideAnnotationOf from a sun internal package in my generated code?
In Java 6 and up, oracle moved the JAXB implementation embedded within the JRE to a different package to prevent potential collisions with the external JAXB reference implementation.
So the class OverrideAnnotationOf got moved from package com.sun.xml.bind.annotation to package com.sun.xml.internal.bind.annotation.
The embedded xjc, however, still generates java files that are annotated with com.sun.xml.bind.annotation.OverrideAnnotationOf(!)
Therefore, the JAXB implementation shipped with Java 7 will not understand its own output generated with generateMixedExtensions="true". Even if you use the -XDignore.symbol.file option.
I have a project that does JAXB generation with framework.xsd. This generates a jar with the xsd and the jaxb objects and other classes around that stuff.
Then another group(two different groups) will be extending framework.xsd and subxmling using the schema extends stuff to extend objects in framework.xsd. They also want to generate jaxb objects BUT they want their SomeClass.java to obviously extend my Framework.java and don't want to end up with a whole new heirarchy.
Is this even possible?
How to do something like this? as the solution would need to
tell the jaxb compiler that the namespace yy is already generated so do not generate
tell the jaxb compiler that it needs to refer to the classes in the package zzzzzz or to look at the xjb file from the framework jar file or something.
Is this possible?
thanks,
Dean
You want to use an episode file : http://weblogs.java.net/blog/kohsuke/archive/2006/09/separate_compil.html when generating JAXB classes for your first schema.
$ xjc -episode framework.episode framework.xsd
Then the other group that consumes your framework.jar should:
1) import your schema in their own schema e.g.:
<xsd:import namespace="http://www.myorg.com/framework" schemaLocation="framework.xsd"/>
2) generate their JAXB classes
$ xjc extend.xsd -b framework.episode
(they'll need a copy of your xsd and episode file at xjc time, as well as the framework.jar in the classpath)
Note that according to the blog post above, you can also place the framework.episode file inside your jar (e.g. /META-INF/sun-jaxb.episode for JAXB RI at least - other JAXB impl may have other ways of accomplishing the same thing), so that the -b framework.episode option can be omitted. I personally find it a bit impractical, you still need the XSD anyway.