user841717
user841717

Reputation: 163

Reference no namespace schema element in a schema with target namespace

I have a schema A with no name space declaration, and another schema B with a default and tns (both pointing to the same uri). I want to reference an element from Schema A in Schema B. How can i do that? Here are my schemas:

Schema A (messageType.xsd):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
<xs:element name="message" type="messageType" minOccurs="0" />
 <xs:complexType name="messageType">
        <xs:sequence>
            <xs:element name="messageId" type="xs:string" minOccurs="0" />
            <xs:element name="severity" type="severityType" minOccurs="0" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Schema B (say messageText.xsd):

<xs:schema elementFormDefault="unqualified" xmlns="http://www.myorg/schema/ref" targetNamespace="http://www.myorg/schema/ref" xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:import schemaLocation="messageType.xsd" />
   <xs:element name="messages" type="inheritedMessageType"/>

  <xs:complexType name="inheritedMessageType">
    <xs:complexContent>
      <!-- messageType is declared in no namespace schema messageType.xsd -->
      <xs:extension base="messageType">
          <xs:element name="messageText" type="xs:string"/>
      </xs:extension>
    </xs:complexContent>
   </xs:complexType>
</xs:schema>

I want to reference the messageType declared in messageType.xsd from messageText.xsd, but cannot add a namespace to messageType.xsd as it'll break other existing schemas.

Any help is appreciated. Thanks

Upvotes: 1

Views: 1137

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122394

In schema B, instead of

xmlns="http://www.myorg/schema/ref"

change it to

xmlns:tns="http://www.myorg/schema/ref"

This means that any references within schema B to its own elements and types will need to use the tns: prefix, e.g.

<xs:element name="messages" type="tns:inheritedMessageType"/>

but now a plain messageType with no prefix will refer to the no-namespace type (from schema A) rather than the non-existent one in schema B's target namespace.

Upvotes: 2

Related Questions