I'm using the JAXB xjc tool to generate some Java interface files from xsds. I'm running into a problem where I'm getting a tonne of errors of the form:
org.xml.sax.SAXParseException: ct-props-correct.4: Error for type 'COCT_MT110200CA.PayeeRole'. Duplicate attribute uses with the same name and target namespace are specified. Name of duplicate attribute use is 'nullFlavor'.
The relevant parts of the schema are as follows:
<xs:complexType name="COCT_MT110200CA.Holder">
<xs:sequence>
<xs:group ref="InfrastructureRootElements"/>
<xs:element name="payeeRole" type="COCT_MT110200CA.PayeeRole" minOccurs="1" maxOccurs="1">
</xs:element>
</xs:sequence>
<xs:attributeGroup ref="InfrastructureRootAttributes"/>
<xs:attribute name="nullFlavor" type="NullFlavor" use="optional"/>
<xs:attribute name="typeCode" type="ParticipationType" use="optional" fixed="HLD"/>
</xs:complexType>
Where COCT_MT110200CA.PayeeRole is defined as:
<xs:complexType name="COCT_MT110200CA.PayeeRole">
<xs:sequence>
<xs:group ref="InfrastructureRootElements"/>
<xs:element name="id" type="II" minOccurs="0" maxOccurs="unbounded">
</xs:element>
<xs:choice>
<xs:element name="payeePerson" type="COCT_MT110200CA.PayeePerson" nillable="true"
minOccurs="0"
maxOccurs="1">
</xs:element>
<xs:element name="payeeOrganization" type="COCT_MT110200CA.PayeeOrganization"
nillable="true"
minOccurs="0"
maxOccurs="1">
</xs:element>
</xs:choice>
</xs:sequence>
<xs:attributeGroup ref="InfrastructureRootAttributes"/>
<xs:attribute name="nullFlavor" type="NullFlavor" use="optional"/>
<xs:attribute name="classCode" type="RoleClass" use="optional" fixed="PAYEE"/>
</xs:complexType>
Any thoughts? I don't see anything wrong with this myself...
Oooh, I think I figured this out, there was a pesky attributeGroup (InfrastructureRootAttributes) there, and in that attributeGroup was the nullFlavor duplicate...
Related
I have a XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="Structure">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="Tag1"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
<xs:element minOccurs="0" ref="Properties"/>
<xs:element minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="url"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag1">
<xs:complexType>
<xs:attribute name="attr"/>
<xs:attribute name="attr2"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag2">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element minOccurs="0" ref="Object"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
</xs:sequence>
<xs:attribute name="filter"/>
</xs:complexType>
</xs:element>
<xs:element name="Object">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="tag35">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="attr4"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attr48"/>
</xs:complexType>
</xs:element>
<xs:element name="element52">
<xs:annotation>
<xs:documentation>Text54</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="el58"/>
</xs:sequence>
<xs:attribute name="vector"/>
</xs:complexType>
</xs:element>
<xs:element name="el66" type="el66Type"/>
<xs:element name="el58">
<xs:complexType mixed="true">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag3">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" name="MetaProperty">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="value"/>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
<xs:attribute name="ID"/>
<xs:attribute name="language" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="Property">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any minOccurs="0" processContents="skip"/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="type"/>
</xs:complexType>
</xs:element>
<xs:element name="Properties">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType mixed="true" name="el66Type">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:schema>
And an xml:
<Structure url="/Test/url">
<Tag1/>
<Tag2>
<Object>
<tag35/>
</Object>
</Tag2>
<Properties>
<Property name="bla" type="value1"/>
<Property name="bla2" type="value2"/>
</Properties>
</Structure>
I am doing a validation, and I am getting the following error:
org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'Tag2'. One of '{Tag1, Tag2, Tag3}' is expected.
I checked in previous questions and always the solution is related with elementFormDefault="qualified" but now it is not the case so, could it be possible that there is a bug there? Any clue, will be grateful.
Your XML is valid against your XSD as posted in your question.
The error message in your question would not be returned. It must have been from different XML or XSD documents.
As Michael Kay states in the comments:
elementFormDefault only affects local elements declarations.
elementFormDefault only comes into play when the XSD has a targetNamespace.
For more details about elementFormDefault see this answer.
I'm struggling to understand why XJC is generating an ObjectFactory that wraps instances of the class corresponding to the root element in a JAXBElement wrapper.
The root element does not use an anonymous complex type. (You have to be a little careful when reviewing this schema because there are pairs of complexType names that at first glance look the same, but one is plural and the other singular -- e.g. optionalParametersType and optionalParameterType)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:com-mbm-parameters"
targetNamespace="urn:com-mbm-parameters"
elementFormDefault="qualified"
jaxb:version="2.0">
<xs:element name="parameters" type="parametersType" />
<xs:complexType name="parametersType">
<xs:sequence>
<xs:element name="option-parameters" type="optionParametersType" minOccurs="0" maxOccurs="1"/>
<xs:element name="positional-parameters" type="positionalParametersType" minOccurs="0" maxOccurs="1"/>
<xs:element name="mutually-exclusive-parameters" type="mutuallyExclusiveParametersType" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="properties-file" type="xs:string" use="optional"/>
</xs:complexType>
<!--
-->
<xs:complexType name="optionParametersType">
<xs:sequence>
<xs:element name="option-parameter" type="optionParameterType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!--
-->
<xs:complexType name="optionParameterType">
<xs:sequence>
<xs:element name="description" type="descriptionType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="command-line-name" type="xs:string" use="optional"/>
<xs:attribute name="default-value" type="xs:string" use="optional"/>
</xs:complexType>
<!--
-->
<xs:complexType name="positionalParametersType">
<xs:sequence>
<xs:element name="positional-parameter" type="positionalParameterType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!--
-->
<xs:complexType name="positionalParameterType">
<xs:sequence>
<xs:element name="description" type="descriptionType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<!--
-->
<xs:complexType name="descriptionType">
<xs:attribute name="forValue" type="xs:string" use="required"/>
</xs:complexType>
<!--
-->
<xs:complexType name="mutuallyExclusiveParametersType">
<xs:sequence>
<xs:element name="mutually-exclusive-parameter-set" type="mutuallyExclusiveParameterSetType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!--
-->
<xs:complexType name="mutuallyExclusiveParameterSetType">
<xs:sequence>
<xs:element name="mutually-exclusive-parameter" type="mutuallyExclusiveParameterType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!--
-->
<xs:complexType name="mutuallyExclusiveParameterType">
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="value" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
Here's the snippet of ObjectFactory that returns instances of the ParametersType class:
/**
* Create an instance of {#link JAXBElement }{#code <}{#link ParametersType }{#code >}}
*
*/
#XmlElementDecl(namespace = "urn:com-mbm-parameters", name = "parameters")
public JAXBElement<ParametersType> createParameters(ParametersType value) {
return new JAXBElement<ParametersType>(_Parameters_QNAME, ParametersType.class, null, value);
}
This is the only place the JAXBElement is used within the entire ObjectFactory, so it's not there to disambiguate two or more possible elements.
Any idea why this is happening and how to remove the need to use the wrapper?
You get JAXBElement for top-level elements, which, in this case, is your parameters element.
The rationale is to separate types and elements (which you can see as instances of types). So it is, actually, to disambiguate two or more possible elements - you may have another schema which has a different element of the same parametersType. Global elements are sometimes references via <xs:element ref="..."/>, they can have substitution groups etc., so in general always have JAXBElements for global elements is not completely unreasonable. I'm not saying it is the best design decision, I'm just saying there is some rationale in it.
To get rid of the wrapper, try using xjc:simple or adding #XmlRootElement annotation on your own:
https://stackoverflow.com/a/39778670/303810
JAXB xjc:simple binding does not generate #XmlRootElement for base class
But better don't fight it, embrace it.
I have to modify a project using XSD validation. I had got an XML, I transformed it to XSD via Intellij and then generated the associated code with the JAXB tool. All was fine.
But for the project needs, I had to modify the entire structure of my XML, I did it. I generate the XSD as well. But, When I tried to generate the Java code via the jaxb tools, it was impossible.
When I click in this jaxb tool, it just generated another XSD.
How can I do to generate, once again, the code ?
Thank you !
There is my XSD.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PurgeConfigurationRacine" type="PurgeConfigurationRacineType"/>
<xs:complexType name="BusinessBaseType">
<xs:sequence>
<xs:element type="ArchiveDataOperationType" name="ArchiveDataOperation"/>
<xs:element type="PurgeDataOperationType" name="PurgeDataOperation"/>
<xs:element type="DeleteArchivedDataOperationType" name="DeleteArchivedDataOperation"/>
</xs:sequence>
<xs:attribute type="xs:string" name="dataSourceRef"/>
</xs:complexType>
<xs:complexType name="SqlQueryType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="name" use="optional"/>
<xs:attribute type="xs:string" name="step" use="optional"/>
<xs:attribute type="xs:string" name="multi" use="optional"/>
<xs:attribute type="xs:string" name="param" use="optional"/>
<xs:attribute type="xs:string" name="value" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="ArchiveDataOperationType">
<xs:sequence>
<xs:element type="SqlQueryType" name="SqlQuery" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PurgeDataOperationType">
<xs:sequence>
<xs:element type="SqlQueryType" name="SqlQuery" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PurgeConfigurationRacineType">
<xs:sequence>
<xs:element type="BusinessBaseType" name="BusinessBase"/>
<xs:element type="DataBaseType" name="DataBase"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="DeleteArchivedDataOperationType">
<xs:sequence>
<xs:element type="SqlQueryType" name="SqlQuery" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="DataBaseType">
<xs:sequence>
<xs:element type="ArchiveDataOperationType" name="ArchiveDataOperation"/>
<xs:element type="PurgeDataOperationType" name="PurgeDataOperation"/>
</xs:sequence>
<xs:attribute type="xs:string" name="dataSourceRef"/>
</xs:complexType>
</xs:schema>
The problem that you are facing is a known issue, it is due to the fact that you have an attribute called value which is a protected name, such that you need to specify to xjc that it has to use a different name, it can be done by adding some meta information in your XSD file as next:
<xs:attribute type="xs:string" name="value" use="optional">
<xs:annotation>
<xs:appinfo>
<jaxb:property name="sqlQueryTypeValue"/>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
You will also need to add the corresponding namespace declaration and the jaxb version to use, for example:
<xs:schema ... xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1">
More information about how to customize JAXB binding
I have a xsd where there are 2 complextype elements under <Customer> complextype. One is <NormalCustomer> and <PrivilegedCustomer>. In my xml, I want either Normal or Privileged customer to be present under <Customer> tag based on the id attribute of Normal/Privileged customers.
below is my xsd
<xs:choice>
<xs:element name="normalCustomer" type="tns:normalCustomer" minOccurs="0" nillable="true"/>
<xs:element name="privilegedcustomer" type="tns:privilegedcustomer" minOccurs="0" nillable="true"/>
</xs:choice>
Complextype NormalCustomer
<xs:complexType name="normalCustomer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="customerName" type="xs:string" minOccurs="1"/>
<xs:element name="customerType" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
Complextype PrivilegedCustomer
<xs:complexType name="privilegedCustomer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="customerName" type="xs:string" minOccurs="1"/>
<xs:element name="customerType" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
Note: Am using JAXB for processing
Please suggest me how can I modify the tag to achieve my requirement.
Remove minOccurs="0" nillable="true" from both elements. The <xs:choice> enforces that exactly one of the elements are present. If none present are allowed, add minOccurs="0" to <xs:choice>.
I'm using JAXB for the first time and trying to create class files using my xsd files but unfortunately getting errors.
This is my xsd :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DbOperation" type="DbOperation"/>
<xs:complexType name="DbOperation">
<xs:sequence>
<xs:element name="ruleList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Rule">
<xs:sequence>
<xs:element name="Action" type="xs:string" minOccurs="0"/>
<xs:element name="TableName" type="xs:string" minOccurs="0"/>
<xs:element name="conditionList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="Conditions" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Conditions">
<xs:sequence>
<xs:element name="Condition" type="Condition" minOccurs="0"/>
<xs:element name="Operator" type="xs:string" minOccurs="0"/>
<xs:element name="Condition" type="Condition" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Condition">
<xs:sequence>
<xs:element name="fieldName" type="xs:string" minOccurs="0"/>
<xs:element name="Operation" type="xs:string" minOccurs="0"/>
<xs:element name="Value" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
And these are the errors I'm getting :
parsing a schema...
[ERROR] src-resolve: Cannot resolve the name 'Rule' to a(n) 'element declaration
' component.
line 11 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd
[ERROR] src-resolve: Cannot resolve the name 'Conditions' to a(n) 'element decla
ration' component.
line 25 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd
[ERROR] cos-nonambig: Condition and Condition (or elements from their substituti
on group) violate "Unique Particle Attribution". During validation against this
schema, ambiguity would be created for those two particles.
line 32 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd
Failed to parse a schema.
This is the sample xml for which I've created xsd:
<?xml version="1.0" encoding="UTF-8"?>
<DbOperation>
<Rule>
<Action>Delete</Action>
<TableName>
issue
</TableName>
<Conditions>
<Condition>
<fieldName>
trader
</fieldName>
<Operation>
>=
</Operation>
<Value>
250
</Value>
</Condition>
<Operator>AND</Operator>
<Condition>
<fieldName>
traderName
</fieldName>
<Operation>
=
</Operation>
<Value>
jk
</Value>
</Condition>
</Conditions>
</Rule>
</DbOperation>
Use an XML editor to validate your schema first...
You are referencing a type as if it were an element
<xs:element ref="Conditions" minOccurs="0" maxOccurs="unbounded"/>
<xs:complexType name="Conditions">
<xs:sequence> ....
<xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>
The "REF" attribute indicates that the element and not the type.
Error can be corrected in two ways:
first:
<xs:complexType name="DbOperation">
<xs:sequence>
<xs:element name="ruleList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Rule" type="Rule" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
second:
<xs:complexType name="DbOperation">
<xs:sequence>
<xs:element name="ruleList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="Rule" type="Rule"/>