Skip to main content
Solved

condition in XML Templater

  • April 18, 2018
  • 4 replies
  • 68 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

Best answer by takashi

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 ""
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

takashi
Celebrity
  • 7842 replies
  • April 18, 2018
In order to clarify where the issue is, could you please post the template expression you defined and the error messages you got?

 

 


  • Author
  • 10 replies
  • April 18, 2018
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,

 


takashi
Celebrity
  • 7842 replies
  • Best Answer
  • April 18, 2018

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 ""

  • Author
  • 10 replies
  • April 18, 2018

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!