Cannot find the declaration of the element 'diops' - java

i'm new on this XML stuff, and i'm having one issue with the location of the element diops.
Here is my xml
<diops
xmlns:ans="ttp://dados.wsh.com.br/diopsxml-2016/xsd/2016/xsd/2016"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://dados.wsh.com.br/diopsxml-2016/xsd/2016 Diops2016.xsd">
And here is my XSD
<xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.ans.gov.br/ws/diops/financeiro/schema/v2016"
elementFormDefault="qualified"
targetNamespace="http://www.ans.gov.br/ws/diops/financeiro/schema/v2016"
version="1.1">
<xs:include schemaLocation="DiopsComplexTypes2016.xsd"/>
<xs:element name="diops">
<xs:complexType>
<xs:sequence>
<xs:element ref="tns:identificacao"/>
<xs:element name="financeiro" type="tns:financeiro"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The message that appear to me is: "Cannot find the declaration of the element 'diops'.
But he is the first element that i declare in the XSD file, with the other elements i dont have any issue, only with the element called 'diops'.
Sorry about my poor english.

I guess you're trying to validate yout XML document against the schema.
Anyways, your schema defines the <diops> element to be in the http://www.ans.gov.br/ws/diops/financeiro/schema/v2016 namespace (that's what the targetNamespace attribute does). However, in your XML document, the <diops> element belongs to the empty namespace.
You should simply add a namespace declaration in your XML document:
<diops xmlns="http://www.ans.gov.br/ws/diops/financeiro/schema/v2016" ... >

Related

nested xsd: Cannot resolve the name ... to a type defintion component

My webservice should be able to validate xml-input against a schema(request.xsd). The complextyped only element in the schema owns different subelements of types which are defined in other xsds. I am importing all thess xsds. At the end however element type cannot be resolved.
this ist my "request.xsd":
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fe="http://www.some.dir/featurenamespace"
xmlns="http://this.is.my.space"
targetNamespace="http://this.is.my.space"
elementFormDefault="qualified">
<xs:import schemaLocation=" http://www.some.dir/featurenamespace/cmplxtypes.xsd "
namespace=" http://www.some.dir/featurenamespace"/>
<xs:element name="elem1" type="request"/>
<xs:complexType name="request">
<xs:complexContent>
<xs:extension base="fe:complexElement">
<xs:sequence>
<xs:element name="record" type="fe:complexElement "/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
This is the schema (e.g. cmplxtypes.xsd) where the type 'complexElement' is defined:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fe="http://www.some.dir/featurenamespace"
xmlns:ofe ="http://www.some.dir/otherfeaturenamespace"
targetNamespace="http://www.some.dir/featurenamespace"
version="1.1.0"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:include schemaLocation="fe_types.xsd" />
<xs:import schemaLocation="ofe_types.xsd" namespace="http://www.some.dir/otherfeaturenamespace"/>
<xs:complexType name="complexElement">
<xs:sequence>
<xs:element name="feature" type="fe:FeatureType "/>
<xs:element name="otherfeature" type="ofe:otherFeatureType"/>
</sequence>
</xs:complexType>
The files: cmplxtypes.xsd, fe_types.xsd and ofe_types.xsd share the same namespace.
Now, trying to validate myxml against request.xsd I get: cannot resolve 'complexElement' to a 'type defintion' component.
I do appreciate any help.
There are several inconsistencies in the schemas that may explain the error.
request.xsd doesn't appear to be importing cmplxtypes.xsd, where fe:complexElement is defined.
<xs:import
schemaLocation="http://www.some.dir/featurenamespace/cmplxtypes.xsd"
namespace=" http://www.some.dir/featurenamespace" />
cmplxtypes.xsd includes ofe_types.xsd even though the namespace does not match according to the import in request.xsd. Namespaces much match when including other schemas (or the included schema must have no target namespace).

referencing/including xsd files in a jar

I want to add an xsd file to our project that relies on types defined in another xsd that is in a jar. We use jaxb to generate Java classes from the xsds.
How do i refer to SchemaContainingFooTypeIsInaJAR.xsd so that 'FooType' resolves correctly and the proper Java classes get generated
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="SchemaContainingFooTypeIsInaJAR.xsd"/>
<xs:complexType name="FooMoreType">
<xs:complexContent>
<xs:extension base="FooType">
<xs:sequence>
<xs:element name="something" type="xs:boolean" minOccurs="0">
<xs:annotation>
<xs:documentation xml:lang="en">
something something
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
You might be interested in these features:
http://confluence.highsource.org/display/MJIIP/User+Guide#UserGuide-CompilingaschemafromaMavenartifact
http://confluence.highsource.org/display/MJIIP/User+Guide#UserGuide-Usingcatalogs
In short, you can write a catalog file which would point to your schema in a JAR file:
REWRITE_SYSTEM "some/url/a.xsd" "maven:org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-tests-episodes-a!/a.xsd"
DISCLAIMER: I am the principal dev of maven-jaxb2-plugin.
See XML Schema reference and JAXB SchemaFactory source order must follow import order between schemas?
The consensus appears to be: you need to supply your own resolver.
Replace your line:
<xs:include schemaLocation="SchemaContainingFooTypeIsInaJAR.xsd"/>
with
<xs:include schemaLocation="jar:file:///path/to/jar/thing.jar!/path/inside/jar/to/file.xsd"/>

JAXB converting XSD to Java classes

When I run the following command:
xjc -b xmlSchema.xjb -d src -p com.q1labs.qa.xmlgenerator.model.generatedxmlclasses xmlSchema.xsd
It creates Java classes however I've found that my root class does not have the correct name and does not have #XmlRootElement which declares it as a root element which means when I use the classes to generate XML it is not formed properly.
XSD Schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" targetNamespace="http://ibm.org/seleniumframework"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Test" type="sel:TestType" xmlns:sel="http://ibm.org/seleniumframework"/>
<xs:complexType name="TestType">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element type="sel:Option1" name="Option1" xmlns:sel="http://ibm.org/seleniumframework"/>
<xs:element type="sel:Option2" name="Option2" xmlns:sel="http://ibm.org/seleniumframework"/>
<xs:element type="sel:Option3" name="Option3" xmlns:sel="http://ibm.org/seleniumframework"/>
</xs:choice>
</xs:complexType>
This is the output I am getting:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testType xmlns="http://ibm.org/seleniumframework"/>
Generated classes correspond to complex types. Anonymous complex types that are declared as part of global elements will get an #XmlRootElement annotation. Others will have a #XmlElementDecl annotation generated on the ObjectFactory class. This is because there may be more than one global element that corresponds to the same complex type.
For More Information
http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html

JAXB Unmarshall error

I'm using JAXB on my project, but from time to time, I face some problems that I can't solve. I have setup my environment like this:
Armor Class
package com.fortresswars.entity.component;
#XmlType(name = "armor", namespace = "http://fortresswars.com")
public class ArmorComponent extends AbstractComponent
package-info.java
#XmlSchema(xmlns = #XmlNs(namespaceURI = "http://fortresswars.com", prefix = "fw"), elementFormDefault = XmlNsForm.UNQUALIFIED, namespace = "http://fortresswars.com")
package com.fortresswars.entity.component;
The generated schema header is almost correct:
<xs:schema elementFormDefault="unqualified" version="1.0" targetNamespace="http://fortresswars.com" xmlns:fw="http://fortresswars.com" xmlns:tns="http://fortresswars.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
The only thing I didn't like is that TNS prefix that JAXB puts there and I can't remove. I'm using an ant task (com.sun.tools.jxc.SchemaGenTask), and I remember reading somewhere that this was the problem.
The rest of the generated scheme follows below. I'll show only the relevant part about armor:
<xs:complexType name="armor">
<xs:complexContent>
<xs:extension base="tns:abstractComponent">
<xs:sequence>
<xs:element name="value" type="xs:short" minOccurs="0"/>
<xs:element name="type" type="tns:armor-type" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
And the element that is using the armor component:
<xs:complexType name="character">
<xs:complexContent>
<xs:extension base="tns:thing">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="armor" type="tns:armor"/>
<xs:element name="model" type="tns:model"/>
<xs:element name="status" type="tns:status"/>
<xs:element name="costs" type="tns:costs"/>
</xs:choice>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
To test this, I created a XML Document (see the TNS prefix, I need to put it, or the fw prefix, along with the xmlns:fw also).
<?xml version="1.0" encoding="UTF-8"?><tns:character xmlns:tns="http://fortresswars.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="/home/shirkit/jMonkeyProjects/Fortress Wars/Core/schema/full.xsd">
<armor>
<value>5</value>
<type>NORMAL</type>
</armor>
</tns:character>
But when I'm unmarshalling this document, here's the error I get:
Exception: unexpected element (uri:"", local:"armor"). Expected elements are <{http://fortresswars.com}armor>,<{http://fortresswars.com}attacks>,<{http://fortresswars.com}costs>,<{http://fortresswars.com}model>,<{http://fortresswars.com}abilities>,<{http://fortresswars.com}status>,<{http://fortresswars.com}movement>
I have setup elementFormDefault to UNQUALIFIED, and even though this doesn't work. Why I'm getting this exception? And can I remove TNS prefix from the generated schema?
when you define a namespace prefix for elements of http://fortresswars.com you need to prefix all elements with it, not only character. So this should work
<?xml version="1.0" encoding="UTF-8"?>
<tns:character
xmlns:tns="http://fortresswars.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/home/shirkit/jMonkeyProjects/Fortress Wars/Core/schema/full.xsd">
<tns:armor>
<tns:value>5</tns:value>
<tns:type>NORMAL</tns:type>
</tns:armor>
</tns:character>
The tns prefix used in the schema is unrelated to what you use as prefix for XML text that you unmarshal. You may choose any other in xmlns:whatyoulike="http://fortresswars.com". The key that connects the elements in the XML document to the definitions in the schema file is the namespace URI, in your case "http://fortresswars.com". If you define your namespace as default namespace, you can omit the prefix on every element:
<?xml version="1.0" encoding="UTF-8"?>
<character
xmlns="http://fortresswars.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/home/shirkit/jMonkeyProjects/Fortress Wars/Core/schema/full.xsd">
<armor>
<value>5</value>
<type>NORMAL</type>
</armor>
</character>
The namespace prefix in the XSD file helps to avoid name clashes if you wan't to use the schema file together with other schema files that define types or elements with the same name. It does not force you to use it in the xml files you want to unmarshal.
On the other hand, when you marhshal objects to XML, the file package-info.java defines what prefix JAXB uses, but this only works in recent versions of JAXB and it's not always easy to assure that the correct version is in use when your code runs. But you may use a NamespacePrefixMapper to control that.

Java XSD validator can't find element declaration

I have a simple schema validator method:
// Throws runtime exception if anything goes wrong.
public void validate(String schemaURL, String xml) throws Throwable {
SAXParserFactory oSAXParserFactory = SAXParserFactory.newInstance();
SAXParser oSAXParser = null;
oSAXParserFactory.setNamespaceAware(true);
SchemaFactory oSchemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
oSAXParserFactory.setSchema(oSchemaFactory.newSchema(new URL(schemaURL)));
oSAXParser = oSAXParserFactory.newSAXParser();
SaxErrorHandler handler = new SaxErrorHandler();
oSAXParser.parse(new InputSource(new StringReader(xml)),handler);
}
I have a schema hosted at http://myserver.com/schemas/app-config/1.0/app-config-1.0.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="application">
<xs:complexType>
<xs:choice>
<xs:element ref="info"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="info">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:schema>
Observe the follow instance of that schema:
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://myserver.com/schemas/app-config/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://myserver.com/schemas/app-config/1.0
http://myserver.com/schemas/app-config/1.0/app-config-1.0.xsd">
<info
name="Dummy Application"
/>
</application>
When I pass my validate method the following:
String xmlInstance = readXMLIntoString();
String schemaURL = "http://myserver.com/schemas/app-config/1.0/app-config-1.0.xsd";
validate(schemaURL, xmlInstance);
I get the following error:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'application'.
Is something wrong with my schema?
Is something invalid with my instance?
Is there a problem with me actually hosting the schema at the URL (although the one used in this example is a mockup, I assure you that the XSD file really is hosted at the URL I'm hitting in the code)?
Why can't the validator find application declaration?
You're XML Schema is missing a targetNamespace.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://myserver.com/schemas/app-config/1.0"
elementFormDefault="qualified">
...
Instead of declaring a targetNamespace in your XML Schema you could drop all that mumbo jumbo from your XML.
<application>
...
Still, try to stick to the first solution.
My original answer stated that schemaLocation was incorrect, which is in fact the case, but for a different reason than I suggested originally. As Kohányi Róbert said, your schema is missing a targetNamespace. Either follow his approach and modify your schema, or replace schemaLocation with noNamespaceSchemaLocation.

Categories