Add xml-stylesheet processing instructions to boost property_tree

左心房为你撑大大i 提交于 2019-11-26 22:10:46

问题


I am using boost/property_tree to create an XML file. Unfortunately I cannot figure out how to add xml-stylesheet processing instructions to the file.

Desirable output:

<?xml version="1.0" encoding="utf-8"?> <-- This is added automatically
<?xml-stylesheet type="text/xsl" href="report.xsl"?> <-- How to add this line
<report>
...
</report>

Is that possible with boost/property_tree/ptree?


回答1:


It appears that boost/property_tree xml writer doesn't have a support for xml stylesheets processing instructions. First line (xml version) is simply hardcoded in the write_xml_internal function.

So I've just written my own write xml function, which is doing exactly the same, plus adds xml stylesheet.

void WriteXML(std::ostream &output, ptree &root)
{
 boost::property_tree::xml_writer_settings<char> settings('\t', 1);

 output << "<?xml version=\"1.0\" encoding=\"";
 output << settings.encoding;
 output << "\"?>\n";
 output << "<?xml-stylesheet type=\"text/xsl\" href=\"report.xsl\"?>\n";

 write_xml_element(output, std::basic_string<ptree::key_type::value_type>(), root, -1, settings);
}



回答2:


My hack is to use the writer settings:

boost::property_tree::xml_writer_settings<char> settings('\t', 1, "utf-8\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"report.xsl");
boost::property_tree::write_xml(yourAbsolutePath, yourPropertyTree, std::locale(), settings);

Result:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="report.xsl"?>


来源:https://stackoverflow.com/questions/12805633/add-xml-stylesheet-processing-instructions-to-boost-property-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!