Project

Profile

Help

Support #4392

closed

S9API and JSON input

Added by Gerben Abbink over 4 years ago. Updated over 4 years ago.

Status:
Closed
Priority:
Low
Assignee:
Category:
-
Sprint/Milestone:
-
Start date:
2019-11-22
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:
Platforms:

Description

I am using the S9API to evaluate XPath expressions, like this:

Processor processor = new Processor();
XPathCompiler compiler = processor.newXPathCompiler();
XPathExecutable executable = compiler.compile(XPATH_EXPRESSION);
XPathSelector selector = executable.load();
selector.setContextItem(node);
selector.setURIResolver(URI_RESOLVER);
selector.evaluate();

The resolver URI_RESOLVER is used to resolve URLs in function fn:doc(), for instance:

doc("http://www.bbc.co.uk/news_sitemap.xml")//*

So far so good.

Now, i am trying to parse JSON expressions. This works:

...
XPathExecutable executable = compiler.compile("json-doc('http://api.open-notify.org/iss-now.json')" + JSON_EXPRESSION);
...

I have two questions:

  • The resolver URI_RESOLVER is not used to resolve URLs in fn:json-doc(). Is this a bug?
  • My solution works but it is a bit clumsy: is there a way to evaluate JSON expressions without the json-doc() function? I tried ParseJsonFn.parse(), but the resulting Item was not accepted by setContextItem().
Actions #1

Updated by Michael Kay over 4 years ago

The JAXP-defined URIResolver class returns a Source object, which is oriented to XML resources, so we don't use it for non-XML resources such as JSON. Instead we define our own UnparsedTextResolver callback.

Actions #2

Updated by Michael Kay over 4 years ago

Sorry, I've just come back to this and realise that my answer was rather terse and rather incomplete.

Firstly, if you have a chunk of JSON in a string, then you can parse it using fn:parse-json() rather than fn:json-doc(). The fn:son-doc function is really just a composition of fn:unparsed-text and fn:parse-json, with slightly different rules for inferring file encoding.

Like fn:unparsed-text, the fn:json-doc uses any registered UnparsedTextResolver to dereference URIs. The JAXP URIResolver object is not suitable because it's so tied up with XML.

If you want to invoke the logic of fn:parse-json directly from Java, you could consider using the static method XdmFunctionItem.getSystemFunction() to get an XdmFunctionitem representing the fn:parse-json function, which you can then invoke using its call() method. This saves the considerable cost of parsing an XPath expression.

Actions #3

Updated by Michael Kay over 4 years ago

  • Status changed from New to Closed
  • Assignee set to Michael Kay

Please register to edit this issue

Also available in: Atom PDF