On 31/01/11 12:27, Mateusz Loskot wrote:
On 31/01/11 11:42, Alberto Massari wrote:
Hi Mateusz,
the DOMPrint example specifies the root element as the context node, not
the document node; so the correct behavior is for "bookstore" to return
an empty set, and "." to return the root element.

Alberto,

Your explanation makes sense, however I'm not sure if I get it how to
solve on the code level. I used DOMPrint as an example, but trying to
solve the same issue in my own application. Shortly, the issue is the
XPath expression do not select anything (not only at root level)
unless I use full absolute path anchored at root.

So, code (based on DOMPrint) discussed in my previous post boils
down to the following code - root element as the context node:

XercesDOMParser* parser = ...'
DOMDocument* doc = parser->getDocument();
DOMElement* root = doc->getDocumentElement(); // root is context node
DOMXPathNSResolver* resolver = doc->createNSResolver(root);
DOMXPathResult* result =
doc->evaluate(xpathStr, root, resolver,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL);

Now, how to make the document a context node?
Simple replacement of root with doc is not working, obviously:

DOMXPathResult* result =
doc->evaluate(xpathStr, doc, resolver,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL)

Do I need to configure resolver to achieve equivalent of?

DOMXPathNSResolver* resolver = doc->createNSResolver(doc);


I must admit the Xerces C++ documentation (not API reference) regarding
XPath is not very helpful here, so I'd be thankful for any piece of advice.

I have quickly modified the DOMPrint.cpp sample to:

1) Not to use NS resolver - my bookstore.xml sample does not use namespaces, AFAIU the Xerces API docs, NULL as resolver should not throw exceptions if no namespace prefix is used.

2) Use doc as context node

The modified code looks as follows:

if(gXPathExpression!=NULL)
{
   XMLCh* xpathStr=XMLString::transcode(gXPathExpression);
   DOMElement* root = doc->getDocumentElement();
   try
   {
      DOMXPathNSResolver* resolver=NULL; // doc->createNSResolver(root);
      DOMXPathResult* result=doc->evaluate(
         xpathStr,
         doc, //root,
         NULL, // resolver,
         DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
         NULL);

   ...
   }
   ...
}
...


No matter what XPath expression I pass (., /bookstore, bookstore) to
the modified version of DOMPrint sample, I always get the following exception:

An error occurred during processing of the XPath expression. Msg is:
implementation does not support the requested type of object or operation

How should I interpret it? Xerces limitation and I rather should use
Xalan? Or I'm completely misusing the XPath engine in Xerces?

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org
Member of ACCU, http://accu.org

Reply via email to