[xsd-users] Link to stylesheet in xml output

Boris Kolpackov boris at codesynthesis.com
Sun Apr 22 15:40:39 EDT 2007


Hi Bao,

Bao Young <bao.young at gmail.com> writes:

> Is that a way for XSD to include a link to a stylesheet in the output xml
> file?
>
> <?xml-stylesheet type="text/xsl" href="simple.xsl"?>

XSD can't do it but you can do it yourself by (1) serializing the tree
representation to DOM (using the corresponding serialization function),
(2) adding the processing instruction (that's what that thing above is
called), and (3) serializing the DOM document to XML.

Here is an example code that does this:

using namespace xercesc;
namespace xml = xsd::cxx::xml;

XMLPlatformUtils::Initialize ();

{
  foo& f = ... // f is the root of the tree representation

  // (1)
  //
  xml_schema::namespace_infomap map;
  xml::dom::auto_ptr<DOMDocument> doc = bar (f, map);

  // (2)
  //
  DOMElement* root = doc->getDocumentElement();

  DOMProcessingInstruction* pi = doc->createProcessingInstruction (
    xml::string ("xml-stylesheet").c_str (),
    xml::string ("type=\"text/xsl\" href=\"simple.xsl\"").c_str ());

  doc->insertBefore(pi, root);

  // (3)
  //
  std::ofstream ofs ("out.xml");
  serialize (ofs, *doc);
}

XMLPlatformUtils::Terminate ();


The serialize() function is taken from the FAQ entry 3.2:

http://wiki.codesynthesis.com/Tree/FAQ


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20070422/8be1d92b/attachment.pgp


More information about the xsd-users mailing list