从不同命名空间中的 XSD 导入复杂类型

Import complex types from XSD in different namespace

我在不同的命名空间中有两个 XSD 文件,如下所示

book_request.xsd

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

    <xsd:element name="book-request" type="book-request"/>

    <xsd:complexType name="book-request">
        <xsd:sequence>
            <xsd:element name="booking-code" type="xsd:string"/>
            <xsd:element name="room-type-code" type="xsd:string"/>
            <xsd:element name="customer-ip-address" type="xsd:string" 
                         minOccurs="0"/>
            <xsd:element name="booking-amount" type="xsd:double"/>
            <xsd:element name="customer" type="customer"/>
            <xsd:element name="payment" type="payment"/>
            <xsd:element name="provisional-book-id" type="xsd:string"
                         minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="customer">
        <xsd:sequence>
            <xsd:element name="title" type="xsd:string"/>
            <xsd:element name="first-name" type="xsd:string"/>
            <xsd:element name="middle-name" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

临时_book_request.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
    targetNamespace="provisional-book-request"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:common="common"
    xmlns="provisional-book-request"
    elementFormDefault="qualified">

    <xsd:import schemaLocation="book_request.xsd" namespace="book-request"/>

    <xsd:element name="provisional-book-request" type="provisional-book-request"/>

    <xsd:complexType name="provisional-book-request">
        <xsd:sequence>
            <xsd:element name="nri" type="xsd:boolean"/>
            <xsd:element name="hotel-id" type="xsd:string"/>
            <xsd:element name="check-in-date" type="xsd:date"/>
            <xsd:element name="check-out-date" type="xsd:date"/>
            <xsd:element name="customer" type="customer:customer"/>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

我正在尝试将复杂类型 customer 从 book_request.xsd 导入到 provisional_book_request.xsd 并且我尝试通过 <xsd:element name="customer" type="customer:customer"/>

来实现它

但是我遇到以下错误

"Invalid attribute value for 'ref' in element 'element'. Recorded reason: UndeclaredPrefix: Cannot resolve 'customer:customer' as a QName: the prefix 'customer' is not declared."

由于我是 XSD 的新手,如有任何帮助,我们将不胜感激

provisional_book_request.xsd,

中为导入的命名空间声明一个命名空间前缀
<xsd:schema

     xmlns:br="book-request"

>

并用它来引用导入的类型:

<xsd:element name="customer" type="br:customer"/>

那么你的错误就会消失。


这里是完整的 provisional_book_request.xsd,已按描述更正:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
    targetNamespace="provisional-book-request"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="provisional-book-request"
    xmlns:br="book-request"
    elementFormDefault="qualified">
  <xsd:import schemaLocation="book_request.xsd" namespace="book-request"/>
  <xsd:element name="provisional-book-request" type="provisional-book-request"/>
  <xsd:complexType name="provisional-book-request">
    <xsd:sequence>
      <xsd:element name="nri" type="xsd:boolean"/>
      <xsd:element name="hotel-id" type="xsd:string"/>
      <xsd:element name="check-in-date" type="xsd:date"/>
      <xsd:element name="check-out-date" type="xsd:date"/>
      <xsd:element name="customer" type="br:customer"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>