Solved

condition in XML Templater

  • 18 April 2018
  • 4 replies
  • 15 views

Hi,

I'm trying to figure out seemingly easy condition in XML Templator, but something is just not working out.

 

What I would like to do is write out an XML element only if a feature attribute has a value.

 

Here's an example:

<OperatingTimeList>

<OpeningTime>{fme:get-attribute("opening_time")}</OpeningTime>

<ClosingTime>{fme:get-attribute("closing_time")}</ClosingTime>

<OperatingTime dayOfweek="" note="we are open">

</OperatingTime>

</OperatingTimeList>

And I would like the "note" to show up in the XML only when there are some values in OpeningTime

I know I should follow if then else logic, but so far I'm only getting XMLTemplater errors.

 

 

(FME Desktop 2018)

 

Thanks,

Linda

icon

Best answer by takashi 18 April 2018, 14:47

View original

4 replies

Userlevel 3
Badge +17
In order to clarify where the issue is, could you please post the template expression you defined and the error messages you got?

 

 

In order to clarify where the issue is, could you please post the template expression you defined and the error messages you got?

 

 

Hi @takashi, thanks for the quick response.

 

 

This is the expression:

 

<OperatingTimeList>

 

<OpeningTime>{fme:get-attribute("opening_time")}</OpeningTime>

 

<ClosingTime>{fme:get-attribute("closing_time")}</ClosingTime>

 

<OperatingTime dayOfweek="" note="">

 

{ if( fme:has-attribute{fme:get-attribute("opening_time")} and

 

not(fme:get-attribute{fme:get-attribute("closing_time")} eq "") )

 

then <OperatingTime dayOfweek="" note="we are open">

 

else ()

 

}

 

</OperatingTime>

 

</OperatingTimeList>

 

 

And the error messages:

 

XMLTemplater(XMLTemplaterFactory): The following error occurred near line 403, column 43 of the query:

 

XMLTemplater(XMLTemplaterFactory): invalid expression: syntax error, unexpected ExprSingle (missing comma "," between expressions?)

 

XMLTemplater(XMLTemplaterFactory): An error occurred while parsing the ROOT template

 

XMLTemplater(XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details

 

A fatal error has occurred. Check the logfile above for details,

 

Userlevel 3
Badge +17

Hi @llindish, the syntax error seems to be caused by wrong usage of "fme:has-attribute" and "fme-get-attribute" functions.

If you want to create the "OperatingTime" element only if the "opening_time" attribute exists and the "closing_time" is not empty,  this expression could work for you.

<OperatingTimeList> 
    <OpeningTime>{fme:get-attribute("opening_time")}</OpeningTime> 
    <ClosingTime>{fme:get-attribute("closing_time")}</ClosingTime> 
    {
        if (fme:has-attribute("opening_time") and not(fme:get-attribute("closing_time") eq ""))
            then <OperatingTime dayOfweek="" note="we are open" /> 
            else () 
    } 
</OperatingTimeList> 
not(fme:get-attribute("closing_time") eq "")

is equivalent to

fme:get-attribute("closing_time") ne ""

Hi @llindish, the syntax error seems to be caused by wrong usage of "fme:has-attribute" and "fme-get-attribute" functions.

If you want to create the "OperatingTime" element only if the "opening_time" attribute exists and the "closing_time" is not empty,  this expression could work for you.

<OperatingTimeList> 
    <OpeningTime>{fme:get-attribute("opening_time")}</OpeningTime> 
    <ClosingTime>{fme:get-attribute("closing_time")}</ClosingTime> 
    {
        if (fme:has-attribute("opening_time") and not(fme:get-attribute("closing_time") eq ""))
            then <OperatingTime dayOfweek="" note="we are open" /> 
            else () 
    } 
</OperatingTimeList> 
not(fme:get-attribute("closing_time") eq "")

is equivalent to

fme:get-attribute("closing_time") ne ""
Thank you, works perfectly now!

 

 

Reply