Help - newbie : variable evaluation within xpath expression evaluation 2003-01-22 - By Dhananjay Nene
Hello,
I am attempting to evaluate xpath on a dom and am currently using the w3c api to do so. I am attempting perform xpath evaluation using apis for the first time ... so my code may need some optimisation.
I wish to run almost similar xpath expressions repeatedly except for a few changing values in the expression which I embed in the XPATH expression string using $variable syntax. Subsequently I want to continuously reevaluate the expression repeatedly for differing values of $variable.
The current code look somewhat like following (shortened and cleaned up for brevity)
public void foo(String xpath, File fileToOpen) { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(fileToOpen); XPathEvaluator evaluator = new XPathEvaluatorImpl(doc); XPathNSResolver resolver = evaluator.createNSResolver(doc); XPathExpression = evaluator.createExpression(xpath,resolver); XPathResult result = (XPathResult)expr.evaluate( doc, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null); Node resultNode = null; while ((resultNode = result.iterateNext()) != null) { ... blah ... } }
When I embed the $variable (e.g. the XPath looks like "/Foo1/Foo2[Foo3=$variable]", I get the following exception
Variable not resolvable: variable at org.apache.xpath.axes.PredicatedNodeTest.acceptNode(PredicatedNodeTest.java:449) .....
I am certain the xpath engine expects me to supply the value for variable, but I do not know how to do so. Can someone help me out ?
My feeling is that this objective may not be solved using the javax.xml.transform + org.w3c apis, and I shall need to resort to org.apache.xalan / org.apache.xpath apis. I shall welcome any suggestions using that approach also.
Dhananjay
__________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com
|
|