0

I am trying to use the format-number function from XQuery/XPath 3.0.

According to Saxon documentation, this is supported from v9.3-9.4.

I'm using Saxon-EE 9.5:

    <dependency>
        <groupId>com.saxonica</groupId>
        <artifactId>Saxon-EE</artifactId>
        <version>9.5.1-3</version>
    </dependency>

A very simple test throws an exception with the following error:

@Test
public void testFormatNumber() throws SaxonApiException {
  Processor proc = new Processor(false);
  XQueryCompiler comp = proc.newXQueryCompiler();
  XQueryExecutable exp = comp.compile("format-number(number('123.4'),'#.00000000')");
}

This throws the following error:

Error on line 1 column 0 
  XPST0017 XQuery static error near #...(number('123.4'),'#.00000000')#:
    System function format-number#2 is not available with this host language/version

This works using Saxon-HE-9.6.0-4, but I need the EE edition to do Schema Validation, and Saxon-EE 9.5.1-3 is the version we have the license for.

Any solution around Saxon-EE? Is there any alternative mature library that supports XQuery 3.0 & Schema Validation?

XSen
  • 188
  • 1
  • 2
  • 9
  • 1
    If you want to use XQuery 3.0 then the first thing to try is to use a query saying that: `comp.compile("xquery version \"3.0\"; format-number(number('123.4'),'#.00000000')");`. I think in that case Saxon 9.5 EE will treat your query as an XQuery 3.0 query. – Martin Honnen May 14 '15 at 09:02
  • Thanks; this by itself does not solve the issue, but combined with the suggestion from @flafoux it works! – XSen May 15 '15 at 10:29

3 Answers3

1

From this site, it looks like you need to enable xpath 3.0 :

((net.sf.saxon.xpath.XPathEvaluator)xpath).setXPathLanguageLevel("3.0");
flafoux
  • 2,080
  • 1
  • 12
  • 13
  • This doesn't seem applicable for XQuery (notice here I'm using XQueryCompiler). This function is also an XPath function but the question is really about XQuery. – XSen May 13 '15 at 13:02
  • 1
    you can try specify it to the xquerycompiler `comp.setLanguageVersion("3.0")` – flafoux May 13 '15 at 13:15
  • Thanks for that... it almost worked :( this throws a java.lang.UnsupportedOperationException: XQuery 3.0 extensions are supported only in Saxon-PE when compiling the format-number XQuery expression... – XSen May 13 '15 at 13:39
0

Combining the answers from @flafoux and @martin-honnen, the solution is:

    @Test
    public void testFormatNumber() throws SaxonApiException {
      Processor proc = new Processor(true);
      XQueryCompiler comp = proc.newXQueryCompiler();
      comp.setLanguageVersion("3.0");
      XQueryExecutable exp = comp.compile("xquery version \"3.0\"; format-number(number('123.4'),'#.00000000')");

      XQueryEvaluator qe = exp.load();
      Serializer out = new Serializer(new StringWriter());
      qe.run(out);

      for(XdmItem i : qe) {
        System.out.println(i.getStringValue());
      }
    }
XSen
  • 188
  • 1
  • 2
  • 9
0

In Saxon 9.5, XQuery 3.0 support was only available in -PE and -EE and needed to be explicitly enabled. This changed in Saxon 9.6, where "core" XQ 3.0 features became available in the -HE product.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164