In my schema file below, I have an element Stat which extends Entity. As far as I am aware, I follow w3's example, but when I go to parse the schema (and the xml that uses the schema) with through java's DocumentBuilderFactory and SchemaFactory I get this exception:
org.xml.sax.SAXParseException; systemId: file:/schema/characterschema.xsd;
src-resolve.4.2: Error resolving component 'cg:Entity'. It was detected that
'cg:Entity' is in namespace 'http://www.schemas.theliraeffect.com/chargen/entity',
but components from this namespace are not referenceable from schema document
'file:/home/andrew/QuasiWorkspace/CharacterGenerator/./schema/characterschema.xsd'.
If this is the incorrect namespace, perhaps the prefix of 'cg:Entity' needs
to be changed. If this is the correct namespace, then an appropriate 'import'
tag should be added to '/schema/characterschema.xsd'.
So, it seems that I cannot see my namespace within my schema. Do I need to import my schema into itself or am I completely misreading this exception? Could it be that I am declaring my schema incorrectly?
This is my schema for reference:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cg="http://www.schemas.theliraeffect.com/chargen/entity"
elementFormDefault="qualified">
<xs:element name="Entity">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="description" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="Stat">
<xs:complexType>
<xs:complexContent>
<xs:extension base="cg:Entity">
<xs:sequence>
<xs:element name="table" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
There are two problems with your schema. First, you don't declare a targetNamespace so the Entity and Stat elements you are defining are not in a namespace. Second, a type can't extend an element, only another type.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cg="http://www.schemas.theliraeffect.com/chargen/entity"
targetNamespace="http://www.schemas.theliraeffect.com/chargen/entity"
elementFormDefault="qualified">
<xs:complexType name="EntityType">
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="description" type="xs:string"/>
</xs:complexType>
<xs:element name="Entity" type="cg:EntityType" />
<xs:complexType name="StatType">
<xs:complexContent>
<xs:extension base="cg:EntityType">
<xs:sequence>
<xs:element name="table" type="xs:string" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="Stat" type="cg:StatType" />
</xs:schema>
Here I'm defining two types, one of which extends the other, and two top level elements of the respective types. All the top-level types and elements are defined into the targetNamespace of the schema, and the nested table element inside StatType is also in this namespace because of the elementFormDefault="qualified" - without this the Entity and Stat elements would be in the http://www.schemas.theliraeffect.com/chargen/entity namespace but the table element would be in no namespace.
Related
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
Sample XML response from REST WS -
<UserInfoDataContract xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<DisplayName>Test User</DisplayName>
<Email>test#test.com</Email>
<FirstName>Test</FirstName>
<IsSuperUser>false</IsSuperUser>
<LastName>User</LastName>
<Password>testuser1</Password>
<PortalID>0</PortalID>
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>Registered Users</a:string>
</Roles>
<UserID>43</UserID>
<Username>testuser</Username>
</UserInfoDataContract>
XSD generated using http://xmlgrid.net/xml2xsd.html
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- XML Schema Generated from XML Document on Thu Apr 09 2015 11:18:33 GMT-0500 (CDT) -->
<!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
<xs:element name="UserInfoDataContract">
<xs:complexType>
<xs:sequence>
<xs:element name="DisplayName" type="xs:string"></xs:element>
<xs:element name="Email" type="xs:string"></xs:element>
<xs:element name="FirstName" type="xs:string"></xs:element>
<xs:element name="IsSuperUser" type="xs:string"></xs:element>
<xs:element name="LastName" type="xs:string"></xs:element>
<xs:element name="Password" type="xs:string"></xs:element>
<xs:element name="PortalID" type="xs:int"></xs:element>
<xs:element name="Roles">
<xs:complexType>
<xs:sequence>
<xs:element name="a:string" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="UserID" type="xs:int"></xs:element>
<xs:element name="Username" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
When i try to create JAXB Classes from the above XSD in eclipse, it gives me errors related to
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
and
<xs:attribute name="xmlns:a" in Roles element
and
<xs:element name="a:string" type="xs:string"></xs:element>
So i removed them and added
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" targetNamespace="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified" attributeFormDefault="unqualified">
on the top.
How will the "Roles" from my XML be referenced in the XSD so that i can create a correct POJO class?
Taking reference from http://www.w3.org/TR/xmlschema-0/#ListDt, lists should be declared this way
<xsd:simpleType name="listOfMyIntType">
<xsd:list itemType="myInteger"/>
</xsd:simpleType>
I am not able to figure out how i can apply this to my XSD. Any help will be appreciated.
I think in xml in
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
the xmlns:a
is considered as attribute by the ide that generates xsd, that's why in generated schema you have
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
So I agree with deleting
<xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
from xsd.
My only guess is try setting
targetNamespace="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared"
as it is used explicitly in your xml
In my xsd, I have something like this:
<xs:complexType name="Disk">
<xs:attribute name="index" type="xs:int"></xs:attribute>
<xs:attribute name="providerId" type="xs:int"></xs:attribute>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
<xs:complexType name="Server">
<xs:sequence>
<xs:element name="Disk" maxOccurs="unbounded" type="Disk"></xs:element>
</xs:sequence>
</xs:complexType>
I generated JAXB classes from above xsd and create an object of type Server and populated the arraylist named disk. Now when I try to marshal Server object to xml, I see:
<server><Disk index="0" providerId="123" size="10000"/><Disk index="1" providerId="1234" size="10000"/></server>
Is there any way by which I should see XML like:
<server><Disks><Disk index="0" providerId="123" size="10000"/><Disk index="1" providerId="1234" size="10000"/></Disks></server>
I want the list of disks xml tags to be enclosed in the tag. If it is possible, how to do it?
If u want tag can occurs many times in server tag use this.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Disk">
<xs:attribute name="index" type="xs:int"></xs:attribute>
<xs:attribute name="providerId" type="xs:int"></xs:attribute>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
<xs:complexType name="Server">
<xs:sequence>
<xs:element name="Disks" maxOccurs="unbounded" type="Disks"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Disks">
<xs:sequence>
<xs:element name="Disk" maxOccurs="unbounded" type="Disk"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Or if you want Disks tag only once use following XSD
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Disk">
<xs:attribute name="index" type="xs:int"></xs:attribute>
<xs:attribute name="providerId" type="xs:int"></xs:attribute>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
<xs:complexType name="Server">
<xs:sequence>
<xs:element name="Disks" minOccurs="0" maxOccurs="1" type="Disks"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Disks">
<xs:sequence>
<xs:element name="Disk" maxOccurs="unbounded" type="Disk"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
I want to create a Java bean using the XML schema. I tried to create an XML schema from XML using freeformatter.com's XSD/XML Schema Generator. This is the XML schema that I have generated and is producing an error:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://open-services.net/ns/core#" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shortTitle">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="rdf:parseType" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="discussedBy">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="rdf:resource" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="instanceShape">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="rdf:resource" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="serviceProvider">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="rdf:resource" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
This is the error:
- src-resolve.4.2: Error resolving component 'rdf:parseType'. It was detected that 'rdf:parseType' is in namespace 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', but
components from this namespace are not referenceable from schema document 'file:///home/workspace/jaxbexe/xsdfile.xsd'. If this is the incorrect namespace,
perhaps the prefix of 'rdf:parseType' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///home/
workspace/jaxbexe/xsdfile.xsd'.
- s4s-elt-invalid-content.1: The content of '#AnonType_shortTitle' is invalid. Element 'attribute' is invalid, misplaced, or occurs too often.
Yes, this free tool seems to be unable to cope with namespaces. So either complain to the author to get it fixed, or use a different tool - there are plenty around.