java - Is it allowed to have the same name of inner and upper elements in XSD? -
<xs:schema attributeformdefault="unqualified" elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="records"> <xs:complextype> <xs:sequence> <xs:element name="contract"> <xs:complextype> <xs:sequence> <xs:element name="records"> <xs:complextype> <xs:sequence> <xs:element type="xs:string" name="general"/> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element>
when generating pojo above xsd, occurs error "records defined in package"
and want know, xsd valid? can create complextype inside of same name upper element?
this legal in xsd. however, xjc has known issues name clashes, can override in jaxb bindings. in answer i explained few days ago how can done. solution same, though cause of error different.
note that, mentioned in comments, irrelevant names used, long tell jaxb xsd element map java member. (de)serializer make sure round-trippable.
something like:
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/xmlschema" version="2.1"> <jaxb:bindings schemalocation="yourschemalocation.xsd"> <jaxb:bindings node="//xs:element[@name='contract'] /xs:complextype/xs:sequence/xs:element[@name='records'] /xs:complextype"> <jaxb:class name="nestedrecords"/> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings>
you can add bindings commandline using -b
option: xjc -d out -b binding.xml yourschemalocation.xsd
, binding.xml
file above.
another alternative, if have control on xsd schema, use xsd annotations control generated classnames, as explained answer.
Comments
Post a Comment