Project

Profile

Help

How to use the xml-to-json function with Saxon-JS

Added by Andrew Weiss over 6 years ago

Trying to execute fn:xml-to-json but not sure how to do so with Saxon-JS. Getting "XPath parsing error: lexical analysis failed~while expecting ..." errors.

Attempting a simple example as such:


SaxonJS.XPath.evaluate('xml-to-json(1is1)');

Full error:

"XPath parsing error: lexical analysis failed
while expecting [S, EOF, '!', '!=', '(', '(:', ')', '*', '+', ',', '-', '/', '//', ':', '<', '<<', '<=', '=', '=>', '>', '>=', '>>', '?', '[', ']', 'and', 'cast', 'castable', 'div', 'else', 'eq', 'except', 'ge', 'gt', 'idiv', 'instance', 'intersect', 'is', 'le', 'lt', 'mod', 'ne', 'or', 'return', 'satisfies', 'to', 'treat', 'union', '|', '||', '}']
at line 1, column 28:
...http://www.w3.org/2005/xpath-functions">11is1")"

Replies (4)

Please register to reply

RE: How to use the xml-to-json function with Saxon-JS - Added by John Lumley over 6 years ago

The reason is that what you're supplying isn't valid XPath, as XPath doesn't permit XML constructs. (It might be valid XQuery but I have no expertise in that area.)

To generate the XML within XPath you'll need to use @parse-xml($arg as xs:string?) as document-node(element(*))?@ (https://www.w3.org/TR/xpath-functions-31/#func-parse-xml) to generate from a string, viz:

parse-xml('1is1')

and use that as the argument to @xml-to-json()@. It does work as anticipated, but note that you'll have some fun in getting the doubly embedded quotes right!

RE: How to use the xml-to-json function with Saxon-JS - Added by Andrew Weiss over 6 years ago

Thanks John. So when I run this:


SaxonJS.XPath.evaluate('parse-xml(\'1is1\')');

I get this error:

Invalid type:document-node(element(*))

RE: How to use the xml-to-json function with Saxon-JS - Added by John Lumley over 6 years ago

This is due to an error in the type system of the XPath compiler, that had already been fixed, but not yet issued in a maintenance release. The next maintenance release (1.0.2?) should produce the correct results.

(I tested under the development build which already had the problem patched - sorry for the false hope.....)

RE: How to use the xml-to-json function with Saxon-JS - Added by Andrew Weiss over 6 years ago

Ah ok. Thanks for the heads up. Will be on the lookout for the next patch release.

    (1-4/4)

    Please register to reply