c++pugixml

Add xml namespace to xml_document using pugixml


How can I add an xml namespace declaration to my xml_document using pugixml?

I tried this, which results in a invalid xml (invalid char ":", says my validator):

xml_document doc;

auto declarationNode = doc.append_child(node_declaration);

declarationNode.append_attribute("xmlns\:xsi") = "http://www.w3.org/2001/XMLSchema-instance";

I guess a namespace declaration is not the same as an xml attribute.

But how can I add that namespace declaration?


Solution

  • I'm quoting zeux, the creator of pugixml, who was so kind to answer this question here on github

    This code appends the xmlns attribute to the node; you should append it to the document element instead:

    doc.document_element().append_attribute("xmlns:xsi") = "http://www.w3.org/2001/XMLSchema-instance";
    

    The code I noted adds the attribute to “rootnode” element and as such must be ran after you add rootnode to the document.