使用 pugixml 将 xml 命名空间添加到xml_document

Add xml namespace to xml_document using pugixml

本文关键字:xml document 添加 命名空间 pugixml 使用      更新时间:2023-10-16

如何使用pugixml将xml namespace声明添加到我的xml_document中?

我试过这个,这导致了无效的 xml(无效的字符":",我的验证器说(:

xml_document doc;
auto declarationNode = doc.append_child(node_declaration);
declarationNode.append_attribute("xmlns:xsi") = "http://www.w3.org/2001/XMLSchema-instance";

我想命名空间声明与 xml 属性不同。

但是如何添加该命名空间声明呢?

我引用了 pugixml 的创建者zeux,他非常友好地在 github 上回答了这个问题。

此代码将 xmlns 属性追加到节点;您应该 改为将其附加到文档元素:

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

我注意到的代码将属性添加到"rootnode"元素,因此 必须在将根节点添加到文档后运行。