Home  /  Blog  /  How to : referencing a Schema in schemaLocation

How to : referencing a Schema in schemaLocation

March 2021

In this short how-to guide we will show you how to reference a schema in schemaLocation, the settings that need to be changed and their effects of the generated result.

The xsi:schemaLocation attribute locates schemas for elements and attributes that are in a specified namespace. Its value is 
is a sequence of alternating namespace URI's followed by relative or absolute URL's where the schema for that namespace can be found. 
An XML documents can be linked with their schemas using the schemaLocation attribute. It is most commonly attached to the root element but can appear further down the tree.

This information may identify the location of the associated schemas, as well as further identify the schema types used, which opens a new level of flexibility and interaction between schemas and instance documents in the design of XML applications. 

In order for the XML serialization process to produce this attribute, the following must settings must be set :

In the xsd2code++ settings, you can specify the SchemaLocation. This option can be found in the Serialization, XmlOuput, SchemaLocation section.

Of course it is necessary to enable the generation of XML attributes in the section "Output property settings", you need to set the XmlAttribute.Enabled setting to true.

Image showing the XML output setting

The code that will be generated will contain an additional public variable in each of the classes so that the serialization makes the expected attribute:

XML Output:

<?xml version="1.0" encoding="utf-8"?>
<MyObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:SchemaLocation="http://www.w3.org/2001/XMLSchema-instance" xmlns="MyObject.MemoryLayout">
  <FileInformation dateTimeCreated="0001-01-01T00:00:00" dateTimeChanged="0001-01-01T00:00:00" xmlns="AllegroMicro.Common">

And that is how you reference a schema in SchemaLocation. Have fun !