XPATH Extension functions supported ?

Help
sid
2004-11-26
2012-10-08
  • sid

    sid - 2004-11-26

    hi

    I am a newbie to saxon and I had a query regarding its capabilities.I know and have a working example for an extension function which I am using ina xslt stylsheet. I was wondering if I could use the same extension function in xpath expression outside a stylsheet.

    See code snippet below :
    InputSource is = new InputSource(new File(filename).toURL().toString());
    SAXSource ss = new SAXSource(is);
    XPathEvaluator xpe = new XPathEvaluator(ss);
    XPathExpression extension = xpe.createExpression("x:reverse(\"hello1\")");

    I use the same reverse function ina stylsheet and it works like a charm but when i try to use it in xpath thru java code it gives me an error given below

    Exception in thread "main" net.sf.saxon.xpath.XPathException$Static: XPath synta
    x error in {x:reverse("hello1")}:
    Prefix x has not been declared
    at net.sf.saxon.expr.ExpressionParser.grumble(ExpressionParser.java:99)

    Note that my top level xml root element has the prefix defined as
    <BOOKLIST xsi:noNamespaceSchemaLocation="books.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:x="java:test.XPathExtension">

    I also tried an alternative way by setting a NodeContext and giving it a NamespaceNode, but that did not work either.

    Am I missing something ?
    Any help would be appreciated.
    thanks.

     
    • Michael Kay

      Michael Kay - 2004-11-26

      The XPathEvaluator gets the static context from a StandaloneContext object, which has a declareNamespace method allowing you to associate the namespace used in your function call with a suitable URI.

      So you can do

      xpe.getStaticContext().declareNamespace("prefix", "uri")

      Saxon 8.2 will include support for the XPath API defined in JAXP 1.3, alongside the existing API.

      Michael Kay

       
    • sid

      sid - 2004-11-26

      hi
      I am using saxon8B and in that there is no such method for the StaticContext.

      see trace below :
      XPathExample.java:74: cannot resolve symbol
      symbol : method declareNamespace (java.lang.String,java.lang.String)

      Any other way around ?
      thanks

       
      • Michael Kay

        Michael Kay - 2004-11-26

        I don't know what you're doing wrong. The methods I referred to, XPathEvaluator.getStaticContext and StandaloneContext.declareNamespace, have been in the product for many releases.

        Michael Kay

         
    • sid

      sid - 2004-11-26

      Sorry for the hurried previous post.
      StandAloneContext is a child of StaticContext and that has the required method.

      Everything works !
      thanks a lot.

       
    • sid

      sid - 2004-12-02

      Everything works.
      My previous posts got reordered incorrectly(see timestamp), so you felt I still had a problem.

      thanks

       

Log in to post a comment.