zeux/pugixml

Not able to attach a xsl via a `<?xml-stylesheet ...?>` node

Closed this issue · 1 comments

When trying to add a xsl reference to a xml document using the below code, it does not record the attributes.

xml_document doc;

xml_node decl = doc.append_child(pugi::node_declaration);
decl.append_attribute("version").set_value("1.0");
decl.append_attribute("encoding").set_value("utf-8");

xml_node xsl = doc.append_child(pugi::node_pi);
xsl.set_name("xml-stylesheet");
xsl.append_attribute("href").set_value("feed.xsl");
xsl.append_attribute("type").set_value("text/xsl");

xml_node feed = doc.append_child("feed");
feed.append_attribute("xmlns").set_value("http://www.w3.org/2005/Atom");

doc.print(std::cout, "\t", pugi::encoding_utf8);

Expected result:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/feed.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom">
</feed>

Actual Result:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet?>
<feed xmlns="http://www.w3.org/2005/Atom">
</feed>
zeux commented

Please refer to https://pugixml.org/docs/manual.html#node_pi; node_pi nodes have values but not attributes, because XML does not restrict the PI node contents to attribute-like format (eg <?this is completely valid XML unfortunately?>).

So you'd need something like xsl.set_value("href=\"/feed.xsl\" type=\"text/xsl\"");