owczarek
owczarek

Reputation: 347

Namespaces in xml when two schemas used

I have one main xsd file with the xml schema defined and the second one, that contains extensions. I need to have something like this:

<ns:Node>
    <InnerNode>
        <Value1>value 1</Value1>
        <Value2>value 2</Value2>
    </InnerNode>
</ns:Node>

The InnerNode definition is in the main file, the Node in the extension one. My extension definition looks like:

<xs:element name="Node">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="InnerNode" type="InnerNodeType"/>
        </xs:sequence>
    </xs:complexType>       
</xs:element>

The namespaces are defined in both schemas, I have imports for both. When I validate the schema with sample xml, I get the error, that also the InnerNode should have the namespace, not only the Node element. I tried using the form attribute with the unqualified value, but it did not help - the validator (XMLSpy) suggests inserting the empty xmlns attribute.

Is it possible to define the InnerNode element that would not need the namespace (some missing attribute)?

EDIT: I have not mentioned that in the main xsd file I use the Node type as a part of the bigger structure (that it probably makes it a bit more complicated):

<OuterNode> <!-- defined in main xsd -->
    <ns:Node> <!-- defined in imported -->
        <InnerNode> <!-- defined in main -->
            <Value1>value 1</Value1>
            <Value2>value 2</Value2>
        </InnerNode>
    </ns:Node>    
</OuterNode>

Is it possible to make it like that?

Upvotes: 0

Views: 413

Answers (2)

Michael Kay
Michael Kay

Reputation: 163587

If the InnerNode element is defined in an element declaration in a separate schema document, then you need ref="InnerNode" rather than name="InnerNode". (Using name= creates a local element declaration that allows any content).

Alternatively, if the type of the InnerNode element is defined in a separate schema document, then you can use name="InnerNode" type="InnerNodeType"

Either way, if the target namespace of the other schema document is absent (i.e. it's a no-namespace schema document), then the value of "type" or "ref" needs to be a no-namespace QName. That means the containing element must not have a default namespace in scope. But yours does: xmlns="http://www.additional.org". So you need to cancel this with an undeclaration:

<xsd:element ref="InnerNode" xmlns=""/>

or

<xsd:element name="InnerNode" form="unqualified" type="InnerNodeType" xmlns=""/>

Upvotes: 1

Iurii  Kvashuk
Iurii Kvashuk

Reputation: 409

I have carried out some research and got the following result:

If you add in your main scheme elementFormDefault="unqualified" and at the same time you add form="unqualified" into the definition of the element InnerNode in extension scheme

   <xs:element name="Node">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="InnerNode" form="unqualified" type="InnerNode"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

it will work and validate properly.

I have attached examples of the schemes below:

main.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.main.org" targetNamespace="http://www.main.org" elementFormDefault="unqualified">
  <xsd:complexType name="InnerNodeType">
    <xsd:sequence>
      <xsd:element name="Value1" type="xsd:string"/>
      <xsd:element name="Value2" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

additional.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.additional.org" targetNamespace="http://www.additional.org"
            xmlns:ext="http://www.main.org" elementFormDefault="qualified">
  <xsd:import schemaLocation="main.xsd" namespace="http://www.main.org"/>
  <xsd:element name="Node">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="InnerNode" type="ext:InnerNodeType" form="unqualified"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Upvotes: 0

Related Questions