[xsd-users] How can I add an xsl reference before serializing

Boris Kolpackov boris at codesynthesis.com
Fri Mar 15 13:48:24 EDT 2013


Hi Roger,

Roger Evans <roger at autodata.no> writes:

> Is there any way with C++/tree to add a (variable) stylesheet reference
> in the serialization? 
> 
> I'm generating hundreds of xml invoices and need to give them 
> different stylesheet references.
>   Specifically, I want to insert
>   <?xml-stylesheet type="text/xsl"
> href="http://faktark2/ehf/999-Invoice.xsl" ?>
> 
> just after the <?xml ...> line in the generated document where the '999'
> can vary.

You will need to first serialize the object model to a DOM document,
then add the processing instruction (that's what that thing is called),
and then serialize the DOM document to XML. Note that you will also
need to initialize and terminate the Xerces-C++ runtime yourself.

To serialize the object model to the DOM document you can use the
appropriate serialization function:

root_type& r = ... // Root of the object model.

xml_schema::namespace_infomap map; // Initialize the map as required.

xml_schema::dom::auto_ptr<xercesc::DOMDocument> doc (root (r, m));

To add the processing instruction, you can do this (you can use
std::ostringstream to create the appropriate data string):

#include <xsd/cxx/xml/string.hxx>

std::string data (
  "type=\"text/xsl\" href=\"http://faktark2/ehf/999-Invoice.xsl\"");

xercesc::DOMProcessingInstruction* pi (
  doc->createProcessingInstruction (
    xsd::cxx::xml::string ("xml-stylesheet").c_str (),
    xsd::cxx::xml::string (data).c_str ()));

doc->insertBefore (pi, doc->getDocumentElement ());

The C++/Tree Mapping FAQ[1] has more information on how to save the
DOM document to XML (Q3.2; you can also find the same function in
the 'messaging' example in dom-serialize.?xx).

[1] http://wiki.codesynthesis.com/Tree/FAQ

Boris



More information about the xsd-users mailing list