Search code examples
namespacesxsdrelative-import

Delimiters in XML-namespaces - relative import of namespaces


I have a conceptual question concerning XML-namespaces: If I have two different xsd-Files in a folder hierarchy:

fileA.xsd
folder/
    fileB.xsd

and in each of those two files, I defined a separate namespace by the targetNamespace attribute:

In fileA.xsd that would be

targetNamespace="http://www.anurlofmine.com/myproject/A"

and in fileB.xsd that would be:

targetNamespace="http://www.anurlofmine.com/myproject/folder/B"

Can I use the path in th eurl to do relative imports ?

Could I for example use

<xs:schema xmlns:relativeA="../A">

in fileB.xsd ?

I'm not really concerned with a way to do it, I know that there are all kinds of ways to specify the location of a schema definition, I'm merely concerned with the meaning of the path in the url.

As far as I know, the url is just a string used to uniquely identify types in xsd files (besides of specifying a place where one might look - no guarantee - for the definition file). Am I correct ?

Hopefully my question is not too weird to be answered :)

Thanks a lot in advance Mischa


Solution

  • The schema location hint in the schemaLocation attribute on the import and include elements in XSD schema documents is specified as being of type xs:anyURI, and that means yes, it can be a relative reference.

    So in your fileB.xsd, you can have an import statement of the form

    <xs:import namespace="http://www.anurlofmine.com/myproject/A"
               schemaLocation="../fileA.xsd"/>
    

    The value of a namespace declaration (such as xmlns:relativeA in your example), on the other hand, is expected to be an absolute URI, not a relative URI. Many programs do not actually check namespace names for legality as URIs, so using "../A" as a namespace name won't necessarily raise an error. But the foundational W3C specs defining the XML technology stack specify that the information set for document which use relative URIs as namespace names is undefined.

    In any case, adding a namespace declaration to an xs:schema element will not have the effect of importing that namespace or referring to any schema document that might be at the location indicated.