Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basics of referencing a xsd schema from another schema

Tags:

xsd

We are writing a new xsd schema that will be based upon and reference another schema (that is an industry de facto standard for GPS data called GPX). Our schema will be used in validating documents generated by our clients, and in testing. I need to better understand how to reference the base schema from our new schema, especially given that our systems cannot be expected to have an Internet connection.

My understanding is that references to xsd documents at some other server are typically there so that a developer can retrieve the document during development but during system run-time validation one would be wise to have a local copy of a schema since it would not be prudent to hang production simply because the remote server was unreachable. Is this correct?

So then if that's the case, does xsd expect me to both reference the remote base xsd schema, but also the name and location of some local copy of the schema? Or is such a reference not needed in my new schema file? Hope that makes sense. Thanks.

like image 443
bethesdaboys Avatar asked Nov 19 '11 12:11

bethesdaboys


1 Answers

To reference another schema it is a convention to have that schema local to the "main" schema you are referencing it from. For example, to import a schema which defines some re-usable types:

<xs:import namespace="http://CommonTypesNamespace/CommonTypes" 
           schemaLocation="CommonTypes.xsd"/>

This is basically saying that there exists a schema called CommonTypes.xsd which will be found in the same directory as my main schema. The schemaLocation attribute is used as a relative path to the actual schema file from my referencing schema (NOTE: It's also completely optional - see my note at bottom of this answer).

To use the types inside CommonTypes.xsd, I first add the common types namespace in my main schema declaration:

<xs:schema targetNamespace="http://MyNamespace/MyTypes" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified" 
           xmlns:common="http://CommonTypesNamespace/CommonTypes">

This is saying that I want to make the types from my common schema available in my main schema using the namespace prefix "common".

Then, for example, to reference an "Address" common type:

<xs:element name="DeliveryAddress" type="common:AddressType" />

This says that my delivery address type is actually the same type as AddressType defined in the common types schema.

Note: schemaLocation can also be used with URI's, absolute or UNC paths, or not at all because it's an entirely optional attribute. If you don't use schemaLocation your parser will most likely scan local directories looking for a schema with the correct namespace defined, but this is dependent on implementation.

like image 78
tom redfern Avatar answered Sep 21 '22 15:09

tom redfern