XMLNamespace
Namespaces are a vital part of the XML standard. They prevent name clashes, so that you can mix elements from different sources even if they have the same name. You must only ensure that they belong to different namespaces.
In BI#, you can use the XMLNamespace
data type to manage XML document namespaces. This example process creates a single document that defines two different namespaces:
#define EngineVersion 5.0
#define RuntimeVersion 5.0
void XmlNamespaceDemo()
{
string exampleComURL = "http://www.example.com";
string inforComURL = "http://www.infor.com";
XMLNamespace ec = XMLCreateNamespace(exampleComURL);
XMLNamespace ic = XMLCreateNamespace(inforComURL);
XMLElement root = XMLCreateElement(ec, "Root");
XMLSetAttributeValue(root, XMLCreateXmlnsNamespace(), "ec", exampleComURL);
XMLSetAttributeValue(root, XMLCreateXmlnsNamespace(), "ic", inforComURL);
XMLElement child = XMLCreateElement(ic, "Child");
XMLAddElement(root, child);
XMLElement differentChild = XMLCreateElement(ec, "AnotherChild");
XMLSetContent(differentChild, "Other content.");
XMLAddElement(child, differentChild);
XMLElement child2 = XMLCreateElement(ec, "Child2");
XMLSetContent(child2, "Content of child #2.");
XMLAddElement(root, child2);
XMLElement child3 = XMLCreateElement(ic, "Child3");
XMLSetContent(child3, "Content of child #2.");
XMLAddElement(root, child3);
WriteLine(ToString(root));
}
The document created by the example process looks like this:
<ec:Root xmlns:ec="http://www.example.com" xmlns:ic="http://www.infor.com">
<ic:Child>
<ec:AnotherChild>Other content.</ec:AnotherChild>
</ic:Child>
<ec:Child2>Content of child #2.</ec:Child2>
<ic:Child3>Content of child #2.</ic:Child3>
</ec:Root>
XMLNamespace
objects, you can use the XMLCreateNamespace
function. It only expects the namespace URL. BI# also
provides two functions named XMLCreateXmlNamespace
and
XMLCreateXmlnsNamespace
to create the standard xml
and xmlns
namespaces. After you have created an XMLNamespace
, you can
use it in all the functions to create XML nodes such as XMLCreateElement
, XMLCreateAttribute
, etc. That
is, you can use them in all places that specify the name of an element or an attribute. The
same is true for retrieving elements and attributes.