users@jaxb.java.net

Validating unmarshalling from chameleon schemas

From: Raymond Wold <raymond.wold_at_teletopia.com>
Date: Mon, 18 Sep 2006 17:32:45 +0200

I'm trying to design chameleon schemas, as there are certain types I
need in many different namespaces. I use XJC to compile java classes
from the schemas, and JAXB to unmarshal (and eventually marshal) objects.

XJC doesn't complain, but I get an extra set of classes under the
package "generated", one for each complexType. This isn't a significan
problem, but it makes me think I am doing something wrong.

More important is that I get an error when I try to unmarshal;

Caused by: org.xml.sax.SAXParseException: src-resolve: Cannot resolve
the name 'myns:phonenumber' to a(n) 'type definition' component.

I am passing both the generic schema file and all the specific schemas
to both XJC and to the unmarshaller, is this wrong? If I leave out the
"generated" package and its schema from the marshaller I get the same
error. I haven't tried leaving out the general schema from the XJC
argument list (since I simply do *.xsd; there's a lot of files), is this
worth trying?

Below follow a simplified set of files. Any comments are welcome, even
if it's just to not use chameleon schemas (though I'd appreciate a
suggestion for an alternative that doesn't involve copy/paste)


<!-- This is my generic type schema (common.xsd), it has more types just
like this, with no target namespace -->
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     elementFormDefault="qualified"
>
     <xsd:simpleType name="phonenumber">
         <xsd:restriction base="xsd:string">
             <xsd:pattern value="\+\d+"/>
             <xsd:minLength value="4"/>
         </xsd:restriction>
     </xsd:simpleType>
</xsd:schema>

<!-- This is one of the specific document schemas (myschema.xsd) -->
<?xml version="1.0" encoding="UTF-8"?>
<schema
     xmlns="http://www.w3.org/2001/XMLSchema"
     targetNamespace="uri:myschema"
     xmlns:myns="uri:myschema"
     elementFormDefault="qualified"
>
     <include schemaLocation="common.xsd"/>
     <element name="message">
         <complexType>
             <attribute name="to" type="myns:phonenumber" use="required"/>
         </complexType>
     </element>
</schema>

<!-- This is an instance of the specific schema (instance.xml) -->
<?xml version="1.0" encoding="UTF-8"?>
<message xmlns='uri:myschema'
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:schemaLocation='uri:myschema myschema.xsd'
     to="+12345678"
     />