How Do I Remove a Namespace Definition

Problem

The XML that the Generated Libraries creates may contain namespace definitions, these are not always required.

Resolution

The offending definitions can be removed from the root element.
In the example below the namespace "http://www.w3.org/2001/XMLSchema-instance" is not used in the document, and can be removed. The sample code shows how this can be accomplished.

<?xml version="1.0" encoding="UTF-8"?> <RootElm xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 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>

Note

Removing namespace definitions that are needed in order to make the XML document valid will then to be inserted into the document when they are needed . This can cause them to be defined multiple times, this is valid but not always desirable.

Sample Code

  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().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 } };

  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.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(); } }

  Java
In the generated classes 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.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(); }

  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.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