Hi Jeroen,
it is working, thank you very much for the solution!

I think I understand it a bit better now: the namespace attribute in the root 
element are themselves in
the xmlns namespace, so it is necessary to use a *NS function to put them into 
that namespace.
Kind regards,
Dieter
Am Di., Nov. 10, 2015 18:33 schrieb Jeroen N. Witmond :
See my comment below.

On 2015-11-09 10:58, Schoen, Dieter wrote:
hi list,

I need to create XML files which contain multiple namespaces. I create
the root element with a default namespace,
and add another namespace ("otherNS") with setAttribute().

The problem is, that when i insert an element (with createElement())
which is prefixed with "otherNS",
xerces adds an empty namespace attribute. when I use createElementNS()
and explicitly state the otherNS URI,
xerces adds the full URI attribute. In my understanding of XML
namespaces, both is wrong.
(Also the examples inhttp://www.w3schools.com/Xml/xml_namespaces.aspdo 
(http://www.w3schools.com/Xml/xml_namespaces.aspdo)
not repeat the namespace attributes in each element).

This is an example output (this code is basically taken from two
xerces sample applications):

|

Xerces-C
XML Parsing Tools
Apache Software Foundation
|

And this is the code:

|        DOMDocument*  doc=  impl->createDocument(
X("http://default.namespace.org/NS"; (http://default.namespace.org/NS)),
X("company"), 0);
DOMElement*  rootElem=  doc->getDocumentElement();

Replace:

rootElem->setAttribute(
X("xmlns:otherNS"),
X("http://other.namespace.org/ONS";)); (http://other.namespace.org/ONS)

with:

rootElem->setAttributeNS(X("http://www.w3.org/2000/xmlns/"; 
(http://www.w3.org/2000/xmlns/)),
X("xmlns:otherNS"),
X("http://other.namespace.org/ONS";)); (http://other.namespace.org/ONS)

DOMElement*   prodElem= 
doc->createElement(X("otherNS:product"));
rootElem->appendChild(prodElem);

DOMText*     prodDataVal= doc->createTextNode(X("Xerces-C"));
prodElem->appendChild(prodDataVal);

DOMElement*   catElem=  doc->createElementNS(
X("http://other.namespace.org/ONS"; (http://other.namespace.org/ONS)),
X("otherNS:category"));
rootElem->appendChild(catElem);|

My questions are:

1. Is my usage of the Xerces API correct? Do I maybe have to add the
second namespace differently, it seems that xerces does not
recognize it.
2. Are there maybe any features of the DOMLSSerializer class, which
change that behaviour, so far I have not found any.

kind regards,
dieter

Reply via email to