XSD 从另一个 XSD 导入元素时出现验证错误

XSD Validation error when Importing element from another XSD

我一直在努力学习 XSD 并尝试在 eclipse 中尝试一些示例项目。所以我创建了两个 xsd 的名称 HospitalSchema.xsd 和 PatientSchema.xsd。我试图通过导入 HospitalSchema.xsd 将 HospitalSchema.xsd 中的复杂类型元素引用到 PatientSchema.xsd 中。这是 xsd 的

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" 是我试图从另一个模式导入的复杂类型。但是 Eclipse 一直在抛出错误

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

两个 XSD 都在同一个 Eclipse 项目中。有人可以帮忙吗?

这是我注意到的两件事(我不是 XSDs 方面的专家...)。

你可以替换

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

来自

<include schemaLocation="HospitalSchema.xsd"/>

此外,我认为您的 targetNamespace 在两个 XSD 文件中应该相同。

进行这些更改后,我现在收到一条错误消息,指出 tns:Insurance 未定义,所以我想这是你的下一个任务。

hoschema:Hospital 是元素声明,不是类型。

我怀疑你想替换

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

来自

<element ref="hoschema:Hospital" />