Support #2459
closedHaving difficulty passing JSON to Saxon command line
0%
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
Updated by Michael Kay about 9 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.
Updated by Jason Mihalick about 9 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.
Updated by Michael Kay about 9 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.)
Updated by Jason Mihalick about 9 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.
Updated by Michael Kay about 9 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...
Updated by Jason Mihalick about 9 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"/>
Updated by Michael Kay about 9 years ago
- Status changed from New to Resolved
- Assignee set to Michael Kay
Closing this as it appears to have been resolved.
Updated by O'Neil Delpratt almost 9 years ago
- Status changed from Resolved to Closed
Please register to edit this issue