I am trying to create web service clients for the following wsdl.
https://sgtestr3xapi.navitaire.com/BookingManager.svc?wsdl
But it keeps throwing the above mentioned error. Tried creating bindings.xml file with separate package for each xsd mentioned in the import section of the wsdl. But that didnt help. Is there any other way to solve this problem. I am using netbeans 8.0.2.
A look at the xsd I found this.
<xs:complexType name="TicketRequest">
<xs:sequence>
<xs:element minOccurs="0" name="SegmentTicketRequests" nillable="true" type="tns:ArrayOfSegmentTicketRequest"/>
</xs:sequence>
</xs:complexType>
<xs:element name="TicketRequest" nillable="true" type="tns:TicketRequest"/>
2 variables with same name. There many such variable pairs. How do I handle this?
Related
I am new to JAXB and dealing with XSDs. I am using the Maven JAXB2 plugin to marshall classes from them. I got them from the provider of a web service that I need to consume, but I am not sure if they have made an error in their documentation or if I just may not be dealing with the duplication correctly.
So I have two XSD file a.xsd and b.xsd, both of which reside in the same directory.
In a.xsd I have the decleration:
<xs:complexType name="AttributeType">
<xs:sequence>
<xs:element ref="AttributeValue" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="AttributeName" type="xs:string" use="required" />
<xs:attribute name="AttributeNamespace" type="xs:string"
use="required" />
</xs:complexType>
In b.xsd I have :
<xs:complexType name="AttributeType">
<xs:sequence>
<xs:element ref="AttributeValue" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="AttributeName" type="xs:string" use="required" />
<xs:attribute name="AttributeNamespace" type="xs:string"
use="required" />
</xs:complexType>
The error I am getting in Maven is
A class/interface with the same name "com.mycompany.voice.gcs.correspondenceservice.v1.AttributeType" is already in use. Use a class customization to resolve this conflict.
They look identical to me and it doesn't make sense to have them duplicated. But as I said, I'm new to this stuff so I wanted to make sure that I wasn't missing something.
Thanks. :)
As long as these two schemas have different namespaces, it is OK. Look for the targetNamespace attribite of the root schema element.
If target namespace is missing or if target namespaces are the same, this might be a problem.
This looks like a "copy-paste" schema design, not a preferred modular design, but it's hard to judge without seeing the schemas.
"Copy-paste" design is not atypical but is also not a good practice.
The service provider gave me the wrong files with duplicating complex types. As a result the duplicates were failing.
Here are two xsd definitions, both of them are almost 90% similar.Below are the skeleton of the first xsd:
XSD1 :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="apf2doc">
<xs:complexType>
<xs:sequence>
<xs:element ref="request"/>
<xs:element ref="account"/>
<xs:element ref="financial_transaction"/>
<xs:element ref="event_data" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And second xsd is:
XSD2:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="apf2doc">
<xs:complexType>
<xs:sequence>
<xs:element ref="request"/>
<xs:element ref="account"/>
<xs:element ref="message"/>
<xs:element ref="event_data" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Now these two xsds generate two sets of classes in two different packages. I am using JAXB to unmarshall the xmls received. The xmls are generated from these two xsds.
While creating a JAXB context it throws me error because most of the classes cause conflict I believe.
Here is the error trace:
The element name {}userid has more than one mapping. This problem is related to the following location:
at public javax.xml.bind.JAXBElement
generated.order.ObjectFactory.createUserid(java.lang.String) at
generated.order.ObjectFactory this problem is related to the following location:
at public javax.xml.bind.JAXBElement
generated.usage.ObjectFactory.createUserid(java.lang.String) at
generated.usage.ObjectFactory
It would be great if someone can suggest me any solution.
Thanks.
Since your 2 XML schemas have global elements with the same name and namespace you won't be able to create a single JAXBContext on both models. You can do one of the following:
Create a separate JAXBContext for each model.
Use namespaces to differentiate the 2 XML schemas.
I reading articles about XSD on w3schools and here many examples. For example this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But after I tried put this .xsd file in xjc - I see error log, dome like this:
The prefix "xs" for element "xs:schema" is not bound...
But all work correct when I change xs on xsd prefix.
So, can somebody, clarify for me what is different between xs and xsd?
Maybe, one prefix - it is old version and other for new version...
xs and xsd are XML prefixes used with qualified names; each prefix must be associated with a namespace. The association is done with an attribute that looks like xmlns:xs="...". xs and xsd are most common for XML Schema documents.
Should you choose s or ns1, it shouldn't make any difference to any tool for your scenario.
The error is not caused by your XML Schema file. I suspect there might be something else in your setup, maybe a custom binding file. Please check that or post additional information.
I would like to allow for an element to either contain an attribute OR define a more complex type.
Something like
<myElement someAttr="..."/>
or
<myElement>
<...>
</myElement>
That is, if someAttr exists then I do not want to allow sub elements and if it doesn't then I want to.
The reason for this is I want to have an "include" feature where I include a file which is essentially inserted into the element. But I don't want both. You can either include additional external xml code into the element or add your own BUT not both. (or also to have it inserted from a separate part of the xml)
This is mainly for simplifying a complex xml so that the structure is easily understood.
I doubt you will be able to express something like that in XML schema at this point.
You can make an attribute optional, e.g. it can be present or not. But you cannot express something like if the attribute is not present, then include other complex content with the current means.
You'll have to either check this programmatically yourself, or maybe investigate if other XML description languages like RelaxNG or Schematron might be able to help.
Perhaps with a static choice and you change the myElement name ?
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:element name="myElementWithAttrs">
<xs:complexType>
<xs:attribute name="someAttrs" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="myElementWithoutAttrs">
<xs:complexType>
<xs:sequence>
<xs:any processContents="skip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
This XSD portion was obtained from: http://www.iana.org/assignments/xml-registry/schema/netconf.xsd
<xs:complexType name="rpcType">
<xs:sequence>
<xs:element ref="rpcOperation"/>
</xs:sequence>
<xs:attribute name="message-id" type="messageIdType" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
<xs:element name="rpc" type="rpcType"/>
And is the core to function calls in NETCONF being the node of an XML document. I am curious as to why it is not something like:
<xs:element name="rpcType">
<xs:complexType>
<xs:sequence>
<xs:element ref="rpcOperation"/>
</xs:sequence>
<xs:attribute name="message-id" type="messageIdType" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
</xs:element>
The reasoning is that in #1 when trying to marshall a bean (in jaxb2) I get the exception:
[com.sun.istack.SAXException2: unable to marshal type "netconf.RpcType" as an element because it is missing an #XmlRootElement annotation]
I have been reading this article over and over again, and really cant get a hold of the difference, and why it would be #1 vs #2...
It's not obvious, I'll grant you. It comes down to the type vs element decision.
When you have something like
<xs:element name="rpcType">
<xs:complexType>
This is essentially an "anonymous type", and is a type which can never occur anywhere other than inside the element rpcType. Because of this certainty, XJC knows that that type will always have the name rpcType, and so generates an #XmlRootElement annotation for it, with the rpcType name.
On the other hand, when you have
<xs:complexType name="rpcType">
then this defines a re-usable type which could potentially be referred to by several different elements. The fact that in your schema it is only referred to by one element is irrelevant. Because of this uncertainty, XJC hedges its bets and does not generate an #XmlRootElement.
The JAXB Reference Implementation has a proprietary XJC flag called "simple binding mode" which, among other things, assumes that the schema you're compiling will never be extended or combined with another. This allows it to make certain assumptions, so if it sees a named complexType only being used by one element, then it will often generate #XmlRootElement for it.
The reality is rather more subtle and complex than that, but in 90% of cases, this is a sufficient explanation.
Quite an involved question. There are many reasons to design schemas using types rather than elements (this approach is called the "venetian blind" approach versus "salami slice" for using global elements). One of the reasons is that types can be sub-typed, and another that it may be useful to only have elements global that can be root elements.
See this article for some more details on the schema side.
Now, as for the JAXB question in particular. The problem is that you created a class corresponding to a type and tried to serialise it. That means JAXB knows its content model, but not what the element name should be. You need to attach your RpcType to an element (JAXBElement), for example:
marshaller.marshal(new ObjectFactory().createRpc(myRpcType));
The ObjectFactory was placed into the package created by JAXB for you.
Advantages of Named Types
The advantage of a schema using global/named types is that child/sub types can be created that extend the parent type.
<xs:complexType name="rpcType">
<xs:sequence>
<xs:element ref="rpcOperation"/>
</xs:sequence>
<xs:attribute name="message-id" type="messageIdType" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
<xs:element name="rpc" type="rpcType"/>
The above fragment would allow the following child type to be created:
<xs:complexType name="myRPCType">
<xs:complexContent>
<xs:extension base="rpcType">
<xs:sequence>
<xs:element name="childProperty" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Impact on JAXB
Another aspect of named types is that they may be used by multiple elements:
<xs:element name="FOO" type="rpcType"/>
<xs:element name="BAR" type="rpcType"/>
This means that the schema to Java compiler cannot simply just pick one of the possible elements to be the #XmlRootElement for the class corresponding to "rpcType".