Skip to main content
Solved

Using functions such as @round() inside the XMLTemplater drops data

  • January 18, 2021
  • 6 replies
  • 166 views

tomf
Supporter
Forum|alt.badge.img+20

This is probably a simple question, but I'm having difficulty rounding values within the xml editor of the XMLTemplator.

With the root template set to:

<feature>
    <number>{fme:get-attribute("NUMBER")}</number>
</feature>

The NUMBER value is written in the output. But when I use:

<feature>
    <number>@round({fme:get-attribute("NUMBER")},3)</number>
</feature>

or even:

<feature>
    <number>@Evaluate(@round({fme:get-attribute("NUMBER")},3))</number>
</feature>

the xml returned contains an empty string:

<?xml version="1.0" encoding="UTF-8"?>
<feature>
    <number/>
</feature>

I could round with an AttributeRounder prior to the XMLTemplater, but although that rounds the values in Visual Preview, the xml output can and does contain the floating point representation errors.

I'm guessing this might be a syntax thing but I've had no joy yet implementing this in the editor.

(FME 2020.1 b20620)

Best answer by david_r

You have to use XQuery expressions inside the XMLTemplater, the FME functions won't work.

Example rounding the attribute "value" to two decimales:

<tag>{fn:round(xs:double(fme:get-attribute("value")), 2)}</tag>

Note that "value" is first cast to a double in case FME returns it as a string, which the round function won't accept.

You can find a reference of all the XQuery and XPath functions here: https://www.w3.org/TR/xpath-functions-31/ 

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.

6 replies

caracadrian
Contributor
Forum|alt.badge.img+23
  • Contributor
  • January 18, 2021

I guess it's an editor issue. @round() is a math function, while XMLTemplater is a string editor.

I am not sure, it's a wild guess.

Try @format(%.3f,@Value(your attribute)). It's a Regular Expressions that usually works in situations like this.


tomf
Supporter
Forum|alt.badge.img+20
  • Author
  • Supporter
  • January 18, 2021

I guess it's an editor issue. @round() is a math function, while XMLTemplater is a string editor.

I am not sure, it's a wild guess.

Try @format(%.3f,@Value(your attribute)). It's a Regular Expressions that usually works in situations like this.

Thanks @caracadrian, sounds promising - will give this a go.

However, it doesn't explain why the editor offers all these lovely maths functions for us to use in the XMLTemplater though 😉 

 

<Edit>

Unfortunately it just returns:

<?xml version="1.0" encoding="UTF-8"?>
<feature><number>@format(%.3f,1.12354684613)</number>
</feature>

 Using @Format(<format>,<number>) results in no xml being returned


david_r
Celebrity
  • Best Answer
  • January 18, 2021

You have to use XQuery expressions inside the XMLTemplater, the FME functions won't work.

Example rounding the attribute "value" to two decimales:

<tag>{fn:round(xs:double(fme:get-attribute("value")), 2)}</tag>

Note that "value" is first cast to a double in case FME returns it as a string, which the round function won't accept.

You can find a reference of all the XQuery and XPath functions here: https://www.w3.org/TR/xpath-functions-31/ 


tomf
Supporter
Forum|alt.badge.img+20
  • Author
  • Supporter
  • January 18, 2021

You have to use XQuery expressions inside the XMLTemplater, the FME functions won't work.

Example rounding the attribute "value" to two decimales:

<tag>{fn:round(xs:double(fme:get-attribute("value")), 2)}</tag>

Note that "value" is first cast to a double in case FME returns it as a string, which the round function won't accept.

You can find a reference of all the XQuery and XPath functions here: https://www.w3.org/TR/xpath-functions-31/ 

Thanks @david_r​, that works.


lovatoe
Contributor
Forum|alt.badge.img+10
  • Contributor
  • February 24, 2025

I try to use this in the SUB template,

 

<tag>{fn:round(xs:double(fme:get-attribute("AMOUNT")), 2)}</tag>

 

but now I get an Xml error and the Templater fails.

 

Any suggestions?


debbiatsafe
Safer
Forum|alt.badge.img+21
  • Safer
  • February 27, 2025

Hello ​@lovatoe 

The XQuery expression you shared does work in an XMLTemplater when I tested it. 

I suspect there may be a typo elsewhere in the XQuery expression or an issue with the attribute value itself (eg. the value in AMOUNT is not a number). The error from the XMLTemplater will usually tell you why the query failed along with the line and column of where it failed.

I would also recommend creating a separate post on the Community with the error for greater visibility.