Issue with generating sources from xsd schema

I’m getting the following error when generating sources for my project. I have extracted a few common types to a schema called CommonTypes.xsd and I get the following error:

org.xml.sax.SAXParseException: src-resolve.4.1: Error resolving component 'nonEmptyString'. It was detected that 'nonEmptyString' has no namespace, but components with no target namesp
ace are not referenceable from schema document 'file:/C:/Workspace/CommonTypes.xsd'. If 'nonEmptyString' is
 intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'nonEmptyString' has no namespace, then an 'import' without a "namespace" attribute should
be added to 'file:/C:/Workspace/lps-performance-calculation-service/pcs-data/src/main/resources/xsd/calc/lps/CommonTypes.xsd'.

The following simple type is defined in my CommonTypes.xsd schema as below:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:unit="http://www.mywebsite.com/unit"
            xmlns:types="http://www.mywebsite.com/types"
            elementFormDefault="qualified" attributeFormDefault="unqualified"
            targetNamespace="http://www.mywebsite.com//types">
    <!-- import types -->
    <xsd:import namespace="http://www.mywebsite.com/unit"/>
    <!-- other common types -->
    <xsd:simpleType name="nonEmptyString">
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="1"/>
            <xsd:pattern value=".*[^s].*"/>
        </xsd:restriction>
    </xsd:simpleType>

And line 241 which causes the error is below:

<xsd:complexType name="Message">
        <xsd:simpleContent>
            <xsd:extension base="nonEmptyString">
                <xsd:attribute type="xsd:string" name="code" use="required"/>
                <xsd:attribute name="category" use="required">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:token">
                            <xsd:enumeration value="Error"/>
                            <xsd:enumeration value="Info"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:attribute>
                <xsd:attribute type="xsd:string" name="component" use="required"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>

Do you have any idea what may cause the error? I’ve tried searching through StackOverflow and experimenting with the targetNamespace and xmlns, but with no success.

Answer

You are trying to refer to a simple type which has

  • name ‘nonEmptyString’
  • namespace “”

But the simple type ‘nonEmptyString’ is defined in this XSD, which has targetNamespace="http://www.mywebsite.com//types". So you should be referring to a simple type which has

  • name ‘nonEmptyString’
  • namespace “http://www.mywebsite.com//types”

You simply need to change this:

<xsd:extension base="nonEmptyString">

to this:

<xsd:extension base="types:nonEmptyString">