KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Creating XML with Multiple Namespaces and Attributes
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: June 24, 2025

When generating XML in 4D with multiple namespaces, developers may face challenges using the DOM Create XML Ref command, especially when adding attributes like xsi:schemaLocation. Consider the goal of creating an XML structure with multiple namespaces and a schema location attribute, such as:

<xyz:DataTransmission
    xmlns:xyz="urn:example:org:xyz"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:example:org:xyz schemas/DataTransmissionSchema.xsd"/>


A common error is including full attribute syntax (e.g., xmlns:xyz or xsi:schemaLocation) in the namespace name parameters of DOM Create XML Ref, causing the command to fail and return 0 for the reference and OK.
The DOM Create XML Ref command requires namespace prefixes (e.g., xyz, xsi) for the $aNSName parameters, not full attribute declarations. Attributes like xsi:schemaLocation must be set using DOM SET XML ATTRIBUTE after creating the root element. Below is a general example of the correct 4D code:


    var $vRootRef : Text
    var $aNSName1; $aNSName2; $aNSValue1;$aNSValue2 :Text

    // Define the root element and its namespace
    $Root:="xyz:DataTransmission"
    $Namespace:="urn:example:org:xyz"

    // Define namespace prefixes and URIs
    $aNSName1:="xyz"
    $aNSValue1:="urn:example:org:xyz"
    $aNSName2:="xsi"
    $aNSValue2:="http://www.w3.org/2001/XMLSchema-instance"

    // Create the XML root element with namespaces

    $vRootRef:=DOM Create XML Ref($Root; $Namespace; $aNSName1; $aNSValue1; $aNSName2; $aNSValue2)

    // Add the xsi:schemaLocation attribute
    If (OK=1)
        DOM SET XML ATTRIBUTE($vRootRef; "xsi:schemaLocation"; "urn:example:org:xyz schemas/DataTransmissionSchema.xsd")
    End if


  • Namespace Prefixes: The $aNSName parameters (e.g., $aNSName1, $aNSName2) must contain only the namespace prefix (xyz, xsi), not xmlns:xyz or the element name.

  • Attributes vs. Namespaces: The xsi:schemaLocation is an attribute in the xsi namespace, not a namespace declaration. Use DOM SET XML ATTRIBUTE to add it after creating the root element.

  • Namespace URI: The $Namespace parameter binds the root element’s prefix (xyz) to its URI. Including it in both $Namespace and $aNSName1/$aNSValue1 ensures compatibility with the command.

  • The xsi:schemaLocation attribute pairs a namespace URI (e.g., urn:example:org:xyz) with a schema file path (e.g., schemas/DataTransmissionSchema.xsd). This informs XML validators where to find the schema for the namespace. Ensure the file path is correct relative to your XML file’s location or use a URL for broader compatibility.