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 SerializationContext 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() { LtXmlLib20::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... LtXmlLib20::CSerializationContext::GetDefaultContext().SetSchemaType(LtXmlLib20::SchemaType_XSD); // LtXmlLib20::CSerializationContext::GetDefaultContext().SetDefaultNamespaceURI(_T("http://www.fpml.org/2003/FpML-4-0")); LtXmlLib20::CSerializationContext::GetDefaultContext().GetNamespaceAliases().Add(_T("xs"), _T("http://www.w3.org/2001/XMLSchema-instance")); LtXmlLib20::CSerializationContext::GetDefaultContext().GetNamespaceAliases().Add(_T("cg"), _T("http://www.code-generator.com/nstest")); LtXmlLib20::CSerializationContext::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.SerializationContext.Default.SchemaType = LiquidTechnologies.Runtime.Net40.SchemaType.XSD; // LiquidTechnologies.Runtime.Net40.SerializationContext.Default.DefaultNamespaceURI = "http://www.fpml.org/2003/FpML-4-0"; LiquidTechnologies.Runtime.Net40.SerializationContext.Default.NamespaceAliases.Add("xs", "http://www.w3.org/2001/XMLSchema-instance"); LiquidTechnologies.Runtime.Net40.SerializationContext.Default.NamespaceAliases.Add("cg", "http://www.code-generator.com/nstest"); LiquidTechnologies.Runtime.Net40.SerializationContext.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.ltxmllib20.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.ltxmllib20.SerializationContext.Default.setSchemaType(com.liquid_technologies.ltxmllib20.SchemaType.XSD); // com.liquid_technologies.ltxmllib20.SerializationContext.Default.setDefaultNamespaceURI("http://www.fpml.org/2003/FpML-4-0"); com.liquid_technologies.ltxmllib20.SerializationContext.Default.getNamespaceAliases().add("xs", "http://www.w3.org/2001/XMLSchema-instance"); com.liquid_technologies.ltxmllib20.SerializationContext.Default.getNamespaceAliases().add("cg", "http://www.code-generator.com/nstest"); com.liquid_technologies.ltxmllib20.SerializationContext.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... LtXmlComLib20.DefaultXmlSerializationContext.SchemaType = LtXmlComLib20.SchemaType_XSD ' LtXmlComLib20.DefaultXmlSerializationContext.DefaultNamespaceURI = "http://www.fpml.org/2003/FpML-4-0" LtXmlComLib20.DefaultXmlSerializationContext.NamespaceAliases.Add "http://www.w3.org/2001/XMLSchema-instance", "xs" LtXmlComLib20.DefaultXmlSerializationContext.NamespaceAliases.Add "http://www.code-generator.com/nstest", "cg" LtXmlComLib20.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 | |
Hand Coded Blocks |