Skip to main content

Hi,

What's the proper syntax for using math functions in XmlTemplater expressions and/or files ?

I have an RGB value that I need to split into its R, G, and B components, but I don't want to have to create 3 extra attributes for each of the 4+ color values.

I need an output like this: <Color R="red" G="green" B="blue" />

I've tried (with attribute "mapinfo_brush_foreground" as my RGB - value = 0xFFFFFE):

<Color R="@Evaluate(floor({fme:get-attribute('mapinfo_brush_foreground')}/65536))" G="floor(fmod({fme:get-attribute('mapinfo_brush_foreground')},65536)/256)" B="@Evaluate(floor(fmod(@Evaluate({fme:get-attribute('mapinfo_brush_foreground')}),256)))" />

But this yields: <Color R="" G="floor(fmod(16777214,65536)/256)" B="" />

So neither none, one, or multiple "Evaluate()" calls produces the desired result.

Please help me out in this.

And PS: it seems like such math functions are no-go when using file based templates. Is this true ?

Cheers.

Math functions are available in the Arithmetic editor but for the XML
template you're using the Text editor, which will interpret math
functions as plain text.

However, the @Format() function can help you here.

Specifically, @Format(%d,<your calculation>) would do the trick. E.g. @Format(%d,1+1) yields 2 in the Text editor.


I usually do the calculation into a separate attribute before the xml templater, never got expressions working properly in the templater.


Math functions are available in the Arithmetic editor but for the XML
template you're using the Text editor, which will interpret math
functions as plain text.

However, the @Format() function can help you here.

Specifically, @Format(%d,<your calculation>) would do the trick. E.g. @Format(%d,1+1) yields 2 in the Text editor.

Thanks redgeographics, I'll try that immidiatedly :-)

 

 


I usually do the calculation into a separate attribute before the xml templater, never got expressions working properly in the templater.

Yeah, but I was trying to avoid that, as explained above.

 

 


Math functions are available in the Arithmetic editor but for the XML
template you're using the Text editor, which will interpret math
functions as plain text.

However, the @Format() function can help you here.

Specifically, @Format(%d,<your calculation>) would do the trick. E.g. @Format(%d,1+1) yields 2 in the Text editor.

I've tried it, but it doesn't work it seems. Your example doesn't contain any math function calls, so are you sure they work here ?

 

 

As for which editor is used, it just says "Template Expression", not "Text" nor "Arithmetic". And all string and math functions seem to be available (in the listboxes to the left).

 


I've tried it, but it doesn't work it seems. Your example doesn't contain any math function calls, so are you sure they work here ?

 

 

As for which editor is used, it just says "Template Expression", not "Text" nor "Arithmetic". And all string and math functions seem to be available (in the listboxes to the left).

 

I had not used a math function in my first sample, but as you can see here it works with functions too. Do check the documentation for the Format function (or the StringFormatter, which uses the same TCL calls)

 


Well, there seems to be some foulness afoot here.

When I replaced {fme:get-attribute('xx') with <amp>Value(xx) it finally started to work. The former type still works when fetching singular value, but apparently not in an expression.

I got the below line to work - but only as an expression:

<Color R="@Evaluate(@floor(@div(@Value(mapinfo_brush_foreground),65536)))" G="@Evaluate(@floor(@div(@fmod(@Value(mapinfo_brush_foreground),65536),256)))" B="@Evaluate(@floor(@fmod(@Value(mapinfo_brush_foreground),256)))" />

When I copy'n'pasted it to a file, and set XmlTemplater to use that file, no evaluation was performed, and my R/G/B values got the expression text as value !?!??

Methinks this interface needs a friendly overhaul and/or much better documentation (using 2016.1).

Cheers


I found XQuery operators are available here. e.g.

<Color
    R="{xs:integer(fme:get-attribute("mapinfo_brush_foreground")) idiv 65536}"
    G="{(xs:integer(fme:get-attribute("mapinfo_brush_foreground")) mod 65536) idiv 256}"
    B="{xs:integer(fme:get-attribute("mapinfo_brush_foreground")) mod 256}"
/>

or

let $v := xs:integer(fme:get-attribute("mapinfo_brush_foreground"))
return <Color R="{$v idiv 65536}" G="{($v mod 65536) idiv 256}" B="{$v mod 256}"/>

See also here. XQuery 3.1: An XML Query Language | Arithmetic Expressions


Reply