Use namespaces
- Last Updated: January 17, 2024
- 1 minute read
- OpenEdge
- Version 12.8
- Documentation
There are three kinds of namespaces:
- A local namespace, which is one that is declared in that tag
- An inherited namespace, which is one specified in an ancestor tag; for example, a namespace declared by the root node can be used in the entire document
- The default namespace, which is an unspecified namespace (no prefix provided)
There are two ways for a namespace to be created when writing
an XML document. They can be created implicitly when starting an
element or they can be declared explicitly after a tag has been
started. Implicit namespaces are created with the methods START-ELEMENT, WRITE-EMPTY-ELEMENT,
and WRITE-DATA-ELEMENT. Explicit namespaces are
created with the methods DECLARE-NAMESPACE or INSERT-ATTRIBUTE.
The following table describes these methods.
| Method call example and resulting tag | Use case explanation |
|---|---|
|
Case: You supply an element name, but no namespace prefix and no namespace URI.Result: The element is written without a prefix and the default namespace is used. |
|
Case: You supply an element name with a namespace prefix, but no namespace URI. The supplied prefix has been previously associated with a namespace URI.Result: The element is written with the supplied prefix and the previously associated namespace URI is used. |
|
Case: You supply an element name with a namespace prefix, but no namespace URI. The supplied prefix has not been previously associated with a namespace URI. The STRICT attribute is set to TRUE.Result: The call generates an error. Only the default namespace can be set to an empty string (""). |
|
Case: You supply an element name with a namespace
prefix, but no namespace URI. The supplied prefix has not
been previously associated with a namespace URI. The STRICT attribute
is set to FALSE.Result: The element is written with the
supplied prefix and an empty namespace is specified. |
|
Case: You supply an element name without a namespace prefix. You supply a namespaceUri that has been previously associated with a namespace prefix.Result: The element is written using the previously associated prefix and the declared namespace URI is used. |
|
Case : You supply an element name without a namespace prefix. You supply a namespaceUri that has not been previously associated with a namespace prefix.Result: The element is written without a prefix and the namespaceUri is set to the namespace URI associated with the default namespace. |
|
Case: You supply an element name with a namespace prefix and a namespaceUri that has been previously associated with the supplied prefix.Result: The element is written with the supplied prefix and name and the previously associated namespace URI is used. Since the namespace matching the supplied prefix and URI pair has already been declared, it will not be redeclared. |
|
Case: You supply an element name with a namespace prefix and a namespaceUri. Either one or both of the namespace prefix and the namespace URI has already been used in a previous declaration, but the pair have not been declared together.Result: The element is written with the supplied prefix and name and namespaceUri declared. This amounts to the declaration of a new namespace. |
|
Case: You supply an element name with a namespace
prefix and a namespaceUri.
The namespace prefix matches the prefix used in the element, but the URIs do
not match. The STRICT attribute is set to
TRUE.Result: The method call generates an error message.
Within an element, namespaces are like attributes and must be
unique.Exception: In the case where the element's URI is the empty
string, the tag will be written with the supplied
namespaceUri. |
|
Case: You supply an element name with a namespace
prefix and a namespaceUri.
The namespace prefix matches the prefix used in the element, but the URIs do
not match. The STRICT attribute is set to
FALSE.Result: The tag is written, but it is not valid
XML. |
|
Case: You supply a new prefix and new namespaceUri that do not overlap with those declared in the element.Result: Creates the expected namespace without error. |
|
Case: The provided namespaceUri matches the namespace URI declared in the element and prefix matches the namespace prefix declared in the element.Result: Re-declaring an implicitly created namespace (the namespace declared in the element) does no harm and does not generate an error message. |
|
Case: namespaceUri is provided and the namespace URI declared in the element is the empty string (""), and the provided prefix matches the namespace prefix declared in the element.Result: The namespace is created without error. In other words, if you are going to explicitly create your namespaces, you do not need to provide the namespace URI when you create the element. |
|
Case:
namespaceUri is provided but it does not match the namespace URI
declared in the element and the supplied prefix does match the
namespace prefix declared in the element. The STRICT attribute is set
to TRUE.Result: The method call generates an error. Within
an element, namespaces are like attributes and must be unique. |
|
Case: You provide only a namespaceUri and an empty string ("") as the namespace prefix.Result: You create the default namespace. |
|
Case: The element has declared a default namespace with a different namespace
URI than the one you provide with namespaceUri.
The STRICT attribute is set to
TRUE.Result: The method call generates an error. Within
an element, namespaces are like attributes and must be unique. |
|
Case: The element has declared a default namespace with a different namespace
URI than the one you provide with namespaceUri.
The STRICT attribute is set to
FALSE.Result: The tag is written, but it is not valid
XML. |
|
Case: You provide an empty string ("") as the namespace URI and supply the
prefix. The STRICT attribute is set to
TRUE.Result: The method call generates an error. Only
the default namespace can be declared as the empty string (""). |
|
Case: You provide an empty string ("") as the namespace URI and supply a
prefix that matches the namespace prefix declared in the
element. The STRICT attribute is set to
FALSE.Result: This can be useful when not running in
STRICT mode to reset the element namespace prefix to the empty string (""). |
|
Case: You provide an empty string ("") as the namespace URI and supply a
prefix. The STRICT attribute is set to
FALSE.Result: The tag is written, but it is not valid
XML. |
|
Case: You provide an empty string ("") as the namespace URI and as the namespace prefix.Result: You create the default namespace as empty. |
|
Case: The element has declared a specific default namespace and you provide
namespaceUri, which is the empty string (""). The
STRICT attribute is set to TRUE.Result:
The method call generates an error. Within an element, namespaces are like attributes
and must be unique. |
|
Case: The element has declared a specific default namespace and you provide
namespaceUri, which is the empty string (""). The
STRICT attribute is set to
FALSE.Result: The tag is written, but it is not valid
XML. |