Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xml namespace to xml_document #354

Closed
nabulke opened this issue May 15, 2020 · 5 comments
Closed

Add xml namespace to xml_document #354

nabulke opened this issue May 15, 2020 · 5 comments
Labels

Comments

@nabulke
Copy link

nabulke commented May 15, 2020

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 save the xml_document to a string stream and need that xml namespace declaration included.

@zeux
Copy link
Owner

zeux commented May 15, 2020

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

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

@zeux zeux added the question label May 15, 2020
@nabulke
Copy link
Author

nabulke commented May 16, 2020

Thanks for your reply.
I added the code as you suggested. I use the following code to serialize the xml_document to a string:

std::string Serialize(const xml_document& doc)
{
std::stringstream ss;
doc.save(ss, "", format_raw, encoding_auto);
return ss.str();
}

Unfortunately the xml namespace declaration does not appear in the serialized string.

<?xml version="1.0" encoding="ISO-8859-1"?><rootnode xsi:type="result" version="1">....

Any idea what is still missing?

@zeux
Copy link
Owner

zeux commented May 16, 2020

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

@nabulke
Copy link
Author

nabulke commented May 16, 2020

I changed my code and as per your suggestion, and now it works perfectly.

Thanks a lot for your help, I appreciate it.

@nabulke nabulke closed this as completed May 16, 2020
@nabulke
Copy link
Author

nabulke commented May 16, 2020

I quoted your answer over in stackoverflow.com, where I asked a similar question. Hope that is ok for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants