You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by rc...@micron.com on 2003/04/30 20:57:23 UTC

XercesDOM/XPath Question.

Sorry, if this has been answered previously.  I checked the archives and sample code and could not find the answer.

Is this the right pattern to execute a XPath Query on Xerces DOM?  This code used to work with the source in CVS repository that I downloaded couple of months back.  The official release causes an access violation on the following line

XalanElement*	rootElem = theWrapper.getDocumentElement();


I am using Xerces 2.2 and Xalan 1.5 on Windows platform.  Any help/suggestions would be really appreciated.  Also, we plan to use XPath expressions heavily (and call this method very often( so any pointers on better implementations to improve performance would be greatly appreciated.

Thanks,

Ravin


-------------------------------------------------------------------------------------
Declarations for objects used....

DOMDocument* xercesDocument;
std::wstring xPathQuery;
DOMNode* m_pDOMNode;
................
................
................
................

XercesDOMSupport theDOMSupport;
XercesDocumentWrapper theWrapper(xercesDocument, false, false);

// Initialize the XPath subsystem
static XPathInit theInit;

// configure the objects needed for XPath to work with the Xerces DOM
XPathEnvSupportDefault	theEnvSupport;
XObjectFactoryDefault	theXObjectFactory;
XPathExecutionContextDefault	theExecutionContext(theEnvSupport, theDOMSupport, theXObjectFactory);
XPathFactoryDefault	theXPathFactory;
XPathProcessorImpl	theXPathProcessor;
		
XalanElement*	rootElem = theWrapper.getDocumentElement();
XalanNode*	currentNode = theWrapper.mapNode(m_pDOMNode);
		
// first get the context nodeset
XPath* const contextXPath = theXPathFactory.create(); //will be deleted by theXPathFactory destructor
XPathConstructionContextDefault		theXPathConstructionContext;
theXPathProcessor.initXPath(*contextXPath, 
                            theXPathConstructionContext,
			          XalanDOMString(strQuery.c_str()), 
			          ElementPrefixResolverProxy(rootElem, theEnvSupport, theDOMSupport));
		
XObjectPtr	xObj = contextXPath->execute(currentNode,
			ElementPrefixResolverProxy(rootElem, theEnvSupport, theDOMSupport),
			theExecutionContext);
		
if( xObj->getType() != XObject::eTypeNodeSet )
{
	//No results returned...
}
else
{
	const NodeRefListBase&	nodeset = xObj->nodeset();
	//ok, now we get the matching xalan node, add to the vector
	size_t len = nodeset.getLength();
	for (size_t i=0; i<len; i++)
	{
		XalanNode* const	node = nodeset.item(i);
			//Push the results in XercesNodeVector using Wrapper.mapNode(node) method.
	}
}

Re: XercesDOM/XPath Question.

Posted by da...@us.ibm.com.



Hi Ravin,

Can you create a _minimal_ standalone set of inputs that we can use to
test?  If so, please create a Bugzilla report and attach the files.

Thanks!

Dave



                                                                                                                                  
                      rchauhan@micron.                                                                                            
                      com                      To:      <xa...@xml.apache.org>                                            
                                               cc:      <rc...@micron.com>, (bcc: David N Bertoni/Cambridge/IBM)               
                      04/30/2003 11:57         Subject: XercesDOM/XPath Question.                                                 
                      AM                                                                                                          
                                                                                                                                  



Sorry, if this has been answered previously.  I checked the archives and
sample code and could not find the answer.

Is this the right pattern to execute a XPath Query on Xerces DOM?  This
code used to work with the source in CVS repository that I downloaded
couple of months back.  The official release causes an access violation on
the following line

XalanElement*            rootElem = theWrapper.getDocumentElement();


I am using Xerces 2.2 and Xalan 1.5 on Windows platform.  Any
help/suggestions would be really appreciated.  Also, we plan to use XPath
expressions heavily (and call this method very often( so any pointers on
better implementations to improve performance would be greatly appreciated.

Thanks,

Ravin


-------------------------------------------------------------------------------------

Declarations for objects used....

DOMDocument* xercesDocument;
std::wstring xPathQuery;
DOMNode* m_pDOMNode;
................
................
................
................

XercesDOMSupport theDOMSupport;
XercesDocumentWrapper theWrapper(xercesDocument, false, false);

// Initialize the XPath subsystem
static XPathInit theInit;

// configure the objects needed for XPath to work with the Xerces DOM
XPathEnvSupportDefault         theEnvSupport;
XObjectFactoryDefault          theXObjectFactory;
XPathExecutionContextDefault         theExecutionContext(theEnvSupport,
theDOMSupport, theXObjectFactory);
XPathFactoryDefault            theXPathFactory;
XPathProcessorImpl             theXPathProcessor;

XalanElement*            rootElem = theWrapper.getDocumentElement();
XalanNode*         currentNode = theWrapper.mapNode(m_pDOMNode);

// first get the context nodeset
XPath* const contextXPath = theXPathFactory.create(); //will be deleted by
theXPathFactory destructor
XPathConstructionContextDefault
theXPathConstructionContext;
theXPathProcessor.initXPath(*contextXPath,
                            theXPathConstructionContext,

XalanDOMString(strQuery.c_str()),

ElementPrefixResolverProxy(rootElem, theEnvSupport, theDOMSupport));

XObjectPtr         xObj = contextXPath->execute(currentNode,
                                     ElementPrefixResolverProxy(rootElem,
theEnvSupport, theDOMSupport),
                                     theExecutionContext);

if( xObj->getType() != XObject::eTypeNodeSet )
{
             //No results returned...
}
else
{
             const NodeRefListBase&        nodeset = xObj->nodeset();
             //ok, now we get the matching xalan node, add to the vector
             size_t len = nodeset.getLength();
             for (size_t i=0; i<len; i++)
             {
                         XalanNode* const        node = nodeset.item(i);
                                     //Push the results in XercesNodeVector
using Wrapper.mapNode(node) method.
             }
}