Skip to main content
Solved

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

  • January 18, 2021
  • 6 replies
  • 105 views

tomf
Contributor
Forum|alt.badge.img+14
  • Contributor

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/ 

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

6 replies

caracadrian
Contributor
Forum|alt.badge.img+22
  • 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
Contributor
Forum|alt.badge.img+14
  • Author
  • Contributor
  • January 18, 2021
caracadrian wrote:

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
Evangelist
  • 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
Contributor
Forum|alt.badge.img+14
  • Author
  • Contributor
  • January 18, 2021
david_r wrote:

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+4
  • 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+20
  • 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.


Reply


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