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 Sami Islam <sa...@detewe.de> on 2004/04/29 09:16:52 UTC

Ways create Xpath Expressions and evaluate them.

Hello,
I found atleast 2 ways to create Xpath Expressions and evaluate them. Is
there any documentation where I can find more info on which is the
better way or perhaps which is suitable for what situations?
Can someone give me some insight on this?

1) With XPathEvaluator e.g.

// Initialize stuff...
XPathEvaluator	theEvaluator;

XalanNode* const theContextNode = theEvaluator.selectSingleNode(
					theDOMSupport,
					theDocument,
					XalanDOMString(argv[2]).c_str(),
					thePrefixResolver);
NodeRefList theNodeList = theEvaluator.selectNodeList(
				theDOMSupport,
				theContextNode,
				XalanDOMString(argv[3]).c_str(),
				thePrefixResolver);

size_t numNodes = theNodeList.getLength();

for(size_t i=0; i<numNodes; ++i)
{	
	XalanNode* const	node = theNodeList.item(i);
	XalanDOMString		str;
	const int theType = node->getNodeType();
	
	// Do stuff with Nodes and Elements.
}

2) With Xpath Object e.g.

// Initialize stuff...
XPath* const	contextXPath = theXPathFactory.create();

theXPathProcessor.initXPath(*contextXPath,
				theXPathConstructionContext,
				XalanDOMString(context),
				ElementPrefixResolverProxy(rootElem,
theEnvSupport, theDOMSupport));

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

const NodeRefListBase&	contextNodeList = xObj->nodeset();

const unsigned int	theLength = contextNodeList.getLength();

XPath* const	xpath = theXPathFactory.create();
theXPathProcessor.initXPath(*xpath,
				theXPathConstructionContext,
				TranscodeFromLocalCodePage(expr),
				ElementPrefixResolverProxy(rootElem,
theEnvSupport, theDOMSupport));

xObj = xpath->execute(contextNodeList.item(0),
				ElementPrefixResolverProxy(rootElem,
theEnvSupport, theDOMSupport),
				theExecutionContext);

switch (xObj->getType())
// Do stuff with Nodes and Elements.

Regards

Sami