Jaxb classes for XML schema 1.1 - jaxb

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.

Related

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.

How can I define an XML schema element that allows either base64 content or an xop:Include element?

I have a XML schema that defines an element that may be either base64 text or an xop:Include element. Currently, this is defined as a base64Binary type:
<xs:element name="PackageBinary" type="xs:base64Binary" minOccurs="1" maxOccurs="1"/>
When I insert the xop:Include element instead, it looks like this:
<PackageBinary>
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="http://google.com/data.bin" />
</PackageBinary>
But this gives an XML validation error (I'm using .NET validator):
The element 'mds:xml-schema:soap11:PackageBinary' cannot contain child
element 'http://www.w3.org/2004/08/xop/include:Include' because the
parent element's content model is text only.
This makes sense because it's not base64 content, but I thought this was common practice...? Is there any way to support this in the schema? (We have existing product that supports this syntax but we are adding validation now.)
The best I could come up with was to create a complex type that allowed any tags but was also tagged as "mixed" so it allowed text. This doesn't explicitly declare the content as base64, but it does let it pass validation.
<xs:complexType name="PackageBinaryInner" mixed="true">
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="PackageBinary" type="PackageBinaryInner" minOccurs="1" maxOccurs="1"/>
The solution I've found is like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://example.org"
elementFormDefault="qualified"
xmlns="http://example.org"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xop="http://www.w3.org/2004/08/xop/include">
<xs:import namespace="http://www.w3.org/2004/08/xop/include"
schemaLocation="http://www.w3.org/2004/08/xop/include"/>
<xs:complexType name="PackageBinary" mixed="true">
<xs:all>
<xs:element ref="xop:Include"/>
</xs:all>
</xs:complexType>
I saw this in an xml document that appeared to allow validation - basically the attribute xmlns:xop="..." did the trick:
<SomeElement xmlns:xop="http://www.w3.org/2004/08/xop/include/" id="465390" type="html">
<SomeElementSummaryURL>https://file.someurl.com/SomeImage.html</SomeElementSummaryURL>
<xop:Include href="cid:1111111#someurl.com"/>
</SomeElement >

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

JAXB XJC - Bind XML schema version into class name

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?

Need help with (recursive) xsd group

I have this xsd:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns="http://myschema.com/schema"
targetNamespace="http://myschema.com/schema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="aType" mixed="true">
<xs:group ref="aElements" minOccurs="0" maxOccurs="unbounded"/>
</xs:complexType>
<xs:group name="aElements">
<xs:choice>
<xs:element name="a" type="aType"/>
</xs:choice>
</xs:group>
<xs:element name="b">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:group ref="aElements"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
and I try to validate this xml document against it:
<?xml version="1.0" encoding="utf-8" ?>
<b xmlns="http://myschema.com/schema">
<a/>
</b>
However, Visual Studio 2008's xml validator complains about the <a> element:
The element 'b' in namespace 'http://myschema.com/schema' has invalid child element 'a' in namespace 'http://myschema.com/schema'. List of possible elements expected: 'a'.
What is the problem?
Edit: Oops, when dumbing down the example I caused forgot to make the element optional inside the element, causing infinite recursion. The problem is still there with this mod, though.
ANSWER: The answer was that the xs:schema tag should include the elementFormDefault="qualified" attribute.
you define aElements with aType, and aType with aElements. i'm not an xsd expert, but how is that supposed to work?
You could make your life much easier using an editor for XSD development. We've been using Liquid XML Studio for ages, it makes life much easer.

Resources