Skip to main content
Solved

Using math functions in XmlTemplater ?

  • February 22, 2017
  • 8 replies
  • 19 views

lifalin2016
Contributor
Forum|alt.badge.img+29

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.

Best answer by takashi

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

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

8 replies

redgeographics
Celebrity
Forum|alt.badge.img+49

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.


itay
Supporter
Forum|alt.badge.img+17
  • Supporter
  • February 22, 2017

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


lifalin2016
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • February 22, 2017
redgeographics wrote:

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 :-)

 

 


lifalin2016
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • February 22, 2017
itay wrote:

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.

 

 


lifalin2016
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • February 22, 2017
redgeographics wrote:

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).

 


redgeographics
Celebrity
Forum|alt.badge.img+49
lifalin2016 wrote:
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)

 


lifalin2016
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • February 22, 2017

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


takashi
Influencer
  • Best Answer
  • April 29, 2017

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


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