How do I Change the Aliases in the output XML?

Background

Namespaces can be used within an XSD to segment your schemas, and to separate reusable entities. The XML created by the wrapper classes qualifies the entities within the XML document accordingly. This will mean that valid XML is created, but this XML may be verbosely qualified with namespace declarations. By default only namespaces aliased in the XSD are explicitly declared in the generated XML document, the rest are aliased as they appear or given arbitrary aliases (AA, AB, AC etc). This behavior although valid is not always ideal.

Problem

When your schema contains namespaces, then generated classes automatically add the namespaces as appropriate when the classes are serialized to XML. This ensures that the XML is valid against the schema. By default the namespaces will be aliased using the aliases defined in the schema, but if non are present, then ones are created (starting AA, AB, AC etc).

Although the aliases used does not make any difference to the validity of the document, we have found that some 3rd parties make assumptions regarding them, so it is sometimes advantageous to be able to control these aliases.

MainSchema.xsd

<?xml version="1.0" encoding="UTF-8" ?> <xs:schema targetNamespace="http://www.code-generator.com/nstest" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:so="http://www.someother.com" xmlns:cg="http://www.code-generator.com/nstest" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:import namespace="http://www.someother.com" schemaLocation="SomeOther.xsd" /> <xs:element name="RootElm"> <xs:complexType> <xs:sequence> <xs:element name="SomeValue" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="FromAnotherNamespace" type="so:BasicComplexType"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

SomeOther.xsd

<?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="http://www.someother.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:so="http://www.someother.com" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:complexType name="BasicComplexType"> <xs:sequence> <xs:element name="StringElm" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema>

Sample.xml

<?xml version="1.0" encoding="UTF-8"?> <RootElm xmlns="http://www.code-generator.com/nstest" xmlns:so="http://www.someother.com"> <SomeValue>1.1</SomeValue> <so:FromAnotherNamespace> <so:StringElm>be1</so:StringElm> </so:FromAnotherNamespace> </RootElm>

Resolution

In the above example the namespace http://www.someother.com has been aliased using so. This mapping is held in the XmlSerializationContext object. This can be altered, see below.

  C++
In the generated classes Enumerations.cpp there is class called CAppLifetime, the default mappings are configured in this class.

class CAppLifetime { static void RegisterLibrary() { LtXmlLib16::Register("Liquid Technologies Limited", "ElmNamespace.xsd", "XXXXXXXXXXXXXXXXXXXX"); // ##HAND_CODED_BLOCK_START ID="Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional namespace declarations here... LtXmlLib16::CXmlSerializationContext::GetDefaultContext().SetSchemaType(LtXmlLib16::SchemaType_XSD); // LtXmlLib16::CXmlSerializationContext::GetDefaultContext().SetDefaultNamespaceURI(_T("http://www.fpml.org/2003/FpML-4-0")); LtXmlLib16::CXmlSerializationContext::GetDefaultContext().GetNamespaceAliases().Add(_T("xs"), _T("http://www.w3.org/2001/XMLSchema-instance")); LtXmlLib16::CXmlSerializationContext::GetDefaultContext().GetNamespaceAliases().Add(_T("cg"), _T("http://www.code-generator.com/nstest")); LtXmlLib16::CXmlSerializationContext::GetDefaultContext().GetNamespaceAliases().Add(_T("so"), _T("http://www.someother.com")); // ##HAND_CODED_BLOCK_END ID="Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS } };

You can either change the mapping ("so"), add new ones or remove unused ones.
Note: If a namespace is not aliased up front, then an alias is generated automatically for it, when its used in the document.
  C#
In the generated classes Enumerations.cs there is class called Registration, the default mappings are configured in this class.

namespace ElmNamespace { internal class Registration { private static int RegisterLicense() { LiquidTechnologies.Runtime.Net40.XmlObjectBase.Register("Liquid Technologies Limited", "ElmNamespace.xsd", "XXXXXXXXXXXXXXXX"); // ##HAND_CODED_BLOCK_START ID="Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional namespace declarations here... LiquidTechnologies.Runtime.Net40.XmlSerializationContext.Default.SchemaType = LiquidTechnologies.Runtime.Net40.SchemaType.XSD; // LiquidTechnologies.Runtime.Net40.XmlSerializationContext.Default.DefaultNamespaceURI = "http://www.fpml.org/2003/FpML-4-0"; LiquidTechnologies.Runtime.Net40.XmlSerializationContext.Default.NamespaceAliases.Add("xs", "http://www.w3.org/2001/XMLSchema-instance"); LiquidTechnologies.Runtime.Net40.XmlSerializationContext.Default.NamespaceAliases.Add("cg", "http://www.code-generator.com/nstest"); LiquidTechnologies.Runtime.Net40.XmlSerializationContext.Default.NamespaceAliases.Add("so", "http://www.someother.com"); // ##HAND_CODED_BLOCK_END ID="Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS return 1; } static public int iRegistrationIndicator = RegisterLicense(); } }

You can either change the mapping ("so"), add new ones or remove unused ones.
Note: If a namespace is not aliased up front, then an alias is generated automatically for it, when its used in the document.
  Java
In the generated classs Registration.java there is class called Registration, the default mappings are configured in this class.

public class Registration { private static int registerLicense() { com.liquid_technologies.ltxmllib16.XmlObjectBase.register("Liquid Technologies Limited", "ElmNamespace.xsd", "XXXXXXXXXXXXXXXXXX"); // ##HAND_CODED_BLOCK_START ID="Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional namespace declarations here... com.liquid_technologies.ltxmllib16.XmlSerializationContext.Default.setSchemaType(com.liquid_technologies.ltxmllib16.SchemaType.XSD); // com.liquid_technologies.ltxmllib16.XmlSerializationContext.Default.setDefaultNamespaceURI("http://www.fpml.org/2003/FpML-4-0"); com.liquid_technologies.ltxmllib16.XmlSerializationContext.Default.getNamespaceAliases().add("xs", "http://www.w3.org/2001/XMLSchema-instance"); com.liquid_technologies.ltxmllib16.XmlSerializationContext.Default.getNamespaceAliases().add("cg", "http://www.code-generator.com/nstest"); com.liquid_technologies.ltxmllib16.XmlSerializationContext.Default.getNamespaceAliases().add("so", "http://www.someother.com"); // ##HAND_CODED_BLOCK_END ID="Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS return 1; } static public int iRegistrationIndicator = registerLicense(); }

You can either change the mapping ("so"), add new ones or remove unused ones.
Note: If a namespace is not aliased up front, then an alias is generated automatically for it, when its used in the document.
  Visual Basic
In the generated classes General.bas there is a function called CF, the default mappings are configured in this class.

public Function CF() as ElmNamespace.ClassFactory If m_ClassFactory Is Nothing Then Set m_ClassFactory = New ElmNamespace.ClassFactory ' ##HAND_CODED_BLOCK_START ID="Default Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS ' Add Additional namespace declarations here... LtXmlComLib16.DefaultXmlSerializationContext.SchemaType = LtXmlComLib16.SchemaType_XSD ' LtXmlComLib16.DefaultXmlSerializationContext.DefaultNamespaceURI = "http://www.fpml.org/2003/FpML-4-0" LtXmlComLib16.DefaultXmlSerializationContext.NamespaceAliases.Add "http://www.w3.org/2001/XMLSchema-instance", "xs" LtXmlComLib16.DefaultXmlSerializationContext.NamespaceAliases.Add "http://www.code-generator.com/nstest", "cg" LtXmlComLib16.DefaultXmlSerializationContext.NamespaceAliases.Add "http://www.someother.com", "so" ' ##HAND_CODED_BLOCK_END ID="Default Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS End If Set CF = m_ClassFactory End Function

You can either change the mapping ("so"), add new ones or remove unused ones.
Note: If a namespace is not aliased up front, then an alias is generated automatically for it, when its used in the document.

 

Notes

Some XML parsers insist that certain namespaces be aliased using specific alias, i.e. xerces insists http://www.w3.org/XML/1998/namespace should be aliased ‘xml’. This can be accomplished by editing the namespace mappings.

Description Value
Article Created 7/2/2006
Versions Liquid XML 2005 (4.1.0) and greater
Also See Setting A Default Namespace
  Working In a multithreaded environment
 

Working With Multiple Schemas

  Hand Coded Blocks