[xml] namespaces in xpath doc fragments



Hi all,

I'm new to libXml, and have tried to solve this problem for several days by
looking in the archives, docs, etc., but to no avail. Sorry to bother you
all with it if I have just overlooked something simple.
If I have a document of the form (using ANSI strings)
 
<a xmlns='http://space1' xmlns:two='http://space2'>
  <b>
    <two:c>fooOne<two:c>
    <two:c>fooTwo<two:c>
    <two:c>fooThree<two:c>
  </b>
  <b>
    <two:c>barOne<two:c>
    <two:c>barTwo<two:c>
    <two:c>barThree<two:c>
  </b>
</a>

and I extract the <b> nodes using xpath, as follows (no error-checking,
cleanup, etc. shown):

SomeMethod( string xml )
{
  xmlDocPtr doc;
  xmlXPathContextPtr xpathCtx;
  xmlXPathObjectPtr nodes;

  doc = xmlParseMemory( xml.c_str(), xml.length() + 1 );

  xpathCtx = xmlXPathNewContext( doc );
                
  xmlXPathRegisterNs( xpathCtx, (xmlChar*)"one",
(xmlChar*)"http://space1"; );


  xmlXPathRegisterNs( xpathCtx, (xmlChar*)"two",
      (xmlChar*)"http://space2"; );

  xmlChar* xpathExpr = (xmlChar*) "/one:a/one:b";
  nodes = xmlXPathEvalExpression( xpathExpr, xpathCtx );

  // etc..
}

I end up with a node collection like:
  <b>
    <two:c>fooFirst<two:c>
    <two:c>fooSecond<two:c>
    <two:c>fooThird<two:c>
  </b>
  <b>
    <two:c>barFirst<two:c>
    <two:c>barSecond<two:c>
    <two:c>barThird<two:c>
  </b>

I need to hand each <b> node off to different objects in my code, where they
will later be processed as needed using XSLT. But the lack of namespace
definitions in the fragments is breaking the XSL. I have tried using
xmlReconciliateNs, passing the doc and the appropriate nodetab for each
node, but it didn't work. What I'd like to end up with for each <b> node is:
 
  <b xmlns='http://space1' xmlns:two='http://space2'>
    <two:c>fooFirst<two:c>
    <two:c>fooSecond<two:c>
    <two:c>fooThird<two:c>
  </b>

Currently I am adding the namespace attributes "by hand" after the fact, but
that is of course a distasteful approach.

I'd be very grateful for any ideas.

Thanks, Malcolm Pollack






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]