Hi all,

I am using Saxon to load an XML fragment and then query it using sxpath components.

My problem is about the XPath queries containing name-space prefixes, like "//Prefix1:fragmentt[ (Prefix1:content//testPrefix:Order and ... ) ]".
For some reason, the XPath queries which contain names-paces prefixes are not recognized when calling xPathEvaluator.createExpression( xPathQuery ) below.
The exception looks like:
----
Prefix testPrefix has not been declared
net.sf.saxon.trans.XPathException: Prefix testPrefix has not been declared
    at net.sf.saxon.expr.ExpressionParser.grumble(ExpressionParser.java:244)
    at net.sf.saxon.expr.ExpressionParser.makeNameCode(ExpressionParser.java:2410)
    at net.sf.saxon.expr.ExpressionParser.makeNameTest(ExpressionParser.java:2519)
    at net.sf.saxon.expr.ExpressionParser.parseNodeTest(ExpressionParser.java:1735)
    at net.sf.saxon.expr.ExpressionParser.parseBasicStep(ExpressionParser.java:1556)
    at net.sf.saxon.expr.ExpressionParser.parseStepExpression(ExpressionParser.java:1454)
    at net.sf.saxon.expr.ExpressionParser.parseRelativePath(ExpressionParser.java:1394)
    at net.sf.saxon.expr.ExpressionParser.parsePathExpression(ExpressionParser.java:1375)
    at net.sf.saxon.expr.ExpressionParser.parseUnaryExpression(ExpressionParser.java:1230)
    at net.sf.saxon.expr.ExpressionParser.parseCastExpression(ExpressionParser.java:849)
    at net.sf.saxon.expr.ExpressionParser.parseCastableExpression(ExpressionParser.java:804)
    at net.sf.saxon.expr.ExpressionParser.parseTreatExpression(ExpressionParser.java:785)
    at net.sf.saxon.expr.ExpressionParser.parseInstanceOfExpression(ExpressionParser.java:767)
    at net.sf.saxon.expr.ExpressionParser.parseIntersectExpression(ExpressionParser.java:1263)
    at net.sf.saxon.expr.ExpressionParser.parseUnionExpression(ExpressionParser.java:1245)
    ...
----

I should note that these name-spaces prefixes / URLs are declared in the XML fragment.
But I *assume* that the ExpressionParser does not retrieve the name-spaces from the XML document before interpreting the query.
So I have to provide the name-spaces somehow to the ExpressionParser.
Is there a way to retrieve the name-spaces available in the Source and place them on the xPathEvaluator?

As a parenthesis, I have tried the AxisIterator approach like below, but I get NullPointerExceptions:
---       
   AxisIterator iterator = node.iterateAxis(Axis.NAMESPACE);
   // String string = iterator.atomize().getStringValue(); this throws NullPointerException
   String string = iterator.getStringValue().toString(); // this throws, too
---
Perhaps I should load the XML fragment in a different way? Please see below how the XML fragment is loaded.


To be a bit more precise, my code looks like the one below (reduced for simplicity):
---
   // build the context, evaluator and expression
   IndependentContext context = new IndependentContext();
   XPathEvaluator xPathEvaluator = new XPathEvaluator();
   xPathEvaluator.setStaticContext(context);
   xPathExpression = xPathEvaluator.createExpression(xPathQuery);

   // load the XML document (which is stored in xmlDocument)
   StreamSource source = new StreamSource(new ByteArrayInputStream(xmlDocument.getBytes("UTF-8")));
   Configuration config = xPathEvaluator.getConfiguration();
    NodeInfo node = config.buildDocument(source);

   // query the input node
   xPathExpression = xPathEvaluator.createExpression(xPathQuery);  <--- here is where the "Prefix has not been declared" exception occurs.
   List<NodeInfo> resultNodeList = (List<NodeInfo>) (List< ? >) xPathExpression.evaluate(node);
---

Thanks in advance,
M.