[xsd-users] Re: Adding namspaces

Boris Kolpackov boris at codesynthesis.com
Thu Jul 13 10:55:53 EDT 2006


Hi Matt,

Matt Burnham <mwburn at mhpcc.hpc.mil> writes:

>    I understand the concepts of this, but can't get the implementation to
>    work.  I've attached a test case that has an "event" schema and "embedded"
>    sub-schema.

While we only help with your application code if you have a priority
support contract with us, I guess we can make an exception ;-). I've
fixed your example to print:

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

  <stuff attr="test"/>

  <detail>
    <embedded attr1="X" attr2="Y"/>
  </detail>

</event>


Two things you need to do to get it working:


(1) Recompile embedded.xsd with

  --root-element embedded

  instead of

  --root-element-none

  because we need serialization functions for the embedded element.


(2) Here is the modified createXML function:


void createXML()
{
  foo::type event_type( "embedded" );
  foo::stuff my_stuff( "test" );
  foo::event my_event( my_stuff, event_type );

  my_event.detail (foo::detail ()); // create empty detail element

  foo::embedded::attr1::type a1( "X" );
  foo::embedded::attr2::type a2( "Y" );
  foo::embedded my_embedded( a1, a2 );

  std::cerr << "event: " << my_event << std::endl;
  std::cerr << std::endl;
  std::cerr << "embedded: " << my_embedded << std::endl;

  std::ostringstream buff;

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

  XMLPlatformUtils::Initialize ();
  {
    // (1) serialize event and embedded into two separate DOM documents

    xml_schema::namespace_infomap nsm;

    dom::auto_ptr<DOMDocument> event_doc(
      foo::event_(my_event, nsm) );

    dom::auto_ptr<DOMDocument> embedded_doc(
      foo::embedded_(my_embedded, nsm) );


    // (2) copy the embedded sub-tree to the event document

    DOMElement* ed_el(
      embedded_doc->getDocumentElement() ); // 'embedded' element

    DOMNode* ed_el_copy (event_doc->importNode( ed_el, true));

    // Now we need to insert the embedded element (ed_el_copy) in
    // the right place in the event document.

    DOMElement* event_el(
      event_doc->getDocumentElement() );  // 'event' element

    DOMNodeList* nl(
      event_el->getElementsByTagName(xml::string ("detail").c_str ()));
    DOMElement* detail_el( static_cast<DOMElement*>(nl->item (0)) );

    detail_el->appendChild (ed_el_copy); // insert 'embedded' element


    // (3) write the resulting DOM document to XML

    DOMImplementation* impl(
      DOMImplementationRegistry::getDOMImplementation(
        xml::string("LS").c_str()) );

    std::auto_ptr<DOMWriter> writer( impl->createDOMWriter() );


    if( writer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent, true) )
    {
      writer->setFeature( XMLUni::fgDOMWRTDiscardDefaultContent, true );
    }

    if( writer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true) )
    {
      writer->setFeature( XMLUni::fgDOMWRTFormatPrettyPrint, true );
    }

    dom::ostream_format_target ft (buff);
    writer->writeNode (&ft, *event_doc);
  }

  XMLPlatformUtils::Terminate ();

  std::cerr << std::endl;
  std::cerr << buff.str() << std::endl;
}

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/20060713/53a75ffb/attachment.pgp


More information about the xsd-users mailing list