[xml] How to add a Namespace?



Hi,

I have a question about adding a namespace to a node. I can't figure out the correct way to do it.

My code looks like this:

xmlDocPtr SOAPResponse;
xmlNsPtr curns;
  
SOAPResponse = xmlNewDoc("1.0");
// make child node
SOAPResponse->children = xmlNewNode (NULL, "Envelope");
// create namespace on SOAPResponse->children
curns = xmlNewNs (SOAPResponse->children, "http://schemas.xmlsoap.org/soap/envelope/";, "SOAP-ENV");
// add namespace (but unsure what it does)
xmlSetNs (SOAPResponse->children, curns);
    
My question now has to do with the last line. As far as I can figure out, it is not required; in the xmlNewNs 
()-call, I already specify that I want a namespace to be attached to the same node (SOAPResponse->children). 
In fact, if I leave out the xmlSetNs()-call, something seems to go right, since xmlDocDump() reports this:
<Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/";>

But I would have expected xmlDocDump() to report this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/";> 

I would have expected this kind of behaviour, because in my xmlNewNs()-call, I specified the node to attach 
to. As far as I can see in the documentation, source code and even a gdb-run, xmlSetNs () is completely 
redundant; but it isn't since WITH the xmlSetNs ()-call, everything goes as expected, and I get the (desired) 
output with the SOAP-ENV:-prefix on the Envelope-element.

So my question is: what is it I don't understand? Why is there such a difference in behaviour? What is 
xmlSetNs doing that xmlNewNs doesn't do?

Kind regards,

Roelant.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]