Namespaces in XML

Namespaces in XML can be defined as attributes of a node. You define the namespace and the prefix to use for it. If your document contains namespaces and you want to execute X-Path Queries where you want to find nodes that are in specific namespaces, you have to tell the parser which namespaces you want to use and which prefixes to use for this namespaces. XmlSetNamespaces allows to set the namespaces that should be recognized by the parser. The definition is like the definition in the XML document. xmlns:prefix=namespace. Multiple definitions can be separated by blank. See the following document:

<root xmlns:ns1='MyName1' xmlns:ns2='MyName2'>
<ns1:child1>value1</ns1:child1>
<ns2:child2>value2</ns2:child2>
</root>

If you want to execute a query like /root/child1 you have to define child1 in a namespace with a namespace prefix of your choice (doesn't have to be the same prefix like in the source document), and you have to tell the compiler which namespace should be assigned to that prefix. The table below describes the different possibilities to execute the same query:

X-Path SetNamespaces
/root/ns1:child1 xmlns:ns1='MyName1'
/root/myprefix:child1 xmlns:myprefix='MyName1'
dcltrans
transaction TMain
var
  hDoc, hChild : number;
  sValue, sXML : string;
begin
  hDoc   := XmlCreateDocumentFromXml("<root xmlns:ns1='MyName1'><ns1:child>value1</ns1:child></root>");
  XmlSetNamespaces(hDoc, "xmlns:ns1='MyName1'");
  hChild := XmlSelectSingleNode("/root/ns1:child");
  ...
  XmlFreeHandle(hChild);
  XmlFreeHandle(hDoc);
end TMain;