I write an xml file with xerces-c 3.2.1 which looks like
<?xml version="1.0" encoding="UTF-16" standalone="yes" ?>
<Test xmlns="my_namespace"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="my_namespace myschema.xsd">
<Elem />
</Test>
with my own default namespace and the xml and xsi namespaces declared.
In my namespace, I have an attribute called dim which needs a namespace declaration, otherwise it would get mixed up with pre-existing xml:dim.
When I set this attribute with
elem->setAttributeNS("my_namespace", "myprefix:dim", data);
then my xml file looks like
<?xml version="1.0" encoding="UTF-16" standalone="yes" ?>
<Test xmlns="my_namespace"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="my_namespace myschema.xsd">
<Elem xmlns:myprefix="my_namespace" myprefix:dim="..."/>
</Test>
with the namespace declaration at each and every element that uses dim attribute I write, which is bad, because for filesize reasons, I would like to have xerces-c writing files like
(the golden one)
<Test xmlns="my_namespace"
xmlns:myprefix="my_namespace"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="my_namespace myschema.xsd">
<Elem myprefix:dim="..."/>
</Test>
with the namespace prefix declaration just at the root node. But if I add such an entry to the root node using root->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:myprefix", "my_namespace");
then xerces-c produces xml files like
<Test xmlns="my_namespace"
xmlns:myprefix="my_namespace"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="my_namespace myschema.xsd">
<myprefix:Elem1 ../>
<myprefix:Elem2 ../>
<Elem myprefix:dim="..."/>
</Test>
prefixing all other elements myprefix:elem1, myprefix:elem2, except the one with my dim, which is a pain in the neck...
How can I force xerces-c to my will to write a minimum amount of namespace declarations and prefixes like in the golden one ??
Finally the trick to get the golden one is:
to add the prefix to the element name (or attribute name) e.g. L"my_prefix:Elem" (additionally to the namespace)
DOMElement * e4 = doc->createElementNS(defaultNS, (const XMLCh*)L"my_prefix:Elem");
root->appendChild(e4);
This saves the space in the resulting XML file, but requires more space in the code producing the XML :( and requires even more code as the prefix should not be hardcoded..