0
votes

I have been trying to learn XSD and was trying some sample Project in eclipse. So I have created two xsd's by name HospitalSchema.xsd and PatientSchema.xsd. I am trying to refer a Complex Type element from HospitalSchema.xsd into PatientSchema.xsd by importing the HospitalSchema.xsd. Here are the xsd's

HospitalSchema.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/HospitalSchema"
xmlns:tns="http://www.example.org/HospitalSchema" elementFormDefault="qualified">

<element name="Hospital">
    <complexType>
        <all>
            <element name="name" type="string"/>
            <element name="Address" type="string"/>
        </all>
    </complexType>  
</element>

PatientSchema.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/PatientSchema"
xmlns:tns="http://www.example.org/PatientSchema" elementFormDefault="qualified"
xmlns:hoschema="http://www.example.org/HospitalSchema">

<import namespace="http://www.example.org/HospitalSchema" schemaLocation="HospitalSchema.xsd"/>

<element name="patient" type="tns:patientType" />

<complexType name="patientType">
    <sequence>
        <element name="id" type="tns:ID" />
        <element name="name" type="tns:string15Chars" />
        <element name="dob" type="date" />
        <element name="gender" type="tns:Gender" />
        <element name="payBy" type="tns:Payment"/>
        <element name="hospitalName" type="hoschema:Hospital" />
    </sequence>
</complexType>

<simpleType name="ID">
    <restriction base="int">
        <pattern value="[0-9]*"></pattern>  
    </restriction>
</simpleType>

<simpleType name="string15Chars">
    <restriction base="string">
        <maxLength value="15"></maxLength>
    </restriction>  
</simpleType>

<simpleType name="Gender">
    <restriction base="string">
        <enumeration value="M"></enumeration>
        <enumeration value="F"></enumeration>
    </restriction>
</simpleType>

<complexType name="Payment">
    <choice>
        <element name="cash" type="int"/>
        <element name="insurance" type="tns:Insurance"/>
    </choice>   
</complexType>

<complexType name="Insurance">
    <all>
        <element name="provider" type="string"/>
        <element name="limit" type="int"/>
    </all>
</complexType>

type="hoschema:Hospital" is the complexType I am trying to import from another schema. But Eclipse has been throwing error saying

src-resolve: Cannot resolve the name 'hoschema:Hospital' to a(n) 'type definition' component. PatientSchema.xsd /Patient line 17 XML Schema Problem

Both the XSD's are in the same Eclipse Project. Can someone please help?

2

2 Answers

0
votes

hoschema:Hospital is an element declaration, not a type.

I suspect you want to replace

<element name="hospitalName" type="hoschema:Hospital" />

by

<element ref="hoschema:Hospital" />
-1
votes

Here are 2 things I noticed (I'm not an expert on XSDs...).

You could replace

<import namespace="http://www.example.org/HospitalSchema" schemaLocation="HospitalSchema.xsd"/>

by

<include schemaLocation="HospitalSchema.xsd"/>

Also I think your targetNamespace should be the same in both XSD files.

After making those changes I now get an error stating that tns:Insurance is not defined, so I guess that was your next task.