Project

Profile

Help

XPath Expression

Added by Anonymous over 16 years ago

Legacy ID: #4986856 Legacy Poster: Markku Laine (mplaine)

Hi, A newbie problem: I am trying to compile two XPath expressions using the code below: ------------------------------------------------------------------------------------ 1: package test; 2: 3: import javax.xml.xpath.XPath; 4: import javax.xml.xpath.XPathExpression; 5: import javax.xml.xpath.XPathFactory; 6: 7: public class SaxonXPathTest { 8: 9: public static void main( String[] args ) { 10: try { 11: XPathFactory xpathFactory= XPathFactory.newInstance(); 12: XPath xpath = xpathFactory.newXPath(); 13: 14: // XPath Expression #1 (Okay?) 15: String xpathStr1 = "/blog/posts/post[ @id = $id ]/comments"; 16: XPathExpression xpathExpression1 = xpath.compile( xpathStr1 ); 17: 18: // XPath Expression #2 (contains an error) 19: String xpathStr2 = "/blog/posts/post[ this-should-produce-an-error@id = $id ]/comments"; 20: XPathExpression xpathExpression2 = xpath.compile( xpathStr2 ); 21: System.out.println( "XPath expression has been successfully compiled." ); 22: } catch ( Exception ex ) { 23: ex.printStackTrace(); 24: } 25: } 26: } ------------------------------------------------------------------------------------ When I run the code I will get an exception (see below) at the point where the code tries to compile the first XPath Expression. ------------------------------------------------------------------------------------ net.sf.saxon.trans.XPathException: Variable is used in XPath expression, but no JAXP VariableResolver is available at net.sf.saxon.xpath.JAXPXPathStaticContext.bindVariable(JAXPXPathStaticContext.java:187) at net.sf.saxon.expr.ExpressionParser.parseBasicStep(ExpressionParser.java:1276) at net.sf.saxon.expr.ExpressionParser.parseStepExpression(ExpressionParser.java:1223) at net.sf.saxon.expr.ExpressionParser.parseRelativePath(ExpressionParser.java:1163) at net.sf.saxon.expr.ExpressionParser.parsePathExpression(ExpressionParser.java:1149) at net.sf.saxon.expr.ExpressionParser.parseUnaryExpression(ExpressionParser.java:1038) at net.sf.saxon.expr.ExpressionParser.parseCastExpression(ExpressionParser.java:701) at net.sf.saxon.expr.ExpressionParser.parseCastableExpression(ExpressionParser.java:656) at net.sf.saxon.expr.ExpressionParser.parseTreatExpression(ExpressionParser.java:637) at net.sf.saxon.expr.ExpressionParser.parseInstanceOfExpression(ExpressionParser.java:619) at net.sf.saxon.expr.ExpressionParser.parseIntersectExpression(ExpressionParser.java:1071) at net.sf.saxon.expr.ExpressionParser.parseUnionExpression(ExpressionParser.java:1053) at net.sf.saxon.expr.ExpressionParser.parseMultiplicativeExpression(ExpressionParser.java:990) at net.sf.saxon.expr.ExpressionParser.parseAdditiveExpression(ExpressionParser.java:970) at net.sf.saxon.expr.ExpressionParser.parseRangeExpression(ExpressionParser.java:886) at net.sf.saxon.expr.ExpressionParser.parseComparisonExpression(ExpressionParser.java:855) at net.sf.saxon.expr.ExpressionParser.parseAndExpression(ExpressionParser.java:433) at net.sf.saxon.expr.ExpressionParser.parseOrExpression(ExpressionParser.java:415) at net.sf.saxon.expr.ExpressionParser.parseExprSingle(ExpressionParser.java:364) at net.sf.saxon.expr.ExpressionParser.parseExpression(ExpressionParser.java:316) at net.sf.saxon.expr.ExpressionParser.parseStepExpression(ExpressionParser.java:1233) at net.sf.saxon.expr.ExpressionParser.parseRemainingPath(ExpressionParser.java:1195) at net.sf.saxon.expr.ExpressionParser.parsePathExpression(ExpressionParser.java:1128) at net.sf.saxon.expr.ExpressionParser.parseUnaryExpression(ExpressionParser.java:1038) at net.sf.saxon.expr.ExpressionParser.parseCastExpression(ExpressionParser.java:701) at net.sf.saxon.expr.ExpressionParser.parseCastableExpression(ExpressionParser.java:656) at net.sf.saxon.expr.ExpressionParser.parseTreatExpression(ExpressionParser.java:637) at net.sf.saxon.expr.ExpressionParser.parseInstanceOfExpression(ExpressionParser.java:619) at net.sf.saxon.expr.ExpressionParser.parseIntersectExpression(ExpressionParser.java:1071) at net.sf.saxon.expr.ExpressionParser.parseUnionExpression(ExpressionParser.java:1053) at net.sf.saxon.expr.ExpressionParser.parseMultiplicativeExpression(ExpressionParser.java:990) at net.sf.saxon.expr.ExpressionParser.parseAdditiveExpression(ExpressionParser.java:970) at net.sf.saxon.expr.ExpressionParser.parseRangeExpression(ExpressionParser.java:886) at net.sf.saxon.expr.ExpressionParser.parseComparisonExpression(ExpressionParser.java:836) at net.sf.saxon.expr.ExpressionParser.parseAndExpression(ExpressionParser.java:433) at net.sf.saxon.expr.ExpressionParser.parseOrExpression(ExpressionParser.java:415) at net.sf.saxon.expr.ExpressionParser.parseExprSingle(ExpressionParser.java:364) at net.sf.saxon.expr.ExpressionParser.parseExpression(ExpressionParser.java:316) at net.sf.saxon.expr.ExpressionParser.parse(ExpressionParser.java:238) at net.sf.saxon.expr.ExpressionTool.make(ExpressionTool.java:72) at net.sf.saxon.xpath.XPathEvaluator.createExpressionInternal(XPathEvaluator.java:202) at net.sf.saxon.xpath.XPathEvaluator.compile(XPathEvaluator.java:381) at test.SaxonXPathTest.main(SaxonXPathTest.java:16) --------------- linked to ------------------ javax.xml.xpath.XPathExpressionException at net.sf.saxon.xpath.XPathEvaluator.compile(XPathEvaluator.java:383) at test.SaxonXPathTest.main(SaxonXPathTest.java:16) Caused by: net.sf.saxon.trans.XPathException: Variable is used in XPath expression, but no JAXP VariableResolver is available at net.sf.saxon.xpath.JAXPXPathStaticContext.bindVariable(JAXPXPathStaticContext.java:187) at net.sf.saxon.expr.ExpressionParser.parseBasicStep(ExpressionParser.java:1276) at net.sf.saxon.expr.ExpressionParser.parseStepExpression(ExpressionParser.java:1223) at net.sf.saxon.expr.ExpressionParser.parseRelativePath(ExpressionParser.java:1163) at net.sf.saxon.expr.ExpressionParser.parsePathExpression(ExpressionParser.java:1149) at net.sf.saxon.expr.ExpressionParser.parseUnaryExpression(ExpressionParser.java:1038) at net.sf.saxon.expr.ExpressionParser.parseCastExpression(ExpressionParser.java:701) at net.sf.saxon.expr.ExpressionParser.parseCastableExpression(ExpressionParser.java:656) at net.sf.saxon.expr.ExpressionParser.parseTreatExpression(ExpressionParser.java:637) at net.sf.saxon.expr.ExpressionParser.parseInstanceOfExpression(ExpressionParser.java:619) at net.sf.saxon.expr.ExpressionParser.parseIntersectExpression(ExpressionParser.java:1071) at net.sf.saxon.expr.ExpressionParser.parseUnionExpression(ExpressionParser.java:1053) at net.sf.saxon.expr.ExpressionParser.parseMultiplicativeExpression(ExpressionParser.java:990) at net.sf.saxon.expr.ExpressionParser.parseAdditiveExpression(ExpressionParser.java:970) at net.sf.saxon.expr.ExpressionParser.parseRangeExpression(ExpressionParser.java:886) at net.sf.saxon.expr.ExpressionParser.parseComparisonExpression(ExpressionParser.java:855) at net.sf.saxon.expr.ExpressionParser.parseAndExpression(ExpressionParser.java:433) at net.sf.saxon.expr.ExpressionParser.parseOrExpression(ExpressionParser.java:415) at net.sf.saxon.expr.ExpressionParser.parseExprSingle(ExpressionParser.java:364) at net.sf.saxon.expr.ExpressionParser.parseExpression(ExpressionParser.java:316) at net.sf.saxon.expr.ExpressionParser.parseStepExpression(ExpressionParser.java:1233) at net.sf.saxon.expr.ExpressionParser.parseRemainingPath(ExpressionParser.java:1195) at net.sf.saxon.expr.ExpressionParser.parsePathExpression(ExpressionParser.java:1128) at net.sf.saxon.expr.ExpressionParser.parseUnaryExpression(ExpressionParser.java:1038) at net.sf.saxon.expr.ExpressionParser.parseCastExpression(ExpressionParser.java:701) at net.sf.saxon.expr.ExpressionParser.parseCastableExpression(ExpressionParser.java:656) at net.sf.saxon.expr.ExpressionParser.parseTreatExpression(ExpressionParser.java:637) at net.sf.saxon.expr.ExpressionParser.parseInstanceOfExpression(ExpressionParser.java:619) at net.sf.saxon.expr.ExpressionParser.parseIntersectExpression(ExpressionParser.java:1071) at net.sf.saxon.expr.ExpressionParser.parseUnionExpression(ExpressionParser.java:1053) at net.sf.saxon.expr.ExpressionParser.parseMultiplicativeExpression(ExpressionParser.java:990) at net.sf.saxon.expr.ExpressionParser.parseAdditiveExpression(ExpressionParser.java:970) at net.sf.saxon.expr.ExpressionParser.parseRangeExpression(ExpressionParser.java:886) at net.sf.saxon.expr.ExpressionParser.parseComparisonExpression(ExpressionParser.java:836) at net.sf.saxon.expr.ExpressionParser.parseAndExpression(ExpressionParser.java:433) at net.sf.saxon.expr.ExpressionParser.parseOrExpression(ExpressionParser.java:415) at net.sf.saxon.expr.ExpressionParser.parseExprSingle(ExpressionParser.java:364) at net.sf.saxon.expr.ExpressionParser.parseExpression(ExpressionParser.java:316) at net.sf.saxon.expr.ExpressionParser.parse(ExpressionParser.java:238) at net.sf.saxon.expr.ExpressionTool.make(ExpressionTool.java:72) at net.sf.saxon.xpath.XPathEvaluator.createExpressionInternal(XPathEvaluator.java:202) at net.sf.saxon.xpath.XPathEvaluator.compile(XPathEvaluator.java:381) ... 1 more ------------------------------------------------------------------------------------ I will receive this exception if I have all the JAR files from Saxon-B-9.0.0.4 for Java in my classpath, including saxon9-xpath.jar. However, if I exclude saxon9-xpath.jar from my classpath and run the code again I will get a different exception (see below), as excepted, at the point where the code tries to compile the second XPath Expression. ------------------------------------------------------------------------------------ javax.xml.transform.TransformerException: Expected ], but found: @ at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.consumeExpected(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.Predicate(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source) at test.SaxonXPathTest.main(SaxonXPathTest.java:20) --------------- linked to ------------------ javax.xml.xpath.XPathExpressionException at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source) at test.SaxonXPathTest.main(SaxonXPathTest.java:20) Caused by: javax.xml.transform.TransformerException: Expected ], but found: @ at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.consumeExpected(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.Predicate(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source) ... 2 more ------------------------------------------------------------------------------------ Have I do something wrong i.e. should I use JAXP VariableResolver (how) to bind the variable? Thanks in advance! -Markku Ps. I am using JDK 1.5.0.


Replies (1)

RE: XPath Expression - Added by Anonymous over 16 years ago

Legacy ID: #4986904 Legacy Poster: Michael Kay (mhkay)

The first exception means what it says: Variable is used in XPath expression, but no JAXP VariableResolver is available See the javadoc at http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPath.html specifically: "If the expression contains a variable reference, its value will be found through the XPathVariableResolver. An XPathExpressionException is raised if the variable resolver is undefined or the resolver returns null for the variable. " >>However, if I exclude saxon9-xpath.jar from my classpath and run the code again I will get a different exception (see below), as excepted, at the point where the code tries to compile the second XPath Expression. ------------------------------------------------------------------------------------ javax.xml.transform.TransformerException: Expected ], but found: @ at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) If saxon9-xpath.jar is not on your classpath, then the JAXP factory mechanism chooses apache.xpath as the XPath engine. I don't know why this engine isn't reporting an exception for the first XPath expression; this would appear to be in violation of the specification, but you will have to ask on an Apache list for an explanation. Perhaps the Apache engine requires a VariableResolver only if the expression is actually evaluated.

    (1-1/1)

    Please register to reply