JAXB XJC - Bind XML schema version into class name - jaxb

We have two XML schemas with same element name but different namespaces. When I use xjc, the compiler is assigning elements to the same classpath and element. As shown below, the root issue is in the handling of the XML Schema namespace with leading digit; specifically 1.0 and 1.1. XJC is compiling these difference URIs to a the same classpath; specifically _1. This is causing collision with the same classpath:
com.companyabc.namespaces.eda.process._1.TheChangeType
What is the syntax in the bindings.xjb to bind 1.0 to _1_0 and 1.1 to _1_1 ?
Thanks!!!
XML Schema 1 : http://namespaces.companyABC.com/EDA/Process/1.0/TheChange
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p10="http://namespaces.companyABC.com/EDA/Process/1.0"
targetNamespace="http://namespaces.companyABC.com/EDA/Process/1.0"
elementFormDefault="qualified">
<xs:element name="TheChange" type="p10:TheChangeType" />
<xs:complexType name="TheChangeType">
<xs:sequence>
<xs:element name="Field1" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
XML Schema 2 : http://namespaces.companyABC.com/EDA/Process/1.1/TheChange
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p11="http://namespaces.companyABC.com/EDA/Process/1.1"
targetNamespace="http://namespaces.companyABC.com/EDA/Process/1.1"
elementFormDefault="qualified">
<xs:element name="TheChange" type="p11:TheChangeType" />
<xs:complexType name="TheChangeType">
<xs:sequence>
<xs:element name="Field1" type="xs:string" />
<xs:element name="Field2" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Error:
[ant:xjc] [ERROR] A class/interface with the same name "com.companyabc.namespaces.eda.process._1.TheChangeType" is already in use. Use a class customization to resolve this conflict.
[ant:xjc] line 3 of file:/D:/source/1.0/TheChange.xsd
This is the solution using XSD schema annotation. Yet the solution should be implemented as a binding pattern in bindings.xjb versus annotations. Annotations will require each schema to be annotated, this is a problem.
<xsd:annotation>
<xsd:appinfo>
<jaxb:schemaBindings>
<jaxb:package name="com.companyabc.namespaces.eda.process._1_0" />
</jaxb:schemaBindings>
</xsd:appinfo>
</xsd:annotation>
<jaxb:package name="com.companyabc.namespaces.eda.process._1_1" />
How is this annotation implemented as a binding pattern in bindings.xjb?

Related

Absense of targetNamespace causing exceptions to schema

I'm trying to understand why the absence of a targetNamespace causes the following errors in my sample XML schema. If I remove the targetNamespace (targetNamespace="http://tempuri.org/XMLSchema1.xsd"), I get the following errors:
Namespace 'http://tempuri.org/XMLSchema1.xsd' is not
available to be referenced in this
schema. XMLSchema1.xsd 30
The 'http://tempuri.org/XMLSchema1.xsd:ChildNamePK' identity
constraint is not declared. XMLSchema1.xsd 30
If I add the targetNamespace back in, these errors go away but this is actually a sample from a sql annotated schema that I've shortened up for the purposes of this post. Since all the elements are local, I wanted to remove the targetNamespace in my real schema. How would I correct the schema without having to add a targetNamespace?
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Parent">
<xs:complexType>
<xs:sequence>
<xs:element name="Child" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="OldestChild">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="ChildNamePK">
<xs:selector xpath=".//Child" />
<xs:field xpath="Name" />
</xs:key>
<xs:keyref name="OldestChildFK" refer="ChildNamePK">
<xs:selector xpath=".//OldestChild" />
<xs:field xpath="Name" />
</xs:keyref>
</xs:element>
</xs:schema>
The issue here is the line:
xmlns="http://tempuri.org/XMLSchema1.xsd"
This line says that any QNames that do not have a namespace prefix should be treated as if the namespace were http://tempuri.org/XMLSchema1.xsd. This is called the default namespace.
The refer attribute in the xs:keyref element is one of these QNames, and the value you have set it to is ChildNamePK which does not have a namespace prefix. Since there is no namespace prefix, and since you have defined a default namespace, XML schema effectively treats this as if it were
refer="{http://tempuri.org/XMLSchema1.xsd}ChildNamePK"
i.e. as if it referenced a ChildNamePK element in the http://tempuri.org/XMLSchema1.xsd namespace.
However, by removing the targetNamespace, you are saying the your elements, including the ChildNamePK key, are not in a namespace. So the thing that the refer attribute references doesn't actual exist. This is what the second error is trying to tell you--it's looking for http://tempuri.org/XMLSchema1.xsd:ChildNamePK' but it doesn't exist.
The right solution here is probably to just remove the xmlns="..." line so that there is no default namespace. That way the refer attribute will reference an a QName that is not in a namespace, which is exactly what ChildNamePK is when you remove the targetNamespace line.

getting error while generating jaxb xsd from eclipse

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.

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found

I am trying to understand <any> element in xsd. I had two xsds.
Book Catalogue.xsd
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="BookCatalogue">
<xs:complexType>
<xs:sequence>
<xs:element name="Book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string" />
<xs:element name="Author" type="xs:string" />
<xs:element name="Date" type="xs:string" />
<xs:element name="ISBN" type="xs:string" />
<xs:element name="Publisher" type="xs:string" />
<xs:any namespace="##any" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Reviewer.xsd
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="Reviewer">
<xs:complexType>
<xs:sequence>
<xs:element name="Name">
<xs:complexType>
<xs:sequence>
<xs:element name="First" type="xs:string" />
<xs:element name="Last" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But if i validate the below xml based on above xsd, i am getting cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'p:Reviewer'. error. Does both xsd file should not be in same namespace?
<?xml version="1.0" encoding="UTF-8"?>
<pr:BookCatalogue xmlns:pr="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com AddRequest.xsd ">
<pr:Book>
<pr:Title>pr:Title</pr:Title>
<pr:Author>pr:Author</pr:Author>
<pr:Date>pr:Date</pr:Date>
<pr:ISBN>pr:ISBN</pr:ISBN>
<pr:Publisher>pr:Publisher</pr:Publisher>
<p:Reviewer xmlns:p="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com Children.xsd ">
<p:Name>
<p:First>p:First</p:First>
<p:Last>p:Last</p:Last>
</p:Name>
</p:Reviewer>
</pr:Book>
</pr:BookCatalogue>
Two options...
Option One: If you do not want to have to have the definition of p:Reviewer present, add processContents="lax" to your xs:any element:
<xs:any namespace="##any" minOccurs="0" processContents="lax"/>
Per XML Schema Part 0: Primer Second Edition:
The lax value of the processContents attribute instructs an XML
processor to validate the element content on a can-do basis: It will
validate elements and attributes for which it can obtain schema
information, but it will not signal errors for those it cannot obtain
any schema information.
See also XML Validation in Java: processContents=“lax” seems not to work correctly.
You should also carefully adjust your xsi:schemaLocation values to point to the actual filename of each XSD for each namespace in play. Here is your XML instance with the changes that I made:
<?xml version="1.0" encoding="UTF-8"?>
<pr:BookCatalogue
xmlns:pr="http://www.w3schools.com"
xmlns:p="http://www.w3schools.com/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com BookCatalogue.xsd http://www.w3schools.com/1 Reviewer.xsd">
<pr:Book>
<pr:Title>pr:Title</pr:Title>
<pr:Author>pr:Author</pr:Author>
<pr:Date>pr:Date</pr:Date>
<pr:ISBN>pr:ISBN</pr:ISBN>
<pr:Publisher>pr:Publisher</pr:Publisher>
<p:Reviewer>
<p:Name>
<p:First>p:First</p:First>
<p:Last>p:Last</p:Last>
</p:Name>
</p:Reviewer>
</pr:Book>
</pr:BookCatalogue>
Note: Make sure that the targetNamespace in Review.xsd matches what's declared for it in BookCatalogue.xml's xsi:schemaLocation attribute.
Option Two: If you do want to insist that the definition of p:Reviewer be present, just make the above changes to be sure that Review.xsd can be found per the xsi:schemaLocation mechanism. No processContents setting is required; it defaults to strict.

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

XSD my data type

I would like to ask about .XSD document. I cannot find anything about creating my own type, for example:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Client">
<xs:sequence>
<xs:element name="FirstName" type="string"/>
<xs:element name="SecondName" type="string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Contact">
<xs:sequence>
<xs:element name="contacts" type="Client" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xsd:schema>
And I would like to know is that right way to define my own type contact?
A few points that were not quite right.
The xsd: namespace alias on the closing schema tag should be just xs:
The primitive string types need there types qualifying, ie xs:string.
From a style point of view ComplexTypes should end Type.
If you want to use the schema (presumably via the Contact) then you need to declare a root element.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2012 Developer Edition (Trial) 10.0.1.3941 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ClientType">
<xs:sequence>
<xs:element name="FirstName" type="xs:string" />
<xs:element name="SecondName" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ContactType">
<xs:sequence>
<xs:element name="contacts" type="ClientType" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:element name="Client" type="ClientType" />
</xs:schema>
Basically XML Schemas are complex things to write without a tool. I'd seriously look into getting a good schema designer, I'd recommend Liquid XML Studio.

Resources