Project

Profile

Help

Support #2459

closed

Having difficulty passing JSON to Saxon command line

Added by Jason Mihalick over 8 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
-
Sprint/Milestone:
-
Start date:
2015-09-25
Due date:
% Done:

0%

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

Description

It appears that I cannot pass JSON to a Saxon transform on the command line using an XSLT parameter IF the JSON has any spaces in it? Example:

@

$ JSON='{foo bar"}'; java -cp $PUBSLIB/saxon-ee-bundle.jar:$PUBSLIB/Saxon-EE.jar net.sf.saxon.Transform -s:sample.xml -xsl:saxon-json.xslt -o:output.xml json=${JSON}

Unrecognized option: bar"}

Saxon-EE 9.5.1.8J from Saxonica

Usage: see http://saxonica.com/documentation/html/using-xsl/commandline.html

Format: net.sf.saxon.Transform options params

Options available: -? -a -catalog -config -cr -dtd -expand -explain -ext -im -init -it -l -m -now -o -opt -or -outval -p -quit -r -repeat -s -sa -strip -t -T -threads -TJ -TP -traceout -tree -u -val -versionmsg -warnings -x -xi -xmlversion -xsd -xsdversion -xsiloc -xsl -xsltversion -y

Use -XYZ:? for details of option XYZ

Params:

param=value Set stylesheet string parameter

+param=filename Set stylesheet document parameter

?param=expression Set stylesheet parameter using XPath

!param=value Set serialization parameter

@

See attached sample.xml and saxon-json.xslt if you want to reproduce the sample command that I have included.

Is there a trick to getting this to work? Thanks for the help.


Files

sample.xml (40 Bytes) sample.xml Jason Mihalick, 2015-09-26 00:33
saxon-json.xslt (622 Bytes) saxon-json.xslt Jason Mihalick, 2015-09-26 00:33
Actions #1

Updated by Michael Kay over 8 years ago

Different shell interpreters have different conventions for quoting and escaping special characters (so it won't be quite the same on the Windows shell as in bash, for example), but I would think it likely that you have to (a) escape quotes (and perhaps the curly braces) using a backslash, and (b) enclose the whole value in quotes because it contains spaces. I would think it's probably easier to put the JSON in a file and pass the filename.

Note that this is a shell programming question, not a Saxon question.

Actions #2

Updated by Jason Mihalick over 8 years ago

Hi, I tried many incarnations with KSH and I could not get it to work. I also looked for a way to pass in the XSLT params via a file, which would have been perfectly acceptable as well to me, but I could not find a way to pass in JSON as the value of a parameter in a file. I just gave another look at the documentation, however, and perhaps the JSON can be passed in as the content of an XML tag?

From http://www.saxonica.com/html/documentation/using-xsl/commandline.html:

A param preceded by a leading plus sign (+) is interpreted as a filename or directory. The content of the file is parsed as XML, and the resulting document node is passed to the stylesheet as the value of the parameter.
Actions #3

Updated by Michael Kay over 8 years ago

According to this reference: http://www.informit.com/articles/article.aspx?p=99035&seqNum=6

you should be able to put the JSON text within single quotes, provided it does not contain single quotes.

If the JSON is in a file test.json@, you can pass a parameter @json-file=test.json and then within the stylesheet you can do parse-json(unparsed-text($json-file)) subject of course to the base URI being correct. (There's also a function fn:json-doc() but I don't think it's implemented in Saxon 9.6.)

Actions #4

Updated by Jason Mihalick over 8 years ago

Notice in the example that I provided, I did put the JSON in single quotes. The reason I originally filed this is because the parsing error (Unrecognized option: bar"}) appears to be coming out of Saxon command line parser, not the shell.

That said, however, I will try your other suggestion of using unparsed-text first thing on Monday. Thanks for the suggestions and assist.

Actions #5

Updated by Michael Kay over 8 years ago

The fact that Saxon objects to "bar" shows that the shell has split the JSON string at the space character and has supplied two separate parameters to Saxon. Saxon isn't going to attempt to recombine them...

Actions #6

Updated by Jason Mihalick over 8 years ago

Thank you again. I was able to get the following to work:

@$ JSON='{foo bar"}'; java -cp $PUBSLIB/saxon-ee-bundle.jar:$PUBSLIB/Saxon-EE.jar net.sf.saxon.Transform -s:sample.xml -xsl:saxon-json.xslt -o:output.xml json="$JSON"

@

Output:

<?xml version="1.0" encoding="UTF-8"?><foo-element foo-attr="foo bar"/>

Actions #7

Updated by Michael Kay over 8 years ago

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

Closing this as it appears to have been resolved.

Actions #8

Updated by O'Neil Delpratt over 8 years ago

  • Status changed from Resolved to Closed

Please register to edit this issue

Also available in: Atom PDF