The first problem is that "//" is not a valid XPath. You need a node test,
such as '*' or 'foo':
"//*"
//foo
Once you get past that, you're code is going to cause many more problems.
It's actually much easier than you're making it. However, there is one
limitation to using our bridge with the Xerces DOM -- you cannot wrap a
subtree, so you cannot wrap the m_LoadActiveNode node separately. You also
do not need any of the XalanSourceTree objects, but you do need some
XercesParserLiaison objects:
bool
MyClass::XpathSolve(
const char *XPathExp,
const char *XPathContextExp)
{
const XalanDOMString XPathEx(XPathExp);
const XalanDOMString XPathContextEx(XPathContextExp);
int theResult = 0;
try
{
XMLPlatformUtils::Initialize();
XPathEvaluator::initialize();
{
XercesParserLiaisonDOMSupport theDOMSupport;
XercesParserLiaison theLiaison(theDOMSupport);
theLiaison.setBuildBridgeNodes(true);
//convert DOM to Xalan
XalanDocument* const XDocBridge =
theParserLiaison.createDocument(m_LoadDOMDocument);
XPathEvaluator theEvaluator;
OK, let's find the context node...
XalanNode* const theContextNode =
theEvaluator.selectSingleNode(
theDOMSupport,
XDocBridge,
XPathContextEx.c_str());
if (theContextNode == 0)
{
cerr << "Warning -- No nodes matched the location path" <<
endl;
}
else
{
// OK, let's evaluate the expression...
const XObjectPtr theResult(
theEvaluator.evaluate(
theDOMSupport,
theContextNode,
XPathEx.c_str()));
assert(theResult.null() == false);
}
}
XPathEvaluator::terminate();
XMLPlatformUtils::Terminate();
}
catch(XPathParserException &e)
{
cerr << "XPATH-PARSER EXCEPTION" <<endl
<< e.getMessage().c_str() << endl
<< e.getType().c_str() <<endl
<< e.getLineNumber() <<endl
<< e.getColumnNumber() <<endl;
}
catch(const XPathException &e)
{
cerr << "XPATH EXCEPTION" <<endl
<< e.getMessage().c_str() << endl
<< e.getType().c_str() <<endl
<< e.getLineNumber() <<endl
<< e.getColumnNumber() <<endl;
}
catch(...)
{
cerr << "Exception caught!" << endl;
theResult = -1;
}
return theResult;
}
I've added a second parameter to your function, which specifies the XPath
expression to find the context node. In general, you do not need to first
find a context node, as the usual context node is the document you are
searching.
However, if the XPath expression is relative, and you want to provide the
option for a context node other than the root (the document, _not_ the root
element), then this is where the new parameter to the function is used.
What you cannot do is use the m_LoadActiveNode data member as the context
node, since there's no way to map from a DOM_Node instance to its
corresponding XalanNode instance.
Hope that helps...
Dave
"Troels Rossing"
<[EMAIL PROTECTED] To: "Xalan user maillist"
<[email protected]>
e.com> cc: (bcc: David N
Bertoni/CAM/Lotus)
Subject: Simple XPath
12/12/2001 09:58
AM
Hi
I have a DOM_Document and want to use a DOM_Element in that document. I
which to apply an XPath expression to any selected node in the
DOM_Document.
Tried to use the SimpleXPathAPI example but I get a XPathParserException.
What am I doing wrong?? *XPathExp contains //
MY CODE!!!!!!!!!!!!!!!!
bool MyClass::XpathSolve(char *XPathExp)
{
XalanDOMString XPathEx(XPathExp);
int theResult = 0;
try
{
XMLPlatformUtils::Initialize();
XPathEvaluator::initialize();
{
// Initialize the
XalanSourceTree subsystem...
XalanSourceTreeInit
theSourceTreeInit;
// We'll use these to
parse the XML file.
XalanSourceTreeDOMSupport
theDOMSupport;
XalanSourceTreeParserLiaison theLiaison(theDOMSupport);
//convert DOM to Xalan
XercesDocumentBridge *XDocBridge = new
XercesDocumentBridge(m_LoadDOMDocument); //convert DOM_Document
XercesBridgeNavigator *BridgeNav = new
XercesBridgeNavigator(XDocBridge);
XercesElementBridge *XElemBridge = new
XercesElementBridge(m_LoadActiveNode ,*BridgeNav); //convert DOM_Element
XalanElement* rootElem = XElemBridge;
// Hook the two
together...
theDOMSupport.setParserLiaison(&theLiaison);
XPathEvaluator
theEvaluator;
// OK, let's find the
context node...
XalanNode* const
theContextNode =
theEvaluator.selectSingleNode(
theDOMSupport,
rootElem,
XPathEx.c_str(),0);
if (theContextNode == 0)
{
cerr
<< "Warning -- No nodes matched the location path" << endl;
}
else
{
// OK, let's
evaluate the expression...
const
XObjectPtr theResult(
theEvaluator.evaluate(
theDOMSupport,
theContextNode,
XPathEx.c_str(),
0));
assert(theResult.null() == false);
}
}
XPathEvaluator::terminate();
XMLPlatformUtils::Terminate();
}
catch(XPathParserException &e)
{
cerr << "XPATH-PARSER EXCEPTION" <<endl
<< e.getMessage().c_str() << endl
<< e.getType().c_str() <<endl
<< e.getLineNumber() <<endl
<< e.getColumnNumber() <<endl;
}
catch(const XPathException &e)
{
cerr << "XPATH EXCEPTION" <<endl
<< e.getMessage().c_str() << endl
<< e.getType().c_str() <<endl
<< e.getLineNumber() <<endl
<< e.getColumnNumber() <<endl;
}
catch(...)
{
cerr << "Exception caught!" << endl;
theResult = -1;
}
return theResult;
}
regards
Troels Rossing
QualiWare
Phone +45 45 47 07 46