Assigning empty string if XML node doesn't exist in Freemarker - freemarker

I have an XML document passed as root to a Freemarker template. I want some values from this XML to be assigned to variables as a string and later concatenate/print them out.
<#assign MyVar = root.child1.child2.child3.mynode>
The issue here is that even when a path doesn't exist MyVar gets assigned with a sequence+hash which cannot be printed out or converted to string. This variable although returns false for ?has_content, it needs an extra step for these checks and I have this same issue with many variables and across template files and modules.
The only solution I have been able to find was
<#assign MyVar = root.child1.child2.child3.mynode>
<#assign MyVar = MyVar ?has_content?then(MyVar , "")>
I am looking for something like the Default Value Operator which also checks for nulls like ?has_content.
Does Freemarker provide any simpler one line function to check if a variable has no content and assign it with a default?

In short:
<#assign myVar = root.child1.child2.child3.mynode[0]!''>
Or just <#assign myVar = root.child1.child2.child3.mynode[0]!> if the implicit multi-typed default value doesn't cause problems (like when you just print it with ${}).
Why: XML queries (just like XPath queries) always return a sequence of matching nodes. There are maybe 0 such nodes (or 1, or multiple). An empty sequence is not a "missing value" according the template language. It's an inconvenient mismatch with the XML data-model. But while the sequence always exists, its 1st element ([0]) doesn't, so you can use all the missing value handler operators with it as usual.

Related

Fetch value from XML using dynamic tag in ESQL

I have an xml
<family>
<child_one>ROY</child_one>
<child_two>VIC</child_two>
</family>
I want to fetch the value from the XML based on the dynamic tag in ESQL. I have tried like this
SET dynamicTag = 'child_'||num;
SET value = InputRoot.XMLNSC.parent.(XML.Element)dynamicTag;
Here num is the value received from the input it can be one or two. The result should be value = ROY if num is one and value is VIC if num is two.
The chapter ESQL field reference overview describes this use case:
Because the names of the fields appear in the ESQL program, they must be known when the program is written. This limitation can be avoided by using the alternative syntax that uses braces ( { ... } ).
So can change your code like this:
SET value = InputRoot.XMLNSC.parent.(XMLNSC.Element){dynamicTag};
Notice the change of the element type as well, see comment of #kimbert.

Apache freemarker template assign and compare values

I am assigning value to a variable i_type using below assign statement.
<#assign i_type>
<#if x.has("type")>
<#if x.type == "ABC">"ABC"<#else>"${x.type?lower_case}"</#if>
<#else>"pqr"</#if>
</#assign>
Then I want to assign a variable in ftl conversion as:
"final_type" : <#if i_type?has_content && i_type == "pqr">1<#else>0</#if>
But value of final_type is always coming out to be 0 in all cases.
I explicitly printed value of i_type and even though it was "pqr" but condition is always coming out to be false.
what should be changed?
Why the original example doesn't work is that you have quotation marks in <#else>"pqr"</#if>, and on the other similar places. That way the captured value itself will contain the quotation marks, because the nested content of FreeMarker directives is not expressions, instead it's just like top-level template content. So just write <#else>pqr</#if>.
Anyway, a better way to write what you did is this:
<#assign i_type =
x.has("type")?then(
(x.type == "ABC")?then(x.type, x.type?lower_case),
"pqr"
)
>
You also don't need the i_type?has_content condition in the second piece of code, since something is always assigned to i_type. (But even if in reality it isn't, you can write i_type! to default a missing value to "".) So that can be written like this:
"final_type" : ${(i_type == "pqr")?then("1", "0")}
Once I used
"final_type" : <#if i_type?has_content && i_type?eval == "pqr">1<#else>0</#if>
it worked fine.

Assigning a variable from another variable

I am trying to assign a variable from another variable. My code looks like this
<#macro ctglink c rhs x y z m e b>
<#assign ctg>
<#if ctgroutes["${y}..${x}-${m}"]??>ctgroutes['${y}..${x}-${m}']
<#elseif ctgroutes["${x}..${y}-${m}"]??>ctgroutes['${x}..${y}-${m}']
<#else>{}</#if>
</#assign>
However, this ctg variable is evaluating to just ctgroutes['227..257-TPPMD04X02'] its not actually evaluating the string itself.
I have tried ?eval, and ?interpret and a bunch of other very hacky things to get this to work, no go. Even the {} is a string
Basically, I need the assign function to work like the old PHP eval() function or something. I am trying to access values in a Map whose keys are derived from the state of the data, so I don't see any easy way to query my Map without evaluating keys.
Update:
I forgot to include the elseif in there
Either way, I tried <#assign ctg = ctgroutes["${y}..${x}-${m}"]!ctgroutes["${x}..${y}-${m}"]> but I get the following error:
Caused by: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> ctgroutes["${y}..${x}-${m}"]!ctgroutes["${x}..${y}-${m}"] [in template "RouteCompare-WptTable.ftlh" at line 5, column 24]
I would like a null result to just return an empty map, however that doesn't seem possible:
Caused by: java.lang.RuntimeException: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> ctgroutes["${y}..${x}-${m}"]!ctgroutes["${x}..${y}-${m}"] [in template "RouteCompare-WptTable.ftlh" at line 5, column 24]
So basically, my goal is I need to assign a variable that can take 1 of 3 values:
ctgroutes["${y}..${x}-${m}"] // Assuming it is not null
ctgroutes["${x}..${y}-${m}"] // Assuming it is not null
{} // An empty map
What is the best way to do that?
If I understand well what you want to achieve, you can write it like this:
<#assign ctg = ctgroutes["${y}..${x}-${m}"]!ctgroutes["${x}..${y}-${m}"]!{}>
Also note that <#assign target>...</#assign> is for capturing the output printed between the two tags into the target variable (instead of actually printing it). So target will always store a string or markup value. Also things outside FreeMarker tags and ${} are just static text, and won't be parsed. So, the naive but working approach is just using #if/#elseif/#else and have a separate #assign ctg = ... inside each of them, but you can make this much sorter with the ! operator, as it was shown.

How can I set a FreeMarker variable with an interpolated value? error: "You can't use "${" here as you are already in FreeMarker-expression-mode."?

I am absolutly new in FreeMarker and I have the following problem working on a Spring MVC application that use this template engine.
So into a controller method I put an int representing the current year (2016) into the model, in this way:
model.addAttribute("annoCorrente", annoCorrente);
Then, into my FreeMarker page I have to assign this value to a variable, so I write the following expression:
<#assign a = ${annoCorrente}>
But in this way I obtain the following error message:
[col. 86] You can't use "${" here as you are already in FreeMarker-expression-mode. Thus, instead of ${myExpression}, just write myExpression. (${...} is only needed where otherwise static text is expected, i.e, outside FreeMarker tags and ${...}-s.)
Why? How can I correctly initizialize a FreeMarker variable with the value obtained from the model associated to this view?
Change <#assign a = ${annoCorrente}> to <#assign a = annoCorrente>
(or you can do <#assign a = "${annoCorrente}"> but this is not recommended)

How to set null to a variable in freemarker

I can't find anything related to this on any question, and it is something really basic, but I can't figure it out.
So my problem is that I don't know how to set null to a variable in freemarker.
Example:
${hi!"bye"} <#-- Prints "bye" because hi is undefined -->
<#assign hi="hi"> <#-- Sets a value to the var hi -->
${hi!"bye"} <#-- Prints "hi" because hi has a value -->
<#assign hi=null> <#-- This does not work but is what I am looking for -->
${hi!"bye"} <#-- I want it to print "bye" because hi should be undefined -->
I have this problem because I iterate over a list and set a var if some logic to the specific item validates, and then check if the var exists, but if the first item creates the var, then I will have the var set for the rest of the items in the list.
No, there's no "unassign", nor the concept of null exists in FreeMarker (until 2.4.0 at least, but that's far away anyway). It only have missing variables (maybe technically a null, maybe doesn't exist at all) and those that are there. I don't really get why is that needed in your case. Can you show a simplified example of the situation?
You could assign an empty string to your variable and check with the buit-in ?has_content if it is set:
${hi?has_content?then(hi, "bye")}
<#assign hi="hi">
${hi?has_content?then(hi, "bye")}
<#assign hi="">
${hi?has_content?then(hi, "bye")}
This will render:
bye
hi
bye
Depending on what you need it for, you can use a different type to indicate a "missing" value.
For instance, if you have myVariable that is normally a number, assign false to it, and then instead of checking myVariable??, check myVariable!false?is_number. This will cover both cases (non-existent and "unset").
${ (myVariable!false?is_number)?c }
<#assign myVariable = 12 >
${ (myVariable!false?is_number)?c }
<#assign myVariable = false >
${ (myVariable!false?is_number)?c }
Result:
false
12
false
Go try.

Resources