Skip to main content
Solved

Avoid if else() structure in FMETemplater to generate empty lines in the output.


Forum|alt.badge.img

We used an XMLTemplater (2022.2) to generate an xml message using Excel file input with some optional columns.

The XML template contains some if structures like this :

            {

            if (fme:has-attribute("STATUS")) then

<Info>

<keyName>STATUS</keyName>

<value>{fme:get-attribute("STATUS")}</value>

</Info>

            else()

            }

The output xml is correct, but contains empty lines for every missing column, which we want to avoid.

Leaving out the else() clause is not allowed as it results in a parsing error.

How can we avoid those empty lines in the output xml ?

Best answer by debbiatsafe

Hello @ivl​ 

The XMLFormatter is the correct transformer to use. The parameter which should remove the empty lines from generated by the if/else() statements is called Whitespace Handling in the Formatting Options section.

By default, this parameter is set to "Preserve all whitespace". Try changing the parameter value to "Remove excess whitespace" to create the XML output without the empty lines.

View original
Did this help you find an answer to your question?

5 replies

redgeographics
Celebrity
Forum|alt.badge.img+50

Would it be acceptable to have a generic value there (something along the lines of "no data" or "n/a" or something like that)? If so, you can use a NullAttributeMapper to replace empty or missing values with something of your choosing.


Forum|alt.badge.img
  • Author
  • January 4, 2024

There is no problem with the empty values, the XML content is "correct". It only contains empty lines for any missing optional attribute.

 

Let's say the status column is present but empty and the 3 other optional columns are not present we will get something like

<component>

            <Info>

<keyName>STATUS</keyName>

<value/>

            </Info>

             

             

             

  </component>

 

instead of :

  <component>

<Info>

<keyName>STATUS</keyName>

<value/>

</Info>

  </component>

 


Forum|alt.badge.img
  • Author
  • January 4, 2024

We tried to add an XMLFormatter after the XMLTemplater to remove the empty lines, but it doesn't seem to have an option to remove empty lines.


debbiatsafe
Safer
Forum|alt.badge.img+20
  • Safer
  • Best Answer
  • January 5, 2024

Hello @ivl​ 

The XMLFormatter is the correct transformer to use. The parameter which should remove the empty lines from generated by the if/else() statements is called Whitespace Handling in the Formatting Options section.

By default, this parameter is set to "Preserve all whitespace". Try changing the parameter value to "Remove excess whitespace" to create the XML output without the empty lines.


Forum|alt.badge.img
  • Author
  • January 6, 2024

We tried this already and it didn't seem to work. An inspector at the output of the XMLFormatter still shows the empty lines.

However, meanwhile we noticed it's a inspector bug. It turns out in reality the XMLFormatter did remove the empty lines, the Inspector doesn't show the real output.


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