Search code examples
xmlxsdxsd-validationxml-validation

Import complex types from XSD in different namespace


I have two XSD files in different namespaces like below

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>

I am trying to import the complex type customer from book_request.xsd to provisional_book_request.xsd and I try to achieve it by <xsd:element name="customer" type="customer:customer"/>

But I face the following error

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

As I am very new to XSD, any help would be appreciated


Solution

  • Declare a namespace prefix for the imported namespace in provisional_book_request.xsd,

    <xsd:schema
    
         xmlns:br="book-request"
    
    >
    

    and use it to reference the imported type:

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

    then your error will go away.


    Here is the full provisional_book_request.xsd, corrected as described:

    <?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>