how to import XSD types into root schema?
Asked Answered
C

2

13

This is my existing XSD schema in foo.xsd, that declares just the type:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
  targetNamespace="foo">
  <xs:complexType name="alpha">
    <!-- skipped -->
  </xs:complexType>
</xs:schema>

This is another schema, that declares the element:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
  targetNamespace="foo">
  <xs:import schemaLocation="foo.xsd" namespace="foo" />
  <xs:element name="RootElement" type="alpha"/>
</xs:schema>

This is what I'm getting from SAX parser in Java:

"The namespace attribute 'foo' of an <import> element information 
item must not be the same as the targetNamespace of the schema it exists in."

What am I doing wrong?

Clarion answered 18/1, 2012 at 3:30 Comment(0)
N
22

When the targetNamespace (tns) attributes of the involved XSDs are specified and the same, only xsd:include is allowed (a targetNamespace attribute cannot have empty string as its value).

However, one may include a schema (s1) without a tns from a schema (s2) that has a tns; the net effect is s1 components assume the namespace of the s2 schema. This usage is often referred to as chameleonic composition.

A reference on SO describing the difference between the two, is here.

Nalor answered 18/1, 2012 at 4:12 Comment(1)
Is "tns" the same as "targetNamespace"?Trinitrobenzene
V
0

From @Petru Gardea I am making it simpler for people who find it difficult to understand.

In root xsd where you want to import another XSD, Just use include tag as below. namespace not required here.

<xs:include schemaLocation="Child.xsd"  />

targetNamespace is not mandatory.

Varicolored answered 10/3, 2022 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.