You can use the attribute
or attributes
function in your expression to build the logic you want.
The fragment attribute($currentfeature,'layer1colname')
will return the value of that attribute in the current feature, if it exists, and NULL if not as opposed to an error. Ditto attributes()['layer1colname']
. You could then use CASE WHEN
and/or if ... is NULL
to pick out the right one.
Actually, attributes()
alone returns a map of all attributes of the current and/or other specified feature as a map (not cartographic map, a Python map...) with the attribute names as keys. You could use this for fancier logic to parse for relevant attribute names in a more complex manner.
Note in the above the attribute name is in single quotes '...'
since it is a string being passed to the attribute()
function to work with; with the more usual "..."
the expression evaluation engine would try to substitute the value of the attribute before invoking the function.
Finally, my original thought was to use try("Attr1","Attr2")
which in general ought to return its 2nd argument if the 1st one generates an error, but seems to in this case only generate a NULL if "Attr1"
is invalid in the layer.