Liquid XML Data Binder (C++, Java, VB6) / Using the Code / Working with Namespaces / How Do I Setup a Default Namespace
In This Topic
    How Do I Setup a Default Namespace
    In This Topic

    Problem

    If an XSD defines namespaces, then typically elements and attributes within the XML documents must be qualified with a namespace. This can cause every element and attribute in the document to be qualified, which is verbose, and sometimes inconvenience.

    Resolution

    By setting a default namespace, you don't need to explicitly qualify elements and attributes that are part members of the default namespace.

    Warning : Declaring a default namespace in some XML Documents, may cause the XML created to be invalid.

    Sample XSD

    <?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="http://sample" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://sample" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="Customer" type="CustomerType"> <xs:annotation> <xs:documentation>Comment describing your root element</xs:documentation> </xs:annotation> </xs:element> <xs:complexType name="CustomerType"> <xs:sequence> <xs:element name="FirstName" type="xs:string"/> <xs:element name="MiddleInitial" type="xs:string"/> <xs:element ref="LastName"/> </xs:sequence> <xs:attribute name="clubCardMember" type="xs:boolean"/> </xs:complexType> <xs:element name="LastName" type="xs:string"/> </xs:schema>

    Sample Output From the Generated Libraries

    <?xml version="1.0"?> <AA:Customer xmlns:AA="http://sample" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" clubCardMember="false"> <AA:FirstName>Joe</AA:FirstName> <AA:MiddleInitial>J</AA:MiddleInitial> <AA:LastName>Bloggs</AA:LastName> </AA:Customer>

    As you can see the document is verbosely qualified using the alias ‘AA’. Although this alias can be changed (See Changing Namespace Aliases), it can be removed entirely by adding a default namespace declaration to the start of the file.

    <?xml version="1.0"?> <Customer xmlns="http://sample" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" clubCardMember="false"> <FirstName>Joe</FirstName> <MiddleInitial>J</MiddleInitial> <LastName>Bloggs</LastName> </Customer>

    The namespaces are controlled via the SerializationContext object.

    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() { 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://sample")); // ##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.SerializationContext.Default.SchemaType = LiquidTechnologies.Runtime.Net40.SchemaType.XSD; LiquidTechnologies.Runtime.Net40.SerializationContext.Default.DefaultNamespaceURI = "http://sample"; // ##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.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://sample"); // ##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 classs 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://sample" ' ##HAND_CODED_BLOCK_END ID="Default Namespace Declarations"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS End If Set CF = m_ClassFactory End Function

     

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

    Description Value
    Article Created 7/2/2006
    Versions Liquid XML 2005 (4.1.0) and greater
    Also See Changing Namespace Aliases
      Working In a multithreaded environment
      Working With Multiple Schemas
      Hand Coded Blocks