Solved

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

  • 4 January 2024
  • 5 replies
  • 2 views

Badge

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 ?

icon

Best answer by debbiatsafe 5 January 2024, 01:48

View original

5 replies

Userlevel 5
Badge +26

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.

Badge

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>

 

Badge

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.

Userlevel 3
Badge +18

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.

Badge

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.

Reply