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
not repeat the namespace attributes in each element).
This is an example output (this code is basically taken from two
xerces sample applications):
|<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<company xmlns="http://default.namespace.org/NS"
xmlns:otherNS="http://other.namespace.org/ONS">
<otherNS:product xmlns="">Xerces-C</otherNS:product>
<otherNS:category xmlns:otherNS="http://other.namespace.org/ONS"
idea="great">XML Parsing Tools</otherNS:category>
<developedBy xmlns="">Apache Software Foundation</developedBy>
</company>|
And this is the code:
| DOMDocument* doc= impl->createDocument(
X("http://default.namespace.org/NS"),
X("company"), 0);
DOMElement* rootElem= doc->getDocumentElement();
Replace:
rootElem->setAttribute(
X("xmlns:otherNS"),
X("http://other.namespace.org/ONS"));
with:
rootElem->setAttributeNS(X("http://www.w3.org/2000/xmlns/"),
X("xmlns:otherNS"),
X("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"),
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