I'm trying to compile a JSLT expression in Java with com.schibsted.spt.data. The expression needs intermediate variables and I got compilation errors.
This is what i tried:
String jsltExpression = "let parts = split(.id, \"/\") { \"result\": parts[1] + parts[3] }";
Expression jslt = Parser.compileString(jsltExpression);
error:
com.schibsted.spt.data.jslt.JsltException: Parse error: Encountered " "[" "[ "" at line 1, column 46.
Was expecting:
"(" ...
at <inline>:1:41
at com.schibsted.spt.data.jslt.parser.ParserImpl.compileExpression(ParserImpl.java:62)
at com.schibsted.spt.data.jslt.Parser.compile(Parser.java:226)
at com.schibsted.spt.data.jslt.Parser.compileString(Parser.java:86)
at com.schibsted.spt.data.jslt.Parser.compileString(Parser.java:74)
Also tried:
String jsltExpression = "{ \"result\": let(parts = split(.id, \"/\")) parts[1] + parts[3] }";
Expression jslt = Parser.compileString(jsltExpression);
error:
com.schibsted.spt.data.jslt.JsltException: Parse error: Encountered " "let" "let "" at line 1, column 13.
Was expecting one of:
<INTEGER> ...
<DECIMAL> ...
<STRING> ...
"[" ...
"{" ...
"true" ...
"false" ...
"." ...
"if" ...
"(" ...
<IDENT> ...
<PIDENT> ...
<VARIABLE> ...
at <inline>:1:11
at com.schibsted.spt.data.jslt.parser.ParserImpl.compileExpression(ParserImpl.java:62)
at com.schibsted.spt.data.jslt.Parser.compile(Parser.java:226)
at com.schibsted.spt.data.jslt.Parser.compileString(Parser.java:86)
at com.schibsted.spt.data.jslt.Parser.compileString(Parser.java:74)
Is it possible to compile an expressien lika that with com.schibsted.spt.data? or is there any limitation?
You have to use $
to access the parts
value, like this:
String jsltExpression = "let parts = split(.id, \"/\") { \"result\": $parts[1] + $parts[3] }";
You can try jslt expression in the playground (https://www.garshol.priv.no/jslt-demo), it gives you more feedback on errors and the chanche to craft specific input data for testing your expressions.