Cannot find declaration of element 'xsd:schema' - jaxb

Unmarshalling using jaxb keeps displaying same error. The xsd content is listed below, followed by the error. Grateful for any help.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="HouseName" type="xsd:string"/>
</xsd:schema>
Error: lineNumber: 2; columnNumber: 58; cvc-elt.1: Cannot find the declaration of element 'xsd:schema'.]
I tried without using the xsd on a simple xml file and it worked fine, I tried generating the xsd from a simple xml. But when I tried and include the SchemaFactory.newInstance to XMLConstants.W3C_XML_SCHEMA_NS_URI and the schema filename and run the unmarshaller it displays the error above. I checked previous comments about similar situations but they do not apply.

Related

xsd import from xsd include

I'm having the following "imp.xsd":
<xsd:schema targetNamespace="http://imported"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Imported" type="xsd:string"/>
</xsd:schema>
imported from "incl.xsd":
<xsd:schema targetNamespace="http://main"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://imported" schemaLocation="file:///C:/.../imp.xsd"/>
<xsd:element name="Included" type="xsd:string"/>
</xsd:schema>
which in turn is included from "main.xsd":
<xsd:schema targetNamespace="http://main"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="file:///C:/.../incl.xsd"/>
<xsd:complexType name="dummy">
<xsd:sequence xmlns:impt="http://imported" xmlns:incl="http://main">
<xsd:element ref="incl:Included"/>
<xsd:element ref="impt:Imported"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Question: is "main.xsd" a valid xml schema document?
When validated with the xmllint parser (or from python lxml library, which in turn uses xmllint), I get the following error:
Element '{http://www.w3.org/2001/XMLSchema}element', attribute 'ref':
References from this schema to components in the namespace
'http://imported' are not allowed, since not indicated by an import
statement. WXS schema main.xsd failed to compile
However, another (IBM) parser accepts this XSD without any complaints.
As far as I understand xsd:include, it should behave as if the whole included XSD is inlined into the main document. So, in my opinion, the "main.xsd" should be valid. Is it?
Edit: I was experimenting with xmllint a bit, and when I add the line
<xsd:import namespace="http://imported" schemaLocation="file:///C:/.../imp.xsd"/>
into "main.xsd", xmllint accepts that schema. However, when I add the same line while omitting file:///, I get the following warning:
Element '{http://www.w3.org/2001/XMLSchema}import': Skipping import of
schema located at 'C:/.../imported.xsd' for the namespace
'http://imported', since this namespace was already imported with the
schema located at 'file:///V:/.../imported.xsd'.
Hence I deduce that the original "main.xsd" is valid and that this is a bug in xmllint.
See this thread on the xmlschema-dev#w3.org mailing list. In his answer, Henry S. Thompson quotes following part of the spec:
For a ·QName· to resolve to a schema component [...] the
·namespace name· of the ·QName· is the same as one of the following:
[...]
The ·actual value· of the namespace [attribute] of some <import>
element information item contained in the <schema> element
information item of that schema document [emphasis added].
So indirect imports are not supposed to work.

CXF JAXB basics Equals/Hashcode

I have a problem generating equals and hashcode methods for my imported xsd files which are output to a separate location to my wsdl files. I currently have a .wsdl and .xsd file in the same folder. The .wsdl file imports these xsd files which have a different namespace to the wsdl file. When these xsd files are generated they do not contain equals or hashcode methods. Below is a sample of my pom/xml config and files:
<wsdlOption>
<wsdl>
src/main/xml/wsdl/Rule.wsdl
</wsdl>
<wsdlLocation>classpath:wsdl/Rule.wsdl</wsdlLocation>
<bindingFiles>
<bindingFile>src/main/xml/wsdl/bindings.xjb</bindingFile>
</bindingFiles>
<extraargs>
<extraarg>-impl</extraarg>
<extraarg>-verbose</extraarg>
<extraarg>-xjc-XsimpleEquals</extraarg>
<extraarg>-xjc-XsimpleHashCode</extraarg>
</extraargs>
</wsdlOption>
Sample wsdl (imported Rule.xsd is not generating methods):
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.ws.com/Rule/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Rule"
targetNamespace="http://www.ws.com/Rule/">
<wsdl:types>
<xsd:schema targetNamespace="http://http://www.ws.com/Rule/">
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ws.com/Rule/"
xmlns:pref="http://www.xsd.com/Rule" xmlns:pref1="http://www.xsd.com/Common">
<xsd:import schemaLocation="Rule.xsd"
namespace="http://www.xsd.com/Rule">
</xsd:import>
<xsd:import schemaLocation="Common.xsd" namespace="http://www.xsd.com/Common"></xsd:import>
<xsd:element name="ListGrid">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="request"
type="pref:test" maxOccurs="1" minOccurs="1">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
I assume this is a problem with the location of the generated xsd as I can get it to work by moving this to the same parent folder as the wsdl namespace. I would like to get this to work without moving these files locations as I have a large codebase that would require a large refactor if I do this.
Any suggestions would be welcome.
I found a solution to this problem by adding the following to the pom.xml configuration:
<extraarg>-p</extraarg>
<extraarg>http://www.xsd.com/Rule=com.xsd.rule</extraarg>
Even though this does not change the namespace/package location it seems to now generate the imported files equals and hashcode methods.

XSD Validation: Use attribute in same file as definition?

I have a large .xsd file structured like this:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="foo:bar:baz" xmlns:quux="foo:bar:baz"
quux:attr1="A" quux:attr2="5">
<xsd:attribute name="attr1" type="xsd:string"/>
<xsd:attribute name="attr2" type="xsd:int"/>
<xsd:annotation>
<xsd:documentation>
<xhtml:h1 quux:attr1="A" quux:attr2="5">
Documentation here
</xhtml:h1>
</xsd:documentation>
</xsd:annotation>
<xsd:complexType name=... />
</xsd:schema>
I get the error: "The foo:bar:baz:attr1" attribute is not declared.
Why is it not finding the attribute? It's right there. How can I make these attributes available to the documentation's header?
Future edit: the above schema got the green light from actual XML Validators. Guess there was just something Visual Studio was reading incorrectly.
The XSD you show can be imported or included into another XSD that includes an element declaration, but alone it cannot be used to validate an XML file because it does not declare even a single element.

Error when trying to generate schema using XJC

So I parsed a bunch of XMLs to generate a giant schema file using trang which went fine. However, when I try to generate sources using xjc, I get the following error,
xjc reutersXMLSchema.xsd
parsing a schema...
[ERROR] no-xsi: The {target namespace} of an attribute declaration must not match 'http://www.w3.org/2001/XMLSchema-instance'.
line 11 of file:/Users/cqin/Downloads/trang-20081028/xsi.xsd
Failed to parse a schema.
The schema looks like the following,
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.w3.org/2001/XMLSchema-instance" xmlns:filter="http://schemas.reuters.com/ns/2006/04/14/rmds/webservices/news/filter" xmlns:ns0="http://www.reuters.com/ns/2006/05/01/webservices/rkd/News_1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://schemas.reuters.com/ns/2006/04/14/rmds/webservices/news/headlineml" xmlns:cache_1="http://www.reuters.com/ns/2008/03/01/webservices/rkd/Cache_1" xmlns:ns2="http://www.reuters.com/ns/2006/05/01/webservices/rkd/TokenManagement_1" xmlns:global="http://www.reuters.com/ns/2006/05/01/webservices/rkd/Common_1" xmlns:a="http://www.w3.org/2005/08/addressing">
<xs:import namespace="http://schemas.reuters.com/ns/2006/04/14/rmds/webservices/news/filter" schemaLocation="filter.xsd"/>
<xs:import namespace="http://schemas.reuters.com/ns/2006/04/14/rmds/webservices/news/headlineml" schemaLocation="ns1.xsd"/>
<xs:import namespace="http://www.reuters.com/ns/2006/05/01/webservices/rkd/Common_1" schemaLocation="global.xsd"/>
<xs:import namespace="http://www.reuters.com/ns/2006/05/01/webservices/rkd/News_1" schemaLocation="reutersXMLSchema.xsd"/>
<xs:import namespace="http://www.reuters.com/ns/2006/05/01/webservices/rkd/TokenManagement_1" schemaLocation="ns2.xsd"/>
<xs:import namespace="http://www.reuters.com/ns/2008/03/01/webservices/rkd/Cache_1" schemaLocation="cache_1.xsd"/>
<xs:import namespace="http://www.w3.org/2003/05/soap-envelope" schemaLocation="s.xsd"/>
<xs:import namespace="http://www.w3.org/2005/08/addressing" schemaLocation="a.xsd"/>
<xs:attribute name="type" type="xs:NCName"/>
</xs:schema>
Any idea why it won't work?
I've tried changing the targetNameSpace to something unique but I get more errors so I'm wondering if there is something I can do with the original error.
Thanks!
Yes, this isn't right:
targetNamespace="http://www.w3.org/2001/XMLSchema-instance"
This is saying that the target namespace of your schema is actually the namespace of the Schema definition schema itself (i.e. the schema which defines the things like <xs:schema> and <xs:import> actually mean). This schema is fixed, you can't write your own schema targeting it, which is why XJC is rejecting it.
I've tried changing the targetNameSpace to something unique but I get more errors
Keep trying that, you'll get further than with what you have now. However, the fact that trang is generating this bad schema at all is suggestive that you've done something wrong further back along the line. Perhaps you asked it to generate a combined schema which included the http://www.w3.org/2001/XMLSchema-instance schema itself, which is almost certainly not what you want to do.

JAXB - Creating modules for reuse

Does JAXB support modular code generation?
Most of my background is with JibX for XML marshalling, but for legacy reasons our firm is using JAXB.
One feature that was available for JIBX was modular code generation. Say I have a main schema but I have several different envelopes for that schema. With JibX I could create a jar file out of the JibX'ed core schema, and then in separate projects I could JibX my envelope schemas and simply point to the shared jar instead of having to duplicate the code generation of the core schemas for each envelope.
I don't yet see a way for JAXB to handle this - has anyone been successful doing something like this?
Thanks in advance,
Roy
For the JAXB RI, that's handled with "episode" files (these are really just customization files). Process the core schema first, making sure to have xjc use the -episode <file> arg. Package the results of that processing into a JAR file with the episode file in META-INF/sun-jaxb.episode. Then, pass that JAR file as an arg to xjc when processing the other schemas.
Using a JAXB 2.1 implementation (Metro, EclipseLink MOXy, Apache JaxMe, etc), you can specify that schema types correspond to existing classes in order to prevent them from being generated.
For example:
root.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/root">
<xsd:import schemaLocation="imported.xsd" namespace="http://www.example.com/imported"/>
<xsd:complexType name="root">
<xsd:attribute name="root-prop" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
imported.xsd
<?xml version="1.0"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/imported"
targetNamespace="http://www.example.com/imported">
<xsd:complexType name="imported">
<xsd:attribute name="imported-prop" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
Problem Statement
If you use the XJC tool to generate java classes from the XML schema:
xjc -d out root.xsd
You the following is generated:
com\example\imported\Imported.java
com\example\imported\ObjectFactory.java
com\example\imported\package-info.java
com\example\root\ObjectFactory.java
com\example\root\Root.java
com\example\root\package-info.java
imported-bindings.xml
You can use a JAXB bindings file to specify that types from imported.xsd point to existing classes:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="imported.xsd">
<jxb:bindings node="//xs:complexType[#name='imported']">
<jxb:class ref="com.example.imported.Imported"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Running the XJC
Now if we run XJC with out bindings file:
xjc -d out -b imported-bindings.xml root.xsd
None of the files specified in the bindings file will be generated:
com\example\root\ObjectFactory.java
com\example\root\Root.java
com\example\root\package-info.java
Alternative Approach
The code generated from the imported schema directly (xjc imported.xsd) and indirectly (xjc root.xsd) is the same. You can simply drop the code generated indirectly and point at the project containing the code that was generated directly.

Resources