Skip to main content

Got a situation where I would like the store attribute values, @Value(attribute_name), in some HTML code which itself is stored in a SQL table. The idea is that the @Value(attribute_name) will be different for each of our clients, sending them a personalised email based on their own data.

imageimageCurrently FME reads the table in and keeps everything as plain text, @Value(attribute_name) does not get replaced with the appropiate attribute value. Is there a way to get FME to recognise the attribute names in the HTML code and replace it with the appropiate attribute values?

 

if I copy and paste the HTML code into atttributecreator then it works as expected, however we would like to manage all this in SQL server - saving us having to update the workbench everytime there is a change required to the HTML

 

Thanks

Hi @michaelz​ ,

Do you mean that a string literal like <p>@Value(insight_id)</p> is stored in a column of a DB record, and FME will read it then should replace with <p>123</p> if "insight_id" attribute stores 123 at run time?


Hi @michaelz​ ,

Do you mean that a string literal like <p>@Value(insight_id)</p> is stored in a column of a DB record, and FME will read it then should replace with <p>123</p> if "insight_id" attribute stores 123 at run time?

Hi @Takashi Iijima​ that is precisely what I’m after


Hi @Takashi Iijima​ that is precisely what I’m after 

Thank you for confirmation.

A possible way I can think of is, extract pairs of "@Value(attribute name)" and "attribute name" as list attributes from the string literal with StringSearcher, and replace expression "@Value(attribute name)" with the value of an attribute called "attribute name" using a Python script (PythonCaller).

 

For example, if the input feature has attributes "html", "client_name", and "insight_id", and the "html" contains an HTML template containing the expressions "@Value(client_name)" and "@Value(insight_id)", the workflow looks like this.

evaluate_at_value_function 

StringSearcher setting:

  • Search In: html
  • Contains Regular Expression: @Value\((.+?)\)
  • Case Sensitive: Yes
  • All Matches List Name: _all
  • Subexpression Matches List Name: _sub

 

PythonCaller script setting:

import fme
import fmeobjects
 
class FeatureProcessor(object):
    def __init__(self):
        pass
 
    def input(self, feature):
        html = feature.getAttribute('html')
        expr = feature.getAttribute('_all{}.match')
        attr = feature.getAttribute('_sub{}.part')
        for e, a in zip(expr, attr):
            html = html.replace(e, str(feature.getAttribute(a)))
        feature.setAttribute('html', html)
        self.pyoutput(feature)
 
    def close(self):
        pass

 


Hi @Takashi Iijima​ that is precisely what I’m after

Alternatively, this is a nice use case for the AttributeDereferencer.

imageThe StringSearcher uses a regex subexpression to get the attribute name 'insight_id' in _SubResults{0}.part

I tried using StringReplacers or a StringPairReplacer, as it would make the process more clear, but the presence of '@Value' in the literal string makes it difficult to use those transformers.


Hi @Takashi Iijima​ that is precisely what I’m after

Thanks @Takashi Iijima​ works perfectly 😁


Reply