Skip to main content
Solved

condition in XML Templater


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 ""
View original
Did this help you find an answer to your question?

4 replies

takashi
Influencer
  • 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
  • April 18, 2018
takashi wrote:
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
Influencer
  • 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
  • April 18, 2018
takashi wrote:

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!

 

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings