getting error while generating jaxb xsd from eclipse - jaxb

I am trying to generate jaxb classes from from the xsd mentioned below.
Page.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://www.m.com/a" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="NewXMLSchema5.xsd"/>
<xs:element name="collection" type="tns:collection"/>
<xs:element name="links" type="tns:links"/>
<xs:complexType name="collection">
<xs:complexContent>
<xs:extension base="basePage">
<xs:sequence>
<xs:element ref="tns:links" minOccurs="0"/>
<xs:element name="element" type="xs:anyType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="pageData" type="PageData" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="type" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="links">
<xs:all/>
</xs:complexType>
</xs:schema>
In the above xsd, extension basePage definition is there under NewXMLSchema5.xsd. Because of NewXMLSchema5.xsd, when I am generating jaxb classes for the Page.xsd , it is generating jaxb classes for page.xsd and NewXMLSchema5.xsd.
My requirement is I need to generate jaxb classes only for Page.xsd .it should ignore the NewXMLSchema5.xsd which is imported in page.xsd.But the basePage definition should be available in NewXMLSchema5.xsd.
Can anybody advice how to ignore NewXMLSchema5.xsd in page.xsd and at the same basePage definition should be available in page.xsd.

You can use episode compilation to use the existing (already generated) types from another schema.
Firstly, you will have to execute the NewXMLSchema5.xsd with
xjc -episode newschema.episode NewXMLSchema5.xsd
Then use the episode to generate the Page.xsd
xjc Page.xsd -extension -b newschema.episode
This way, the the baseType shall not be generated but used from previous generation.
refer this link for similar answer.

Related

XSD circular imports

Is it valid to have two xsd's import each other?
For example, the first one is 'MyService.xsd=48 and it looks like:
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:tns="http://documentation" xmlns:ns1="http://documentHistory" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" version="1.0" targetNamespace="http://documentation">
<xs:import namespace="http://documentHistory" schemaLocation="MyService.xsd=49" />
<xs:complexType name="item">
<xs:sequence>
<xs:element name="internalId" type="xs:long" minOccurs="0" />
<xs:element name="readOnly" type="xs:boolean" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="data">
<xs:sequence>
<xs:element name="histories" type="ns1:history" nillable="true" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
The second xsd is MyService.xsd=49 and looks like:
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:tns="http://documentHistory" xmlns:ns1="http://documentation" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" version="1.0" targetNamespace="http://documentHistory">
<xs:import namespace="http://documentation" schemaLocation="MyService.xsd=48" />
<xs:complexType name="history">
<xs:complexContent>
<xs:extension base="ns1:item">
<xs:sequence>
<xs:element name="dateReceived" type="xs:dateTime" minOccurs="0" />
<xs:element name="dateSent" type="xs:dateTime" minOccurs="0" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Notice that the first one imports the second one and the second one imports the first one. I have an issue that I'm trying to track down and I don't know if this is part of the problem or not. The issue I'm trying to solve has to do with TCL not seeming to parse my wsdl correctly. After TCL parses the wsdl into a dict, the 'history' object doesn't have the properties from the 'item' object, even though the xsd shows that history should extend from item.
The question is: is it valid for two xsd's to import each other or does this look like a problem?
Also, I already looked at this post XSD circular import but found the answer difficult to understand. Any help / insight would be greatly appreciated. Thanks!

Jaxb classes for XML schema 1.1

How can we create the jaxb classes out of XSD which is having schema version 1.1.
"xs:override" give me validation error when I am trying to create java classes
Sample XSD:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema targetNamespace="{target namespace}"
xmlns="{xmlns}"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xlink="http://www.w3.org/1999/xlink"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:override schemaLocation="o1_3_2/o1.xsd">
<xs:group name="ReferenceDataElementExtension">
<xs:sequence>
<xs:element name="USERPROF" type="xs:string"/>
<xs:element name="FACLPROF" type="xs:string"/>
<xs:element name="STDYPROF" type="xs:string"/>
<xs:element name="TRAINING" type="xs:string"/>
<xs:sequence>
<xs:group name="ReferenceDataElementExtension">
</xs:override>
</xs:schema>
But while generating classes using xjc command it give validation error:
s4s-elt-invalid-content.1: The content of 'schema' is invalid. Element 'override' is invalid, misplaced, or occurs too often.
I don't think XJC (JAXB's schema compiler) supports XML Schema 1.1.
XJC uses the library named XSOM to process XML Schema. But XSOM does not seem to support xs:override. Take a look here:
https://svn.java.net/svn/xsom~sources/trunk/src/xmlschema.rng
No override, sorry.

Unmarshalling XMLs generated from two XSDs

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.

How do I define one-to-many relationships across different XSD Files, and why does xs:extends not work in this situation?

I have been developing some xml schema files over the past few days, and learned of a specific element to extend simpletypes and complextype elements.
I am currently using the visual studio 2012 professional edition, and I am currently testing relationships of XSD files (I daresay parent-child relationships , or one to many relationships) between these files, for example (I am using objects from Google DFA API):
RichMediaAsset
∟ RichMediaExpandingHtmlAsset
∟ RichMediaExpandingHtmlAsset
∟ RichMediaFloatingHtmlAsset
...
All these classes "extend" or "Inherit" from RichMediaAsset (which is the base, or abstract). I have defined RichMediaAsset as the following in XSD
<?xml version="1.0" standalone="yes"?>
<xs:schema id="RedirectCreativeBase" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<!-- simpleTypes=primitive -->
<!-- extBooleanMethodPrefix=is -->
<xs:complexType name="RichMediaAssetWrapper" abstract="true">
<xs:sequence>
<xs:element name="fileName" type="xs:string" minOccurs="0" />
<xs:element name="fileSize" type="xs:int" minOccurs="0" />
<xs:element name="id" type="xs:long" minOccurs="0" />
<xs:element name="parentAssetId" type="xs:long" minOccurs="0" />
<xs:element name="type" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I have defined the second file, RichMediaExpandingHtmlAsset as follows:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="RichMediaExpandingHtmlAssetWrapper" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="./RichMediaAsset.xsd"/>
<xs:complexType name="RichMediaExpandingHtmlAssetWrapper" abstract="false" >
<xs:extension base="RichMediaAssetWrapper"> <!-- Not happy here -->
<xs:sequence>
<!-- content to be included,extending RichMediaAsset's complex type called RichMediaAssetWrapper -->
</xs:sequence>
</xs:extension>
</xs:complexType>
</xs:schema>
The part which I mentioned VS2012 is not happy with is defined as follows:
Warning 1 The 'http://www.w3.org/2001/XMLSchema:extension' element is not supported in this context. C:\eclipse\Workspace\aem_adservices_google_dfa\aem.adservices.google.dfa\xsd\Creative\RichMediaExpandingHtmlAsset.xsd 5 6 Miscellaneous Files
Warning 2 The element 'complexType' in namespace 'http://www.w3.org/2001/XMLSchema' has invalid child element 'extension' in namespace 'http://www.w3.org/2001/XMLSchema'. List of possible elements expected: 'annotation, simpleContent, complexContent, group, all, choice, sequence, attribute, attributeGroup, anyAttribute' in namespace 'http://www.w3.org/2001/XMLSchema'. C:\eclipse\Workspace\aem_adservices_google_dfa\aem.adservices.google.dfa\xsd\Creative\RichMediaExpandingHtmlAsset.xsd 5 6 Miscellaneous Files
The question now: Is this a possible bug of 2012, have I made an error, is this simply not supported (even though I checked the usage examples at w3schools.com), or is there better ways for me to define the one to many relationships?
I found the issue with my XSD. It was in fact missing a tag. In these cases, xs:complexContent or xs:simpleContent must be defined in order to define extensions or restrictions on a complex/simple type respectively that contains mixed content or elements only.
Here is my solution, and the errors in my code also disappeared.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="RichMediaExpandingHtmlAssetWrapper" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="./RichMediaAsset.xsd"/>
<xs:complexType name="RichMediaExpandingHtmlAssetWrapper" abstract="false" >
<xs:complexContent>
<xs:extension base="RichMediaAssetWrapper">
<xs:sequence>
...
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Sources: ComplexContent; SimpleContent

Generate Nested Types instead of Global Types with xsd.exe

Using xsd.exe in a C# class, is there a way to produce a xsd file with Nested Type, instead of Global Types?
I want to use this xsd file, with SSIS - Sql Server Integration Services, and look SSIS is not reading my xsd very well.
I want to generate the xsd like this, with Nested Types :
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Country">
<xs:complexType>
<xs:sequence>
<xs:element name="City">
<xs:complexType>
<xs:sequence>
<xs:element name="CityName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CoutryName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
but xsd.exe produce this , with Global Types, and SSIS don't read it. I need to change this xsd manually to be like above.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Country">
<xs:complexType>
<xs:sequence>
<xs:element name="City" type="City">
</xs:element>
<xs:element name="CoutryName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="City">
<xs:sequence>
<xs:element name="CityName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Any suggestion? Or other tool that I can use.
Thanks a lot.
I'll further assume that not "very well" means you're not seeing CountryName in your XML Source output.
The documentation on MSDN is a good reading, although in my opinion it fails to describe why you would encounter the behavior you see.
I believe that it has to do with the way XML Source outputs are being determined. SSIS infers a data set from the XML structure; the top level entity, that corresponds to the root element, is not mapped to an output, so all the attributes associated with it, in your case CountryName, will not show up.
The easiest way to prove it, is to add another root element that wraps your Country (equivalent to having a dummy "root" class that has a Country-type property).
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
If you add the above schema fragment to your schema, you should get your expected results. At the beginning I thought that it has to do with the issue described here; while you can still use the tool to visualize the data set as described by the MSDN link above, in your case, the authoring style you suggested (basically a russian-doll) can't change the outcome.

Resources