I have the following xsd schema (snippet) and want to generate java classes with correect naming conventions for classname, attributes and methods -> camelCase. I found the CamelCase Always plugin but it is outdated and I have no idea how to use it. Is there a way to do this with jaxb and/or xjc?
<xs:complexType name="AB_NAME">
<xs:sequence>
<xs:element name="ELEMENT_ID" type="tns:DECIMAL_38_0"/>
<xs:element name="DATEN_ID" type="tns:DECIMAL_38_0"/>
<xs:element name="DATE" type="tns:TIMESTAMP_0"/>
</xs:sequence>
</xs:complexType>
Or is it possible to rename the attributes and methods with another plugin? E.g. you can generate it with <jaxb:globalBindings underscoreBinding="asCharInWord">
After the generation is finished replace the names in the class like this getELEMENT_ID() to getElementId() ?
You can either use the CamelCase Always plugin:
How can I get JAXB2 to emit CamelCase bindings?
Or use a bunch of jaxb:property and jaxb:class binding customizations to fix names:
<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
<jaxb:bindings node="xsd:complexType[#name='AB_NAME']/xs:sequence/xs:element[#name='ELEMENT_ID']">
<jaxb:property name="ElementId"/>
</jaxb:bindings>
</jaxb:bindings>
Related
Sorry for such a long question. I'll try to make it as simple as it gets.
We are working on a modular computational framework. The framework reads the configuration from xml file and we would like to validate the configuration against xsd. While adding new modules, we would like the xsd to be as simple as possible in the sense of maintainability.
The structure of framework is the following (in c#):
interface IModule { ... }
interface ISource : IModule { ... }
interface IFilter : IModule { ... }
interface IProcessor : IModule { ... }
and we have abstract implementation of various module interfaces and the implementation of modules (e.g. ObjLoader : AbstractSource). The structure of xml is following:
<Test>
<Sources>
<ObjLoader .../>
</Sources>
<Filters>
...
</Filters>
<Processors>
...
</Test>
Currently to avoid the necessity to heavily modify the xsd with every new module, we have the following test.xsd:
<xs:include schemaLocation="framework.xsd"/>
<xs:include ... modules />
<!-- adding includes for every module or group
of modules is the only modification -->
<xs:element name="Test">
<xs:complexType>
<xs:sequence>
<xs:element ref="this:Sources"
minOccurs="1"
maxOccurs="1"/>
...
</xs:sequence>
</xs:complexType>
</xs:element>
and common framework.xsd:
<xs:element name="Sources">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="this:AbstractSource"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="AbstractSource"
abstract="true"/>
<xs:complexType name="ISource">
<xs:complexContent>
<xs:extension base="this:IModule">
...
</xs:extension>
</xs:complexContent>
</xs:complexType>
...
Each module (or group of modules) has its own xsd file with its definition and this file is included in the test.xsd:
<xs:include schemaLocation="framework.xsd"/>
<xs:element name="ObjLoader"
substitutionGroup="this:AbstractSource">
<xs:complexType>
<xs:complexContent>
<xs:extension base="this:ISource">
...
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
So far so good and everything works well. For each dll with modules we provide corresponding xsd and include it in test.xsd. But during the development, modules that are primarily of some kind, but can produce another operation as a side effect appeared. It is not a problem in the c# side as we can have the following class:
class GreatProcessingFilter : AbstractProcessor,
IFilter { ... }
Is there a way to modify our xsd to be able to handle such elements that can be a substitution of multiple abstract elements? Something like:
<xs:element name="GreatProcessingFilter"
substitutionGroup="AbstractProcessor"
substitutionGroup="AbstractFilter"> ...
XSD 1.1 does allow multiple QNames in the substitutionGroup attribute. It does not, however, cause the element declared as a member of multiple substitution groups to be given a type constructed dynamically from the types assigned to the various substitution-group heads. So it probably doesn't qualify as multiple inheritance in the sense you are looking for. You may find XML schemas with multiple inheritance of interest.
[Addition, responding to OP's comment] In XSD 1.1, a top-level element can be declared with multiple substitution groups; it can then be substituted for any of the elements named (unless blocked by the relevant final attribute on the substitution-group head). The constraints on substitution-group affiliations are as in 1.0: the type of the element being declared must be validly substitutable for the type of each substitution group head. If no type is given for the element, it defaults to having the same type as the first substitution-group head.
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.
I reading articles about XSD on w3schools and here many examples. For example this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But after I tried put this .xsd file in xjc - I see error log, dome like this:
The prefix "xs" for element "xs:schema" is not bound...
But all work correct when I change xs on xsd prefix.
So, can somebody, clarify for me what is different between xs and xsd?
Maybe, one prefix - it is old version and other for new version...
xs and xsd are XML prefixes used with qualified names; each prefix must be associated with a namespace. The association is done with an attribute that looks like xmlns:xs="...". xs and xsd are most common for XML Schema documents.
Should you choose s or ns1, it shouldn't make any difference to any tool for your scenario.
The error is not caused by your XML Schema file. I suspect there might be something else in your setup, maybe a custom binding file. Please check that or post additional information.
I would like to allow for an element to either contain an attribute OR define a more complex type.
Something like
<myElement someAttr="..."/>
or
<myElement>
<...>
</myElement>
That is, if someAttr exists then I do not want to allow sub elements and if it doesn't then I want to.
The reason for this is I want to have an "include" feature where I include a file which is essentially inserted into the element. But I don't want both. You can either include additional external xml code into the element or add your own BUT not both. (or also to have it inserted from a separate part of the xml)
This is mainly for simplifying a complex xml so that the structure is easily understood.
I doubt you will be able to express something like that in XML schema at this point.
You can make an attribute optional, e.g. it can be present or not. But you cannot express something like if the attribute is not present, then include other complex content with the current means.
You'll have to either check this programmatically yourself, or maybe investigate if other XML description languages like RelaxNG or Schematron might be able to help.
Perhaps with a static choice and you change the myElement name ?
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:element name="myElementWithAttrs">
<xs:complexType>
<xs:attribute name="someAttrs" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="myElementWithoutAttrs">
<xs:complexType>
<xs:sequence>
<xs:any processContents="skip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
This XSD portion was obtained from: http://www.iana.org/assignments/xml-registry/schema/netconf.xsd
<xs:complexType name="rpcType">
<xs:sequence>
<xs:element ref="rpcOperation"/>
</xs:sequence>
<xs:attribute name="message-id" type="messageIdType" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
<xs:element name="rpc" type="rpcType"/>
And is the core to function calls in NETCONF being the node of an XML document. I am curious as to why it is not something like:
<xs:element name="rpcType">
<xs:complexType>
<xs:sequence>
<xs:element ref="rpcOperation"/>
</xs:sequence>
<xs:attribute name="message-id" type="messageIdType" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
</xs:element>
The reasoning is that in #1 when trying to marshall a bean (in jaxb2) I get the exception:
[com.sun.istack.SAXException2: unable to marshal type "netconf.RpcType" as an element because it is missing an #XmlRootElement annotation]
I have been reading this article over and over again, and really cant get a hold of the difference, and why it would be #1 vs #2...
It's not obvious, I'll grant you. It comes down to the type vs element decision.
When you have something like
<xs:element name="rpcType">
<xs:complexType>
This is essentially an "anonymous type", and is a type which can never occur anywhere other than inside the element rpcType. Because of this certainty, XJC knows that that type will always have the name rpcType, and so generates an #XmlRootElement annotation for it, with the rpcType name.
On the other hand, when you have
<xs:complexType name="rpcType">
then this defines a re-usable type which could potentially be referred to by several different elements. The fact that in your schema it is only referred to by one element is irrelevant. Because of this uncertainty, XJC hedges its bets and does not generate an #XmlRootElement.
The JAXB Reference Implementation has a proprietary XJC flag called "simple binding mode" which, among other things, assumes that the schema you're compiling will never be extended or combined with another. This allows it to make certain assumptions, so if it sees a named complexType only being used by one element, then it will often generate #XmlRootElement for it.
The reality is rather more subtle and complex than that, but in 90% of cases, this is a sufficient explanation.
Quite an involved question. There are many reasons to design schemas using types rather than elements (this approach is called the "venetian blind" approach versus "salami slice" for using global elements). One of the reasons is that types can be sub-typed, and another that it may be useful to only have elements global that can be root elements.
See this article for some more details on the schema side.
Now, as for the JAXB question in particular. The problem is that you created a class corresponding to a type and tried to serialise it. That means JAXB knows its content model, but not what the element name should be. You need to attach your RpcType to an element (JAXBElement), for example:
marshaller.marshal(new ObjectFactory().createRpc(myRpcType));
The ObjectFactory was placed into the package created by JAXB for you.
Advantages of Named Types
The advantage of a schema using global/named types is that child/sub types can be created that extend the parent type.
<xs:complexType name="rpcType">
<xs:sequence>
<xs:element ref="rpcOperation"/>
</xs:sequence>
<xs:attribute name="message-id" type="messageIdType" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
<xs:element name="rpc" type="rpcType"/>
The above fragment would allow the following child type to be created:
<xs:complexType name="myRPCType">
<xs:complexContent>
<xs:extension base="rpcType">
<xs:sequence>
<xs:element name="childProperty" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Impact on JAXB
Another aspect of named types is that they may be used by multiple elements:
<xs:element name="FOO" type="rpcType"/>
<xs:element name="BAR" type="rpcType"/>
This means that the schema to Java compiler cannot simply just pick one of the possible elements to be the #XmlRootElement for the class corresponding to "rpcType".