Got CommonsXsdSchemaCollection with inline = true with one xsd which xs:include others. It has targetNamespace. Almost all of included xsds has the same namespace, but one xsd doesn't have targetNamespace. Xsd without targetNamespace have xs:attributeGroup with name and elements which refs to it. But when spring tries to inline this xsd exception appears: Attribute group must have ref name set. I debugged the source code of: org.apache.ws.xmlschema:xmlschema-core and I see that xs:attributeGroup element with name gets targetNamespace from xsd which includes it, and xs:attributeGroup with ref doesn't get this namespace, so this error appears.Question is: How to get rid off this error? Is there a way to put targetNamespace to all included elements? Or run this inlining without targetNamespace in some schemas. Or any other ways?
Related
I have the situation there is an web service implemented in old Visual Studio 2008, which accepts the SOAP requests with content having namespaces only in this form:
<Root xmlns="http://A">
<A>
<A1 />
</A>
<B xmlns="http://B">
<B1 />
</B>
</Root>
All other forms of the XML (unqualified, or qualified with namespace prefix) are causing problems even if the wsdl and referenced xsd structures are manipulated and modified in various ways and the service is recompiled in Visual Studio 2008, the author of the ASP.NET web service is not able to get any other form of XML working.
Is there any way how to achieve the generation of the classes for our Java client accessing this web service by using the external JAXB binding specification, used during the class generation, so the XML would be from the objects marshalled in the above described form?
Am I right, if I think, this could be achieved by the forcing the JAXB to generate the class(ses in multiple directories)
package-info.java
instead of the default form
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://some/custom/namespace",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package some.custom.namespace;
in the following form?
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://some/custom/namespace",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
xmlns = {#XmlNs(prefix = "", namespaceURI = "http://some/custom/namespace")})
package some.custom.namespace;
and if I am right, is there any way to do it? I am unfamiliar with writing the XJB bindings, so any pointer would be appreciated...
Or maybe, after the generation the source code of this class should be postprocessed by something like ant task, which would modify the initially generated JAXB form into the form I think it was the desired content of the class package-info.java?
Could this be somehow achieved by one universal jxb binding file for all usages without necessity to modify it for every namespace folder creating its own package?
I hope this could really work even with multiple different namespaces without prefixes, because the xmlns definitions are then used in JAXBContextImpl as a set of XmlNs types, so if there is an prefix or not hopefully should not matter... Am I right?
I am confused as to how the #XmlElementDecl annotation is generated from a WSDL file that was generated by an XSD file. I have noticed that there are tags in my XSD file that are formatted in the following manner:
<xsd:element>...</xsd:element>
and others are formatted as such:
<element>...</element>
or they have a type="element" attribute. The latter two types do not generate any code that contains the #XmlElementDecl annotation, but the first format does.
The annotation is present in the ObjectFactory file.
Does this mean there is a requirement for the schema to have xsd:element as a prefix?
You have to use #XmlElementDecl along with #XmlRegistry annotation in the class to generate the element. You can find this link with an example.
https://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlElementDecl.html
I have generated Java classes from an XSD, and in my XML responses I am getting a ns2: prefix which I do not need.
For example, I am getting this response:
<ns2:location xmlns:ns2="http://www.example.com/">
<ns2:response/>
</ns2:location>
But the response I am expecting is as follows:
<location>
<response/>
</location>
Your example shows not just a namespace prefix (ns2) declaration but also an actual namespace (http://www.example.com/ for the location and response elements) as not being desired.
If you have full control over the XSD, you can remove the
targetNamespace="http://www.example.com/"
attribute from the xs:schema element in the XSD. Elements of XML documents conforming to this XSD will then no longer be in any namespace, and your generated Java classes will reflect this new setting.
However, be aware that deleting (or changing) targetNamespace effectively changes the names of the components defined in the XSD. Think twice about making such a change if the XSD is defined by another party or otherwise already in use by others.
When using wsimport the "standard" way:
wsimport.exe -d C:/temp/generatedClasses -s C:/temp/sourceFiles C:/temp/myWsdl.wsdl
I get source files generated like this:
#XmlRootElement(name = "PingRequest")
public class PingRequest{
Last time the classes were generated the same WSDL/ XSDs should have been used and generated a output like this:
#XmlRootElement(name = "PingRequest", namespace = "http://me.foo.bar/any/")
public class PingRequest {
So the schemas namespace was included as attribute of the annotation. Since the generated class package-info.java has the following entry:
#javax.xml.bind.annotation.XmlSchema(namespace = "http://me.foo.bar/any", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
I assume adding the namespace-attribute is a done by configuration of wsimport/ jaxb schema compiler.
Could anyone explain me how to archive the namespace attribute beeing included?
Update: As Blaise answered correctly (described in the blog-link) the generated file package-info.java defines the namespace for all classes/ types inside the according package. Above example is obsolete if always the same namespace is included per #RootElement. Setting namespaces on #RootElement level may be used to have a certain #RootElement have its own namespace (which in case of wsimport should happen automaticially).
Very many thanks for any suggestions
What i tried:
used google, found https://www.java.net/node/681149 (exaclty my question from back in 2008) with no answer :(
read wsimport/ jaxb schema compiler options, tried out various that only controlled package ouput
read 12 similar question/ answer that poped up when i typed in the title of this question
Closest i found that has something to do with the namespaces was to have 'elementFormDefault="qualified' specified in both the XSD itself and the import part inside the WSDL which i have done.
Specifying the following annotation at the package level, and not specifying the namespace on all the #XmlElement/#XmlRootElement annotations.
#javax.xml.bind.annotation.XmlSchema(
namespace = "http://me.foo.bar/any",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
Is the equivalent to not having #XmlSchema and adding the namespace parameter to all the #XmlElement/#XmlRootElement annotations. They will produce/consume the same XML documents.
For More Information
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
I have many xsd's out which i have to generate jaxb classes. now my problem is that. all the xsd's have targetName space which is not mine. i want to generate jaxb classes with different name space. every time.
I have tried this with
targetNamespace elment in bindings.xjb in globalbindings
But i got an error saying that targetNamespace is allowed in jxb:globalbindings..
Can any one help me ..
Thanks In Advance.
Reagrds,
PhaniKiran.Gutha
Try using the XmlSchema annotation, e.g.:
#javax.xml.bind.annotation.XmlSchema(namespace="http://your.custom.namespace.com",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
This annotation has to be placed over a package. That's why you need to create a package-info.java file in each package you have the objects you serialize.
You can find more information in Javadoc: http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlSchema.html