Vu Nguyen
Vu Nguyen

Reputation: 1088

XML Schema: how to reference a type from a no namespace XSD

So I have an XSD that has no namespace specified.

I then import it into another schema (B.xsd) that has namespace specified with "targetnamespace='my-name-space'"

<import schemalocation="A.xsd"/>

After that I want to create an element in B.xsd

<element name="AuthenticationRequest" type="AuthenticationRequest"/>

This fails the validation saying that type AuthenticationRequest are no found. This type is actually defined in A.xsd.

How do I reference this type from A.xsd in B.xsd??

Upvotes: 1

Views: 4230

Answers (2)

Ghislain Fourny
Ghislain Fourny

Reputation: 7279

The idea is that the type attribute is a QName, meaning that it is sensitive to prefix bindings.

If the imported schema has no namespace, which seems to be the case here, then the value of the type attribute should be unprefixed. However, since in the schema snippet it seems that the default namespace is defined and identical to the XML Schema namespace (http://www.w3.org/2001/XMLSchema), the engine attempts looking up a type called AuthenticationRequest in the XML Schema namespace. This can be solved by binding the XML Schema namespace to a prefix, often xs or xsd, rather than making it default.

<xs:schema
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  ...
>
    <xs:import schemalocation="A.xsd"/>
    <xs:element name="AuthenticationRequest" type="AuthenticationRequest"/>
</xs:schema>

For completeness: if the imported schema has a target namespace, two things need to be done:

  1. Binding the target namespace of the imported schema to some prefix, say, imported
  2. Prefixing the value of the type attribute with this prefix, like so:

    <xs:schema
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:imported="http://www.example.com/imported"
      ...
    >
      <xs:import schemalocation="A.xsd" namespace="http://www.example.com/imported"/>
      <xs:element name="AuthenticationRequest" type="imported:AuthenticationRequest"/>
    </xs:schema>
    

As mentioned in the link posted on chameleon design, an alternative to importing a schema is including a schema.

However, even with this design, the value of the type attribute still needs to be defined properly, that is:

  • either with no prefix (and no default namespace) if there is no target namespace
  • or with the appropriate prefix, bound to the target namespace
  • or with no prefix, with the target namespace being defined as the default namespace

Very importantly, the above applies even if there is only one schema, with no other schemas imported or included. It works out of the box if the schema has no namespace, but it needs to be considered if there is a target namespace.

Upvotes: 2

Vu Nguyen
Vu Nguyen

Reputation: 1088

I figured out after spending time reading online. I read about "chameleon" design and it helps me understand more about namespace.

http://www.xfront.com/ZeroOneOrManyNamespaces.html

Upvotes: 1

Related Questions