1

I've been working on a XML reader/writer, and I used Boost's property trees to do so.

Everything is working, only one thing is missing in the output file: I'd like to add two header tags at the top of the file. Right now, the only header is this one, automatically written by Boost's write_xml() function:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

However, I'd like to add these two below the one already there:

<!-- Custom stylesheet -->
<?xml-stylesheet type="text/xsl" href="browser_view.xslt"?>
<!-- Authentic View -->
<?xmlspysps authentic_view.sps?>

Does anyone know how I could do that without editing the file after generating it with Boost?

Nepho
  • 1,074
  • 1
  • 11
  • 32
  • The word is "processing instruction". And I'm pretty sure you can't (why would they implement that? There is no Boost Xml library after all) – sehe Dec 15 '14 at 13:52
  • @sehe Well, there is a write_xml() function, so why would it not be possible? – Nepho Dec 15 '14 at 13:53
  • Because it doesn't write xml! It write a property tree. (duh) (It happens to write in a format that's a subset of XML, but in no way does that suggest other XML features must be present) – sehe Dec 15 '14 at 13:54
  • It writes a property tree, but in a xml way. I don't see what you're trying to prove, I'm pretty sure there is a way to do what I want, I just didn't find it in the documentation. – Nepho Dec 15 '14 at 13:55
  • good luck then. Why did you ask, again? – sehe Dec 15 '14 at 13:55
  • Why did I ask? To have a real answer. You only said "I'm pretty sure you can't", so I'll just wait for someone who knows for sure. But thanks for your input! – Nepho Dec 15 '14 at 13:56
  • 1
    You were actually right, @sehe. With your keyword I found a thread saying it can't be done, but offering a solution. – Nepho Dec 15 '14 at 13:59
  • It can't be done directly, but a solution can be found [here][1]. It was supposed to be the answer but SO treats it as a "trivial comment"... Oh well. [1]: http://stackoverflow.com/questions/12805633/add-xml-stylesheet-processing-instructions-to-boost-property-tree – Nepho Dec 15 '14 at 14:00
  • I've marked it a duplicate of that question. Do note that `write_xml_element` is an undocumented implementation detail. Though it was what I was looking for myself – sehe Dec 15 '14 at 14:24

1 Answers1

3

The word is "processing instruction". And I'm pretty sure you can't (why would they implement that? There is no Boost Xml library after all).

After double checking the xml_writer_settings there is indeed nothing that controls the printing of the processing instructions (otherwise you could ave suppressed them and instead printed the whole preamble yourself).

Here's my take on it with PugiXML:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

#include <pugixml.hpp>

int main() {

    std::stringstream ss;

    {
        boost::property_tree::ptree pt;
        pt.add("demo", "bla");
        boost::property_tree::xml_parser::write_xml(ss, pt);
    }

    {
        pugi::xml_document doc;
        doc.load(ss);

        auto pi = doc.prepend_child(pugi::xml_node_type::node_pi);
        pi.set_name("xmlspysps");
        pi.set_value("authentic_view.sps");

        pi = doc.prepend_child(pugi::xml_node_type::node_pi);
        pi.set_name("xml-stylesheet");
        pi.set_value("type=\"text/xsl\" href=\"browser_view.xslt\"");

        doc.save_file("test.xml");
    }
}

Saves:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="browser_view.xslt"?>
<?xmlspysps authentic_view.sps?>
<demo>bla</demo>

Of course that's horribly inefficient if you really want to just serialize a ptree - but apparently you're not just serializing. You're marking up, for which you need a markup-library, preferrably an XML capable one.

sehe
  • 374,641
  • 47
  • 450
  • 633