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

时间:2017-12-06 23:38:38

标签: xml xsd xsd-validation xml-validation

我有两个不同命名空间的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>

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: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"/>

来实现它

但我面临以下错误

  

“元素'元素'中'ref'的属性值无效。记录   原因:UndeclaredPrefix:无法解析'customer:customer'为   QName:未声明前缀'customer'。“

由于我对XSD很陌生,所以我们将不胜感激

1 个答案:

答案 0 :(得分:1)

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>