I tried the solution of the question here JAXB Simplify plugin vs *.xjb.
but it failed with the following exception
" compiler was unable to honor this simplify:as-element-property customization. It is attached to a wrong place, or its inconsistent with other bindings. "
this is customization binding I used
<jaxb:bindings node="//xs:complexType[#name='Op']//xs:choice/xs:element[#name='Time']">
<simplify:as-element-property/>
</jaxb:bindings>
the jaxb simplify plugin confluence page is not accessible, so has anyone used this plugin and can give an example please?
Here is my updated schema according to the answer
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns="http://www.amadeus.com/APT/FOM/00" targetNamespace="http://www.amadeus.com/APT/FOM/00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify" jaxb:extensionBindingPrefixes="simplify">
...
...
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="1">
<xs:element name="Time" type="xs:dateTime" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property />
</xs:appinfo>
</xs:annotation>
</xs:element>
... ...
</xs:choice>
...
I got the exception during the maven build like "Unsupported binding namespace "http://jaxb2-commons.dev.java.net/basic/simplify". Perhaps you meant "http://jaxb.dev.java.net/plugin/code-injector"?"
Disclaimer: I am the author of the Simplify plugin which is the part of JAXB2 Basics.
The plugin an the project is well and alive, but my documentation server dies from time to time. I have no resources to maintain an own hosting so I'm moving all my projects to GitHub.
You can find the JAXB2 Basics project here:
https://github.com/highsource/jaxb2-basics
Documentation is not moved yet, but here's a link to one of test projects which use it:
https://github.com/highsource/jaxb2-basics/tree/master/tests/issues
Below is a fragment of the schema which uses simplify:as-element-property customization:
<xs:complexType name="gh1" mixed="true">
<xs:sequence>
<xs:element name="a" type="xs:string">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="b" type="xs:int"/>
</xs:sequence>
</xs:complexType>
I'll bring the server back online in few hours.
Please post your schema/customization for us to check. The problem you have is probably that you have put the customization in the wrong place. This is sometimes hard to figure out.
Update
This error:
"Unsupported binding namespace "http://jaxb2-commons.dev.java.net/basic/simplify". Perhaps you meant "http://jaxb.dev.java.net/plugin/code-injector"?"
Means that the plugin is missing or not activated. I assume you use maven-jaxb2-plugin. Then make sure you have jaxb2-basics as JAXB2 plugin and also included the -Xsimplify switch. Here's a sample:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-Xsimplify</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
</plugin>
</plugins>
</configuration>
</plugin>
Next, your original error "unable to honor this ... customization" may be related to WHERE you place te customization. You have placed it on the element (which is what I'd do also).
But in some cases XJC reads these customizations from other schema components. In your case, try to place the customization on xs:choice instead.
If the error persists, please file an issue on GitHub providing the minimal schema which reproduces the error. I'll take care of it then.
Update 2
The server is back online, but I have now moved the documentation of the JAXB2 Simplify Plugin to the GitHub:
https://github.com/highsource/jaxb2-basics/wiki/JAXB2-Simplify-Plugin
Update 3
The final solution with version 0.9.1 is sketched here:
https://github.com/highsource/jaxb2-basics/issues/3
Customize the class with:
<simplify:property name="type2OrType3">
<simplify:as-element-property/>
</simplify:property>
Example.
Related
I need to use "factoryMethod" to resolve "Two declarations cause a collision in the ObjectFactory class.". However, I receive "Compiler was unable to honor this factoryMethod customization". I could find no info about "factoryMethod". It seems I'm not using it right. Please look at my simplified schema:
<?xml version='1.0' encoding='UTF-8' ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.1">
<xs:element name="UseCase">
<xs:complexType>
<xs:sequence>
<xs:element name="StepAction" >
<xs:annotation>
<xs:appinfo>
<jxb:factoryMethod name="createStepAction" />
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I use "xjc %schemaname%" to produce classes.
It seems that jxb:factoryMethod can be used only for elements xjc complains about. In my example, I had to cause "Compiler was unable to honor this factoryMethod customization" by adding other element at the same level as StepAction:
<?xml version='1.0' encoding='UTF-8' ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.1">
<xs:element name="UseCase">
<xs:complexType>
<xs:sequence>
<xs:element name="StepAction" >
<xs:annotation>
<xs:appinfo>
<jxb:factoryMethod name="createStepAction" />
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="Step-Action"/> <!-- causes Compiler was unable to honor this factoryMethod customization -->
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XJC doesn't throw "Compiler was unable to honor this factoryMethod customization" in this case.
My conclusions:
One cannot use jxb:factoryMethod to change a name of ObjectFactory's createXXX method if XJC doesn't produce "Two declarations cause a collision in the ObjectFactory class." error
"Two declarations cause a collision in the ObjectFactory class." is produced when there are more than one element with conflicting names at the same level (siblings). If they are at different level, xjc won't complain. By conflicting names I mean names which are the same after XJC's tranformation (e.g. "Step-Action" and "StepAction" are the same)
jxb:class annotation has no effect on ObjectFactory's createXXX methods
One of the things I've written down for the airing-of-grievances for Festivus this year is how Xerces/SAX2 reports parsing errors.
Take this bit of XSD:
<xs:sequence>
<xs:element ref="element1" />
<xs:element ref="element2" />
<xs:element ref="element3" />
<xs:element ref="element4" minOccurs="0" />
<xs:element ref="element5" />
<xs:element ref="element6" minOccurs="1" />
<xs:element ref="element7" minOccurs="0" />
<xs:element ref="element8" minOccurs="0" />
<xs:choice minOccurs="0">
<xs:element ref="choiceElement1" />
<xs:element ref="choiceElement2" />
</xs:choice>
<xs:element ref="element9" minOccurs="0" />
</xs:sequence>
and sample XML
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xmldocument xmlns="http://www.somewebsite.com/xsd/xmldocument" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.somewebsite.com/xsd/xmldocument xmldocument.xsd">
<transaction msgId="MESSAGE-ID">
<element1>KS0003</element1>
<element2>2016-05-09</element2>
<element3>10:20:50</element3>
<element5>99433</element5>
<element8>jesse</element8>
</transaction>
</xmldocument>
I get this error:
RAW SAX2 ERROR: Error at file "/tmp/QACXV0Z346", line=10, column=17,
XML element=element8, Element 'element8' is not valid for content
model:
'((element1,element2,element3,element4,element5,element6,element7,element8,(choiceElement1|choiceElement1)),element9)'
Seems to me the problem here isn't element8, it's element6, which is set to required but is the one actually missing in the XML.
I have some code that attempts to parse out this string and figure out what the real problem is, but the error string doesn't contain any information about optional elements, etc. I may not be setting things up correctly - maybe. I have a problem in general with SAXException - it's nearly useless - so what I need is more information from something that tells me what the real problem is.
We're using Xerces 2.6 or 2.8 because we're running on an IBM i and they don't provide updates to stuff like this unless you upgrade the OS.
Xerces error messages are actually quite good.
You might argue that in this particular case, it would be better to say something along the lines of
Encountered element8 but element6 was expected.
That's fine for this simple case, but realize that in the general case, there can be an arbitrarily complex expression covering what possibly could have been expected. Be prepared to introduce a whole lot of complexity to concisely explain what all would be allowed at a given point where parsing goes awry. Citing the first point of contradiction along with the violated parent content model requirement is not a bad diagnostic in general.
Given a top level xsd that imports a second level which in turn imports a third, is it possible to use a type from the third in the first? Or does the first have to import the third directly?
Good question!
From reading the spec, I can't really tell. It doesn't explicitly address the issue of transitive imports explicitly.
In the Primer (part 0), they only talk about one level of importing: http://www.w3.org/TR/xmlschema-0/#import
In Structure (part 1), it also only defines direct importing http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#composition-schemaImport Since it talks about "a means of addressing such foreign component" (which I think means namespace), maybe it's reasonable to assume that an explicit way of addressing imported schemas is necessary - in other words, an explicit namespace in an import is needed for each one.
A google search shows that other people have also been confused by this issue:
http://xsd.stylusstudio.com/2002Apr/post00021.htm
http://xsd.stylusstudio.com/2005Mar/post05007.htm
What's of most concern about those posts is that different xsd processors have different behaviour - suggesting that the writers of the processors were also confused.
Although that may have changed since those posts (2002, 2005), the wisest course seems to be to avoid the issue, and just use direct imports, because this will work with all processors.
As I said: good question.
Here is a test, to check an xsd processor (of course, this won't guarantee that it will work for someone else using some other xsd processor...). I found that my favourite one (xmllint) does not allow transitive imports.
The test is three schemas: a.xsd imports b.xsd which in turn imports c.xsd; and a type defined in c.xsd is referenced from a.xsd:
<!-- a.xsd -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.c.com" targetNamespace="http://www.a.com">
<xsd:import namespace="http://www.b.com" schemaLocation="b.xsd"/>
<!-- UNCOMMENT FOR DIRECT IMPORT
<xsd:import namespace="http://www.c.com" schemaLocation="c.xsd"/>
-->
<xsd:element name="eg" type="c:TypeC"/>
</xsd:schema>
<!-- b.xsd -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.c.com" schemaLocation="c.xsd"/>
</xsd:schema>
<!-- c.xsd -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.c.com">
<xsd:simpleType name="TypeC">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>
<!-- a.xml -->
<eg xmlns="http://www.a.com">hello world</eg>
$ xmllint --schema a.xsd a.xml --noout
a.xsd:6: element element: Schemas parser error : Element
'{http://www.w3.org/2001/XMLSchema}element', attribute 'type': References from
this schema to components in the namespace 'http://www.c.com' are not allowed,
since not indicated by an import statement.
WXS schema a.xsd failed to compile
The error message is: References from this schema to components in the namespace 'http://www.c.com' are not allowed, since not indicated by an import statement., suggesting that the developers of xmllint at least are quite sure that imports are not transitive.
If type is what you are talking about then .. It's <xs:Include> not <xs:Import> ..
And the answer is : Parser takes care of linking all XSDs together
see the example below:
<?xml version="1.0" encoding="utf-8"?>
<root>
<child>trial</child>
<child2>trial2</child2>
<trunk>
<branch>1</branch>
<branch>2</branch>
</trunk>
<specialchild>test</specialchild>
</root>
For the above XML I will design an XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="include multiple XSDs2.xsd"/>
<xs:element name="root" type ="root"/>
<xs:complexType name="root">
<xs:sequence>
<xs:element name="child" type="xs:string" />
<xs:element name="child2" type="xs:string" />
<xs:element name="trunk" type="trunk"/>
<xs:element name="specialchild" type="specialchild"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
where type trunk is defined in import multiple XSDs2.xsd file and linked by using <xs:include> .. (which is residing in the same folder).. And the code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="include multiple XSDs3.xsd"/>
<xs:complexType name="trunk">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="branch" type="branch" />
</xs:sequence>
</xs:complexType>
</xs:schema>
and type branch is a simple type defined in include multiple XSDs3.xsd file, and the code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="branch">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="specialchild">
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:schema>
*Now the tricky part is: specialchild is declared in XSD_1 where as defined in XSD_3 and these two XSDs are linked by XSD_2..
You can observe that parser by default takes care of linking all XSDs and treating them all as one! *
Hope this solves your question!
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
I have the following in a schema:
<xs:element name="td">
<xs:complexType>
<xs:complexContent>
<xs:extension base="cell.type"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="cell.type" mixed="true">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="p"/>
</xs:sequence>
</xs:complexType>
Some parsers allow PCDATA directly in the element, while others don't. There's something in the XSD recommendation (3.4.2) that says when a complex type has complex content, and neither has a mixed attribute, the effective mixed is false. That means the only way mixed content could be in effect is if the extension of cell.type causes the mixed="true" to be inherited.
Could someone more familiar with schemas comment on the correct interpretation?
(BTW: if I had control of the schema I would move the mixed="true" to the element definition, but that's not my call.)
Anyone reading my question might want to read this thread also (by Damien). It seems my answer isn't entirely right: parsers/validators don't handle mixed attribute declarations on base/derived elements the same way.
Concerning extended complex types, sub-section 1.4.3.2.2.1 of section 3.4.6 in part 1 of W3C's XML Schema specification says that
Both [derived and base] {content type}s must be mixed or both must be element-only.
So yes, it is inherited (or more like you cannot overwrite it—same thing in the end).
Basically, what you've described is the desired (and as far as I'm concerned) the most logical behavior.
I've created a simple schema to run a little test with Eclipse's XML tools.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="c">
<xs:complexType>
<xs:complexContent mixed="false">
<xs:extension base="a"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="a" mixed="true">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="b"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
The above schema is valid, in the sense that not Eclipse's nor W3C's "official" XML Schema validator notices any issues with it.
The following XML passes validation against the aforementioned schema.
<?xml version="1.0" encoding="UTF-8"?>
<c xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
x
<b/>
y
</c>
So basically you cannot overwrite the mixedness of a complex base type. To support this statement further, try and swap the base and dervied types' mixedness. In that case the XML fails to validate, because the derived type won't be mixed as it (yet again) cannot overwrite the base's mixedness.
You've also said that
Some parsers allow PCDATA directly in the element, while others don't
It couldn't hurt to clarify which parsers are you talking about. A good parser shouldn't fail when it encounters mixed content. A validating parser, given the proper schema, will fail if it encounters mixed content when the schema does not allow it.