Skip to main content

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)

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.


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


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/ 


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.


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?


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