Unmarshal Jaxb class error - jaxb

I have a xml string which is a response from third party server.
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<GetRateQuoteResponse>
<GetRateQuoteResult>
</GetRateQuoteResult>
</GetRateQuoteResponse>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
I want to parse it in JAX-WS.
So i converted this String to xsd file using this web site
The web site gave me the xsd as follows.
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="SOAP-ENV:Envelope">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="SOAP-ENV:Body">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="GetRateQuoteResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="GetRateQuoteResult" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
I now generate JaxB classes from this xsd, it tries to create class named
SOAP-ENV:Envelope and SOAP-ENV:Body which is invalid name for a class and it fails to do so.
Also even if i modify xsd to Envelope and Body the Unmarshalling fails.
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{}SOAP-ENV:Envelope>
Please guide.

The XSD should not contain any SOAP elements; only the WSDL does. The XSD by definition is or data only. The transport protocol information (SOAP) should not be in the data definition as XSDs have applications outside of SOAP.
Omit the SOAP schema elements from the entry you pass to the XSD generato. By including that stuff in there, you're indicating to the generator that those elements will be part of your JAXB-generated classes (which shouldn't be the case). What you should feed the generator is the excerpt below, which would rightly generate the GetRateQuoteResponse-containing schema
<GetRateQuoteResponse>
<GetRateQuoteResult>
</GetRateQuoteResult>
</GetRateQuoteResponse>
The result:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="GetRateQuoteResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="GetRateQuoteResult" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Related

Unmarshalling xsd with selective element replacement from another xsd

I am new to working with xsd and jaxb, so if my questions are ignorant, please educate me. Also, if you need any more info, let me know and I will provide what I can.
At our company we are working with xsd sources from a 3rd party. We need to implement a SOAP - Java translation and we have a bunch of .wsdl and .xsd files to work with. We are running into problems with two of the .xsd files. The 3rd party provides a "datatypes.xsd", with a org.hl7.v3 namespace and a "datatypes-extensions-hl7nl.xsd", with a nl.hl7.v3 namespace.
When I unmarshall the xds sources, the resultant .java files get created under a different package, named after their own namespaces: org/hl7/v3 and nl/hl7/v3.
Whereas the 3rd party has intended that, where applicable, the types from "datatypes-extensions.xsd" have precedence and should be generated instead of those in "datatypes.xsd", and in the same package.
What I have tried so far
Since I cannot alter the xsd sources, I have (unsuccessfully) tried 2 things using a .jxb file.
1: generating the .java files in a single directory. This led to overwriting issues.
2: providing a binding to point to the correct type:
<jaxb:bindings schemaLocation="../coreschemas/datatypes.xsd">
<jaxb:bindings node="//xs:complexType[#name='TEL']">
<jaxb:class ref="nl.hl7.v3.TEL"/>
</jaxb:bindings>
</jaxb:bindings>
This worked initially, until I changed the reference of a supertype, at which point the compiler complained it could no longer locate the proper supertype for the extending types.
Sources
The sources are opensource so if you'd like to see them for yourself: https://decor.nictiz.nl/pub/vzvz/kz-vzvz-xml-20190122T131422.zip
The .wsdl:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:hl7="urn:hl7-org:v3"
targetNamespace="urn:hl7-org:v3"
name="OpvragenOverdrachtconcerns">
<documentation> WSDL implementatie van OpvragenOverdrachtconcerns</documentation>
<types>
<xsd:schema targetNamespace="urn:hl7-org:v3" elementFormDefault="qualified">
<xsd:include schemaLocation="../schemas_codeGen/REPC_IN990110NL.xsd"/>
</xsd:schema>
<xsd:schema targetNamespace="urn:hl7-org:v3" elementFormDefault="qualified">
<xsd:include schemaLocation="../schemas_codeGen/REPC_IN990111NL.xsd"/>
</xsd:schema>
</types>
<message name="REPC_IN990110NL">
<part name="body" element="hl7:REPC_IN990110NL"/>
</message>
<message name="REPC_IN990111NL">
<part name="body" element="hl7:REPC_IN990111NL"/>
</message>
The header of response message type: REPC_IN990111NL:
<?xml version="1.0" encoding="UTF-8"?>
<!-- REPC_IN990111NL - Opleveren overdrachtconcerns --><!--Note: this file was generated. DO NOT EDIT HERE.--><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:hl7-org:v3"
targetNamespace="urn:hl7-org:v3"
elementFormDefault="qualified">
The header and definition of a conflicting type in datatypes.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hl7="urn:hl7-org:v3" xmlns:hl7nl="urn:hl7-nl:v3"
xmlns:sch="http://www.ascc.net/xml/schematron" elementFormDefault="qualified" targetNamespace="urn:hl7-org:v3" xmlns="urn:hl7-org:v3">
<xsd:include schemaLocation="voc.xsd"/>
<xsd:import namespace="urn:hl7-nl:v3" schemaLocation="datatypes-extensions-hl7nl.xsd"/>
<xsd:complexType name="TEL">
<xsd:complexContent>
<xsd:extension base="URL">
<xsd:sequence>
<xsd:element name="useablePeriod" type="IVL_TS" minOccurs="0"
maxOccurs="unbounded">
<!-- FIXME: one would expect the name "GTS" here but it's not
SXCM does ALL the right things, it's just not the right NAME. -->
</xsd:element>
<xsd:element name="id" type="II" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="use" type="set_cs_TelecommunicationAddressUse" use="optional">
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
The header and type in datatypes-extension-hl7nl.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:hl7="urn:hl7-org:v3" xmlns="urn:hl7-nl:v3" xmlns:sch="http://purl.oclc.org/dsdl/schematron" targetNamespace="urn:hl7-nl:v3">
<xsd:import schemaLocation="datatypes.xsd" namespace="urn:hl7-org:v3"/>
<xsd:complexType name="TEL">
<xsd:complexContent>
<xsd:extension base="ANY">
<xsd:sequence>
<xsd:element name="useablePeriod" type="QSET_TS" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="value" type="xsd:anyURI" use="optional"/>
<xsd:attribute name="use" type="set_TelecommunicationAddressUse" use="optional"/>
<xsd:attribute name="capabilities" type="set_TelecommunicationCapability" use="optional"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
Expected and actual results
As stated above, instead of generating two packages according to the different namespaces, one should be created with the "nl.hl7.v3" types being generated intead of the "org.hl7.v3" ones with the same name.
Thank you for your time in advance.
Max

XSD namespace "ns2" issue

I have schema defined in Request.xsd which will refer common.xsd.
I'm expecting the output should come as below
<Request xmlns="http://ws.myref.com/schemas/test"
xmlns="http://ps.myref.com/schemas/2008/Common">
<EmailList>
<Mail>test#gmail.com</Mmail>
</EmailList>
</Request>
But i'm getting extra namespace "ns2" issue. Can anybody help me out to resolve this issue
<ns2:Request xmlns:ns2="http://ps.myref.com/schemas/test"
xmlns="http://ps.myref.com/schemas/Common">
<ns2:EmailList>
<Mail>test#gmail.com</Mail>
</ns2:EmailList>
</ns2:Request>
Request.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" targetNamespace="http://ps.myref.com/schemas/schemas/test"
xmlns="http://ps.myref.com/schemas/schemas/test" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:com="http://ps.myref.com/schemas/Common">
<xsd:import namespace="http://ps.myref.com/schemas/Common" schemaLocation="../schemas/common/common.xsd"/>
<xsd:element name="Request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EmailLists" type="com:EmailList" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Common.xsd
<?xml version="1.0"?>
<xsd:schema xmlns="http://ps.myref.com/schemas/2008/Common" elementFormDefault="unqualified"
targetNamespace="http://ps.myref.com/schemas/Common"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:complexType name="EmailList">
<xsd:sequence>
<xsd:element name="Mail" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Your expectation is unreasonable in this instance.
Because the type "EmailList" is defined under the namespace http://ps.myref.com/schemas/2008/Common in the common.xsd file, you have no option but to differentiate it in some way when you use the EmailList type in another schema. If you look at request.xsd, you can see that this is exectyly what happens here:
<xsd:element name="EmailLists" type="com:EmailList" />
The com: in this case is a prefix designed to show that the type is defined in another schema and under a different namespace to the one being used.
In the same way, when the xsd validator uses the request.xsd to validate a schema instance, it has to ensure that the EmailList type you are using in your instance is the same EmailList type which is defined in the common.xsd schema, and the only way it can do that is by using the namespace.
Your expectation can therefore be summarized thus:
"I should be able to mix types defined in two different schema definitions freely together without differentiating them and the parser should understand that."
So you should be able to see now how your expectation does not make logical sense.
If you don't want the "ns2:" in there, your only other alternative is to do this:
<Request xmlns"http://ps.myref.com/schemas/test">
<EmailList xmlns"http://ps.myref.com/schemas/test">
<Mail xmlns="http://ps.myref.com/schemas/Common">test#gmail.com</Mail>
</EmailList>
</Request>

Error in prolog when parsing xsd using xjc

I am getting "Error in Prolog" when parsing followinf xsd by xjc
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="info">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="a" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
I have checked the validity of the xsd and not getting any error. no character before start also...
The problem seems to be with encoding="utf-16". Your file doesn't correspond it.
Set the encoding to something else (e.g. utf-8):
<?xml version="1.0" encoding="utf-8"?>

XSD two elements with the same inner structure

I am working with an xsd, trying to get it to validate an xml.
The xml is used to create objects. There are two types of objects that can be created by the elements in the list: SC and SMSC. SMSC is an SC, and extends it.
SMSC doesn't contain any new properties. From the perspective of the xml, an SMSC is identical to an SC in every way, except that the elements that define its properties are wrapped by <SMSC> tags instead of <SC> tags.
Our XSD looks like this:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name='Definitions'>
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="SC">
<!--SNIP properties of SC and SMSC -->
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Is there a way to change this to allow either SC or SMSC as the element, other than duplicating all of the property definitions in an SMSC element? We don't want to have to double the length of the document and duplicate all of the property definitions.
As it stands, the only validation error we have in our XML is where we have an SMSC element. If there isn't a way to fix this without duplicating all the property definitions we'll leave it as-is, but we'd obviously prefer to eliminate the warning this throws if practical.
While it is confusing by tags instead of tags, I would think that below is either answering your question, or elicits better explanations.
So, what you see is avoiding duplication; you don't actually need the additional type SMSC (see Definitions2), but I've put it just in case (Definitions). Making the SMSC element of the SC type would work exactly the same.
The difference between Definitions / Definitions2 and Definitions3 is that one uses substitution groups instead of choices. I personally prefer substitution groups to choices, yet is not that uncommon to run into issues related to substution groups (i.e. they are poorly supported here and there).
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="SC">
<xsd:sequence>
<!-- Stuff goes here -->
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="SMSC">
<xsd:complexContent>
<xsd:extension base="SC"/>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Definitions">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="SC" type="SC"/>
<xsd:element name="SMSC" type="SMSC"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<!-- Another way -->
<xsd:element name="Definitions2">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="SC" type="SC"/>
<xsd:element name="SMSC" type="SC"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<xsd:element name="Definitions3">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="SC" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SC" type="SC" />
<xsd:element name="SMSC" type="SMSC" substitutionGroup="SC" />
</xsd:schema>

Perl XSD parsing

I have a XSD file that is to be parsed and converted into a XML file.
Is there any way to do it using Perl ??? After xml file is generated, i have to parse that xml file which is the second part, but i was struck in the first part of converting the XSD file to XML format.
XSD is given below
<?xml version="1.0" encoding="UTF-8"?>
<!-- This document was generated by the Objective Systems ASN2XSD Compiler
(http://www.obj-sys.com). Version: 6.3.0, Date: 06-May-2010. -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.obj-sys.com/S1AP-PDU-Descriptions"
targetNamespace="http://www.obj-sys.com/S1AP-PDU-Descriptions"
xmlns:S1AP-CommonDataTypes="http://www.obj-sys.com/S1AP-CommonDataTypes"
xmlns:S1AP-PDU-Contents="http://www.obj-sys.com/S1AP-PDU-Contents"
xmlns:S1AP-Constants="http://www.obj-sys.com/S1AP-Constants"
xmlns:asn1="http://www.obj-sys.com/v1.0/XMLSchema"
elementFormDefault="qualified">
<xsd:import namespace="http://www.obj-sys.com/S1AP-CommonDataTypes"
schemaLocation="S1AP-CommonDataTypes.xsd"/>
<xsd:import namespace="http://www.obj-sys.com/S1AP-PDU-Contents"
schemaLocation="S1AP-PDU-Contents.xsd"/>
<xsd:import namespace="http://www.obj-sys.com/S1AP-Constants"
schemaLocation="S1AP-Constants.xsd"/>
<xsd:import namespace="http://www.obj-sys.com/v1.0/XMLSchema"
schemaLocation="http://www.obj-sys.com/v1.0/XMLSchema/asn1.xsd"/>
<!-- PDU definition -->
<xsd:element name="s1AP-PDU" type="S1AP-PDU"/>
<xsd:complexType name="S1AP-PDU">
<xsd:choice>
<xsd:element name="initiatingMessage" type="InitiatingMessage"/>
<xsd:element name="successfulOutcome" type="SuccessfulOutcome"/>
<xsd:element name="unsuccessfulOutcome" type="UnsuccessfulOutcome"/>
<xsd:any namespace="##other" processContents="lax"/>
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="InitiatingMessage">
<xsd:sequence>
<xsd:element name="procedureCode" type="S1AP-CommonDataTypes:ProcedureCode"/>
<xsd:element name="criticality" type="S1AP-CommonDataTypes:Criticality"/>
<xsd:element name="value" type="asn1:OpenType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="SuccessfulOutcome">
<xsd:sequence>
<xsd:element name="procedureCode" type="S1AP-CommonDataTypes:ProcedureCode"/>
<xsd:element name="criticality" type="S1AP-CommonDataTypes:Criticality"/>
<xsd:element name="value" type="asn1:OpenType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="UnsuccessfulOutcome">
<xsd:sequence>
<xsd:element name="procedureCode" type="S1AP-CommonDataTypes:ProcedureCode"/>
<xsd:element name="criticality" type="S1AP-CommonDataTypes:Criticality"/>
<xsd:element name="value" type="asn1:OpenType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
It looks like you used one of the free tools from Objective Systems to convert some ASN.1 data. I believe that you used their tool, ASN2XSD, when you meant to use their ASN2XML tool instead. The XSD tool produces a description document like the one you show in your question that helps another tool format and describe your data, which is not what you wanted.
If you use their ASN2XML tool from the first link above to convert your ASN.1 data, it will produce the XML output you need without losing any data. You can then use Perl's XML tools to parse that data.

Resources